Subversion Repositories computer_asset_manager_v1

Rev

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

Rev Author Line No. Line
9 rodolico 1
#! /usr/bin/perl -w
2
 
17 rodolico 3
#
4
# v0.8.1 20160218 RWR
5
# removed print statement to STDOUT (just commented it out)
6
#
21 rodolico 7
# v0.8.2 20160407 RWR
8
# Added checking for missing sysinfo reports
9
# if a report is not shown in current day, but has been seen in the 
10
# past five days, an entry is made on the report
17 rodolico 11
 
9 rodolico 12
use DBI;
13
use Cwd 'abs_path';
14
#use Data::Dumper;
15
use File::Basename;
16
use YAML::XS;
17
 
18
 
17 rodolico 19
my $VERSION = '0.8.1';
9 rodolico 20
my $MY_DIRECTORY = abs_path(dirname(__FILE__) );
21
my $message; # the message to send
22
my $query = qq/select
23
                  sysinfo_report.sysinfo_report_id 'ID', 
24
                  sysinfo_report.report_date 'Date',
25
                  client.name 'Client',
26
                  device.name 'Device',
27
                  sysinfo_report.notes 'Notes'
28
               from 
29
                  sysinfo_report 
30
                  join device using (device_id) 
31
                  join site site using (site_id) 
32
                  join client using (client_id) 
33
               where 
34
                  sysinfo_report.added_date > date_sub( now(), interval 1 day)
35
                  and sysinfo_report.notes is not null/;
36
 
37
my $count = qq/select 
38
                  count(*) 'Count'
39
               from sysinfo_report
40
               where sysinfo_report.added_date > date_sub( now(), interval 1 day)/;
21 rodolico 41
 
22 rodolico 42
#my $missing = qq/select
43
#                  device_id ID,
44
#                  device.name name
45
#               from (
46
#                  select 
47
#                     distinct( device_id ) 
48
#                  from 
49
#                     sysinfo_report
50
#                  where 
51
#                     datediff( current_date(),added_date) < 5
52
#                  ) last_five
53
#                  join device using ( device_id )
54
#               where 
55
#                  device.device_id not in (
56
#                     select 
57
#                        distinct( device_id ) 
58
#                     from 
59
#                        sysinfo_report 
60
#                     where 
61
#                        datediff( current_date(),added_date) < 1)/;
62
 
21 rodolico 63
my $missing = qq/select
22 rodolico 64
                  last_five.device_id ID,
21 rodolico 65
                  device.name name
66
               from (
67
                  select 
68
                     distinct( device_id ) 
69
                  from 
70
                     sysinfo_report
71
                  where 
72
                     datediff( current_date(),added_date) < 5
73
                  ) last_five
22 rodolico 74
                  left join
75
                  (
76
                  select 
77
                     distinct( device_id ) 
78
                  from 
79
                     sysinfo_report
80
                  where 
81
                     datediff( current_date(),added_date) < 1
82
                  ) today using (device_id)
21 rodolico 83
                  join device using ( device_id )
84
               where 
22 rodolico 85
                  today.device_id is null/;
86
 
9 rodolico 87
 
88
# following are used to find the configuration file
89
my $confFileName = "sysinfoRead.conf.yaml";
90
my @searchPaths = ( '/etc/camp', '/opt/camp', '/opt/camp/sysinfo', $MY_DIRECTORY );
91
 
92
 
93
sub loadConfig {
94
   my ( $confFileName, @searchPaths ) = @_;
95
   my $configuration;
96
   for ( $i = 0; $i < @searchPaths; $i++ ) {
97
      $filename = $searchPaths[$i] . '/' . $confFileName;
98
      if ( -e $filename ) {
99
         #print "Found $filename\n";
100
         open CONF, "<$filename" or warn "Could not read $filename: $!\n";
101
         $configuration = Load( join( '', <CONF> ) );
102
         close CONF;
103
         last; # exit out of the loop; we don't try to load it more than once
104
      } # if
105
   } # foreach
106
   return $configuration;
107
} # sub loadConfig
108
 
