| 3 | rodolico | 1 | #! /usr/bin/perl -w
 | 
        
           |  |  | 2 |   | 
        
           |  |  | 3 | use IO::Interactive qw(is_interactive interactive busy);
 | 
        
           |  |  | 4 |   | 
        
           |  |  | 5 | my $CRON= ! is_interactive();
 | 
        
           |  |  | 6 |   | 
        
           |  |  | 7 | my $DATADIR = '/home/rodolico/temp/sysinfo_reports';
 | 
        
           |  |  | 8 | my $UNPROCESSED = "$DATADIR/unprocessed";
 | 
        
           |  |  | 9 | my $MAXTOPROCESS = 10000;
 | 
        
           |  |  | 10 | my %filesProcessed;
 | 
        
           |  |  | 11 |   | 
        
           |  |  | 12 | # assumes parse_sysinfo.php returns one of the following codes
 | 
        
           |  |  | 13 | # 0 - Processed Normally
 | 
        
           |  |  | 14 | # 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
 | 
        
           |  |  | 16 | # 3 - Invalid Report, invalid machine name
 | 
        
           |  |  | 17 | # 4 - Duplicate Report
 | 
        
           |  |  | 18 | # 5 - Valid report, but no entry for client and/or machine in database
 | 
        
           |  |  | 19 | # 6 - Valid report, but waiting for client and/or machine to be added in database
 | 
        
           |  |  | 20 |   | 
        
           |  |  | 21 | sub storeFile {
 | 
        
           |  |  | 22 |    use File::Basename;
 | 
        
           |  |  | 23 |    my $file = shift;
 | 
        
           |  |  | 24 |    my $targetDirectory = shift;
 | 
        
           |  |  | 25 |    unless ( $targetDirectory ) {
 | 
        
           |  |  | 26 |       my ($name,$path) = fileparse($file);
 | 
        
           |  |  | 27 |       my ( $date,$time,$client,$server,$serial) = split( '_', $name );
 | 
        
           |  |  | 28 |       my ( $year, $month, $day ) = split( '-', $date );
 | 
        
           |  |  | 29 |       $targetDirectory = "/$year/$month";
 | 
        
           |  |  | 30 |    }
 | 
        
           |  |  | 31 |    $targetDirectory = "$DATADIR/$targetDirectory";
 | 
        
           |  |  | 32 |    `mkdir -p '$targetDirectory'` unless -d $targetDirectory;
 | 
        
           |  |  | 33 |    `mv '$file' '$targetDirectory'`;
 | 
        
           |  |  | 34 | }
 | 
        
           |  |  | 35 |   | 
        
           |  |  | 36 |   | 
        
           |  |  | 37 | # fetch all messages pending
 | 
        
           |  |  | 38 | `/usr/bin/php getSysinfoMail.php`;
 | 
        
           |  |  | 39 |   | 
        
           |  |  | 40 | # get a list of all messages waiting to be processed
 | 
        
           |  |  | 41 | opendir ( my $dh, $UNPROCESSED ) or die "Could not open $UNPROCESSED for read: $!";
 | 
        
           |  |  | 42 | @files = map{ "$UNPROCESSED\/$_" } sort grep { ! /^\./ && -f "$UNPROCESSED/$_" } readdir( $dh );
 | 
        
           |  |  | 43 | closedir $dh;
 | 
        
           |  |  | 44 |   | 
        
           |  |  | 45 | my $count = 0;
 | 
        
           |  |  | 46 |   | 
        
           |  |  | 47 | foreach my $thisFile ( sort @files ) {
 | 
        
           |  |  | 48 |   $results .=  `php parse_sysinfo.php <'$thisFile'`;
 | 
        
           |  |  | 49 |   #print "$results\n\n";
 | 
        
           |  |  | 50 |   if ( $? == -1 ) {
 | 
        
           |  |  | 51 |    $exitCode = -1;
 | 
        
           |  |  | 52 |    die "Parsing failed: $!\n";
 | 
        
           |  |  | 53 |   } else {
 | 
        
           |  |  | 54 |     $exitCode = $? >> 8;
 | 
        
           |  |  | 55 |     if ( $exitCode == 0 ) {
 | 
        
           |  |  | 56 |        $filesProcessed{ 'valid' }++;
 | 
        
           |  |  | 57 |        &storeFile( $thisFile );
 | 
        
           |  |  | 58 |     } elsif ( $exitCode == 1 ) {
 | 
        
           |  |  | 59 |        $filesProcessed{ 'Invalid Format' }++;
 | 
        
           |  |  | 60 |        &storeFile( $thisFile, 'InvalidFormat' );
 | 
        
           |  |  | 61 |     } elsif ( $exitCode == 2 || $exitCode == 3 ) {
 | 
        
           |  |  | 62 |        $filesProcessed{ 'Invalid Report' }++;
 | 
        
           |  |  | 63 |        &storeFile( $thisFile, 'InvalidReport' );
 | 
        
           |  |  | 64 |     } elsif ( $exitCode == 4 ) {
 | 
        
           |  |  | 65 |        $filesProcessed{ 'Duplicate Report' }++;
 | 
        
           |  |  | 66 |        &storeFile( $thisFile, 'DuplicateReport' );
 | 
        
           |  |  | 67 |     } 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";
 | 
        
           |  |  | 69 |     } else {
 | 
        
           |  |  | 70 |        # at this point, we only have reports waiting for manual CAMP
 | 
        
           |  |  | 71 |        # updates, so just leave them where they are
 | 
        
           |  |  | 72 |        $filesProcessed{ 'Waiting CAMP Updates' }++;
 | 
        
           |  |  | 73 |     }
 | 
        
           |  |  | 74 |     #print STDERR '.';
 | 
        
           |  |  | 75 |     # printf "command exited with value %d\n", $? >> 8;
 | 
        
           |  |  | 76 |   }
 | 
        
           |  |  | 77 |   last if ++$count >= $MAXTOPROCESS;
 | 
        
           |  |  | 78 |   print STDERR "\r$count" unless $CRON;
 | 
        
           |  |  | 79 | }
 | 
        
           |  |  | 80 |   | 
        
           |  |  | 81 | print "\n" unless $CRON; 
 | 
        
           |  |  | 82 | $count= 0;
 | 
        
           |  |  | 83 | foreach my $key ( sort keys %filesProcessed ) {
 | 
        
           |  |  | 84 |    $count += $filesProcessed{$key};
 | 
        
           |  |  | 85 |    print "$filesProcessed{$key}\t$key\n";
 | 
        
           |  |  | 86 | }
 | 
        
           |  |  | 87 | print "$count\tTotal Files Processed\n";
 | 
        
           |  |  | 88 | print "--------------------------------\n\n";
 | 
        
           |  |  | 89 | print $results;
 | 
        
           |  |  | 90 | 1;
 |