| 1 | rodolico | 1 | #!  /usr/bin/perl -w
 | 
        
           |  |  | 2 |   | 
        
           |  |  | 3 | # part of the command line tools for process_sysinfo. Will take a list of one or more
 | 
        
           |  |  | 4 | # directories and read the message in. These directories are assumed to be reports, possibly
 | 
        
           |  |  | 5 | # with e-mail headers. Will then search for the key [report date] in the file and, if found,
 | 
        
           |  |  | 6 | # adds the file name (and date) to the list of files to be processed.
 | 
        
           |  |  | 7 | # when the files to be processed has been created, will call process_sysinfo with each file
 | 
        
           |  |  | 8 | # as input
 | 
        
           |  |  | 9 |   | 
        
           |  |  | 10 | my $TESTING = 1;
 | 
        
           |  |  | 11 | my $saveDir;
 | 
        
           |  |  | 12 | my @mailDir;
 | 
        
           |  |  | 13 | my $SYSINFO;
 | 
        
           |  |  | 14 | my $output = ''; # place to store any error messages
 | 
        
           |  |  | 15 | my $startTime = `date`;
 | 
        
           |  |  | 16 |   | 
        
           |  |  | 17 | # standard find and load configuration file
 | 
        
           |  |  | 18 | sub loadConfigurationFile {
 | 
        
           |  |  | 19 |    my $configuration_file = shift;
 | 
        
           |  |  | 20 |   | 
        
           |  |  | 21 |    use File::Basename;
 | 
        
           |  |  | 22 |    use Cwd qw(realpath);
 | 
        
           |  |  | 23 |   | 
        
           |  |  | 24 |    my $filename  = realpath($0); # get my real path
 | 
        
           |  |  | 25 |    my $directories;
 | 
        
           |  |  | 26 |    my $suffix;
 | 
        
           |  |  | 27 |    print "$configuration_file\n" if $TESTING;
 | 
        
           |  |  | 28 |    $configuration_file = $filename unless $configuration_file;
 | 
        
           |  |  | 29 |    print "$configuration_file\n" if $TESTING;
 | 
        
           |  |  | 30 |   | 
        
           |  |  | 31 |    if ( $configuration_file !~ m/\// ) { # no path information
 | 
        
           |  |  | 32 |       ($filename, $directories, $suffix) = fileparse($filename,qr/\.[^.]*/); # break filename apart
 | 
        
           |  |  | 33 |       #print "No Path Given\n";
 | 
        
           |  |  | 34 |    } else {
 | 
        
           |  |  | 35 |       ($filename, $directories, $suffix) = fileparse($configuration_file,qr/\.[^.]*/); # break filename apart
 | 
        
           |  |  | 36 |       $configuration_file = '';
 | 
        
           |  |  | 37 |       #print "Path included\n";
 | 
        
           |  |  | 38 |    }
 | 
        
           |  |  | 39 |    unless (-e $directories . ($configuration_file ? $configuration_file : $filename) . '.conf' ) {
 | 
        
           |  |  | 40 |       $lookingIn = $directories;
 | 
        
           |  |  | 41 |       while ($lookingIn) {
 | 
        
           |  |  | 42 |          $lookingIn =~ m/^(.*\/)[^\/]+\//;
 | 
        
           |  |  | 43 |          $lookingIn = $1;
 | 
        
           |  |  | 44 |          print "$lookingIn\n" if $TESTING;
 | 
        
           |  |  | 45 |          if (-e $lookingIn . ($configuration_file ? $configuration_file : $filename) . '.conf' ) {
 | 
        
           |  |  | 46 |             $directories = $lookingIn;
 | 
        
           |  |  | 47 |             $lookingIn = '';
 | 
        
           |  |  | 48 |          }
 | 
        
           |  |  | 49 |       }
 | 
        
           |  |  | 50 |    }
 | 
        
           |  |  | 51 |    $configuration_file = $directories . ($configuration_file ? $configuration_file : $filename) . '.conf'; # add the .conf
 | 
        
           |  |  | 52 | #   print "$configuration_file\n";
 | 
        
           |  |  | 53 | #   die;
 | 
        
           |  |  | 54 |    open CONFFILE, "<$configuration_file" or die "Can not open configuration file $configuration_file";
 | 
        
           |  |  | 55 |    my $confFileContents = join( '', <CONFFILE> );
 | 
        
           |  |  | 56 |    close CONFFILE;
 | 
        
           |  |  | 57 |    return $confFileContents;
 | 
        
           |  |  | 58 | }
 | 
        
           |  |  | 59 |   | 
        
           |  |  | 60 | sub readFile {
 | 
        
           |  |  | 61 |    my $filename = shift;
 | 
        
           |  |  | 62 |    open DATAFILE,"<$filename" or return ''; # open the file
 | 
        
           |  |  | 63 |    my @contents = join('', <DATAFILE>);
 | 
        
           |  |  | 64 |    close DATAFILE;
 | 
        
           |  |  | 65 |    #while (@contents and ($contents[0] !~ m/^[\[<]sysinfo/)) { # skip past any header stuff
 | 
        
           |  |  | 66 |    #   shift @contents; # just throw it away
 | 
        
           |  |  | 67 |    #}
 | 
        
           |  |  | 68 |    return join('',@contents);
 | 
        
           |  |  | 69 | }
 | 
        
           |  |  | 70 |   | 
        
           |  |  | 71 |   | 
        
           |  |  | 72 | sub getReportDate {
 | 
        
           |  |  | 73 |    my $filename = shift;
 | 
        
           |  |  | 74 |    my $date = '';
 | 
        
           |  |  | 75 |    if (my $contents = &readFile( $filename )) {
 | 
        
           |  |  | 76 |       if ($contents =~ m/^\[report date\](\d+)$/m) {
 | 
        
           |  |  | 77 |          $date = $1;
 | 
        
           |  |  | 78 |          #print "Old Style $date\n";
 | 
        
           |  |  | 79 |       } elsif ($contents =~ m/<report>.*<date>(\d{4}-\d{2}-\d{2} \d{2}:\d{2})<\/date>/s) {
 | 
        
           |  |  | 80 |          $date = $1;
 | 
        
           |  |  | 81 |          $date =~ s/[^0-9]//g;
 | 
        
           |  |  | 82 |          #print "New Style $date\n";
 | 
        
           |  |  | 83 |       }
 | 
        
           |  |  | 84 |    }
 | 
        
           |  |  | 85 |    return $date;
 | 
        
           |  |  | 86 | }
 | 
        
           |  |  | 87 |   | 
        
           |  |  | 88 | #print join "\n", @ARGV;
 | 
        
           |  |  | 89 | while (@ARGV) {
 | 
        
           |  |  | 90 |    push @mailDir, shift;
 | 
        
           |  |  | 91 | }
 | 
        
           |  |  | 92 | #@mailDir = @ARGV if @ARGV;
 | 
        
           |  |  | 93 |   | 
        
           |  |  | 94 | BEGIN{
 | 
        
           |  |  | 95 |    # load the configuration file
 | 
        
           |  |  | 96 |    eval ( &loadConfigurationFile('process_sysinfo') );
 | 
        
           |  |  | 97 |    push @INC, $LIBRARIES;
 | 
        
           |  |  | 98 | }
 | 
        
           |  |  | 99 |   | 
        
           |  |  | 100 | #@@mailDir = @ARGV if @ARGV;
 | 
        
           |  |  | 101 |   | 
        
           |  |  | 102 | #print '[' . join("\n",@mailDir) . ']';
 | 
        
           |  |  | 103 | #die;
 | 
        
           |  |  | 104 | my @fileList;  # global that will hold the list of all files, formatted datetime\tfilename
 | 
        
           |  |  | 105 | while ( $directory = shift @mailDir ) { # while the user has passed in a directory to process
 | 
        
           |  |  | 106 |    #print "Processing $directory\n";
 | 
        
           |  |  | 107 |    #next;
 | 
        
           |  |  | 108 |    opendir (DATADIR,$directory)  || die "can't opendir $directory: $!"; # read the directory
 | 
        
           |  |  | 109 |    while ( my $thisFile = readdir(DATADIR) ) { # for each file
 | 
        
           |  |  | 110 |       #print "Looking at $thisFile\n";
 | 
        
           |  |  | 111 |       next if $thisFile =~ m/^\./; # skip if it is a "dot" file
 | 
        
           |  |  | 112 |       #print "It is not a dot file\n";
 | 
        
           |  |  | 113 |       next unless -f "$directory/$thisFile";
 | 
        
           |  |  | 114 |       $thisFile = $directory . '/' . $thisFile; # ok, get fully qualified path/filename
 | 
        
           |  |  | 115 |       # print "Preparing $thisFile\n";
 | 
        
           |  |  | 116 |       next unless my $date = &getReportDate($thisFile);
 | 
        
           |  |  | 117 |       push @fileList,"$date\t$thisFile"; # create an entry to be processed
 | 
        
           |  |  | 118 |    }
 | 
        
           |  |  | 119 |    closedir(DATADIR);
 | 
        
           |  |  | 120 | } # while
 | 
        
           |  |  | 121 |   | 
        
           |  |  | 122 | #print join( "\n", sort @fileList ) . "\n";
 | 
        
           |  |  | 123 | #die "\n";
 | 
        
           |  |  | 124 |   | 
        
           |  |  | 125 | #qx(mysql -u root computer_asset_manager < ./create_table.sql);
 | 
        
           |  |  | 126 | my $fileCount = @fileList;
 | 
        
           |  |  | 127 | my $thisFileNumber = 0;
 | 
        
           |  |  | 128 | my $tempFile = '/tmp/process_sysinfo.log';
 | 
        
           |  |  | 129 | unlink $tempFile if -e $tempFile;
 | 
        
           |  |  | 130 | foreach my $thisFile ( sort @fileList ) {
 | 
        
           |  |  | 131 | #   exit if $thisFileNumber >= 5;
 | 
        
           |  |  | 132 |    print "\x0dProcessing file $thisFileNumber of $fileCount [$thisFile]" if $TESTING;
 | 
        
           |  |  | 133 |   | 
        
           |  |  | 134 |    $thisFileNumber++;
 | 
        
           |  |  | 135 |    my ($date,$filename) = split ("\t", $thisFile);
 | 
        
           |  |  | 136 |   | 
        
           |  |  | 137 |    open( SYSINFO, "|$SYSINFO >> $tempFile" ) or die "Could not open sysinfo at $SYSINFO\n";
 | 
        
           |  |  | 138 |    print SYSINFO &readFile($filename);
 | 
        
           |  |  | 139 |    close SYSINFO;
 | 
        
           |  |  | 140 |    if ( $? ) {# it worked
 | 
        
           |  |  | 141 |       &postProcess( $filename );
 | 
        
           |  |  | 142 |    }
 | 
        
           |  |  | 143 |    #my $messages = qx($SYSINFO < $filename);
 | 
        
           |  |  | 144 |    #$output .= $messages;
 | 
        
           |  |  | 145 |    #if ($messages !~ m/^ERROR:/) { # only post_process the file if no error occurred
 | 
        
           |  |  | 146 |    #   &postProcess( $filename );
 | 
        
           |  |  | 147 |    #}
 | 
        
           |  |  | 148 | }
 | 
        
           |  |  | 149 |   | 
        
           |  |  | 150 | my $endTime = `date`;
 | 
        
           |  |  | 151 | chomp $endTime;
 | 
        
           |  |  | 152 | chomp $startTime;
 | 
        
           |  |  | 153 | print "\nBegan Processing at $startTime\nFinished at $endTime\n$fileCount files processed";
 | 
        
           |  |  | 154 | open LOG, "<$tempFile" or die "Could not read Log File $tempFile: $!";
 | 
        
           |  |  | 155 | while (my $line = <LOG>) {
 | 
        
           |  |  | 156 |    print $line;
 | 
        
           |  |  | 157 | }
 | 
        
           |  |  | 158 | close LOG;
 | 
        
           |  |  | 159 |   | 
        
           |  |  | 160 | 1;
 |