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