Subversion Repositories computer_asset_manager_v1

Rev

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

my $TESTING = 1;
my $saveDir;
my @mailDir;
my $SYSINFO;
my $output = ''; # place to store any error messages
my $startTime = `date`;

# 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 = join('', <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 = '';
   if (my $contents = &readFile( $filename )) {
      if ($contents =~ m/^\[report date\](\d+)$/m) {
         $date = $1;
         #print "Old Style $date\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;
         #print "New Style $date\n";
      }
   }
   return $date;
}

#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 "Preparing $thisFile\n";
      next unless my $date = &getReportDate($thisFile);
      push @fileList,"$date\t$thisFile"; # 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;
   print "\x0dProcessing file $thisFileNumber of $fileCount [$thisFile]" if $TESTING;
   
   $thisFileNumber++;
   my ($date,$filename) = split ("\t", $thisFile);
   
   open( SYSINFO, "|$SYSINFO >> $tempFile" ) or die "Could not open sysinfo at $SYSINFO\n";
   print SYSINFO &readFile($filename);
   close SYSINFO;
   if ( $? ) {# it worked
      &postProcess( $filename );
   }
   #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;