Rev 74 | Go to most recent revision | Blame | Compare with Previous | Last modification | View Log | Download | RSS feed
#!/usr/bin/env perl
use strict;
use warnings;
# v0.2 20160205 RWR
# Removed echoing the results from parse_sysinfo for all normally processed
# reports. Only if a report is invalid, duped, or waiting on user input
# is anything from parse_sysinfo echoed.
#
# v0.3 20160205 RWR
# Removed most of the reporting, and put it in script sendReport.pl
# which is called from here.
use IO::Interactive qw(is_interactive interactive busy);
use YAML::XS;
use Cwd 'abs_path';
#use Data::Dumper;
use File::Basename;
my $VERSION = '0.8';
my $MY_DIRECTORY = abs_path(dirname(__FILE__) );
my $CRON= ! is_interactive(); # determine if we are in interactive shell
# control the flow of the program. $CHECKMAIL is pretty clear.
# if $PROCESS is false and $MOVEFILES is true, the files will be moved
# but not processed (ie, they were processed some other way)
my $CHECKMAIL=1; #controls whether we will check the mail or not
my $PROCESS=1; # controls whether we will process the files
my $MOVEFILES=1; # controls whether we will move the files successfully processed
my $DATADIR; # will hold the reports directory
my $UNPROCESSED; # will hold the location for unprocessed reports
my $MAXTOPROCESS = 10000;
my %filesProcessed;
# following are used to find the configuration file
my $confFileName = "sysinfoRead.conf.yaml";
my @searchPaths = ( '/etc/camp', '/opt/camp', '/opt/camp/sysinfo', $MY_DIRECTORY );
sub loadConfig {
my ( $confFileName, @searchPaths ) = @_;
my $configuration;
for ( my $i = 0; $i < @searchPaths; $i++ ) {
my $filename = $searchPaths[$i] . '/' . $confFileName;
if ( -e $filename ) {
#print "Found $filename\n";
open CONF, "<$filename" or warn "Could not read $filename: $!\n";
$configuration = Load( join( '', <CONF> ) );
close CONF;
last; # exit out of the loop; we don't try to load it more than once
} # if
} # foreach
return $configuration;
} # sub loadConfig
# 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'`;
}
sub validateFileType {
my %fileTypeConversions = (
'ISO-8859' => 'iconv -f ISO-8859-1 -t UTF-8'
);
# use File::Type;
my $thisFile = shift;
# my $ft = File::Type->new();
# die $ft->checktype_filename($thisFile);
my $filetype = `file '$thisFile'`;
chomp $filetype;
$filetype =~ m/:\s+([a-z0-9-]+)/i;
$filetype = $1;
return 1 if $filetype eq 'ASCII' || $filetype eq 'UTF-8';
if ( $fileTypeConversions{ $filetype } ) {
my $command = "mv '$thisFile' /tmp/toConvert ; $fileTypeConversions{ $filetype } -o '$thisFile' /tmp/toConvert";
print "$command\n";
#return 1;
} else {
warn "Unknown file type $filetype\n";
}
return 0;
}
# get our configuration set up first
my $config = &loadConfig( $confFileName, @searchPaths );
die "Could not find configuration file $confFileName in " . join( ', ', @searchPaths) . "\n" unless $config;
# just some convenience variables
$DATADIR = $$config{'datapath'};
$UNPROCESSED=$DATADIR . '/' . $$config{'unprocessed_path'};
# check that the executables exist before continuing
my $getMailScript = "$MY_DIRECTORY/" . $$config{'getMailScript'};
my $processMailScript = "$MY_DIRECTORY/" . $$config{'processMailScript'};
my $reportScript = "$MY_DIRECTORY/" . $$config{'reportScript'};
die "Could not find the getMailScript [$getMailScript] in $MY_DIRECTORY\n" unless -e $getMailScript;
die "Could not find the processMailScript [$processMailScript] in $MY_DIRECTORY\n" unless -e $processMailScript;
die "Could not find the reportScript [$reportScript] in $MY_DIRECTORY\n" unless -e $reportScript;
#die "$getMailScript\n$processMailScript\n$reportScript\n";
# fetch all messages pending from e-mail accounts
`php $getMailScript` if $CHECKMAIL;
# get a list of all messages waiting to be processed
opendir ( my $dh, $UNPROCESSED ) or die "Could not open $UNPROCESSED for read: $!";
my @files = map{ "$UNPROCESSED\/$_" } sort grep { ! /^\./ && -f "$UNPROCESSED/$_" } readdir( $dh );
closedir $dh;
#print join( "\n", @files ) . "\n"; die;
my $count = 0;
my $results = '';
foreach my $thisFile ( sort @files ) {
my $exitCode;
my $tempResults;
# next unless &validateFileType( $thisFile );
# die "Stopping on valid file $thisFile\n";
if ( $PROCESS ) {
$tempResults .= `php $processMailScript <'$thisFile'`;
if ( $? == -1 ) {
$exitCode = -1;
die "Parsing failed: $!\n";
} else {
$exitCode = $? >> 8;
}
} else {
$results .= "$thisFile moving without processing\n";
$exitCode = 0;
}
if ( $exitCode == 0 ) {
$filesProcessed{ 'valid' }++;
$tempResults = '';
&storeFile( $thisFile ) if $MOVEFILES;
} elsif ( $exitCode == 1 ) {
$filesProcessed{ 'Invalid Format' }++;
&storeFile( $thisFile, 'InvalidFormat' ) if $MOVEFILES;
} elsif ( $exitCode == 2 || $exitCode == 3 ) {
$filesProcessed{ 'Invalid Report' }++;
&storeFile( $thisFile, 'InvalidReport' ) if $MOVEFILES;
} elsif ( $exitCode == 4 ) {
$filesProcessed{ 'Duplicate Report' }++;
&storeFile( $thisFile, 'DuplicateReport' ) if $MOVEFILES;
} elsif ( $exitCode != 5 && $exitCode != 6 ) { ## not any other of our valid exit codes
die "parse_sysinfo.php returned an unknown exit code $exitCode for $thisFile\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' }++;
}
$results .= $tempResults;
last if ++$count >= $MAXTOPROCESS;
print STDERR "\r$count" unless $CRON;
}
my $emailString;
print "\n" unless $CRON;
$count= 0;
foreach my $key ( sort keys %filesProcessed ) {
$count += $filesProcessed{$key};
$emailString .= "$filesProcessed{$key}\t$key\n";
}
$emailString .= "$count\tTotal Files Processed\n";
$emailString .= "--------------------------------\n\n";
$emailString .= $results;
open SEND, "|$reportScript" or die "Could not find sendReport.pl: $!\n";
print SEND $emailString;
close SEND;
1;