Subversion Repositories computer_asset_manager_v1

Rev

Rev 24 | Go to most recent revision | Details | Last modification | View Log | RSS feed

Rev Author Line No. Line
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)
23
my $CHECKMAIL=1; #controls whether we will check the mail or not
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
29
my $MAXTOPROCESS = 10000;
30
my %filesProcessed;
31
 
32
# following are used to find the configuration file
33
my $confFileName = "backupRead.conf.yaml";
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);
66
      my ( $date,$time,$client,$server,$serial) = split( '_', $name );
67
      my ( $year, $month, $day ) = split( '-', $date );
68
      $targetDirectory = "/$year/$month";
69
   }
70
   $targetDirectory = "$DATADIR/$targetDirectory";
71
   `mkdir -p '$targetDirectory'` unless -d $targetDirectory;
72
   `mv '$file' '$targetDirectory'`;
73
}
74
 
75
# get our configuration set up first
76
 
77
my $config = &loadConfig( $confFileName, @searchPaths );
78
die "Could not find configuration file $confFileName in " . join( ', ', @searchPaths) . "\n" unless $config;
79
 
80
# just some convenience variables
81
$DATADIR = $$config{'datapath'};
82
$UNPROCESSED=$DATADIR . '/' . $$config{'unprocessed_path'};
83
 
84
# check that the executables exist before continuing
85
$getMailScript = "$MY_DIRECTORY/" . $$config{'getMailScript'};
86
$processMailScript = "$MY_DIRECTORY/" . $$config{'processMailScript'};
87
$reportScript = "$MY_DIRECTORY/" . $$config{'reportScript'};
88
 
89
die "Could not find the getMailScript [$getMailScript] in $MY_DIRECTORY\n" unless -e $getMailScript;
90
die "Could not find the processMailScript [$processMailScript] in $MY_DIRECTORY\n" unless -e $processMailScript;
91
die "Could not find the reportScript [$reportScript] in $MY_DIRECTORY\n" unless -e $reportScript;
92
 
93
 
94
# fetch all messages pending from e-mail accounts
95
`php $getMailScript` if $CHECKMAIL;
96
 
97
# get a list of all messages waiting to be processed
98
opendir ( my $dh, $UNPROCESSED ) or die "Could not open $UNPROCESSED for read: $!";
99
@files = map{ "$UNPROCESSED\/$_" } sort grep { ! /^\./ && -f "$UNPROCESSED/$_" } readdir( $dh );
100
closedir $dh;
101
 
102
my $count = 0;
103
 
104
foreach my $thisFile ( sort @files ) {
105
   if ( $PROCESS ) {
106
      $tempResults .=  `php $processMailScript <'$thisFile'`;
107
      if ( $? == -1 ) {
108
         $exitCode = -1;
109
         die "Parsing failed: $!\n";
110
      } else {
111
         $exitCode = $? >> 8;
112
      }
113
   } else {
114
     $results .= "$thisFile moving without processing\n";
115
     $exitCode = 0;
116
   }
117
   if ( $exitCode == 0 ) {
118
      $filesProcessed{ 'valid' }++;
119
      $tempResults = '';
120
      &storeFile( $thisFile ) if $MOVEFILES;
121
   } elsif ( $exitCode == 1 ) {
122
      $filesProcessed{ 'Invalid Format' }++;
123
      &storeFile( $thisFile, 'InvalidFormat' ) if $MOVEFILES;
124
   } else {
125
      $filesProcessed{ 'Unknown File Error' }++;
126
   }
127
   $results .= $tempResults;
128
  last if ++$count >= $MAXTOPROCESS;
129
  print STDERR "\r$count" unless $CRON;
130
}
131
 
132
my $emailString;
133
print "\n" unless $CRON; 
134
$count= 0;
135
foreach my $key ( sort keys %filesProcessed ) {
136
   $count += $filesProcessed{$key};
137
   $emailString .=  "$filesProcessed{$key}\t$key\n";
138
}
139
$emailString .=  "$count\tTotal Files Processed\n";
140
$emailString .=  "--------------------------------\n\n";
141
$emailString .=  $results;
142
 
143
open SEND, "|$reportScript" or die "Could not find sendReport.pl: $!\n";
144
print SEND $emailString;
145
close SEND;
146
 
147
1;