| 23 | rodolico | 1 | #! /usr/bin/perl -w
 | 
        
           |  |  | 2 |   | 
        
           |  |  | 3 | #
 | 
        
           |  |  | 4 | # v0.1 20160218 RWR
 | 
        
           |  |  | 5 | # copied from sysinfo/cli_tools/doReports.pl
 | 
        
           |  |  | 6 | #
 | 
        
           |  |  | 7 |   | 
        
           |  |  | 8 | use IO::Interactive qw(is_interactive interactive busy);
 | 
        
           |  |  | 9 | use YAML::XS;
 | 
        
           |  |  | 10 | use Cwd 'abs_path';
 | 
        
           |  |  | 11 | #use Data::Dumper;
 | 
        
           |  |  | 12 |   | 
        
           |  |  | 13 | use File::Basename;
 | 
        
           |  |  | 14 |   | 
        
           |  |  | 15 | my $VERSION = '0.1';
 | 
        
           |  |  | 16 | my $MY_DIRECTORY = abs_path(dirname(__FILE__) );
 | 
        
           |  |  | 17 |   | 
        
           |  |  | 18 | my $CRON= ! is_interactive(); # determine if we are in interactive shell
 | 
        
           |  |  | 19 |   | 
        
           |  |  | 20 | # control the flow of the program. $CHECKMAIL is pretty clear.
 | 
        
           |  |  | 21 | # if $PROCESS is false and $MOVEFILES is true, the files will be moved
 | 
        
           |  |  | 22 | # but not processed (ie, they were processed some other way)
 | 
        
           | 26 | rodolico | 23 | my $CHECKMAIL=1; #controls whether we will check the mail or not
 | 
        
           | 23 | rodolico | 24 | my $PROCESS=1; # controls whether we will process the files
 | 
        
           |  |  | 25 | my $MOVEFILES=1; # controls whether we will move the files successfully processed
 | 
        
           |  |  | 26 |   | 
        
           |  |  | 27 | my $DATADIR; # will hold the reports directory
 | 
        
           |  |  | 28 | my $UNPROCESSED; # will hold the location for unprocessed reports
 | 
        
           | 25 | rodolico | 29 | my $MAXTOPROCESS = 10000;
 | 
        
           | 23 | rodolico | 30 | my %filesProcessed;
 | 
        
           |  |  | 31 |   | 
        
           |  |  | 32 | # following are used to find the configuration file
 | 
        
           | 24 | rodolico | 33 | my $confFileName = 'rsbackupRead.conf.yaml';
 | 
        
           | 23 | rodolico | 34 | my @searchPaths = ( '/etc/camp', '/opt/camp', '/opt/camp/sysinfo', $MY_DIRECTORY );
 | 
        
           |  |  | 35 |   | 
        
           |  |  | 36 |   | 
        
           |  |  | 37 | sub loadConfig {
 | 
        
           |  |  | 38 |    my ( $confFileName, @searchPaths ) = @_;
 | 
        
           |  |  | 39 |    my $configuration;
 | 
        
           |  |  | 40 |   | 
        
           |  |  | 41 |   | 
        
           |  |  | 42 |    for ( $i = 0; $i < @searchPaths; $i++ ) {
 | 
        
           |  |  | 43 |       $filename = $searchPaths[$i] . '/' . $confFileName;
 | 
        
           |  |  | 44 |       if ( -e $filename ) {
 | 
        
           |  |  | 45 |          #print "Found $filename\n";
 | 
        
           |  |  | 46 |          open CONF, "<$filename" or warn "Could not read $filename: $!\n";
 | 
        
           |  |  | 47 |          $configuration = Load( join( '', <CONF> ) );
 | 
        
           |  |  | 48 |          close CONF;
 | 
        
           |  |  | 49 |          last; # exit out of the loop; we don't try to load it more than once
 | 
        
           |  |  | 50 |       } # if
 | 
        
           |  |  | 51 |    } # foreach
 | 
        
           |  |  | 52 |    return $configuration;
 | 
        
           |  |  | 53 | } # sub loadConfig
 | 
        
           |  |  | 54 |   | 
        
           |  |  | 55 | # assumes parse_backup.php returns one of the following codes
 | 
        
           |  |  | 56 | # 0 - Processed Normally
 | 
        
           |  |  | 57 | # 1 - did not process
 | 
        
           |  |  | 58 | # 2 - not an rsbackup file
 | 
        
           |  |  | 59 |   | 
        
           |  |  | 60 | sub storeFile {
 | 
        
           |  |  | 61 |    use File::Basename;
 | 
        
           |  |  | 62 |    my $file = shift;
 | 
        
           |  |  | 63 |    my $targetDirectory = shift;
 | 
        
           |  |  | 64 |    unless ( $targetDirectory ) {
 | 
        
           |  |  | 65 |       my ($name,$path) = fileparse($file);
 | 
        
           | 24 | rodolico | 66 |       my ( $date,$server,$serial) = split( '_', $name );
 | 
        
           |  |  | 67 |       my $year = substr( $date,0,4);
 | 
        
           |  |  | 68 |       my $month = substr( $date, 4, 2);
 | 
        
           |  |  | 69 |       my $day = substr( $date, 6,2);
 | 
        
           | 23 | rodolico | 70 |       $targetDirectory = "/$year/$month";
 | 
        
           |  |  | 71 |    }
 | 
        
           |  |  | 72 |    $targetDirectory = "$DATADIR/$targetDirectory";
 | 
        
           |  |  | 73 |    `mkdir -p '$targetDirectory'` unless -d $targetDirectory;
 | 
        
           |  |  | 74 |    `mv '$file' '$targetDirectory'`;
 | 
        
           |  |  | 75 | }
 | 
        
           |  |  | 76 |   | 
        
           |  |  | 77 | # get our configuration set up first
 | 
        
           |  |  | 78 |   | 
        
           |  |  | 79 | my $config = &loadConfig( $confFileName, @searchPaths );
 | 
        
           |  |  | 80 | die "Could not find configuration file $confFileName in " . join( ', ', @searchPaths) . "\n" unless $config;
 | 
        
           |  |  | 81 |   | 
        
           |  |  | 82 | # just some convenience variables
 | 
        
           |  |  | 83 | $DATADIR = $$config{'datapath'};
 | 
        
           |  |  | 84 | $UNPROCESSED=$DATADIR . '/' . $$config{'unprocessed_path'};
 | 
        
           |  |  | 85 |   | 
        
           |  |  | 86 | # check that the executables exist before continuing
 | 
        
           |  |  | 87 | $getMailScript = "$MY_DIRECTORY/" . $$config{'getMailScript'};
 | 
        
           |  |  | 88 | $processMailScript = "$MY_DIRECTORY/" . $$config{'processMailScript'};
 | 
        
           |  |  | 89 | $reportScript = "$MY_DIRECTORY/" . $$config{'reportScript'};
 | 
        
           |  |  | 90 |   | 
        
           |  |  | 91 | die "Could not find the getMailScript [$getMailScript] in $MY_DIRECTORY\n" unless -e $getMailScript;
 | 
        
           |  |  | 92 | die "Could not find the processMailScript [$processMailScript] in $MY_DIRECTORY\n" unless -e $processMailScript;
 | 
        
           |  |  | 93 | die "Could not find the reportScript [$reportScript] in $MY_DIRECTORY\n" unless -e $reportScript;
 | 
        
           |  |  | 94 |   | 
        
           |  |  | 95 |   | 
        
           |  |  | 96 | # fetch all messages pending from e-mail accounts
 | 
        
           |  |  | 97 | `php $getMailScript` if $CHECKMAIL;
 | 
        
           |  |  | 98 |   | 
        
           | 24 | rodolico | 99 | # see if anything is gzipped and, if so, gunzip it
 | 
        
           |  |  | 100 | `gunzip -f $UNPROCESSED\/*.gz`;
 | 
        
           |  |  | 101 |   | 
        
           | 23 | rodolico | 102 | # get a list of all messages waiting to be processed
 | 
        
           |  |  | 103 | opendir ( my $dh, $UNPROCESSED ) or die "Could not open $UNPROCESSED for read: $!";
 | 
        
           | 24 | rodolico | 104 | @files = sort grep { ! /^\./ && -f "$UNPROCESSED/$_" } readdir( $dh );
 | 
        
           |  |  | 105 | #@files = map{ "$UNPROCESSED\/$_" } sort grep { ! /^\./ && -f "$UNPROCESSED/$_" } readdir( $dh );
 | 
        
           | 23 | rodolico | 106 | closedir $dh;
 | 
        
           |  |  | 107 |   | 
        
           |  |  | 108 | my $count = 0;
 | 
        
           |  |  | 109 |   | 
        
           | 24 | rodolico | 110 | #0 - Ok
 | 
        
           |  |  | 111 | #1 - Invalid Data File Name
 | 
        
           |  |  | 112 | #2 - Could not open the file
 | 
        
           |  |  | 113 | #3 - Duplicate Report
 | 
        
           |  |  | 114 | #4 - Server not found in database
 | 
        
           |  |  | 115 |   | 
        
           |  |  | 116 | my $file;
 | 
        
           |  |  | 117 | my $exitMessage;
 | 
        
           |  |  | 118 | my $exitCode;
 | 
        
           |  |  | 119 |   | 
        
           | 25 | rodolico | 120 | # The script $processMailScript returns three fields as a tab delimited
 | 
        
           |  |  | 121 | # string -- Filename, Exit Code (integer), Exit Message
 | 
        
           |  |  | 122 | # The Exit Codes are
 | 
        
           |  |  | 123 | #  0 - Processed Normally
 | 
        
           |  |  | 124 | #  1 - Invalid Data File Name
 | 
        
           |  |  | 125 | #  2 - Could not open the file
 | 
        
           |  |  | 126 | #  3 - Duplicate Report
 | 
        
           |  |  | 127 | #  4 - Server Not Found
 | 
        
           | 24 | rodolico | 128 |   | 
        
           | 23 | rodolico | 129 | foreach my $thisFile ( sort @files ) {
 | 
        
           |  |  | 130 |    if ( $PROCESS ) {
 | 
        
           | 25 | rodolico | 131 |       $temp =  `php $processMailScript '$thisFile'`;
 | 
        
           |  |  | 132 |       ($file,$exitCode,$exitMessage) = split( "\t", $temp );
 | 
        
           | 23 | rodolico | 133 |    } else {
 | 
        
           | 25 | rodolico | 134 |      $tempResults .= "$thisFile moving without processing\n";
 | 
        
           | 23 | rodolico | 135 |      $exitCode = 0;
 | 
        
           |  |  | 136 |    }
 | 
        
           | 25 | rodolico | 137 |    $tempResults = "$thisFile\t$exitMessage\n";
 | 
        
           | 23 | rodolico | 138 |    if ( $exitCode == 0 ) {
 | 
        
           |  |  | 139 |       $filesProcessed{ 'valid' }++;
 | 
        
           | 25 | rodolico | 140 |       $tempResults = ''; # do not give messages for files which are Ok
 | 
        
           | 24 | rodolico | 141 |       &storeFile( "$UNPROCESSED\/$thisFile" ) if $MOVEFILES;
 | 
        
           | 23 | rodolico | 142 |    } elsif ( $exitCode == 1 ) {
 | 
        
           | 24 | rodolico | 143 |       $filesProcessed{ 'Invalid Data File Name' }++;
 | 
        
           | 25 | rodolico | 144 |       &storeFile( "$UNPROCESSED\/$thisFile", 'InvalidFileName' ) if $MOVEFILES;
 | 
        
           | 24 | rodolico | 145 |    } elsif ( $exitCode == 2 ) {
 | 
        
           |  |  | 146 |       $filesProcessed{ 'Could not open file' }++;
 | 
        
           |  |  | 147 |    } elsif ( $exitCode == 3 ) {
 | 
        
           |  |  | 148 |       $filesProcessed{ 'Duplicate Report' }++;
 | 
        
           |  |  | 149 |       &storeFile( "$UNPROCESSED\/$thisFile", 'Duplicate' ) if $MOVEFILES;
 | 
        
           |  |  | 150 |    } elsif ( $exitCode == 4 ) {
 | 
        
           |  |  | 151 |       $filesProcessed{ 'Server not found in Database' }++;
 | 
        
           | 25 | rodolico | 152 |       &storeFile( "$UNPROCESSED\/$thisFile", 'ServerNotFound' ) if $MOVEFILES;
 | 
        
           | 23 | rodolico | 153 |    } else {
 | 
        
           |  |  | 154 |       $filesProcessed{ 'Unknown File Error' }++;
 | 
        
           |  |  | 155 |    }
 | 
        
           |  |  | 156 |    $results .= $tempResults;
 | 
        
           |  |  | 157 |   last if ++$count >= $MAXTOPROCESS;
 | 
        
           |  |  | 158 |   print STDERR "\r$count" unless $CRON;
 | 
        
           |  |  | 159 | }
 | 
        
           |  |  | 160 |   | 
        
           |  |  | 161 | my $emailString;
 | 
        
           |  |  | 162 | print "\n" unless $CRON; 
 | 
        
           |  |  | 163 | $count= 0;
 | 
        
           |  |  | 164 | foreach my $key ( sort keys %filesProcessed ) {
 | 
        
           |  |  | 165 |    $count += $filesProcessed{$key};
 | 
        
           |  |  | 166 |    $emailString .=  "$filesProcessed{$key}\t$key\n";
 | 
        
           |  |  | 167 | }
 | 
        
           |  |  | 168 | $emailString .=  "$count\tTotal Files Processed\n";
 | 
        
           |  |  | 169 | $emailString .=  "--------------------------------\n\n";
 | 
        
           |  |  | 170 | $emailString .=  $results;
 | 
        
           |  |  | 171 |   | 
        
           | 25 | rodolico | 172 | open SEND, "|$reportScript" or die "Could not find $reportScript: $!\n";
 | 
        
           | 23 | rodolico | 173 | print SEND $emailString;
 | 
        
           |  |  | 174 | close SEND;
 | 
        
           |  |  | 175 |   | 
        
           |  |  | 176 | 1;
 |