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