| 34 | rodolico | 1 | #!/usr/bin/env perl
 | 
        
           | 3 | rodolico | 2 |   | 
        
           | 34 | rodolico | 3 | use strict;
 | 
        
           |  |  | 4 | use warnings;
 | 
        
           |  |  | 5 |   | 
        
           | 9 | rodolico | 6 | # v0.2 20160205 RWR
 | 
        
           |  |  | 7 | # Removed echoing the results from parse_sysinfo for all normally processed
 | 
        
           |  |  | 8 | # reports. Only if a report is invalid, duped, or waiting on user input
 | 
        
           |  |  | 9 | # is anything from parse_sysinfo echoed.
 | 
        
           |  |  | 10 | #
 | 
        
           |  |  | 11 | # v0.3 20160205 RWR
 | 
        
           |  |  | 12 | # Removed most of the reporting, and put it in script sendReport.pl
 | 
        
           |  |  | 13 | # which is called from here.
 | 
        
           |  |  | 14 |   | 
        
           | 3 | rodolico | 15 | use IO::Interactive qw(is_interactive interactive busy);
 | 
        
           | 6 | rodolico | 16 | use YAML::XS;
 | 
        
           |  |  | 17 | use Cwd 'abs_path';
 | 
        
           |  |  | 18 | #use Data::Dumper;
 | 
        
           | 3 | rodolico | 19 |   | 
        
           | 8 | rodolico | 20 | use File::Basename;
 | 
        
           |  |  | 21 |   | 
        
           | 9 | rodolico | 22 | my $VERSION = '0.8';
 | 
        
           | 8 | rodolico | 23 | my $MY_DIRECTORY = abs_path(dirname(__FILE__) );
 | 
        
           |  |  | 24 |   | 
        
           | 6 | rodolico | 25 | my $CRON= ! is_interactive(); # determine if we are in interactive shell
 | 
        
           | 3 | rodolico | 26 |   | 
        
           | 6 | rodolico | 27 | # control the flow of the program. $CHECKMAIL is pretty clear.
 | 
        
           |  |  | 28 | # if $PROCESS is false and $MOVEFILES is true, the files will be moved
 | 
        
           |  |  | 29 | # but not processed (ie, they were processed some other way)
 | 
        
           |  |  | 30 | my $CHECKMAIL=1; #controls whether we will check the mail or not
 | 
        
           |  |  | 31 | my $PROCESS=1; # controls whether we will process the files
 | 
        
           |  |  | 32 | my $MOVEFILES=1; # controls whether we will move the files successfully processed
 | 
        
           |  |  | 33 |   | 
        
           |  |  | 34 | my $DATADIR; # will hold the reports directory
 | 
        
           |  |  | 35 | my $UNPROCESSED; # will hold the location for unprocessed reports
 | 
        
           | 3 | rodolico | 36 | my $MAXTOPROCESS = 10000;
 | 
        
           |  |  | 37 | my %filesProcessed;
 | 
        
           |  |  | 38 |   | 
        
           | 6 | rodolico | 39 | # following are used to find the configuration file
 | 
        
           |  |  | 40 | my $confFileName = "sysinfoRead.conf.yaml";
 | 
        
           | 8 | rodolico | 41 | my @searchPaths = ( '/etc/camp', '/opt/camp', '/opt/camp/sysinfo', $MY_DIRECTORY );
 | 
        
           | 6 | rodolico | 42 |   | 
        
           |  |  | 43 |   | 
        
           |  |  | 44 | sub loadConfig {
 | 
        
           |  |  | 45 |    my ( $confFileName, @searchPaths ) = @_;
 | 
        
           |  |  | 46 |    my $configuration;
 | 
        
           |  |  | 47 |   | 
        
           |  |  | 48 |   | 
        
           | 34 | rodolico | 49 |    for ( my $i = 0; $i < @searchPaths; $i++ ) {
 | 
        
           |  |  | 50 |       my $filename = $searchPaths[$i] . '/' . $confFileName;
 | 
        
           | 6 | rodolico | 51 |       if ( -e $filename ) {
 | 
        
           |  |  | 52 |          #print "Found $filename\n";
 | 
        
           |  |  | 53 |          open CONF, "<$filename" or warn "Could not read $filename: $!\n";
 | 
        
           |  |  | 54 |          $configuration = Load( join( '', <CONF> ) );
 | 
        
           | 8 | rodolico | 55 |          close CONF;
 | 
        
           | 6 | rodolico | 56 |          last; # exit out of the loop; we don't try to load it more than once
 | 
        
           |  |  | 57 |       } # if
 | 
        
           |  |  | 58 |    } # foreach
 | 
        
           |  |  | 59 |    return $configuration;
 | 
        
           |  |  | 60 | } # sub loadConfig
 | 
        
           |  |  | 61 |   | 
        
           | 3 | rodolico | 62 | # assumes parse_sysinfo.php returns one of the following codes
 | 
        
           |  |  | 63 | # 0 - Processed Normally
 | 
        
           |  |  | 64 | # 1 - could not process file (not xml, yaml or ini)
 | 
        
           |  |  | 65 | # 2 - Invalid report, does not have one or more of report date, client name or computer name
 | 
        
           |  |  | 66 | # 3 - Invalid Report, invalid machine name
 | 
        
           |  |  | 67 | # 4 - Duplicate Report
 | 
        
           |  |  | 68 | # 5 - Valid report, but no entry for client and/or machine in database
 | 
        
           |  |  | 69 | # 6 - Valid report, but waiting for client and/or machine to be added in database
 | 
        
           |  |  | 70 |   | 
        
           |  |  | 71 | sub storeFile {
 | 
        
           |  |  | 72 |    use File::Basename;
 | 
        
           |  |  | 73 |    my $file = shift;
 | 
        
           |  |  | 74 |    my $targetDirectory = shift;
 | 
        
           |  |  | 75 |    unless ( $targetDirectory ) {
 | 
        
           |  |  | 76 |       my ($name,$path) = fileparse($file);
 | 
        
           |  |  | 77 |       my ( $date,$time,$client,$server,$serial) = split( '_', $name );
 | 
        
           |  |  | 78 |       my ( $year, $month, $day ) = split( '-', $date );
 | 
        
           |  |  | 79 |       $targetDirectory = "/$year/$month";
 | 
        
           |  |  | 80 |    }
 | 
        
           |  |  | 81 |    $targetDirectory = "$DATADIR/$targetDirectory";
 | 
        
           |  |  | 82 |    `mkdir -p '$targetDirectory'` unless -d $targetDirectory;
 | 
        
           |  |  | 83 |    `mv '$file' '$targetDirectory'`;
 | 
        
           |  |  | 84 | }
 | 
        
           |  |  | 85 |   | 
        
           | 6 | rodolico | 86 | # get our configuration set up first
 | 
        
           | 3 | rodolico | 87 |   | 
        
           | 6 | rodolico | 88 | my $config = &loadConfig( $confFileName, @searchPaths );
 | 
        
           | 8 | rodolico | 89 | die "Could not find configuration file $confFileName in " . join( ', ', @searchPaths) . "\n" unless $config;
 | 
        
           |  |  | 90 |   | 
        
           |  |  | 91 | # just some convenience variables
 | 
        
           | 6 | rodolico | 92 | $DATADIR = $$config{'datapath'};
 | 
        
           |  |  | 93 | $UNPROCESSED=$DATADIR . '/' . $$config{'unprocessed_path'};
 | 
        
           | 3 | rodolico | 94 |   | 
        
           | 8 | rodolico | 95 | # check that the executables exist before continuing
 | 
        
           | 34 | rodolico | 96 | my $getMailScript = "$MY_DIRECTORY/" . $$config{'getMailScript'};
 | 
        
           |  |  | 97 | my $processMailScript = "$MY_DIRECTORY/" . $$config{'processMailScript'};
 | 
        
           |  |  | 98 | my $reportScript = "$MY_DIRECTORY/" . $$config{'reportScript'};
 | 
        
           | 9 | rodolico | 99 |   | 
        
           | 6 | rodolico | 100 | die "Could not find the getMailScript [$getMailScript] in $MY_DIRECTORY\n" unless -e $getMailScript;
 | 
        
           |  |  | 101 | die "Could not find the processMailScript [$processMailScript] in $MY_DIRECTORY\n" unless -e $processMailScript;
 | 
        
           | 9 | rodolico | 102 | die "Could not find the reportScript [$reportScript] in $MY_DIRECTORY\n" unless -e $reportScript;
 | 
        
           | 6 | rodolico | 103 |   | 
        
           | 9 | rodolico | 104 |   | 
        
           | 6 | rodolico | 105 | # fetch all messages pending from e-mail accounts
 | 
        
           |  |  | 106 | `php $getMailScript` if $CHECKMAIL;
 | 
        
           |  |  | 107 |   | 
        
           | 3 | rodolico | 108 | # get a list of all messages waiting to be processed
 | 
        
           |  |  | 109 | opendir ( my $dh, $UNPROCESSED ) or die "Could not open $UNPROCESSED for read: $!";
 | 
        
           | 34 | rodolico | 110 | my @files = map{ "$UNPROCESSED\/$_" } sort grep { ! /^\./ && -f "$UNPROCESSED/$_" } readdir( $dh );
 | 
        
           | 3 | rodolico | 111 | closedir $dh;
 | 
        
           |  |  | 112 |   | 
        
           |  |  | 113 | my $count = 0;
 | 
        
           | 34 | rodolico | 114 | my $results = '';
 | 
        
           | 3 | rodolico | 115 |   | 
        
           |  |  | 116 | foreach my $thisFile ( sort @files ) {
 | 
        
           | 34 | rodolico | 117 |    my $exitCode;
 | 
        
           |  |  | 118 |    my $tempResults;
 | 
        
           |  |  | 119 |   | 
        
           | 6 | rodolico | 120 |    if ( $PROCESS ) {
 | 
        
           | 9 | rodolico | 121 |       $tempResults .=  `php $processMailScript <'$thisFile'`;
 | 
        
           | 6 | rodolico | 122 |       if ( $? == -1 ) {
 | 
        
           |  |  | 123 |          $exitCode = -1;
 | 
        
           |  |  | 124 |          die "Parsing failed: $!\n";
 | 
        
           |  |  | 125 |       } else {
 | 
        
           |  |  | 126 |          $exitCode = $? >> 8;
 | 
        
           |  |  | 127 |       }
 | 
        
           |  |  | 128 |    } else {
 | 
        
           | 12 | rodolico | 129 |      $results .= "$thisFile moving without processing\n";
 | 
        
           | 6 | rodolico | 130 |      $exitCode = 0;
 | 
        
           |  |  | 131 |    }
 | 
        
           |  |  | 132 |    if ( $exitCode == 0 ) {
 | 
        
           |  |  | 133 |       $filesProcessed{ 'valid' }++;
 | 
        
           | 9 | rodolico | 134 |       $tempResults = '';
 | 
        
           | 6 | rodolico | 135 |       &storeFile( $thisFile ) if $MOVEFILES;
 | 
        
           |  |  | 136 |    } elsif ( $exitCode == 1 ) {
 | 
        
           |  |  | 137 |       $filesProcessed{ 'Invalid Format' }++;
 | 
        
           |  |  | 138 |       &storeFile( $thisFile, 'InvalidFormat' ) if $MOVEFILES;
 | 
        
           |  |  | 139 |    } elsif ( $exitCode == 2 || $exitCode == 3 ) {
 | 
        
           |  |  | 140 |       $filesProcessed{ 'Invalid Report' }++;
 | 
        
           |  |  | 141 |       &storeFile( $thisFile, 'InvalidReport' ) if $MOVEFILES;
 | 
        
           |  |  | 142 |    } elsif ( $exitCode == 4 ) {
 | 
        
           |  |  | 143 |       $filesProcessed{ 'Duplicate Report' }++;
 | 
        
           |  |  | 144 |       &storeFile( $thisFile, 'DuplicateReport' ) if $MOVEFILES;
 | 
        
           |  |  | 145 |    } elsif ( $exitCode != 5 && $exitCode != 6 ) { ## not any other of our valid exit codes
 | 
        
           |  |  | 146 |       die "parse_sysinfo.php returned an unknown exit code $exitCode for $thisFile\n";
 | 
        
           |  |  | 147 |    } else {
 | 
        
           |  |  | 148 |       # at this point, we only have reports waiting for manual CAMP
 | 
        
           |  |  | 149 |       # updates, so just leave them where they are
 | 
        
           |  |  | 150 |       $filesProcessed{ 'Waiting CAMP Updates' }++;
 | 
        
           |  |  | 151 |    }
 | 
        
           | 9 | rodolico | 152 |    $results .= $tempResults;
 | 
        
           | 3 | rodolico | 153 |   last if ++$count >= $MAXTOPROCESS;
 | 
        
           |  |  | 154 |   print STDERR "\r$count" unless $CRON;
 | 
        
           |  |  | 155 | }
 | 
        
           |  |  | 156 |   | 
        
           | 6 | rodolico | 157 | my $emailString;
 | 
        
           | 3 | rodolico | 158 | print "\n" unless $CRON; 
 | 
        
           |  |  | 159 | $count= 0;
 | 
        
           |  |  | 160 | foreach my $key ( sort keys %filesProcessed ) {
 | 
        
           |  |  | 161 |    $count += $filesProcessed{$key};
 | 
        
           | 6 | rodolico | 162 |    $emailString .=  "$filesProcessed{$key}\t$key\n";
 | 
        
           | 3 | rodolico | 163 | }
 | 
        
           | 6 | rodolico | 164 | $emailString .=  "$count\tTotal Files Processed\n";
 | 
        
           |  |  | 165 | $emailString .=  "--------------------------------\n\n";
 | 
        
           |  |  | 166 | $emailString .=  $results;
 | 
        
           |  |  | 167 |   | 
        
           | 9 | rodolico | 168 | open SEND, "|$reportScript" or die "Could not find sendReport.pl: $!\n";
 | 
        
           |  |  | 169 | print SEND $emailString;
 | 
        
           |  |  | 170 | close SEND;
 | 
        
           | 6 | rodolico | 171 |   | 
        
           | 3 | rodolico | 172 | 1;
 |