Subversion Repositories computer_asset_manager_v1

Rev

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

Rev Author Line No. Line
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
 
3 rodolico 59
# assumes parse_sysinfo.php returns one of the following codes
60
# 0 - Processed Normally
61
# 1 - could not process file (not xml, yaml or ini)
62
# 2 - Invalid report, does not have one or more of report date, client name or computer name
63
# 3 - Invalid Report, invalid machine name
64
# 4 - Duplicate Report
65
# 5 - Valid report, but no entry for client and/or machine in database
66
# 6 - Valid report, but waiting for client and/or machine to be added in database
67
 
68
sub storeFile {
69
   use File::Basename;
70
   my $file = shift;
71
   my $targetDirectory = shift;
72
   unless ( $targetDirectory ) {
73
      my ($name,$path) = fileparse($file);
74
      my ( $date,$time,$client,$server,$serial) = split( '_', $name );
75
      my ( $year, $month, $day ) = split( '-', $date );
76
      $targetDirectory = "/$year/$month";
77
   }
78
   $targetDirectory = "$DATADIR/$targetDirectory";
79
   `mkdir -p '$targetDirectory'` unless -d $targetDirectory;
80
   `mv '$file' '$targetDirectory'`;
81
}
82
 
6 rodolico 83
# get our configuration set up first
3 rodolico 84
 
6 rodolico 85
my $config = &loadConfig( $confFileName, @searchPaths );
8 rodolico 86
die "Could not find configuration file $confFileName in " . join( ', ', @searchPaths) . "\n" unless $config;
87
 
88
# just some convenience variables
6 rodolico 89
$DATADIR = $$config{'datapath'};
90
$UNPROCESSED=$DATADIR . '/' . $$config{'unprocessed_path'};
3 rodolico 91
 
8 rodolico 92
# check that the executables exist before continuing
6 rodolico 93
$getMailScript = "$MY_DIRECTORY/" . $$config{'getMailScript'};
94
$processMailScript = "$MY_DIRECTORY/" . $$config{'processMailScript'};
9 rodolico 95
$reportScript = "$MY_DIRECTORY/" . $$config{'reportScript'};
96
 
6 rodolico 97
die "Could not find the getMailScript [$getMailScript] in $MY_DIRECTORY\n" unless -e $getMailScript;
98
die "Could not find the processMailScript [$processMailScript] in $MY_DIRECTORY\n" unless -e $processMailScript;
9 rodolico 99
die "Could not find the reportScript [$reportScript] in $MY_DIRECTORY\n" unless -e $reportScript;
6 rodolico 100
 
9 rodolico 101
 
6 rodolico 102
# fetch all messages pending from e-mail accounts
103
`php $getMailScript` if $CHECKMAIL;
104
 
3 rodolico 105
# get a list of all messages waiting to be processed
106
opendir ( my $dh, $UNPROCESSED ) or die "Could not open $UNPROCESSED for read: $!";
107
@files = map{ "$UNPROCESSED\/$_" } sort grep { ! /^\./ && -f "$UNPROCESSED/$_" } readdir( $dh );
108
closedir $dh;
109
 
110
my $count = 0;
111
 
112
foreach my $thisFile ( sort @files ) {
6 rodolico 113
   if ( $PROCESS ) {
9 rodolico 114
      $tempResults .=  `php $processMailScript <'$thisFile'`;
6 rodolico 115
      if ( $? == -1 ) {
116
         $exitCode = -1;
117
         die "Parsing failed: $!\n";
118
      } else {
119
         $exitCode = $? >> 8;
120
      }
121
   } else {
122
     $results .= "Not processing file $thisFile";
123
     $exitCode = 0;
124
   }
125
   if ( $exitCode == 0 ) {
126
      $filesProcessed{ 'valid' }++;
9 rodolico 127
      $tempResults = '';
6 rodolico 128
      &storeFile( $thisFile ) if $MOVEFILES;
129
   } elsif ( $exitCode == 1 ) {
130
      $filesProcessed{ 'Invalid Format' }++;
131
      &storeFile( $thisFile, 'InvalidFormat' ) if $MOVEFILES;
132
   } elsif ( $exitCode == 2 || $exitCode == 3 ) {
133
      $filesProcessed{ 'Invalid Report' }++;
134
      &storeFile( $thisFile, 'InvalidReport' ) if $MOVEFILES;
135
   } elsif ( $exitCode == 4 ) {
136
      $filesProcessed{ 'Duplicate Report' }++;
137
      &storeFile( $thisFile, 'DuplicateReport' ) if $MOVEFILES;
138
   } elsif ( $exitCode != 5 && $exitCode != 6 ) { ## not any other of our valid exit codes
139
      die "parse_sysinfo.php returned an unknown exit code $exitCode for $thisFile\n";
140
   } else {
141
      # at this point, we only have reports waiting for manual CAMP
142
      # updates, so just leave them where they are
143
      $filesProcessed{ 'Waiting CAMP Updates' }++;
144
   }
9 rodolico 145
   $results .= $tempResults;
3 rodolico 146
  last if ++$count >= $MAXTOPROCESS;
147
  print STDERR "\r$count" unless $CRON;
148
}
149
 
6 rodolico 150
my $emailString;
3 rodolico 151
print "\n" unless $CRON; 
152
$count= 0;
153
foreach my $key ( sort keys %filesProcessed ) {
154
   $count += $filesProcessed{$key};
6 rodolico 155
   $emailString .=  "$filesProcessed{$key}\t$key\n";
3 rodolico 156
}
6 rodolico 157
$emailString .=  "$count\tTotal Files Processed\n";
158
$emailString .=  "--------------------------------\n\n";
159
$emailString .=  $results;
160
 
9 rodolico 161
open SEND, "|$reportScript" or die "Could not find sendReport.pl: $!\n";
162
print SEND $emailString;
163
close SEND;
6 rodolico 164
 
3 rodolico 165
1;