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 |
|
25 |
rodolico |
10 |
|
23 |
rodolico |
11 |
my $VERSION = '0.8';
|
|
|
12 |
my $MY_DIRECTORY = abs_path(dirname(__FILE__) );
|
|
|
13 |
my $message; # the message to send
|
27 |
rodolico |
14 |
# query to find all reports in last 24 hours. Assumes the script
|
|
|
15 |
# runs at the same time each day
|
25 |
rodolico |
16 |
my $query = qq|select
|
|
|
17 |
backups_run.backups_run_id 'ID',
|
|
|
18 |
device.device_id 'Device ID',
|
|
|
19 |
device.name 'Source',
|
|
|
20 |
timediff( backups_run.end_time,backups_run.start_time ) 'Run Time',
|
|
|
21 |
backups_run.transferred_count 'Files',
|
|
|
22 |
round(backups_run.transferred_size/1024/1024,2) 'Size (M)',
|
|
|
23 |
backups_run.files_deleted 'Deleted',
|
|
|
24 |
backups_run.skipped 'Skipped',
|
|
|
25 |
round( backups_run.data_sent/1024/1024,2) 'Traffic (M)'
|
|
|
26 |
from
|
|
|
27 |
backups_run
|
|
|
28 |
join backups using (backups_id)
|
|
|
29 |
join device using (device_id)
|
|
|
30 |
where
|
27 |
rodolico |
31 |
backups_run.start_time > date_sub( now(), interval 24 hour)
|
|
|
32 |
order by Source
|
25 |
rodolico |
33 |
|;
|
|
|
34 |
|
23 |
rodolico |
35 |
|
27 |
rodolico |
36 |
# Find number of backups in the past 24 hours.
|
25 |
rodolico |
37 |
my $count = qq/select count(*) 'Count'
|
|
|
38 |
from backups_run
|
27 |
rodolico |
39 |
where
|
|
|
40 |
backups_run.start_time > date_sub( now(), interval 24 hour)/;
|
23 |
rodolico |
41 |
|
27 |
rodolico |
42 |
# looks for backups which did NOT happen in last 24 hours, but did
|
|
|
43 |
# happen in last 7 days, ie backups that did not make it.
|
25 |
rodolico |
44 |
my $missing = qq/select
|
|
|
45 |
device.device_id ID,
|
|
|
46 |
device.name name
|
|
|
47 |
from
|
|
|
48 |
(
|
|
|
49 |
select distinct( backups_id )
|
|
|
50 |
from
|
|
|
51 |
backups_run
|
|
|
52 |
where
|
27 |
rodolico |
53 |
(datediff( now(),added_date) < 7 )
|
25 |
rodolico |
54 |
) last_five
|
|
|
55 |
left join
|
|
|
56 |
(
|
|
|
57 |
select
|
|
|
58 |
distinct( backups_id )
|
|
|
59 |
from
|
|
|
60 |
backups_run
|
|
|
61 |
where
|
27 |
rodolico |
62 |
backups_run.start_time > date_sub( now(), interval 24 hour)
|
25 |
rodolico |
63 |
) today using (backups_id)
|
|
|
64 |
join
|
|
|
65 |
backups using (backups_id)
|
|
|
66 |
join
|
|
|
67 |
device using (device_id)
|
|
|
68 |
where
|
|
|
69 |
today.backups_id is null
|
|
|
70 |
/;
|
|
|
71 |
|
|
|
72 |
|
23 |
rodolico |
73 |
# following are used to find the configuration file
|
25 |
rodolico |
74 |
my $confFileName = "rsbackupRead.conf.yaml";
|
23 |
rodolico |
75 |
my @searchPaths = ( '/etc/camp', '/opt/camp', '/opt/camp/sysinfo', $MY_DIRECTORY );
|
|
|
76 |
|
|
|
77 |
|
|
|
78 |
sub loadConfig {
|
|
|
79 |
my ( $confFileName, @searchPaths ) = @_;
|
|
|
80 |
my $configuration;
|
|
|
81 |
for ( $i = 0; $i < @searchPaths; $i++ ) {
|
|
|
82 |
$filename = $searchPaths[$i] . '/' . $confFileName;
|
|
|
83 |
if ( -e $filename ) {
|
|
|
84 |
#print "Found $filename\n";
|
|
|
85 |
open CONF, "<$filename" or warn "Could not read $filename: $!\n";
|
|
|
86 |
$configuration = Load( join( '', <CONF> ) );
|
|
|
87 |
close CONF;
|
|
|
88 |
last; # exit out of the loop; we don't try to load it more than once
|
|
|
89 |
} # if
|
|
|
90 |
} # foreach
|
|
|
91 |
return $configuration;
|
|
|
92 |
} # sub loadConfig
|
|
|
93 |
|
|
|
94 |
sub sendReport {
|
|
|
95 |
my ($parameters,$report) = @_;
|
|
|
96 |
my %CLIParams ;
|
|
|
97 |
$CLIParams{'-f'} = qq/$$parameters{'mailFrom'}/ if $$parameters{'mailFrom'};
|
|
|
98 |
$CLIParams{'-t'} = qq/$$parameters{'mailTo'}/ if $$parameters{'mailTo'};
|
|
|
99 |
$CLIParams{'-u'} = qq/$$parameters{'mailSubject'}/ if $$parameters{'mailSubject'};
|
|
|
100 |
$CLIParams{'-s'} = qq/$$parameters{'mailServer'}/ if $$parameters{'mailServer'};
|
|
|
101 |
$CLIParams{'-xu'} = qq/$$parameters{'smtpUser'}/ if $$parameters{'smtpUser'};
|
|
|
102 |
$CLIParams{'-xp'} = qq/$$parameters{'smtpPass'}/ if $$parameters{'smtpPass'};
|
|
|
103 |
$CLIParams{'-cc'} = qq/$$parameters{'mailCC'}/ if $$parameters{'mailCC'};
|
|
|
104 |
$CLIParams{'-bcc'}= qq/$$parameters{'mailBCC'}/ if $$parameters{'mailBCC'};
|
|
|
105 |
$CLIParams{'-l'} = qq/$$parameters{'logFile'}/ if $$parameters{'logFile'};
|
|
|
106 |
$CLIParams{'-a'} = qq/$$parameters{'attachment'}/ if $$parameters{'attachment'};
|
|
|
107 |
$CLIParams{'-o tls='} = qq/$$parameters{'tls'}/ if $$parameters{'tls'};
|
|
|
108 |
|
|
|
109 |
$commandLine = qq/$$parameters{'emailScript'}/;
|
|
|
110 |
die "Could not find executable $commandLine in sendEmailScript\n" unless -x $commandLine;
|
|
|
111 |
$commandLine .= ' -q'; # make it act quietly
|
|
|
112 |
foreach my $key ( keys %CLIParams ) {
|
|
|
113 |
# depending on whether the key ends in an = or not, we will or will not use a space
|
|
|
114 |
# between the key and the parameter
|
|
|
115 |
$commandLine .= $key =~ m/=$/ ? " $key'$CLIParams{$key}'" : " $key '$CLIParams{$key}'";
|
|
|
116 |
}
|
|
|
117 |
$commandLine .= ' ' . $$parameters{'otherCLParams'} if $$parameters{'otherCLParams'};
|
|
|
118 |
open SENDMAIL, "|$commandLine" or die "Could not open [$commandLine]: $!\n";
|
|
|
119 |
print SENDMAIL $report;
|
|
|
120 |
close SENDMAIL;
|
|
|
121 |
} # sendReport
|
|
|
122 |
|
|
|
123 |
########################################################################
|
|
|
124 |
# Main Program
|
|
|
125 |
########################################################################
|
|
|
126 |
|
|
|
127 |
# Read anything passed on STDIN to prepend to e-mail
|
|
|
128 |
while ( my $line = <> ) {
|
|
|
129 |
$message .= $line;
|
|
|
130 |
}
|
|
|
131 |
|
|
|
132 |
my $configuration = &loadConfig( $confFileName, @searchPaths );
|
|
|
133 |
die "Could not find configuration file $confFileName in " . join( ', ', @searchPaths) . "\n" unless $configuration;
|
|
|
134 |
|
|
|
135 |
my $dbh = DBI->connect('DBI:mysql:' . $$configuration{'database'}{'database'},
|
|
|
136 |
$$configuration{'database'}{'databaseUsername'},
|
|
|
137 |
$$configuration{'database'}{'databasePassword'} )
|
|
|
138 |
|| die "Could not connect to database: $DBI::errstr";
|
|
|
139 |
|
|
|
140 |
my $sth = $dbh->prepare( $count );
|
|
|
141 |
$sth->execute();
|
|
|
142 |
my $results = $sth->fetchrow_hashref();
|
|
|
143 |
$message .= "$results->{Count} reports in last 24 hours\n";
|
|
|
144 |
$sth->finish();
|
|
|
145 |
|
25 |
rodolico |
146 |
$results = $dbh->selectall_hashref( $missing, 'ID' );
|
|
|
147 |
if ( %$results ) {
|
|
|
148 |
$message .= '='x40 . "\nWARNING, no backup reports for the following machines\n" . '='x40 . "\n";
|
|
|
149 |
foreach my $device ( keys %$results ) {
|
|
|
150 |
$message .= sprintf( "%6d\t%s\n", $device, $results->{$device}->{name} );
|
|
|
151 |
}
|
|
|
152 |
$message .= '='x40 . "\n\n";
|
|
|
153 |
}
|
|
|
154 |
|
23 |
rodolico |
155 |
$results = $dbh->selectall_hashref( $query, 'ID' );
|
25 |
rodolico |
156 |
$message .= 'Device Device Name Run Time Files Size (M) Delete Skip xfer' . "\n" . '='x40 . "\n";
|
23 |
rodolico |
157 |
foreach my $id ( keys %$results ) {
|
25 |
rodolico |
158 |
$message .= sprintf( "%7u %-40s %9s %6u %8.2f %6u %6u %8.2f\n",
|
|
|
159 |
$results->{$id}->{'Device ID'} ,
|
|
|
160 |
$results->{$id}->{'Source'} ,
|
|
|
161 |
$results->{$id}->{'Run Time'} ,
|
|
|
162 |
$results->{$id}->{'Files'} ,
|
|
|
163 |
$results->{$id}->{'Size (M)'} ,
|
|
|
164 |
$results->{$id}->{'Deleted'} ,
|
|
|
165 |
$results->{$id}->{'Skipped'} ,
|
|
|
166 |
$results->{$id}->{'Traffic (M)'}
|
|
|
167 |
);
|
23 |
rodolico |
168 |
}
|
|
|
169 |
$dbh->disconnect();
|
|
|
170 |
print $message;
|
|
|
171 |
&sendReport( $$configuration{'sendReport'}, $message );
|
|
|
172 |
|
|
|
173 |
1;
|