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 |
|
74 |
rodolico |
120 |
sub sendViaSendmail {
|
|
|
121 |
my ( $parameters, $report ) = @_;
|
|
|
122 |
my @header;
|
|
|
123 |
push @header, 'To: ' . qq/$$parameters{'mailTo'}/;
|
|
|
124 |
push @header, 'From: ' . qq/$$parameters{'mailFrom'}/;
|
|
|
125 |
push @header, 'Subject: ' . qq/$$parameters{'mailSubject'}/;
|
|
|
126 |
$report = join( "\n", @header ) . "\n\n" . $report . "\n.\n";
|
75 |
rodolico |
127 |
open SENDMAIL, "|/usr/sbin/sendmail $$parameters{mailTo}" or die "could not open sendmail: $!\n";
|
74 |
rodolico |
128 |
print SENDMAIL $report;
|
|
|
129 |
close SENDMAIL;
|
|
|
130 |
}
|
|
|
131 |
|
|
|
132 |
|
9 |
rodolico |
133 |
########################################################################
|
|
|
134 |
# Main Program
|
|
|
135 |
########################################################################
|
|
|
136 |
|
|
|
137 |
# Read anything passed on STDIN to prepend to e-mail
|
|
|
138 |
while ( my $line = <> ) {
|
|
|
139 |
$message .= $line;
|
|
|
140 |
}
|
|
|
141 |
|
|
|
142 |
my $configuration = &loadConfig( $confFileName, @searchPaths );
|
|
|
143 |
die "Could not find configuration file $confFileName in " . join( ', ', @searchPaths) . "\n" unless $configuration;
|
|
|
144 |
|
114 |
rodolico |
145 |
#print 'DBI:mysql:database=' . $$configuration{'database'}{'database'} . ';host=' . $$configuration{'database'}{'databaseServer'} . "\n" . $$configuration{'database'}{'databaseUsername'}. "\n" . $$configuration{'database'}{'databasePassword'} . "\n";
|
|
|
146 |
my $dbh = DBI->connect('DBI:mysql:database=' . $$configuration{'database'}{'database'} . ';host=' . $$configuration{'database'}{'databaseServer'},
|
9 |
rodolico |
147 |
$$configuration{'database'}{'databaseUsername'},
|
|
|
148 |
$$configuration{'database'}{'databasePassword'} )
|
|
|
149 |
|| die "Could not connect to database: $DBI::errstr";
|
|
|
150 |
|
|
|
151 |
my $sth = $dbh->prepare( $count );
|
|
|
152 |
$sth->execute();
|
|
|
153 |
my $results = $sth->fetchrow_hashref();
|
|
|
154 |
$message .= "$results->{Count} reports in last 24 hours\n";
|
|
|
155 |
$sth->finish();
|
|
|
156 |
|
21 |
rodolico |
157 |
$results = $dbh->selectall_hashref( $missing, 'ID' );
|
|
|
158 |
if ( %$results ) {
|
|
|
159 |
$message .= '='x40 . "\nWARNING, no sysinfo reports for the following machines\n" . '='x40 . "\n";
|
|
|
160 |
foreach my $device ( keys %$results ) {
|
|
|
161 |
$message .= sprintf( "%6d\t%s\n", $device, $results->{$device}->{name} );
|
|
|
162 |
}
|
|
|
163 |
$message .= '='x40 . "\n\n";
|
|
|
164 |
}
|
|
|
165 |
|
74 |
rodolico |
166 |
|
9 |
rodolico |
167 |
$results = $dbh->selectall_hashref( $query, 'ID' );
|
|
|
168 |
foreach my $id ( keys %$results ) {
|
|
|
169 |
$message .= $results->{$id}->{Date} . "\t" . $results->{$id}->{Client} . "\t" . $results->{$id}->{Device} . "\n";
|
|
|
170 |
$message .= $results->{$id}->{Notes} . "\n\n";
|
|
|
171 |
}
|
|
|
172 |
$dbh->disconnect();
|
21 |
rodolico |
173 |
|
17 |
rodolico |
174 |
#print $message;
|
74 |
rodolico |
175 |
if ( $$configuration{'sendReport'}{'emailScript'} ) {
|
|
|
176 |
&sendReport( $$configuration{'sendReport'}, $message );
|
|
|
177 |
} else {
|
|
|
178 |
&sendViaSendmail( $$configuration{'sendReport'}, $message );
|
|
|
179 |
}
|
9 |
rodolico |
180 |
|
|
|
181 |
1;
|
74 |
rodolico |
182 |
|