Subversion Repositories computer_asset_manager_v1

Rev

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