Rev 23 | Rev 32 | Go to most recent revision | Blame | Compare with Previous | Last modification | View Log | Download | RSS feed
#! /usr/bin/perl -w
use DBI;
use Cwd 'abs_path';
#use Data::Dumper;
use File::Basename;
use YAML::XS;
my $VERSION = '0.8';
my $MY_DIRECTORY = abs_path(dirname(__FILE__) );
my $message; # the message to send
my $query = qq|select
backups_run.backups_run_id 'ID',
device.device_id 'Device ID',
device.name 'Source',
timediff( backups_run.end_time,backups_run.start_time ) 'Run Time',
backups_run.transferred_count 'Files',
round(backups_run.transferred_size/1024/1024,2) 'Size (M)',
backups_run.files_deleted 'Deleted',
backups_run.skipped 'Skipped',
round( backups_run.data_sent/1024/1024,2) 'Traffic (M)'
from
backups_run
join backups using (backups_id)
join device using (device_id)
where
report_date > date_sub( now(), interval 1 day)
|;
my $count = qq/select count(*) 'Count'
from backups_run
where added_date > date_sub( now(), interval 1 day)/;
my $missing = qq/select
device.device_id ID,
device.name name
from
(
select distinct( backups_id )
from
backups_run
where
(datediff( current_date(),added_date) < 5 )
) last_five
left join
(
select
distinct( backups_id )
from
backups_run
where
datediff( current_date(),added_date) < 1
) today using (backups_id)
join
backups using (backups_id)
join
device using (device_id)
where
today.backups_id is null
/;
# following are used to find the configuration file
my $confFileName = "rsbackupRead.conf.yaml";
my @searchPaths = ( '/etc/camp', '/opt/camp', '/opt/camp/sysinfo', $MY_DIRECTORY );
sub loadConfig {
my ( $confFileName, @searchPaths ) = @_;
my $configuration;
for ( $i = 0; $i < @searchPaths; $i++ ) {
$filename = $searchPaths[$i] . '/' . $confFileName;
if ( -e $filename ) {
#print "Found $filename\n";
open CONF, "<$filename" or warn "Could not read $filename: $!\n";
$configuration = Load( join( '', <CONF> ) );
close CONF;
last; # exit out of the loop; we don't try to load it more than once
} # if
} # foreach
return $configuration;
} # sub loadConfig
sub sendReport {
my ($parameters,$report) = @_;
my %CLIParams ;
$CLIParams{'-f'} = qq/$$parameters{'mailFrom'}/ if $$parameters{'mailFrom'};
$CLIParams{'-t'} = qq/$$parameters{'mailTo'}/ if $$parameters{'mailTo'};
$CLIParams{'-u'} = qq/$$parameters{'mailSubject'}/ if $$parameters{'mailSubject'};
$CLIParams{'-s'} = qq/$$parameters{'mailServer'}/ if $$parameters{'mailServer'};
$CLIParams{'-xu'} = qq/$$parameters{'smtpUser'}/ if $$parameters{'smtpUser'};
$CLIParams{'-xp'} = qq/$$parameters{'smtpPass'}/ if $$parameters{'smtpPass'};
$CLIParams{'-cc'} = qq/$$parameters{'mailCC'}/ if $$parameters{'mailCC'};
$CLIParams{'-bcc'}= qq/$$parameters{'mailBCC'}/ if $$parameters{'mailBCC'};
$CLIParams{'-l'} = qq/$$parameters{'logFile'}/ if $$parameters{'logFile'};
$CLIParams{'-a'} = qq/$$parameters{'attachment'}/ if $$parameters{'attachment'};
$CLIParams{'-o tls='} = qq/$$parameters{'tls'}/ if $$parameters{'tls'};
$commandLine = qq/$$parameters{'emailScript'}/;
die "Could not find executable $commandLine in sendEmailScript\n" unless -x $commandLine;
$commandLine .= ' -q'; # make it act quietly
foreach my $key ( keys %CLIParams ) {
# depending on whether the key ends in an = or not, we will or will not use a space
# between the key and the parameter
$commandLine .= $key =~ m/=$/ ? " $key'$CLIParams{$key}'" : " $key '$CLIParams{$key}'";
}
$commandLine .= ' ' . $$parameters{'otherCLParams'} if $$parameters{'otherCLParams'};
open SENDMAIL, "|$commandLine" or die "Could not open [$commandLine]: $!\n";
print SENDMAIL $report;
close SENDMAIL;
} # sendReport
########################################################################
# Main Program
########################################################################
# Read anything passed on STDIN to prepend to e-mail
while ( my $line = <> ) {
$message .= $line;
}
my $configuration = &loadConfig( $confFileName, @searchPaths );
die "Could not find configuration file $confFileName in " . join( ', ', @searchPaths) . "\n" unless $configuration;
my $dbh = DBI->connect('DBI:mysql:' . $$configuration{'database'}{'database'},
$$configuration{'database'}{'databaseUsername'},
$$configuration{'database'}{'databasePassword'} )
|| die "Could not connect to database: $DBI::errstr";
my $sth = $dbh->prepare( $count );
$sth->execute();
my $results = $sth->fetchrow_hashref();
$message .= "$results->{Count} reports in last 24 hours\n";
$sth->finish();
$results = $dbh->selectall_hashref( $missing, 'ID' );
if ( %$results ) {
$message .= '='x40 . "\nWARNING, no backup reports for the following machines\n" . '='x40 . "\n";
foreach my $device ( keys %$results ) {
$message .= sprintf( "%6d\t%s\n", $device, $results->{$device}->{name} );
}
$message .= '='x40 . "\n\n";
}
$results = $dbh->selectall_hashref( $query, 'ID' );
$message .= 'Device Device Name Run Time Files Size (M) Delete Skip xfer' . "\n" . '='x40 . "\n";
foreach my $id ( keys %$results ) {
$message .= sprintf( "%7u %-40s %9s %6u %8.2f %6u %6u %8.2f\n",
$results->{$id}->{'Device ID'} ,
$results->{$id}->{'Source'} ,
$results->{$id}->{'Run Time'} ,
$results->{$id}->{'Files'} ,
$results->{$id}->{'Size (M)'} ,
$results->{$id}->{'Deleted'} ,
$results->{$id}->{'Skipped'} ,
$results->{$id}->{'Traffic (M)'}
);
}
$dbh->disconnect();
print $message;
&sendReport( $$configuration{'sendReport'}, $message );
1;