Subversion Repositories computer_asset_manager_v1

Rev

Rev 23 | Rev 27 | Go to most recent revision | Details | Compare with Previous | Last modification | View Log | RSS feed

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