Rev 1 | Blame | Compare with Previous | Last modification | View Log | Download | RSS feed
#! /usr/bin/perl -w
# part of the command line tools for process_sysinfo. Will take a list of one or more
# directories and read the message in. These directories are assumed to be reports, possibly
# with e-mail headers. Will then search for the key [report date] in the file and, if found,
# adds the file name (and date) to the list of files to be processed.
# when the files to be processed has been created, will call process_sysinfo with each file
# as input
# v2.0 20121003
# changed SYSINFO to an array, allowing different processors for different formats of files
my $TESTING = 0;
my $saveDir;
my @mailDir;
my %SYSINFO;
my $output = ''; # place to store any error messages
my $startTime = `date`;
my $VERSION = '2.0';
# standard find and load configuration file
sub loadConfigurationFile {
my $configuration_file = shift;
use File::Basename;
use Cwd qw(realpath);
my $filename = realpath($0); # get my real path
my $directories;
my $suffix;
print "$configuration_file\n" if $TESTING;
$configuration_file = $filename unless $configuration_file;
print "$configuration_file\n" if $TESTING;
if ( $configuration_file !~ m/\// ) { # no path information
($filename, $directories, $suffix) = fileparse($filename,qr/\.[^.]*/); # break filename apart
#print "No Path Given\n";
} else {
($filename, $directories, $suffix) = fileparse($configuration_file,qr/\.[^.]*/); # break filename apart
$configuration_file = '';
#print "Path included\n";
}
unless (-e $directories . ($configuration_file ? $configuration_file : $filename) . '.conf' ) {
$lookingIn = $directories;
while ($lookingIn) {
$lookingIn =~ m/^(.*\/)[^\/]+\//;
$lookingIn = $1;
print "$lookingIn\n" if $TESTING;
if (-e $lookingIn . ($configuration_file ? $configuration_file : $filename) . '.conf' ) {
$directories = $lookingIn;
$lookingIn = '';
}
}
}
$configuration_file = $directories . ($configuration_file ? $configuration_file : $filename) . '.conf'; # add the .conf
# print "$configuration_file\n";
# die;
open CONFFILE, "<$configuration_file" or die "Can not open configuration file $configuration_file";
my $confFileContents = join( '', <CONFFILE> );
close CONFFILE;
return $confFileContents;
}
sub readFile {
my $filename = shift;
open DATAFILE,"<$filename" or return ''; # open the file
my @contents = <DATAFILE>;
close DATAFILE;
#while (@contents and ($contents[0] !~ m/^[\[<]sysinfo/)) { # skip past any header stuff
# shift @contents; # just throw it away
#}
return join('',@contents);
}
sub getReportDate {
my $filename = shift;
my $date = '';
my $format;
my $version;
if (my $contents = &readFile( $filename )) {
if ($contents =~ m/^\[report date\](\d+)$/m) {
$date = $1;
$format = 'winconf';
$contents =~ m/^\s*version\s*=\s*([0-9.]+)/;
$version = $1;
# print "Old Style Format\n";
} elsif ($contents =~ m/<report>.*<date>(\d{4}-\d{2}-\d{2} \d{2}:\d{2})<\/date>/s) {
$date = $1;
$date =~ s/[^0-9]//g;
$format = 'xml';
$contents =~ m/<version>([0-9.]+)<\/version>/;
$version = $1;
# print "XML Format $date\n";
} elsif ($contents =~ m/^\s+date:\s+"(\d{4}-\d{2}-\d{2} \d{2}:\d{2}:\d{2})"$/m) {
$date = $1;
$date =~ s/[^0-9]//g;
$format = 'yaml';
$contents =~ m/^\s+version:\s+([0-9.]+)$/m;
$version = $1;
# print "YAML format\n";
}
}
return ($date, $format, $version);
}
#print join "\n", @ARGV;
while (@ARGV) {
push @mailDir, shift;
}
#@mailDir = @ARGV if @ARGV;
BEGIN{
# load the configuration file
eval ( &loadConfigurationFile('process_sysinfo') );
push @INC, $LIBRARIES;
}
#@@mailDir = @ARGV if @ARGV;
#print '[' . join("\n",@mailDir) . ']';
#die;
my @fileList; # global that will hold the list of all files, formatted datetime\tfilename
while ( $directory = shift @mailDir ) { # while the user has passed in a directory to process
#print "Processing $directory\n";
#next;
opendir (DATADIR,$directory) || die "can't opendir $directory: $!"; # read the directory
while ( my $thisFile = readdir(DATADIR) ) { # for each file
#print "Looking at $thisFile\n";
next if $thisFile =~ m/^\./; # skip if it is a "dot" file
#print "It is not a dot file\n";
next unless -f "$directory/$thisFile";
$thisFile = $directory . '/' . $thisFile; # ok, get fully qualified path/filename
print "Checking $thisFile\n" if $TESTING;
my ($date, $format, $version) = &getReportDate($thisFile);
unless ( $date && $format && $version ) {
print "Skipping file $thisFile\n";
next;
}
print "[$thisFile]\t[$date]\t[$format]\t[$version]\n" if $TESTING;
push @fileList,"$date\t$thisFile\t$format\t$version"; # create an entry to be processed
}
closedir(DATADIR);
} # while
#print join( "\n", sort @fileList ) . "\n";
#die "\n";
#qx(mysql -u root computer_asset_manager < ./create_table.sql);
my $fileCount = @fileList;
my $thisFileNumber = 0;
my $tempFile = '/tmp/process_sysinfo.log';
unlink $tempFile if -e $tempFile;
foreach my $thisFile ( sort @fileList ) {
exit if $thisFileNumber >= 5 and $TESTING > 4;
print "Processing file $thisFileNumber of $fileCount [$thisFile]\n" if $TESTING;
$thisFileNumber++;
my ($date,$filename,$format, $version) = split ("\t", $thisFile);
if ( my $SYSINFO = $SYSINFO{$format} ) {
if ( $TESTING ) {
print "\tProcessing with $SYSINFO\n";
next;
}
open( SYSINFO, "|$SYSINFO >> $tempFile" ) or die "Could not open sysinfo at $SYSINFO\n";
print SYSINFO &readFile($filename);
close SYSINFO;
if ( $? ) {# it worked
&postProcess( $filename );
}
} else {
warn "Error, no SYSINFO entry for format [$format]\n";
}
#my $messages = qx($SYSINFO < $filename);
#$output .= $messages;
#if ($messages !~ m/^ERROR:/) { # only post_process the file if no error occurred
# &postProcess( $filename );
#}
}
my $endTime = `date`;
chomp $endTime;
chomp $startTime;
print "\nBegan Processing at $startTime\nFinished at $endTime\n$fileCount files processed";
open LOG, "<$tempFile" or die "Could not read Log File $tempFile: $!";
while (my $line = <LOG>) {
print $line;
}
close LOG;
1;