| 23 | 
           rodolico | 
           1 | 
           #! /usr/bin/perl -w
  | 
        
        
            | 
            | 
           2 | 
              | 
        
        
            | 
            | 
           3 | 
           use DBI;
  | 
        
        
            | 
            | 
           4 | 
           use Cwd 'abs_path';
  | 
        
        
            | 
            | 
           5 | 
           #use Data::Dumper;
  | 
        
        
            | 
            | 
           6 | 
           use File::Basename;
  | 
        
        
            | 
            | 
           7 | 
           use YAML::XS;
  | 
        
        
            | 
            | 
           8 | 
              | 
        
        
            | 
            | 
           9 | 
              | 
        
        
            | 
            | 
           10 | 
           my $VERSION = '0.8';
  | 
        
        
            | 
            | 
           11 | 
           my $MY_DIRECTORY = abs_path(dirname(__FILE__) );
  | 
        
        
            | 
            | 
           12 | 
           my $message; # the message to send
  | 
        
        
            | 
            | 
           13 | 
           my $query = qq/select
  | 
        
        
            | 
            | 
           14 | 
                             sysinfo_report.sysinfo_report_id 'ID', 
  | 
        
        
            | 
            | 
           15 | 
                             sysinfo_report.report_date 'Date',
  | 
        
        
            | 
            | 
           16 | 
                             client.name 'Client',
  | 
        
        
            | 
            | 
           17 | 
                             device.name 'Device',
  | 
        
        
            | 
            | 
           18 | 
                             sysinfo_report.notes 'Notes'
  | 
        
        
            | 
            | 
           19 | 
                          from 
  | 
        
        
            | 
            | 
           20 | 
                             sysinfo_report 
  | 
        
        
            | 
            | 
           21 | 
                             join device using (device_id) 
  | 
        
        
            | 
            | 
           22 | 
                             join site site using (site_id) 
  | 
        
        
            | 
            | 
           23 | 
                             join client using (client_id) 
  | 
        
        
            | 
            | 
           24 | 
                          where 
  | 
        
        
            | 
            | 
           25 | 
                             sysinfo_report.added_date > date_sub( now(), interval 1 day)
  | 
        
        
            | 
            | 
           26 | 
                             and sysinfo_report.notes is not null/;
  | 
        
        
            | 
            | 
           27 | 
              | 
        
        
            | 
            | 
           28 | 
           my $count = qq/select 
  | 
        
        
            | 
            | 
           29 | 
                             count(*) 'Count'
  | 
        
        
            | 
            | 
           30 | 
                          from sysinfo_report
  | 
        
        
            | 
            | 
           31 | 
                          where sysinfo_report.added_date > date_sub( now(), interval 1 day)/;
  | 
        
        
            | 
            | 
           32 | 
              | 
        
        
            | 
            | 
           33 | 
           # following are used to find the configuration file
  | 
        
        
            | 
            | 
           34 | 
           my $confFileName = "sysinfoRead.conf.yaml";
  | 
        
        
            | 
            | 
           35 | 
           my @searchPaths = ( '/etc/camp', '/opt/camp', '/opt/camp/sysinfo', $MY_DIRECTORY );
  | 
        
        
            | 
            | 
           36 | 
              | 
        
        
            | 
            | 
           37 | 
              | 
        
        
            | 
            | 
           38 | 
           sub loadConfig {
  | 
        
        
            | 
            | 
           39 | 
              my ( $confFileName, @searchPaths ) = @_;
  | 
        
        
            | 
            | 
           40 | 
              my $configuration;
  | 
        
        
            | 
            | 
           41 | 
              for ( $i = 0; $i < @searchPaths; $i++ ) {
  | 
        
        
            | 
            | 
           42 | 
                 $filename = $searchPaths[$i] . '/' . $confFileName;
  | 
        
        
            | 
            | 
           43 | 
                 if ( -e $filename ) {
  | 
        
        
            | 
            | 
           44 | 
                    #print "Found $filename\n";
  | 
        
        
            | 
            | 
           45 | 
                    open CONF, "<$filename" or warn "Could not read $filename: $!\n";
  | 
        
        
            | 
            | 
           46 | 
                    $configuration = Load( join( '', <CONF> ) );
  | 
        
        
            | 
            | 
           47 | 
                    close CONF;
  | 
        
        
            | 
            | 
           48 | 
                    last; # exit out of the loop; we don't try to load it more than once
  | 
        
        
            | 
            | 
           49 | 
                 } # if
  | 
        
        
            | 
            | 
           50 | 
              } # foreach
  | 
        
        
            | 
            | 
           51 | 
              return $configuration;
  | 
        
        
            | 
            | 
           52 | 
           } # sub loadConfig
  | 
        
        
            | 
            | 
           53 | 
              | 
        
        
            | 
            | 
           54 | 
           sub sendReport {
  | 
        
        
            | 
            | 
           55 | 
              my ($parameters,$report) = @_;
  | 
        
        
            | 
            | 
           56 | 
              my %CLIParams ;
  | 
        
        
            | 
            | 
           57 | 
              $CLIParams{'-f'}  = qq/$$parameters{'mailFrom'}/    if $$parameters{'mailFrom'};
  | 
        
        
            | 
            | 
           58 | 
              $CLIParams{'-t'}  = qq/$$parameters{'mailTo'}/      if $$parameters{'mailTo'};
  | 
        
        
            | 
            | 
           59 | 
              $CLIParams{'-u'}  = qq/$$parameters{'mailSubject'}/ if $$parameters{'mailSubject'};
  | 
        
        
            | 
            | 
           60 | 
              $CLIParams{'-s'}  = qq/$$parameters{'mailServer'}/  if $$parameters{'mailServer'};
  | 
        
        
            | 
            | 
           61 | 
              $CLIParams{'-xu'} = qq/$$parameters{'smtpUser'}/    if $$parameters{'smtpUser'};
  | 
        
        
            | 
            | 
           62 | 
              $CLIParams{'-xp'} = qq/$$parameters{'smtpPass'}/    if $$parameters{'smtpPass'};
  | 
        
        
            | 
            | 
           63 | 
              $CLIParams{'-cc'} = qq/$$parameters{'mailCC'}/      if $$parameters{'mailCC'};
  | 
        
        
            | 
            | 
           64 | 
              $CLIParams{'-bcc'}= qq/$$parameters{'mailBCC'}/     if $$parameters{'mailBCC'};
  | 
        
        
            | 
            | 
           65 | 
              $CLIParams{'-l'}  = qq/$$parameters{'logFile'}/     if $$parameters{'logFile'};
  | 
        
        
            | 
            | 
           66 | 
              $CLIParams{'-a'}  = qq/$$parameters{'attachment'}/  if $$parameters{'attachment'};
  | 
        
        
            | 
            | 
           67 | 
              $CLIParams{'-o tls='} = qq/$$parameters{'tls'}/     if $$parameters{'tls'};
  | 
        
        
            | 
            | 
           68 | 
              | 
        
        
            | 
            | 
           69 | 
              $commandLine = qq/$$parameters{'emailScript'}/;
  | 
        
        
            | 
            | 
           70 | 
              die "Could not find executable $commandLine in sendEmailScript\n" unless -x $commandLine;
  | 
        
        
            | 
            | 
           71 | 
              $commandLine .= ' -q'; # make it act quietly
  | 
        
        
            | 
            | 
           72 | 
              foreach my $key ( keys %CLIParams ) {
  | 
        
        
            | 
            | 
           73 | 
                 # depending on whether the key ends in an = or not, we will or will not use a space
  | 
        
        
            | 
            | 
           74 | 
                 # between the key and the parameter
  | 
        
        
            | 
            | 
           75 | 
                 $commandLine .= $key =~ m/=$/ ? " $key'$CLIParams{$key}'" : " $key '$CLIParams{$key}'";
  | 
        
        
            | 
            | 
           76 | 
              }
  | 
        
        
            | 
            | 
           77 | 
              $commandLine .= ' ' . $$parameters{'otherCLParams'} if $$parameters{'otherCLParams'};
  | 
        
        
            | 
            | 
           78 | 
              open SENDMAIL, "|$commandLine" or die "Could not open [$commandLine]: $!\n";
  | 
        
        
            | 
            | 
           79 | 
              print SENDMAIL $report;
  | 
        
        
            | 
            | 
           80 | 
              close SENDMAIL;
  | 
        
        
            | 
            | 
           81 | 
           } # sendReport
  | 
        
        
            | 
            | 
           82 | 
              | 
        
        
            | 
            | 
           83 | 
           ########################################################################
  | 
        
        
            | 
            | 
           84 | 
           # Main Program
  | 
        
        
            | 
            | 
           85 | 
           ########################################################################
  | 
        
        
            | 
            | 
           86 | 
              | 
        
        
            | 
            | 
           87 | 
           # Read anything passed on STDIN to prepend to e-mail
  | 
        
        
            | 
            | 
           88 | 
           while ( my $line = <> ) {
  | 
        
        
            | 
            | 
           89 | 
              $message .= $line;
  | 
        
        
            | 
            | 
           90 | 
           }
  | 
        
        
            | 
            | 
           91 | 
              | 
        
        
            | 
            | 
           92 | 
           my $configuration = &loadConfig( $confFileName, @searchPaths );
  | 
        
        
            | 
            | 
           93 | 
           die "Could not find configuration file $confFileName in " . join( ', ', @searchPaths) . "\n" unless $configuration;
  | 
        
        
            | 
            | 
           94 | 
              | 
        
        
            | 
            | 
           95 | 
           my $dbh = DBI->connect('DBI:mysql:' . $$configuration{'database'}{'database'},
  | 
        
        
            | 
            | 
           96 | 
                                  $$configuration{'database'}{'databaseUsername'}, 
  | 
        
        
            | 
            | 
           97 | 
                                  $$configuration{'database'}{'databasePassword'} )
  | 
        
        
            | 
            | 
           98 | 
                    || die "Could not connect to database: $DBI::errstr";
  | 
        
        
            | 
            | 
           99 | 
              | 
        
        
            | 
            | 
           100 | 
           my $sth = $dbh->prepare( $count );
  | 
        
        
            | 
            | 
           101 | 
           $sth->execute();
  | 
        
        
            | 
            | 
           102 | 
           my $results = $sth->fetchrow_hashref();
  | 
        
        
            | 
            | 
           103 | 
           $message .= "$results->{Count} reports in last 24 hours\n";
  | 
        
        
            | 
            | 
           104 | 
           $sth->finish();
  | 
        
        
            | 
            | 
           105 | 
              | 
        
        
            | 
            | 
           106 | 
           $results = $dbh->selectall_hashref( $query, 'ID' );
  | 
        
        
            | 
            | 
           107 | 
           foreach my $id ( keys %$results ) {
  | 
        
        
            | 
            | 
           108 | 
              $message .= $results->{$id}->{Date} . "\t" . $results->{$id}->{Client} . "\t" . $results->{$id}->{Device} . "\n";
  | 
        
        
            | 
            | 
           109 | 
              $message .= $results->{$id}->{Notes} . "\n\n";
  | 
        
        
            | 
            | 
           110 | 
           }
  | 
        
        
            | 
            | 
           111 | 
           $dbh->disconnect();
  | 
        
        
            | 
            | 
           112 | 
           print $message;
  | 
        
        
            | 
            | 
           113 | 
           &sendReport( $$configuration{'sendReport'}, $message );
  | 
        
        
            | 
            | 
           114 | 
              | 
        
        
            | 
            | 
           115 | 
           1;
  |