109
sub sendReport {
110
   my ($parameters,$report) = @_;
111
   my %CLIParams ;
112
   $CLIParams{'-f'}  = qq/$$parameters{'mailFrom'}/    if $$parameters{'mailFrom'};
113
   $CLIParams{'-t'}  = qq/$$parameters{'mailTo'}/      if $$parameters{'mailTo'};
114
   $CLIParams{'-u'}  = qq/$$parameters{'mailSubject'}/ if $$parameters{'mailSubject'};
115
   $CLIParams{'-s'}  = qq/$$parameters{'mailServer'}/  if $$parameters{'mailServer'};
116
   $CLIParams{'-xu'} = qq/$$parameters{'smtpUser'}/    if $$parameters{'smtpUser'};
117
   $CLIParams{'-xp'} = qq/$$parameters{'smtpPass'}/    if $$parameters{'smtpPass'};
118
   $CLIParams{'-cc'} = qq/$$parameters{'mailCC'}/      if $$parameters{'mailCC'};
119
   $CLIParams{'-bcc'}= qq/$$parameters{'mailBCC'}/     if $$parameters{'mailBCC'};
120
   $CLIParams{'-l'}  = qq/$$parameters{'logFile'}/     if $$parameters{'logFile'};
121
   $CLIParams{'-a'}  = qq/$$parameters{'attachment'}/  if $$parameters{'attachment'};
122
   $CLIParams{'-o tls='} = qq/$$parameters{'tls'}/     if $$parameters{'tls'};
123
 
124
   $commandLine = qq/$$parameters{'emailScript'}/;
125
   die "Could not find executable $commandLine in sendEmailScript\n" unless -x $commandLine;
126
   $commandLine .= ' -q'; # make it act quietly
127
   foreach my $key ( keys %CLIParams ) {
128
      # depending on whether the key ends in an = or not, we will or will not use a space
129
      # between the key and the parameter
130
      $commandLine .= $key =~ m/=$/ ? " $key'$CLIParams{$key}'" : " $key '$CLIParams{$key}'";
131
   }
132
   $commandLine .= ' ' . $$parameters{'otherCLParams'} if $$parameters{'otherCLParams'};
133
   open SENDMAIL, "|$commandLine" or die "Could not open [$commandLine]: $!\n";
134
   print SENDMAIL $report;
135
   close SENDMAIL;
136
} # sendReport
137
 
138
########################################################################
139
# Main Program
140
########################################################################
141
 
142
# Read anything passed on STDIN to prepend to e-mail
143
while ( my $line = <> ) {
144
   $message .= $line;
145
}
146
 
147
my $configuration = &loadConfig( $confFileName, @searchPaths );
148
die "Could not find configuration file $confFileName in " . join( ', ', @searchPaths) . "\n" unless $configuration;
149
 
150
my $dbh = DBI->connect('DBI:mysql:' . $$configuration{'database'}{'database'},
151
                       $$configuration{'database'}{'databaseUsername'}, 
152
                       $$configuration{'database'}{'databasePassword'} )
153
         || die "Could not connect to database: $DBI::errstr";
154
 
155
my $sth = $dbh->prepare( $count );
156
$sth->execute();
157
my $results = $sth->fetchrow_hashref();
158
$message .= "$results->{Count} reports in last 24 hours\n";
159
$sth->finish();
160
 
21 rodolico 161
$results = $dbh->selectall_hashref( $missing, 'ID' );
162
if ( %$results ) {
163
   $message .= '='x40 . "\nWARNING, no sysinfo reports for the following machines\n" . '='x40 . "\n";
164
   foreach my $device ( keys %$results ) {
165
      $message .= sprintf( "%6d\t%s\n", $device, $results->{$device}->{name} );
166
   }
167
   $message .= '='x40 . "\n\n";
168
}
169
 
9 rodolico 170
$results = $dbh->selectall_hashref( $query, 'ID' );
171
foreach my $id ( keys %$results ) {
172
   $message .= $results->{$id}->{Date} . "\t" . $results->{$id}->{Client} . "\t" . $results->{$id}->{Device} . "\n";
173
   $message .= $results->{$id}->{Notes} . "\n\n";
174
}
175
$dbh->disconnect();
21 rodolico 176
 
17 rodolico 177
#print $message;
9 rodolico 178
&sendReport( $$configuration{'sendReport'}, $message );
179
 
180
1;