| Line 1... | Line 1... | 
          
            | 1 | #! /usr/bin/perl -w
 | 1 | #! /usr/bin/perl -w
 | 
          
            | 2 |  
 | 2 |  
 | 
          
            | 3 | use IO::Interactive qw(is_interactive interactive busy);
 | 3 | use IO::Interactive qw(is_interactive interactive busy);
 | 
          
            | - |   | 4 | use YAML::XS;
 | 
          
            | - |   | 5 | use Cwd 'abs_path';
 | 
          
            | - |   | 6 | #use Data::Dumper;
 | 
          
            | 4 |  
 | 7 |  
 | 
          
            | 5 | my $CRON= ! is_interactive();
 | 8 | my $CRON= ! is_interactive(); # determine if we are in interactive shell
 | 
          
            | - |   | 9 |  
 | 
          
            | - |   | 10 | # control the flow of the program. $CHECKMAIL is pretty clear.
 | 
          
            | - |   | 11 | # if $PROCESS is false and $MOVEFILES is true, the files will be moved
 | 
          
            | - |   | 12 | # but not processed (ie, they were processed some other way)
 | 
          
            | - |   | 13 | my $CHECKMAIL=1; #controls whether we will check the mail or not
 | 
          
            | - |   | 14 | my $PROCESS=1; # controls whether we will process the files
 | 
          
            | - |   | 15 | my $MOVEFILES=1; # controls whether we will move the files successfully processed
 | 
          
            | 6 |  
 | 16 |  
 | 
          
            | - |   | 17 | my $MY_DIRECTORY; # will hold the directory of the script
 | 
          
            | 7 | my $DATADIR = '/home/rodolico/temp/sysinfo_reports';
 | 18 | my $DATADIR; # will hold the reports directory
 | 
          
            | 8 | my $UNPROCESSED = "$DATADIR/unprocessed";
 | 19 | my $UNPROCESSED; # will hold the location for unprocessed reports
 | 
          
            | 9 | my $MAXTOPROCESS = 10000;
 | 20 | my $MAXTOPROCESS = 10000;
 | 
          
            | 10 | my %filesProcessed;
 | 21 | my %filesProcessed;
 | 
          
            | 11 |  
 | 22 |  
 | 
          
            | - |   | 23 | # following are used to find the configuration file
 | 
          
            | - |   | 24 | my $confFileName = "sysinfoRead.conf.yaml";
 | 
          
            | - |   | 25 | my @searchPaths = ( '/etc/camp', '/opt/camp', '/opt/camp/sysinfo', getcwd() );
 | 
          
            | - |   | 26 |  
 | 
          
            | - |   | 27 |  
 | 
          
            | - |   | 28 | sub loadConfig {
 | 
          
            | - |   | 29 |    my ( $confFileName, @searchPaths ) = @_;
 | 
          
            | - |   | 30 |    my $configuration;
 | 
          
            | - |   | 31 |  
 | 
          
            | - |   | 32 |  
 | 
          
            | - |   | 33 |    for ( $i = 0; $i < @searchPaths; $i++ ) {
 | 
          
            | - |   | 34 |       $filename = $searchPaths[$i] . '/' . $confFileName;
 | 
          
            | - |   | 35 |       if ( -e $filename ) {
 | 
          
            | - |   | 36 |          #print "Found $filename\n";
 | 
          
            | - |   | 37 |          open CONF, "<$filename" or warn "Could not read $filename: $!\n";
 | 
          
            | - |   | 38 |          $configuration = Load( join( '', <CONF> ) );
 | 
          
            | - |   | 39 |          last; # exit out of the loop; we don't try to load it more than once
 | 
          
            | - |   | 40 |       } # if
 | 
          
            | - |   | 41 |    } # foreach
 | 
          
            | - |   | 42 |    return $configuration;
 | 
          
            | - |   | 43 | } # sub loadConfig
 | 
          
            | - |   | 44 |  
 | 
          
            | - |   | 45 | sub sendReport {
 | 
          
            | - |   | 46 |    my ($parameters,$report) = @_;
 | 
          
            | - |   | 47 |    my %CLIParams ;
 | 
          
            | - |   | 48 |    $CLIParams{'-f'}  = qq/$$parameters{'mailFrom'}/    if $$parameters{'mailFrom'};
 | 
          
            | - |   | 49 |    $CLIParams{'-t'}  = qq/$$parameters{'mailTo'}/      if $$parameters{'mailTo'};
 | 
          
            | - |   | 50 |    $CLIParams{'-u'}  = qq/$$parameters{'mailSubject'}/ if $$parameters{'mailSubject'};
 | 
          
            | - |   | 51 |    $CLIParams{'-s'}  = qq/$$parameters{'mailServer'}/  if $$parameters{'mailServer'};
 | 
          
            | - |   | 52 |    $CLIParams{'-xu'} = qq/$$parameters{'smtpUser'}/    if $$parameters{'smtpUser'};
 | 
          
            | - |   | 53 |    $CLIParams{'-xp'} = qq/$$parameters{'smtpPass'}/    if $$parameters{'smtpPass'};
 | 
          
            | - |   | 54 |    $CLIParams{'-cc'} = qq/$$parameters{'mailCC'}/      if $$parameters{'mailCC'};
 | 
          
            | - |   | 55 |    $CLIParams{'-bcc'}= qq/$$parameters{'mailBCC'}/     if $$parameters{'mailBCC'};
 | 
          
            | - |   | 56 |    $CLIParams{'-l'}  = qq/$$parameters{'logFile'}/     if $$parameters{'logFile'};
 | 
          
            | - |   | 57 |    $CLIParams{'-a'}  = qq/$$parameters{'attachment'}/  if $$parameters{'attachment'};
 | 
          
            | - |   | 58 |    $CLIParams{'-o tls='} = qq/$$parameters{'tls'}/     if $$parameters{'tls'};
 | 
          
            | - |   | 59 |    
 | 
          
            | - |   | 60 |    $commandLine = qq/$$parameters{'emailScript'}/;
 | 
          
            | - |   | 61 |    die "Could not find executable $commandLine in sendEmailScript\n" unless -x $commandLine;
 | 
          
            | - |   | 62 |    $commandLine .= ' -q'; # make it act quietly
 | 
          
            | - |   | 63 |    foreach my $key ( keys %CLIParams ) {
 | 
          
            | - |   | 64 |       # depending on whether the key ends in an = or not, we will or will not use a space
 | 
          
            | - |   | 65 |       # between the key and the parameter
 | 
          
            | - |   | 66 |       $commandLine .= $key =~ m/=$/ ? " $key'$CLIParams{$key}'" : " $key '$CLIParams{$key}'";
 | 
          
            | - |   | 67 |    }
 | 
          
            | - |   | 68 |    $commandLine .= ' ' . $$parameters{'otherCLParams'} if $$parameters{'otherCLParams'};
 | 
          
            | - |   | 69 |    open SENDMAIL, "|$commandLine" or die "Could not open [$commandLine]: $!\n";
 | 
          
            | - |   | 70 |    print SENDMAIL $report;
 | 
          
            | - |   | 71 |    close SENDMAIL;
 | 
          
            | - |   | 72 | } # sendReport
 | 
          
            | - |   | 73 |  
 | 
          
            | 12 | # assumes parse_sysinfo.php returns one of the following codes
 | 74 | # assumes parse_sysinfo.php returns one of the following codes
 | 
          
            | 13 | # 0 - Processed Normally
 | 75 | # 0 - Processed Normally
 | 
          
            | 14 | # 1 - could not process file (not xml, yaml or ini)
 | 76 | # 1 - could not process file (not xml, yaml or ini)
 | 
          
            | 15 | # 2 - Invalid report, does not have one or more of report date, client name or computer name
 | 77 | # 2 - Invalid report, does not have one or more of report date, client name or computer name
 | 
          
            | 16 | # 3 - Invalid Report, invalid machine name
 | 78 | # 3 - Invalid Report, invalid machine name
 | 
          
            | Line 31... | Line 93... | 
          
            | 31 |    $targetDirectory = "$DATADIR/$targetDirectory";
 | 93 |    $targetDirectory = "$DATADIR/$targetDirectory";
 | 
          
            | 32 |    `mkdir -p '$targetDirectory'` unless -d $targetDirectory;
 | 94 |    `mkdir -p '$targetDirectory'` unless -d $targetDirectory;
 | 
          
            | 33 |    `mv '$file' '$targetDirectory'`;
 | 95 |    `mv '$file' '$targetDirectory'`;
 | 
          
            | 34 | }
 | 96 | }
 | 
          
            | 35 |  
 | 97 |  
 | 
          
            | - |   | 98 | # get our configuration set up first
 | 
          
            | - |   | 99 |  
 | 
          
            | - |   | 100 | my $config = &loadConfig( $confFileName, @searchPaths );
 | 
          
            | - |   | 101 | die "Could not find configuration file $confFileName\n" unless $config;
 | 
          
            | - |   | 102 | $DATADIR = $$config{'datapath'};
 | 
          
            | - |   | 103 | $UNPROCESSED=$DATADIR . '/' . $$config{'unprocessed_path'};
 | 
          
            | - |   | 104 |  
 | 
          
            | - |   | 105 | use File::Basename;
 | 
          
            | - |   | 106 | $MY_DIRECTORY = abs_path(dirname(__FILE__) );
 | 
          
            | - |   | 107 | $getMailScript = "$MY_DIRECTORY/" . $$config{'getMailScript'};
 | 
          
            | - |   | 108 | $processMailScript = "$MY_DIRECTORY/" . $$config{'processMailScript'};
 | 
          
            | - |   | 109 | die "Could not find the getMailScript [$getMailScript] in $MY_DIRECTORY\n" unless -e $getMailScript;
 | 
          
            | - |   | 110 | die "Could not find the processMailScript [$processMailScript] in $MY_DIRECTORY\n" unless -e $processMailScript;
 | 
          
            | 36 |  
 | 111 |  
 | 
          
            | 37 | # fetch all messages pending
 | 112 | # fetch all messages pending from e-mail accounts
 | 
          
            | 38 | `/usr/bin/php getSysinfoMail.php`;
 | 113 | `php $getMailScript` if $CHECKMAIL;
 | 
          
            | 39 |  
 | 114 |  
 | 
          
            | 40 | # get a list of all messages waiting to be processed
 | 115 | # get a list of all messages waiting to be processed
 | 
          
            | 41 | opendir ( my $dh, $UNPROCESSED ) or die "Could not open $UNPROCESSED for read: $!";
 | 116 | opendir ( my $dh, $UNPROCESSED ) or die "Could not open $UNPROCESSED for read: $!";
 | 
          
            | 42 | @files = map{ "$UNPROCESSED\/$_" } sort grep { ! /^\./ && -f "$UNPROCESSED/$_" } readdir( $dh );
 | 117 | @files = map{ "$UNPROCESSED\/$_" } sort grep { ! /^\./ && -f "$UNPROCESSED/$_" } readdir( $dh );
 | 
          
            | 43 | closedir $dh;
 | 118 | closedir $dh;
 | 
          
            | 44 |  
 | 119 |  
 | 
          
            | 45 | my $count = 0;
 | 120 | my $count = 0;
 | 
          
            | 46 |  
 | 121 |  
 | 
          
            | 47 | foreach my $thisFile ( sort @files ) {
 | 122 | foreach my $thisFile ( sort @files ) {
 | 
          
            | - |   | 123 |    if ( $PROCESS ) {
 | 
          
            | 48 |   $results .=  `php parse_sysinfo.php <'$thisFile'`;
 | 124 |       $results .=  `php $processMailScript <'$thisFile'`;
 | 
          
            | 49 |   #print "$results\n\n";
 | - |   | 
          
            | 50 |   if ( $? == -1 ) {
 | 125 |       if ( $? == -1 ) {
 | 
          
            | 51 |    $exitCode = -1;
 | 126 |          $exitCode = -1;
 | 
          
            | 52 |    die "Parsing failed: $!\n";
 | 127 |          die "Parsing failed: $!\n";
 | 
          
            | - |   | 128 |       } else {
 | 
          
            | - |   | 129 |          $exitCode = $? >> 8;
 | 
          
            | - |   | 130 |       }
 | 
          
            | 53 |   } else {
 | 131 |    } else {
 | 
          
            | - |   | 132 |      $results .= "Not processing file $thisFile";
 | 
          
            | 54 |     $exitCode = $? >> 8;
 | 133 |      $exitCode = 0;
 | 
          
            | - |   | 134 |    }
 | 
          
            | 55 |     if ( $exitCode == 0 ) {
 | 135 |    if ( $exitCode == 0 ) {
 | 
          
            | 56 |        $filesProcessed{ 'valid' }++;
 | 136 |       $filesProcessed{ 'valid' }++;
 | 
          
            | 57 |        &storeFile( $thisFile );
 | 137 |       &storeFile( $thisFile ) if $MOVEFILES;
 | 
          
            | 58 |     } elsif ( $exitCode == 1 ) {
 | 138 |    } elsif ( $exitCode == 1 ) {
 | 
          
            | 59 |        $filesProcessed{ 'Invalid Format' }++;
 | 139 |       $filesProcessed{ 'Invalid Format' }++;
 | 
          
            | 60 |        &storeFile( $thisFile, 'InvalidFormat' );
 | 140 |       &storeFile( $thisFile, 'InvalidFormat' ) if $MOVEFILES;
 | 
          
            | 61 |     } elsif ( $exitCode == 2 || $exitCode == 3 ) {
 | 141 |    } elsif ( $exitCode == 2 || $exitCode == 3 ) {
 | 
          
            | 62 |        $filesProcessed{ 'Invalid Report' }++;
 | 142 |       $filesProcessed{ 'Invalid Report' }++;
 | 
          
            | 63 |        &storeFile( $thisFile, 'InvalidReport' );
 | 143 |       &storeFile( $thisFile, 'InvalidReport' ) if $MOVEFILES;
 | 
          
            | 64 |     } elsif ( $exitCode == 4 ) {
 | 144 |    } elsif ( $exitCode == 4 ) {
 | 
          
            | 65 |        $filesProcessed{ 'Duplicate Report' }++;
 | 145 |       $filesProcessed{ 'Duplicate Report' }++;
 | 
          
            | 66 |        &storeFile( $thisFile, 'DuplicateReport' );
 | 146 |       &storeFile( $thisFile, 'DuplicateReport' ) if $MOVEFILES;
 | 
          
            | 67 |     } elsif ( $exitCode != 5 && $exitCode != 6 ) { ## not any other of our valid exit codes
 | 147 |    } elsif ( $exitCode != 5 && $exitCode != 6 ) { ## not any other of our valid exit codes
 | 
          
            | 68 |        die "parse_sysinfo.php returned an unknown exit code $exitCode\n";
 | 148 |       die "parse_sysinfo.php returned an unknown exit code $exitCode for $thisFile\n";
 | 
          
            | 69 |     } else {
 | 149 |    } else {
 | 
          
            | 70 |        # at this point, we only have reports waiting for manual CAMP
 | 150 |       # at this point, we only have reports waiting for manual CAMP
 | 
          
            | 71 |        # updates, so just leave them where they are
 | 151 |       # updates, so just leave them where they are
 | 
          
            | 72 |        $filesProcessed{ 'Waiting CAMP Updates' }++;
 | 152 |       $filesProcessed{ 'Waiting CAMP Updates' }++;
 | 
          
            | 73 |     }
 | 153 |    }
 | 
          
            | 74 |     #print STDERR '.';
 | - |   | 
          
            | 75 |     # printf "command exited with value %d\n", $? >> 8;
 | - |   | 
          
            | 76 |   }
 | - |   | 
          
            | 77 |   last if ++$count >= $MAXTOPROCESS;
 | 154 |   last if ++$count >= $MAXTOPROCESS;
 | 
          
            | 78 |   print STDERR "\r$count" unless $CRON;
 | 155 |   print STDERR "\r$count" unless $CRON;
 | 
          
            | 79 | }
 | 156 | }
 | 
          
            | 80 |  
 | 157 |  
 | 
          
            | - |   | 158 | my $emailString;
 | 
          
            | 81 | print "\n" unless $CRON; 
 | 159 | print "\n" unless $CRON; 
 | 
          
            | 82 | $count= 0;
 | 160 | $count= 0;
 | 
          
            | 83 | foreach my $key ( sort keys %filesProcessed ) {
 | 161 | foreach my $key ( sort keys %filesProcessed ) {
 | 
          
            | 84 |    $count += $filesProcessed{$key};
 | 162 |    $count += $filesProcessed{$key};
 | 
          
            | 85 |    print "$filesProcessed{$key}\t$key\n";
 | 163 |    $emailString .=  "$filesProcessed{$key}\t$key\n";
 | 
          
            | 86 | }
 | 164 | }
 | 
          
            | 87 | print "$count\tTotal Files Processed\n";
 | 165 | $emailString .=  "$count\tTotal Files Processed\n";
 | 
          
            | 88 | print "--------------------------------\n\n";
 | 166 | $emailString .=  "--------------------------------\n\n";
 | 
          
            | 89 | print $results;
 | 167 | $emailString .=  $results;
 | 
          
            | - |   | 168 |  
 | 
          
            | - |   | 169 | &sendReport( $$config{'sendReport'}, $emailString );
 | 
          
            | - |   | 170 |  
 | 
          
            | 90 | 1;
 | 171 | 1;
 |