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