Subversion Repositories computer_asset_manager_v1

Rev

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