Subversion Repositories computer_asset_manager_v1

Rev

Rev 17 | Rev 22 | 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
 
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)/;
9 rodolico 62
 
63
# following are used to find the configuration file
64
my $confFileName = "sysinfoRead.conf.yaml";
65
my @searchPaths = ( '/etc/camp', '/opt/camp', '/opt/camp/sysinfo', $MY_DIRECTORY );
66
 
67
 
68
sub loadConfig {
69
   my ( $confFileName, @searchPaths ) = @_;
70
   my $configuration;
71
   for ( $i = 0; $i < @searchPaths; $i++ ) {
72
      $filename = $searchPaths[$i] . '/' . $confFileName;
73
      if ( -e $filename ) {
74
         #print "Found $filename\n";
75
         open CONF, "<$filename" or warn "Could not read $filename: $!\n";
76
         $configuration = Load( join( '', <CONF> ) );
77
         close CONF;
78
         last; # exit out of the loop; we don't try to load it more than once
79
      } # if
80
   } # foreach
81
   return $configuration;
82
} # sub loadConfig
83
 
84
sub sendReport {
85
   my ($parameters,$report) = @_;
86
   my %CLIParams ;
87
   $CLIParams{'-f'}  = qq/$$parameters{'mailFrom'}/    if $$parameters{'mailFrom'};
88
   $CLIParams{'-t'}  = qq/$$parameters{'mailTo'}/      if $$parameters{'mailTo'};
89
   $CLIParams{'-u'}  = qq/$$parameters{'mailSubject'}/ if $$parameters{'mailSubject'};
90
   $CLIParams{'-s'}  = qq/$$parameters{'mailServer'}/  if $$parameters{'mailServer'};
91
   $CLIParams{'-xu'} = qq/$$parameters{'smtpUser'}/    if $$parameters{'smtpUser'};
92
   $CLIParams{'-xp'} = qq/$$parameters{'smtpPass'}/    if $$parameters{'smtpPass'};
93
   $CLIParams{'-cc'} = qq/$$parameters{'mailCC'}/      if $$parameters{'mailCC'};
94
   $CLIParams{'-bcc'}= qq/$$parameters{'mailBCC'}/     if $$parameters{'mailBCC'};
95
   $CLIParams{'-l'}  = qq/$$parameters{'logFile'}/     if $$parameters{'logFile'};
96
   $CLIParams{'-a'}  = qq/$$parameters{'attachment'}/  if $$parameters{'attachment'};
97
   $CLIParams{'-o tls='} = qq/$$parameters{'tls'}/     if $$parameters{'tls'};
98
 
99
   $commandLine = qq/$$parameters{'emailScript'}/;
100
   die "Could not find executable $commandLine in sendEmailScript\n" unless -x $commandLine;
101
   $commandLine .= ' -q'; # make it act quietly
102
   foreach my $key ( keys %CLIParams ) {
103
      # depending on whether the key ends in an = or not, we will or will not use a space
104
      # between the key and the parameter
105
      $commandLine .= $key =~ m/=$/ ? " $key'$CLIParams{$key}'" : " $key '$CLIParams{$key}'";
106
   }
107
   $commandLine .= ' ' . $$parameters{'otherCLParams'} if $$parameters{'otherCLParams'};
108
   open SENDMAIL, "|$commandLine" or die "Could not open [$commandLine]: $!\n";
109
   print SENDMAIL $report;
110
   close SENDMAIL;
111
} # sendReport
112
 
113
########################################################################
114
# Main Program
115
########################################################################
116
 
117
# Read anything passed on STDIN to prepend to e-mail
118
while ( my $line = <> ) {
119
   $message .= $line;
120
}
121
 
122
my $configuration = &loadConfig( $confFileName, @searchPaths );
123
die "Could not find configuration file $confFileName in " . join( ', ', @searchPaths) . "\n" unless $configuration;
124
 
125
my $dbh = DBI->connect('DBI:mysql:' . $$configuration{'database'}{'database'},
126
                       $$configuration{'database'}{'databaseUsername'}, 
127
                       $$configuration{'database'}{'databasePassword'} )
128
         || die "Could not connect to database: $DBI::errstr";
129
 
130
my $sth = $dbh->prepare( $count );
131
$sth->execute();
132
my $results = $sth->fetchrow_hashref();
133
$message .= "$results->{Count} reports in last 24 hours\n";
134
$sth->finish();
135
 
21 rodolico 136
$results = $dbh->selectall_hashref( $missing, 'ID' );
137
if ( %$results ) {
138
   $message .= '='x40 . "\nWARNING, no sysinfo reports for the following machines\n" . '='x40 . "\n";
139
   foreach my $device ( keys %$results ) {
140
      $message .= sprintf( "%6d\t%s\n", $device, $results->{$device}->{name} );
141
   }
142
   $message .= '='x40 . "\n\n";
143
}
144
 
9 rodolico 145
$results = $dbh->selectall_hashref( $query, 'ID' );
146
foreach my $id ( keys %$results ) {
147
   $message .= $results->{$id}->{Date} . "\t" . $results->{$id}->{Client} . "\t" . $results->{$id}->{Device} . "\n";
148
   $message .= $results->{$id}->{Notes} . "\n\n";
149
}
150
$dbh->disconnect();
21 rodolico 151
 
17 rodolico 152
#print $message;
9 rodolico 153
&sendReport( $$configuration{'sendReport'}, $message );
154
 
155
1;