Subversion Repositories computer_asset_manager_v1

Rev

Rev 25 | Go to most recent revision | Blame | Compare with Previous | Last modification | View Log | Download | RSS feed

#! /usr/bin/perl -w

#
# v0.1 20160218 RWR
# copied from sysinfo/cli_tools/doReports.pl
#

use IO::Interactive qw(is_interactive interactive busy);
use YAML::XS;
use Cwd 'abs_path';
#use Data::Dumper;

use File::Basename;

my $VERSION = '0.1';
my $MY_DIRECTORY = abs_path(dirname(__FILE__) );

my $CRON= ! is_interactive(); # determine if we are in interactive shell

# control the flow of the program. $CHECKMAIL is pretty clear.
# if $PROCESS is false and $MOVEFILES is true, the files will be moved
# but not processed (ie, they were processed some other way)
my $CHECKMAIL=1; #controls whether we will check the mail or not
my $PROCESS=1; # controls whether we will process the files
my $MOVEFILES=1; # controls whether we will move the files successfully processed

my $DATADIR; # will hold the reports directory
my $UNPROCESSED; # will hold the location for unprocessed reports
my $MAXTOPROCESS = 10000;
my %filesProcessed;

# following are used to find the configuration file
my $confFileName = 'rsbackupRead.conf.yaml';
my @searchPaths = ( '/etc/camp', '/opt/camp', '/opt/camp/sysinfo', $MY_DIRECTORY );


sub loadConfig {
   my ( $confFileName, @searchPaths ) = @_;
   my $configuration;


   for ( $i = 0; $i < @searchPaths; $i++ ) {
      $filename = $searchPaths[$i] . '/' . $confFileName;
      if ( -e $filename ) {
         #print "Found $filename\n";
         open CONF, "<$filename" or warn "Could not read $filename: $!\n";
         $configuration = Load( join( '', <CONF> ) );
         close CONF;
         last; # exit out of the loop; we don't try to load it more than once
      } # if
   } # foreach
   return $configuration;
} # sub loadConfig

# assumes parse_backup.php returns one of the following codes
# 0 - Processed Normally
# 1 - did not process
# 2 - not an rsbackup file

sub storeFile {
   use File::Basename;
   my $file = shift;
   my $targetDirectory = shift;
   unless ( $targetDirectory ) {
      my ($name,$path) = fileparse($file);
      my ( $date,$server,$serial) = split( '_', $name );
      my $year = substr( $date,0,4);
      my $month = substr( $date, 4, 2);
      my $day = substr( $date, 6,2);
      $targetDirectory = "/$year/$month";
   }
   $targetDirectory = "$DATADIR/$targetDirectory";
   `mkdir -p '$targetDirectory'` unless -d $targetDirectory;
   `mv '$file' '$targetDirectory'`;
}

# get our configuration set up first

my $config = &loadConfig( $confFileName, @searchPaths );
die "Could not find configuration file $confFileName in " . join( ', ', @searchPaths) . "\n" unless $config;

# just some convenience variables
$DATADIR = $$config{'datapath'};
$UNPROCESSED=$DATADIR . '/' . $$config{'unprocessed_path'};

# check that the executables exist before continuing
$getMailScript = "$MY_DIRECTORY/" . $$config{'getMailScript'};
$processMailScript = "$MY_DIRECTORY/" . $$config{'processMailScript'};
$reportScript = "$MY_DIRECTORY/" . $$config{'reportScript'};

die "Could not find the getMailScript [$getMailScript] in $MY_DIRECTORY\n" unless -e $getMailScript;
die "Could not find the processMailScript [$processMailScript] in $MY_DIRECTORY\n" unless -e $processMailScript;
die "Could not find the reportScript [$reportScript] in $MY_DIRECTORY\n" unless -e $reportScript;


# fetch all messages pending from e-mail accounts
`php $getMailScript` if $CHECKMAIL;

# see if anything is gzipped and, if so, gunzip it
`gunzip -f $UNPROCESSED\/*.gz`;

# get a list of all messages waiting to be processed
opendir ( my $dh, $UNPROCESSED ) or die "Could not open $UNPROCESSED for read: $!";
@files = sort grep { ! /^\./ && -f "$UNPROCESSED/$_" } readdir( $dh );
#@files = map{ "$UNPROCESSED\/$_" } sort grep { ! /^\./ && -f "$UNPROCESSED/$_" } readdir( $dh );
closedir $dh;

my $count = 0;

#0 - Ok
#1 - Invalid Data File Name
#2 - Could not open the file
#3 - Duplicate Report
#4 - Server not found in database

my $file;
my $exitMessage;
my $exitCode;

# The script $processMailScript returns three fields as a tab delimited
# string -- Filename, Exit Code (integer), Exit Message
# The Exit Codes are
#  0 - Processed Normally
#  1 - Invalid Data File Name
#  2 - Could not open the file
#  3 - Duplicate Report
#  4 - Server Not Found

foreach my $thisFile ( sort @files ) {
   if ( $PROCESS ) {
      $temp =  `php $processMailScript '$thisFile'`;
      ($file,$exitCode,$exitMessage) = split( "\t", $temp );
   } else {
     $tempResults .= "$thisFile moving without processing\n";
     $exitCode = 0;
   }
   $tempResults = "$thisFile\t$exitMessage\n";
   if ( $exitCode == 0 ) {
      $filesProcessed{ 'valid' }++;
      $tempResults = ''; # do not give messages for files which are Ok
      &storeFile( "$UNPROCESSED\/$thisFile" ) if $MOVEFILES;
   } elsif ( $exitCode == 1 ) {
      $filesProcessed{ 'Invalid Data File Name' }++;
      &storeFile( "$UNPROCESSED\/$thisFile", 'InvalidFileName' ) if $MOVEFILES;
   } elsif ( $exitCode == 2 ) {
      $filesProcessed{ 'Could not open file' }++;
   } elsif ( $exitCode == 3 ) {
      $filesProcessed{ 'Duplicate Report' }++;
      &storeFile( "$UNPROCESSED\/$thisFile", 'Duplicate' ) if $MOVEFILES;
   } elsif ( $exitCode == 4 ) {
      $filesProcessed{ 'Server not found in Database' }++;
      &storeFile( "$UNPROCESSED\/$thisFile", 'ServerNotFound' ) if $MOVEFILES;
   } else {
      $filesProcessed{ 'Unknown File Error' }++;
   }
   $results .= $tempResults;
  last if ++$count >= $MAXTOPROCESS;
  print STDERR "\r$count" unless $CRON;
}

my $emailString;
print "\n" unless $CRON; 
$count= 0;
foreach my $key ( sort keys %filesProcessed ) {
   $count += $filesProcessed{$key};
   $emailString .=  "$filesProcessed{$key}\t$key\n";
}
$emailString .=  "$count\tTotal Files Processed\n";
$emailString .=  "--------------------------------\n\n";
$emailString .=  $results;

open SEND, "|$reportScript" or die "Could not find $reportScript: $!\n";
print SEND $emailString;
close SEND;

1;