| 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 |   | 
        
           | 115 | rodolico | 86 | sub fixEncoding {
 | 
        
           |  |  | 87 |    use Encode qw(decode encode);
 | 
        
           |  |  | 88 |   | 
        
           |  |  | 89 |    my $dir = shift;
 | 
        
           |  |  | 90 |   | 
        
           |  |  | 91 |    opendir(my $dh, $dir) || die "Can't opendir $dir: $!";
 | 
        
           |  |  | 92 |    my @files = map{ "$dir/$_" } grep { /\.yaml$/ && -f "$dir/$_" } readdir($dh);
 | 
        
           |  |  | 93 |    closedir $dh;
 | 
        
           |  |  | 94 |   | 
        
           |  |  | 95 |    my $count = 0;
 | 
        
           |  |  | 96 |   | 
        
           |  |  | 97 |    foreach my $filename ( @files ) {
 | 
        
           |  |  | 98 |       if (  `file -b "$filename"` !~ m/^(ASCII|UTF-8)/ ) {
 | 
        
           |  |  | 99 |          $count++;
 | 
        
           |  |  | 100 |          open DATA, "<$filename" or die "Could not read $filename: $!\n";
 | 
        
           |  |  | 101 |          my $data = join( '', <DATA> );
 | 
        
           |  |  | 102 |          close DATA;
 | 
        
           |  |  | 103 |          $data = encode( 'UTF-8', $data );
 | 
        
           |  |  | 104 |          open DATA, ">$filename" or die "Could not overwrite $filename: $!\n";
 | 
        
           |  |  | 105 |          print DATA $data;
 | 
        
           |  |  | 106 |          close DATA;
 | 
        
           |  |  | 107 |       }
 | 
        
           |  |  | 108 |    }
 | 
        
           |  |  | 109 |   | 
        
           |  |  | 110 | return "convertFile found " . scalar( @files ) . " files and processed $count\n";
 | 
        
           |  |  | 111 |   | 
        
           |  |  | 112 | }
 | 
        
           |  |  | 113 |   | 
        
           | 114 | rodolico | 114 | sub validateFileType {
 | 
        
           |  |  | 115 |    my %fileTypeConversions = (
 | 
        
           |  |  | 116 |       'ISO-8859' => 'iconv -f ISO-8859-1 -t UTF-8'
 | 
        
           |  |  | 117 |       );
 | 
        
           |  |  | 118 | #   use File::Type;
 | 
        
           |  |  | 119 |    my $thisFile = shift;
 | 
        
           |  |  | 120 | #   my $ft = File::Type->new();
 | 
        
           |  |  | 121 | #   die $ft->checktype_filename($thisFile);
 | 
        
           |  |  | 122 |    my $filetype =  `file '$thisFile'`;
 | 
        
           |  |  | 123 |    chomp $filetype;
 | 
        
           |  |  | 124 |    $filetype =~ m/:\s+([a-z0-9-]+)/i;
 | 
        
           |  |  | 125 |    $filetype = $1;
 | 
        
           |  |  | 126 |    return 1 if $filetype eq 'ASCII' || $filetype eq 'UTF-8';
 | 
        
           |  |  | 127 |    if ( $fileTypeConversions{ $filetype } ) {
 | 
        
           |  |  | 128 |       my $command = "mv '$thisFile' /tmp/toConvert ; $fileTypeConversions{ $filetype } -o '$thisFile' /tmp/toConvert"; 
 | 
        
           |  |  | 129 |       print "$command\n";
 | 
        
           |  |  | 130 |       #return 1;
 | 
        
           |  |  | 131 |    } else {
 | 
        
           |  |  | 132 |       warn "Unknown file type $filetype\n";
 | 
        
           |  |  | 133 |    }
 | 
        
           |  |  | 134 |    return 0;
 | 
        
           |  |  | 135 | }   
 | 
        
           |  |  | 136 |   | 
        
           | 6 | rodolico | 137 | # get our configuration set up first
 | 
        
           | 3 | rodolico | 138 |   | 
        
           | 6 | rodolico | 139 | my $config = &loadConfig( $confFileName, @searchPaths );
 | 
        
           | 8 | rodolico | 140 | die "Could not find configuration file $confFileName in " . join( ', ', @searchPaths) . "\n" unless $config;
 | 
        
           |  |  | 141 |   | 
        
           |  |  | 142 | # just some convenience variables
 | 
        
           | 6 | rodolico | 143 | $DATADIR = $$config{'datapath'};
 | 
        
           |  |  | 144 | $UNPROCESSED=$DATADIR . '/' . $$config{'unprocessed_path'};
 | 
        
           | 3 | rodolico | 145 |   | 
        
           | 8 | rodolico | 146 | # check that the executables exist before continuing
 | 
        
           | 34 | rodolico | 147 | my $getMailScript = "$MY_DIRECTORY/" . $$config{'getMailScript'};
 | 
        
           |  |  | 148 | my $processMailScript = "$MY_DIRECTORY/" . $$config{'processMailScript'};
 | 
        
           |  |  | 149 | my $reportScript = "$MY_DIRECTORY/" . $$config{'reportScript'};
 | 
        
           | 9 | rodolico | 150 |   | 
        
           | 6 | rodolico | 151 | die "Could not find the getMailScript [$getMailScript] in $MY_DIRECTORY\n" unless -e $getMailScript;
 | 
        
           |  |  | 152 | die "Could not find the processMailScript [$processMailScript] in $MY_DIRECTORY\n" unless -e $processMailScript;
 | 
        
           | 9 | rodolico | 153 | die "Could not find the reportScript [$reportScript] in $MY_DIRECTORY\n" unless -e $reportScript;
 | 
        
           | 6 | rodolico | 154 |   | 
        
           | 74 | rodolico | 155 | #die "$getMailScript\n$processMailScript\n$reportScript\n";
 | 
        
           | 6 | rodolico | 156 | # fetch all messages pending from e-mail accounts
 | 
        
           |  |  | 157 | `php $getMailScript` if $CHECKMAIL;
 | 
        
           |  |  | 158 |   | 
        
           | 115 | rodolico | 159 | my $count = 0;
 | 
        
           |  |  | 160 | my $results = '';
 | 
        
           |  |  | 161 |   | 
        
           |  |  | 162 | $results .= &fixEncoding( $UNPROCESSED );
 | 
        
           |  |  | 163 |   | 
        
           | 3 | rodolico | 164 | # get a list of all messages waiting to be processed
 | 
        
           |  |  | 165 | opendir ( my $dh, $UNPROCESSED ) or die "Could not open $UNPROCESSED for read: $!";
 | 
        
           | 34 | rodolico | 166 | my @files = map{ "$UNPROCESSED\/$_" } sort grep { ! /^\./ && -f "$UNPROCESSED/$_" } readdir( $dh );
 | 
        
           | 3 | rodolico | 167 | closedir $dh;
 | 
        
           |  |  | 168 |   | 
        
           | 74 | rodolico | 169 | #print join( "\n", @files ) . "\n"; die;
 | 
        
           |  |  | 170 |   | 
        
           | 3 | rodolico | 171 | foreach my $thisFile ( sort @files ) {
 | 
        
           | 34 | rodolico | 172 |    my $exitCode;
 | 
        
           |  |  | 173 |    my $tempResults;
 | 
        
           |  |  | 174 |   | 
        
           | 114 | rodolico | 175 | #   next unless &validateFileType( $thisFile );
 | 
        
           |  |  | 176 | #   die "Stopping on valid file $thisFile\n";
 | 
        
           | 6 | rodolico | 177 |    if ( $PROCESS ) {
 | 
        
           | 9 | rodolico | 178 |       $tempResults .=  `php $processMailScript <'$thisFile'`;
 | 
        
           | 6 | rodolico | 179 |       if ( $? == -1 ) {
 | 
        
           |  |  | 180 |          $exitCode = -1;
 | 
        
           |  |  | 181 |          die "Parsing failed: $!\n";
 | 
        
           |  |  | 182 |       } else {
 | 
        
           |  |  | 183 |          $exitCode = $? >> 8;
 | 
        
           |  |  | 184 |       }
 | 
        
           |  |  | 185 |    } else {
 | 
        
           | 12 | rodolico | 186 |      $results .= "$thisFile moving without processing\n";
 | 
        
           | 6 | rodolico | 187 |      $exitCode = 0;
 | 
        
           |  |  | 188 |    }
 | 
        
           |  |  | 189 |    if ( $exitCode == 0 ) {
 | 
        
           |  |  | 190 |       $filesProcessed{ 'valid' }++;
 | 
        
           | 9 | rodolico | 191 |       $tempResults = '';
 | 
        
           | 6 | rodolico | 192 |       &storeFile( $thisFile ) if $MOVEFILES;
 | 
        
           |  |  | 193 |    } elsif ( $exitCode == 1 ) {
 | 
        
           |  |  | 194 |       $filesProcessed{ 'Invalid Format' }++;
 | 
        
           |  |  | 195 |       &storeFile( $thisFile, 'InvalidFormat' ) if $MOVEFILES;
 | 
        
           |  |  | 196 |    } elsif ( $exitCode == 2 || $exitCode == 3 ) {
 | 
        
           |  |  | 197 |       $filesProcessed{ 'Invalid Report' }++;
 | 
        
           |  |  | 198 |       &storeFile( $thisFile, 'InvalidReport' ) if $MOVEFILES;
 | 
        
           |  |  | 199 |    } elsif ( $exitCode == 4 ) {
 | 
        
           |  |  | 200 |       $filesProcessed{ 'Duplicate Report' }++;
 | 
        
           |  |  | 201 |       &storeFile( $thisFile, 'DuplicateReport' ) if $MOVEFILES;
 | 
        
           |  |  | 202 |    } elsif ( $exitCode != 5 && $exitCode != 6 ) { ## not any other of our valid exit codes
 | 
        
           |  |  | 203 |       die "parse_sysinfo.php returned an unknown exit code $exitCode for $thisFile\n";
 | 
        
           |  |  | 204 |    } else {
 | 
        
           |  |  | 205 |       # at this point, we only have reports waiting for manual CAMP
 | 
        
           |  |  | 206 |       # updates, so just leave them where they are
 | 
        
           |  |  | 207 |       $filesProcessed{ 'Waiting CAMP Updates' }++;
 | 
        
           |  |  | 208 |    }
 | 
        
           | 9 | rodolico | 209 |    $results .= $tempResults;
 | 
        
           | 3 | rodolico | 210 |   last if ++$count >= $MAXTOPROCESS;
 | 
        
           |  |  | 211 |   print STDERR "\r$count" unless $CRON;
 | 
        
           |  |  | 212 | }
 | 
        
           |  |  | 213 |   | 
        
           | 6 | rodolico | 214 | my $emailString;
 | 
        
           | 3 | rodolico | 215 | print "\n" unless $CRON; 
 | 
        
           |  |  | 216 | $count= 0;
 | 
        
           |  |  | 217 | foreach my $key ( sort keys %filesProcessed ) {
 | 
        
           |  |  | 218 |    $count += $filesProcessed{$key};
 | 
        
           | 6 | rodolico | 219 |    $emailString .=  "$filesProcessed{$key}\t$key\n";
 | 
        
           | 3 | rodolico | 220 | }
 | 
        
           | 6 | rodolico | 221 | $emailString .=  "$count\tTotal Files Processed\n";
 | 
        
           |  |  | 222 | $emailString .=  "--------------------------------\n\n";
 | 
        
           |  |  | 223 | $emailString .=  $results;
 | 
        
           |  |  | 224 |   | 
        
           | 9 | rodolico | 225 | open SEND, "|$reportScript" or die "Could not find sendReport.pl: $!\n";
 | 
        
           |  |  | 226 | print SEND $emailString;
 | 
        
           |  |  | 227 | close SEND;
 | 
        
           | 6 | rodolico | 228 |   | 
        
           | 3 | rodolico | 229 | 1;
 |