Rev 6 | Go to most recent revision | Blame | Compare with Previous | Last modification | View Log | Download | RSS feed
#! /usr/bin/perl -w
use IO::Interactive qw(is_interactive interactive busy);
my $CRON= ! is_interactive();
my $DATADIR = '/home/rodolico/temp/sysinfo_reports';
my $UNPROCESSED = "$DATADIR/unprocessed";
my $MAXTOPROCESS = 10000;
my %filesProcessed;
# assumes parse_sysinfo.php returns one of the following codes
# 0 - Processed Normally
# 1 - could not process file (not xml, yaml or ini)
# 2 - Invalid report, does not have one or more of report date, client name or computer name
# 3 - Invalid Report, invalid machine name
# 4 - Duplicate Report
# 5 - Valid report, but no entry for client and/or machine in database
# 6 - Valid report, but waiting for client and/or machine to be added in database
sub storeFile {
use File::Basename;
my $file = shift;
my $targetDirectory = shift;
unless ( $targetDirectory ) {
my ($name,$path) = fileparse($file);
my ( $date,$time,$client,$server,$serial) = split( '_', $name );
my ( $year, $month, $day ) = split( '-', $date );
$targetDirectory = "/$year/$month";
}
$targetDirectory = "$DATADIR/$targetDirectory";
`mkdir -p '$targetDirectory'` unless -d $targetDirectory;
`mv '$file' '$targetDirectory'`;
}
# fetch all messages pending
`/usr/bin/php getSysinfoMail.php`;
# get a list of all messages waiting to be processed
opendir ( my $dh, $UNPROCESSED ) or die "Could not open $UNPROCESSED for read: $!";
@files = map{ "$UNPROCESSED\/$_" } sort grep { ! /^\./ && -f "$UNPROCESSED/$_" } readdir( $dh );
closedir $dh;
my $count = 0;
foreach my $thisFile ( sort @files ) {
$results .= `php parse_sysinfo.php <'$thisFile'`;
#print "$results\n\n";
if ( $? == -1 ) {
$exitCode = -1;
die "Parsing failed: $!\n";
} else {
$exitCode = $? >> 8;
if ( $exitCode == 0 ) {
$filesProcessed{ 'valid' }++;
&storeFile( $thisFile );
} elsif ( $exitCode == 1 ) {
$filesProcessed{ 'Invalid Format' }++;
&storeFile( $thisFile, 'InvalidFormat' );
} elsif ( $exitCode == 2 || $exitCode == 3 ) {
$filesProcessed{ 'Invalid Report' }++;
&storeFile( $thisFile, 'InvalidReport' );
} elsif ( $exitCode == 4 ) {
$filesProcessed{ 'Duplicate Report' }++;
&storeFile( $thisFile, 'DuplicateReport' );
} elsif ( $exitCode != 5 && $exitCode != 6 ) { ## not any other of our valid exit codes
die "parse_sysinfo.php returned an unknown exit code $exitCode\n";
} else {
# at this point, we only have reports waiting for manual CAMP
# updates, so just leave them where they are
$filesProcessed{ 'Waiting CAMP Updates' }++;
}
#print STDERR '.';
# printf "command exited with value %d\n", $? >> 8;
}
last if ++$count >= $MAXTOPROCESS;
print STDERR "\r$count" unless $CRON;
}
print "\n" unless $CRON;
$count= 0;
foreach my $key ( sort keys %filesProcessed ) {
$count += $filesProcessed{$key};
print "$filesProcessed{$key}\t$key\n";
}
print "$count\tTotal Files Processed\n";
print "--------------------------------\n\n";
print $results;
1;