Subversion Repositories computer_asset_manager_v1

Rev

Rev 34 | Rev 114 | Go to most recent revision | Details | Compare with Previous | Last modification | View Log | RSS feed

Rev Author Line No. Line
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
 
6 rodolico 86
# get our configuration set up first
3 rodolico 87
 
6 rodolico 88
my $config = &loadConfig( $confFileName, @searchPaths );
8 rodolico 89
die "Could not find configuration file $confFileName in " . join( ', ', @searchPaths) . "\n" unless $config;
90
 
91
# just some convenience variables
6 rodolico 92
$DATADIR = $$config{'datapath'};
93
$UNPROCESSED=$DATADIR . '/' . $$config{'unprocessed_path'};
3 rodolico 94
 
8 rodolico 95
# check that the executables exist before continuing
34 rodolico 96
my $getMailScript = "$MY_DIRECTORY/" . $$config{'getMailScript'};
97
my $processMailScript = "$MY_DIRECTORY/" . $$config{'processMailScript'};
98
my $reportScript = "$MY_DIRECTORY/" . $$config{'reportScript'};
9 rodolico 99
 
6 rodolico 100
die "Could not find the getMailScript [$getMailScript] in $MY_DIRECTORY\n" unless -e $getMailScript;
101
die "Could not find the processMailScript [$processMailScript] in $MY_DIRECTORY\n" unless -e $processMailScript;
9 rodolico 102
die "Could not find the reportScript [$reportScript] in $MY_DIRECTORY\n" unless -e $reportScript;
6 rodolico 103
 
74 rodolico 104
#die "$getMailScript\n$processMailScript\n$reportScript\n";
6 rodolico 105
# fetch all messages pending from e-mail accounts
106
`php $getMailScript` if $CHECKMAIL;
107
 
3 rodolico 108
# get a list of all messages waiting to be processed
109
opendir ( my $dh, $UNPROCESSED ) or die "Could not open $UNPROCESSED for read: $!";
34 rodolico 110
my @files = map{ "$UNPROCESSED\/$_" } sort grep { ! /^\./ && -f "$UNPROCESSED/$_" } readdir( $dh );
3 rodolico 111
closedir $dh;
112
 
74 rodolico 113
#print join( "\n", @files ) . "\n"; die;
114
 
3 rodolico 115
my $count = 0;
34 rodolico 116
my $results = '';
3 rodolico 117
 
118
foreach my $thisFile ( sort @files ) {
34 rodolico 119
   my $exitCode;
120
   my $tempResults;
121
 
6 rodolico 122
   if ( $PROCESS ) {
9 rodolico 123
      $tempResults .=  `php $processMailScript <'$thisFile'`;
6 rodolico 124
      if ( $? == -1 ) {
125
         $exitCode = -1;
126
         die "Parsing failed: $!\n";
127
      } else {
128
         $exitCode = $? >> 8;
129
      }
130
   } else {
12 rodolico 131
     $results .= "$thisFile moving without processing\n";
6 rodolico 132
     $exitCode = 0;
133
   }
134
   if ( $exitCode == 0 ) {
135
      $filesProcessed{ 'valid' }++;
9 rodolico 136
      $tempResults = '';
6 rodolico 137
      &storeFile( $thisFile ) if $MOVEFILES;
138
   } elsif ( $exitCode == 1 ) {
139
      $filesProcessed{ 'Invalid Format' }++;
140
      &storeFile( $thisFile, 'InvalidFormat' ) if $MOVEFILES;
141
   } elsif ( $exitCode == 2 || $exitCode == 3 ) {
142
      $filesProcessed{ 'Invalid Report' }++;
143
      &storeFile( $thisFile, 'InvalidReport' ) if $MOVEFILES;
144
   } elsif ( $exitCode == 4 ) {
145
      $filesProcessed{ 'Duplicate Report' }++;
146
      &storeFile( $thisFile, 'DuplicateReport' ) if $MOVEFILES;
147
   } elsif ( $exitCode != 5 && $exitCode != 6 ) { ## not any other of our valid exit codes
148
      die "parse_sysinfo.php returned an unknown exit code $exitCode for $thisFile\n";
149
   } else {
150
      # at this point, we only have reports waiting for manual CAMP
151
      # updates, so just leave them where they are
152
      $filesProcessed{ 'Waiting CAMP Updates' }++;
153
   }
9 rodolico 154
   $results .= $tempResults;
3 rodolico 155
  last if ++$count >= $MAXTOPROCESS;
156
  print STDERR "\r$count" unless $CRON;
157
}
158
 
6 rodolico 159
my $emailString;
3 rodolico 160
print "\n" unless $CRON; 
161
$count= 0;
162
foreach my $key ( sort keys %filesProcessed ) {
163
   $count += $filesProcessed{$key};
6 rodolico 164
   $emailString .=  "$filesProcessed{$key}\t$key\n";
3 rodolico 165
}
6 rodolico 166
$emailString .=  "$count\tTotal Files Processed\n";
167
$emailString .=  "--------------------------------\n\n";
168
$emailString .=  $results;
169
 
9 rodolico 170
open SEND, "|$reportScript" or die "Could not find sendReport.pl: $!\n";
171
print SEND $emailString;
172
close SEND;
6 rodolico 173
 
3 rodolico 174
1;