PageRenderTime 23ms CodeModel.GetById 6ms app.highlight 14ms RepoModel.GetById 1ms app.codeStats 0ms

/racf2base.pl

http://cupsfilter.googlecode.com/
Perl | 137 lines | 106 code | 12 blank | 19 comment | 10 complexity | 20c874c36fed2e9f530fbf8bf68a408b MD5 | raw file
  1#!/usr/bin/perl  -w
  2use strict;
  3use DBI;
  4
  5my $DEBUG=1;
  6my $RACF_UTIL_BIN;
  7
  8my @argv = @ARGV;			#keep hands off the argument array
  9my ($printer_ip,$printer_name)=@argv;
 10
 11### ARG CHECKING
 12# If no arguments, print help
 13if (!$argv[0] || scalar(@argv)> 2){
 14        print ("Usage racf2base.pl PRINTER_IP PRINTER_NAME\n");
 15        exit 1;
 16}
 17
 18if ($DEBUG){
 19	$RACF_UTIL_BIN="racf.pl";
 20	$printer_ip="192.168.11.151";
 21	$printer_name="PDF";
 22	
 23	#for(keys(%ENV)) {
 24    #   print (STDERR "$_ = $ENV{$_}\t");
 25    #}
 26}else {
 27        $RACF_UTIL_BIN="/usr/bin/racf.pl";
 28}
 29
 30my @result = save_mandat2base($printer_ip,$printer_name);
 31print "Output $result[1] with code $result[0]";
 32
 33#TODO ???????? ????? ??????? ?????? ? ???
 34#TODO need use SQLShell.pm !!!
 35##----------------------------------------------------------------------
 36
 37sub save_mandat2base{
 38	#Args: printer_ip,printer_name
 39	#Returns 1/0 succsesfull or errors
 40	my($printer_ip,$printer_name)=@_;
 41	my $mandat=get_data_from_racf($printer_ip,$printer_name);
 42	if (defined $mandat){
 43		#Find printer in base
 44		my $q="SELECT id FROM printers WHERE ip='$printer_ip' and printer_name='$printer_name'";
 45		my @res=sql_execute($q,\&get_id_firstRowCow);	
 46		if ($res[0]){
 47			$q="UPDATE printers SET mandat='$mandat' WHERE ip='$printer_ip' and printer_name='$printer_name'";
 48			@res = sql_execute($q);
 49			return @res;  
 50		}else {
 51			#Add printer to base
 52			$q="INSERT INTO printers (ip,printer_name,mandat)VALUES('$printer_ip','$printer_name','$mandat')";
 53			@res = sql_execute($q);
 54			return @res;
 55		}
 56	}
 57	return (0,"unable save printer's mandat into base!");
 58}
 59
 60sub get_id_firstRowCow { 
 61    #Arg: $Ptr to array of rows
 62    #Returns: array [1/0 succesfull or error,value of First Row Cow]
 63    my ($array_ref) =@_;
 64    if (defined $array_ref->[0][0]){
 65	return (1,$array_ref->[0][0]);
 66    }else{
 67          return (0,"Cell not found in base");
 68     }
 69} 
 70sub get_data_from_racf{
 71    #Arg: printer_ip,printer_name
 72    #Returns Mandat or UNDEF 
 73    my ($printer_ip,$printer_name)=@_;
 74    my $pid = open (F,"$RACF_UTIL_BIN $printer_ip $printer_name|") or die "Can't find and execute $!\n";
 75    my @res = <F>;
 76    close (F);
 77    if (defined $res[1]){
 78    	return $res[1];	
 79    }
 80    return; 
 81}
 82
 83sub sql_execute{
 84    #Arg: $query_str,Ptr to function
 85    #Returns: array [1/0 succesfull or error,description]
 86    my ($query,$func)=@_;
 87    my @result;
 88    my $dbname = "CupsLog";# ??? ???? ??????
 89    my $username = "postgres";# ??? ????????????
 90    my $password = "pg";# ??????
 91    my $dbhost = "localhost";# ??? ??? IP ????? ???????
 92    my $dbport = "5432";# ????
 93    my $dboptions = "-e";# ?????
 94    my $dbtty = "ansi";# ????????
 95    my $sth;
 96    my $rv;
 97    my $error_str;
 98    
 99    my $dbh = DBI->connect("dbi:PgPP:dbname=$dbname;host=$dbhost;port=$dbport;options=$dboptions;tty=$dbtty","$username","$password",{PrintError => 0, AutoCommit => 0 });
100	if ($DEBUG){
101		my $trace_file="dbh.log";
102		$dbh->trace(2,$trace_file);
103	}
104	
105    if ($DBI::errstr) {
106        $error_str="CUPS_FILTER: Erorr $DBI::errstr\n";
107        print STDERR $error_str;
108        return (0,$error_str);
109    }else{
110    	#TODO Need refactoring !!!
111    	$dbh->begin_work();
112    	unless (defined $func){
113    		#INSERT, UPDATE or DELETE
114    		$rv=$dbh->do($query);
115    	}else {
116    		#SELECT
117    		$sth = $dbh->prepare($query);
118        	$rv = $sth->execute();
119    	}
120        $dbh->commit();
121        if (!defined $rv) {
122            $error_str="CUPS_FILTER: ??? ?????????? ??????? $query\n ????????? ??????:".$dbh->errstr."\n";
123	    	#print STDERR $error_str;
124            return (0,$error_str);
125        }else{
126            if (defined $func){
127				@result=$func->($sth->fetchall_arrayref());
128                $sth->finish();
129	    	}else{
130	      		push @result,1,"Succsefull execute";
131	    	}
132        }
133    }
134    $dbh->disconnect();
135    return @result;
136}
137