1 |
rodolico |
1 |
#! /usr/bin/perl -w
|
|
|
2 |
|
|
|
3 |
# part of the command line tools for process_sysinfo. Will take a list of one or more
|
|
|
4 |
# directories and read the message in. These directories are assumed to be reports, possibly
|
|
|
5 |
# with e-mail headers. Will then search for the key [report date] in the file and, if found,
|
|
|
6 |
# adds the file name (and date) to the list of files to be processed.
|
|
|
7 |
# when the files to be processed has been created, will call process_sysinfo with each file
|
|
|
8 |
# as input
|
|
|
9 |
|
|
|
10 |
# v2.0 20121003
|
|
|
11 |
# changed SYSINFO to an array, allowing different processors for different formats of files
|
|
|
12 |
|
|
|
13 |
my $TESTING = 0;
|
|
|
14 |
my $saveDir;
|
|
|
15 |
my @mailDir;
|
|
|
16 |
my %SYSINFO;
|
|
|
17 |
my $output = ''; # place to store any error messages
|
|
|
18 |
my $startTime = `date`;
|
|
|
19 |
|
|
|
20 |
my $VERSION = '2.0';
|
|
|
21 |
|
|
|
22 |
# standard find and load configuration file
|
|
|
23 |
sub loadConfigurationFile {
|
|
|
24 |
my $configuration_file = shift;
|
|
|
25 |
|
|
|
26 |
use File::Basename;
|
|
|
27 |
use Cwd qw(realpath);
|
|
|
28 |
|
|
|
29 |
my $filename = realpath($0); # get my real path
|
|
|
30 |
my $directories;
|
|
|
31 |
my $suffix;
|
|
|
32 |
print "$configuration_file\n" if $TESTING;
|
|
|
33 |
$configuration_file = $filename unless $configuration_file;
|
|
|
34 |
print "$configuration_file\n" if $TESTING;
|
|
|
35 |
|
|
|
36 |
if ( $configuration_file !~ m/\// ) { # no path information
|
|
|
37 |
($filename, $directories, $suffix) = fileparse($filename,qr/\.[^.]*/); # break filename apart
|
|
|
38 |
#print "No Path Given\n";
|
|
|
39 |
} else {
|
|
|
40 |
($filename, $directories, $suffix) = fileparse($configuration_file,qr/\.[^.]*/); # break filename apart
|
|
|
41 |
$configuration_file = '';
|
|
|
42 |
#print "Path included\n";
|
|
|
43 |
}
|
|
|
44 |
unless (-e $directories . ($configuration_file ? $configuration_file : $filename) . '.conf' ) {
|
|
|
45 |
$lookingIn = $directories;
|
|
|
46 |
while ($lookingIn) {
|
|
|
47 |
$lookingIn =~ m/^(.*\/)[^\/]+\//;
|
|
|
48 |
$lookingIn = $1;
|
|
|
49 |
print "$lookingIn\n" if $TESTING;
|
|
|
50 |
if (-e $lookingIn . ($configuration_file ? $configuration_file : $filename) . '.conf' ) {
|
|
|
51 |
$directories = $lookingIn;
|
|
|
52 |
$lookingIn = '';
|
|
|
53 |
}
|
|
|
54 |
}
|
|
|
55 |
}
|
|
|
56 |
$configuration_file = $directories . ($configuration_file ? $configuration_file : $filename) . '.conf'; # add the .conf
|
|
|
57 |
# print "$configuration_file\n";
|
|
|
58 |
# die;
|
|
|
59 |
open CONFFILE, "<$configuration_file" or die "Can not open configuration file $configuration_file";
|
|
|
60 |
my $confFileContents = join( '', <CONFFILE> );
|
|
|
61 |
close CONFFILE;
|
|
|
62 |
return $confFileContents;
|
|
|
63 |
}
|
|
|
64 |
|
|
|
65 |
sub readFile {
|
|
|
66 |
my $filename = shift;
|
|
|
67 |
open DATAFILE,"<$filename" or return ''; # open the file
|
|
|
68 |
my @contents = <DATAFILE>;
|
|
|
69 |
close DATAFILE;
|
|
|
70 |
#while (@contents and ($contents[0] !~ m/^[\[<]sysinfo/)) { # skip past any header stuff
|
|
|
71 |
# shift @contents; # just throw it away
|
|
|
72 |
#}
|
|
|
73 |
return join('',@contents);
|
|
|
74 |
}
|
|
|
75 |
|
|
|
76 |
|
|
|
77 |
sub getReportDate {
|
|
|
78 |
my $filename = shift;
|
|
|
79 |
my $date = '';
|
|
|
80 |
my $format;
|
|
|
81 |
my $version;
|
|
|
82 |
if (my $contents = &readFile( $filename )) {
|
|
|
83 |
if ($contents =~ m/^\[report date\](\d+)$/m) {
|
|
|
84 |
$date = $1;
|
|
|
85 |
$format = 'winconf';
|
|
|
86 |
$contents =~ m/^\s*version\s*=\s*([0-9.]+)/;
|
|
|
87 |
$version = $1;
|
|
|
88 |
# print "Old Style Format\n";
|
|
|
89 |
} elsif ($contents =~ m/<report>.*<date>(\d{4}-\d{2}-\d{2} \d{2}:\d{2})<\/date>/s) {
|
|
|
90 |
$date = $1;
|
|
|
91 |
$date =~ s/[^0-9]//g;
|
|
|
92 |
$format = 'xml';
|
|
|
93 |
$contents =~ m/<version>([0-9.]+)<\/version>/;
|
|
|
94 |
$version = $1;
|
|
|
95 |
# print "XML Format $date\n";
|
|
|
96 |
} elsif ($contents =~ m/^\s+date:\s+"(\d{4}-\d{2}-\d{2} \d{2}:\d{2}:\d{2})"$/m) {
|
|
|
97 |
$date = $1;
|
|
|
98 |
$date =~ s/[^0-9]//g;
|
|
|
99 |
$format = 'yaml';
|
|
|
100 |
$contents =~ m/^\s+version:\s+([0-9.]+)$/m;
|
|
|
101 |
$version = $1;
|
|
|
102 |
# print "YAML format\n";
|
|
|
103 |
}
|
|
|
104 |
}
|
|
|
105 |
return ($date, $format, $version);
|
|
|
106 |
}
|
|
|
107 |
|
|
|
108 |
#print join "\n", @ARGV;
|
|
|
109 |
while (@ARGV) {
|
|
|
110 |
push @mailDir, shift;
|
|
|
111 |
}
|
|
|
112 |
#@mailDir = @ARGV if @ARGV;
|
|
|
113 |
|
|
|
114 |
BEGIN{
|
|
|
115 |
# load the configuration file
|
|
|
116 |
eval ( &loadConfigurationFile('process_sysinfo') );
|
|
|
117 |
push @INC, $LIBRARIES;
|
|
|
118 |
}
|
|
|
119 |
|
|
|
120 |
#@@mailDir = @ARGV if @ARGV;
|
|
|
121 |
|
|
|
122 |
#print '[' . join("\n",@mailDir) . ']';
|
|
|
123 |
#die;
|
|
|
124 |
my @fileList; # global that will hold the list of all files, formatted datetime\tfilename
|
|
|
125 |
while ( $directory = shift @mailDir ) { # while the user has passed in a directory to process
|
|
|
126 |
#print "Processing $directory\n";
|
|
|
127 |
#next;
|
|
|
128 |
opendir (DATADIR,$directory) || die "can't opendir $directory: $!"; # read the directory
|
|
|
129 |
while ( my $thisFile = readdir(DATADIR) ) { # for each file
|
|
|
130 |
#print "Looking at $thisFile\n";
|
|
|
131 |
next if $thisFile =~ m/^\./; # skip if it is a "dot" file
|
|
|
132 |
#print "It is not a dot file\n";
|
|
|
133 |
next unless -f "$directory/$thisFile";
|
|
|
134 |
$thisFile = $directory . '/' . $thisFile; # ok, get fully qualified path/filename
|
|
|
135 |
print "Checking $thisFile\n" if $TESTING;
|
|
|
136 |
my ($date, $format, $version) = &getReportDate($thisFile);
|
|
|
137 |
unless ( $date && $format && $version ) {
|
|
|
138 |
print "Skipping file $thisFile\n";
|
|
|
139 |
next;
|
|
|
140 |
}
|
|
|
141 |
print "[$thisFile]\t[$date]\t[$format]\t[$version]\n" if $TESTING;
|
|
|
142 |
push @fileList,"$date\t$thisFile\t$format\t$version"; # create an entry to be processed
|
|
|
143 |
}
|
|
|
144 |
closedir(DATADIR);
|
|
|
145 |
} # while
|
|
|
146 |
|
|
|
147 |
#print join( "\n", sort @fileList ) . "\n";
|
|
|
148 |
#die "\n";
|
|
|
149 |
|
|
|
150 |
#qx(mysql -u root computer_asset_manager < ./create_table.sql);
|
|
|
151 |
my $fileCount = @fileList;
|
|
|
152 |
my $thisFileNumber = 0;
|
|
|
153 |
my $tempFile = '/tmp/process_sysinfo.log';
|
|
|
154 |
unlink $tempFile if -e $tempFile;
|
|
|
155 |
foreach my $thisFile ( sort @fileList ) {
|
|
|
156 |
exit if $thisFileNumber >= 5 and $TESTING > 4;
|
|
|
157 |
print "Processing file $thisFileNumber of $fileCount [$thisFile]\n" if $TESTING;
|
|
|
158 |
|
|
|
159 |
$thisFileNumber++;
|
|
|
160 |
my ($date,$filename,$format, $version) = split ("\t", $thisFile);
|
|
|
161 |
if ( my $SYSINFO = $SYSINFO{$format} ) {
|
|
|
162 |
if ( $TESTING ) {
|
|
|
163 |
print "\tProcessing with $SYSINFO\n";
|
|
|
164 |
next;
|
|
|
165 |
}
|
|
|
166 |
open( SYSINFO, "|$SYSINFO >> $tempFile" ) or die "Could not open sysinfo at $SYSINFO\n";
|
|
|
167 |
print SYSINFO &readFile($filename);
|
|
|
168 |
close SYSINFO;
|
|
|
169 |
if ( $? ) {# it worked
|
|
|
170 |
&postProcess( $filename );
|
|
|
171 |
}
|
|
|
172 |
} else {
|
|
|
173 |
warn "Error, no SYSINFO entry for format [$format]\n";
|
|
|
174 |
}
|
|
|
175 |
#my $messages = qx($SYSINFO < $filename);
|
|
|
176 |
#$output .= $messages;
|
|
|
177 |
#if ($messages !~ m/^ERROR:/) { # only post_process the file if no error occurred
|
|
|
178 |
# &postProcess( $filename );
|
|
|
179 |
#}
|
|
|
180 |
}
|
|
|
181 |
|
|
|
182 |
my $endTime = `date`;
|
|
|
183 |
chomp $endTime;
|
|
|
184 |
chomp $startTime;
|
|
|
185 |
print "\nBegan Processing at $startTime\nFinished at $endTime\n$fileCount files processed";
|
|
|
186 |
open LOG, "<$tempFile" or die "Could not read Log File $tempFile: $!";
|
|
|
187 |
while (my $line = <LOG>) {
|
|
|
188 |
print $line;
|
|
|
189 |
}
|
|
|
190 |
close LOG;
|
|
|
191 |
|
|
|
192 |
1;
|