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