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