Subversion Repositories computer_asset_manager_v1

Rev

Rev 75 | Rev 93 | 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
 
32 rodolico 3
# Version 0.9 20160713 RWR
4
# Added ability to have individual owners receive a summary report
5
 
23 rodolico 6
use DBI;
7
use Cwd 'abs_path';
8
#use Data::Dumper;
9
use File::Basename;
10
use YAML::XS;
11
 
12
 
25 rodolico 13
 
32 rodolico 14
my $VERSION = '0.9';
23 rodolico 15
my $MY_DIRECTORY = abs_path(dirname(__FILE__) );
16
my $message; # the message to send
27 rodolico 17
# query to find all reports in last 24 hours. Assumes the script
18
# runs at the same time each day
25 rodolico 19
my $query = qq|select 
32 rodolico 20
                  backups.responsible_party 'mailto',
25 rodolico 21
                  backups_run.backups_run_id 'ID',
22
                  device.device_id 'Device ID',
23
                  device.name 'Source',
24
                  timediff( backups_run.end_time,backups_run.start_time ) 'Run Time',
25
                  backups_run.transferred_count 'Files',
26
                  round(backups_run.transferred_size/1024/1024,2) 'Size (M)',
27
                  backups_run.files_deleted 'Deleted',
28
                  backups_run.skipped 'Skipped',
29
                  round( backups_run.data_sent/1024/1024,2) 'Traffic (M)'
30
               from
31
                  backups_run
32
                  join backups using (backups_id)
33
                  join device using (device_id)
34
               where
27 rodolico 35
                  backups_run.start_time > date_sub( now(), interval 24 hour)
36
               order by Source
25 rodolico 37
               |;
38
 
23 rodolico 39
 
27 rodolico 40
# Find number of backups in the past 24 hours.                  
25 rodolico 41
my $count = qq/select count(*) 'Count' 
42
               from backups_run 
27 rodolico 43
               where
44
                  backups_run.start_time > date_sub( now(), interval 24 hour)/;
23 rodolico 45
 
27 rodolico 46
# looks for backups which did NOT happen in last 24 hours, but did
47
# happen in last 7 days, ie backups that did not make it.
25 rodolico 48
my $missing = qq/select 
49
                  device.device_id ID,
50
                  device.name name
51
               from
52
                  ( 
53
                     select distinct( backups_id ) 
54
                     from 
55
                        backups_run 
56
                     where 
27 rodolico 57
                        (datediff( now(),added_date) < 7 ) 
25 rodolico 58
                  ) last_five
59
                  left join
60
                  (
61
                     select 
62
                        distinct( backups_id ) 
63
                     from 
64
                        backups_run
65
                     where 
27 rodolico 66
                        backups_run.start_time > date_sub( now(), interval 24 hour)
25 rodolico 67
                  ) today using (backups_id)
68
                  join 
69
                     backups using (backups_id)
70
                  join 
71
                     device using (device_id)
72
               where 
73
                  today.backups_id is null
74
                  /;
75
 
76
 
23 rodolico 77
# following are used to find the configuration file
25 rodolico 78
my $confFileName = "rsbackupRead.conf.yaml";
23 rodolico 79
my @searchPaths = ( '/etc/camp', '/opt/camp', '/opt/camp/sysinfo', $MY_DIRECTORY );
80
 
81
 
82
sub loadConfig {
83
   my ( $confFileName, @searchPaths ) = @_;
84
   my $configuration;
85
   for ( $i = 0; $i < @searchPaths; $i++ ) {
86
      $filename = $searchPaths[$i] . '/' . $confFileName;
87
      if ( -e $filename ) {
88
         #print "Found $filename\n";
89
         open CONF, "<$filename" or warn "Could not read $filename: $!\n";
90
         $configuration = Load( join( '', <CONF> ) );
91
         close CONF;
92
         last; # exit out of the loop; we don't try to load it more than once
93
      } # if
94
   } # foreach
95
   return $configuration;
96
} # sub loadConfig
97
 
98
sub sendReport {
99
   my ($parameters,$report) = @_;
100
   my %CLIParams ;
101
   $CLIParams{'-f'}  = qq/$$parameters{'mailFrom'}/    if $$parameters{'mailFrom'};
102
   $CLIParams{'-t'}  = qq/$$parameters{'mailTo'}/      if $$parameters{'mailTo'};
103
   $CLIParams{'-u'}  = qq/$$parameters{'mailSubject'}/ if $$parameters{'mailSubject'};
104
   $CLIParams{'-s'}  = qq/$$parameters{'mailServer'}/  if $$parameters{'mailServer'};
105
   $CLIParams{'-xu'} = qq/$$parameters{'smtpUser'}/    if $$parameters{'smtpUser'};
106
   $CLIParams{'-xp'} = qq/$$parameters{'smtpPass'}/    if $$parameters{'smtpPass'};
107
   $CLIParams{'-cc'} = qq/$$parameters{'mailCC'}/      if $$parameters{'mailCC'};
108
   $CLIParams{'-bcc'}= qq/$$parameters{'mailBCC'}/     if $$parameters{'mailBCC'};
109
   $CLIParams{'-l'}  = qq/$$parameters{'logFile'}/     if $$parameters{'logFile'};
110
   $CLIParams{'-a'}  = qq/$$parameters{'attachment'}/  if $$parameters{'attachment'};
111
   $CLIParams{'-o tls='} = qq/$$parameters{'tls'}/     if $$parameters{'tls'};
112
 
113
   $commandLine = qq/$$parameters{'emailScript'}/;
114
   die "Could not find executable $commandLine in sendEmailScript\n" unless -x $commandLine;
115
   $commandLine .= ' -q'; # make it act quietly
116
   foreach my $key ( keys %CLIParams ) {
117
      # depending on whether the key ends in an = or not, we will or will not use a space
118
      # between the key and the parameter
119
      $commandLine .= $key =~ m/=$/ ? " $key'$CLIParams{$key}'" : " $key '$CLIParams{$key}'";
120
   }
121
   $commandLine .= ' ' . $$parameters{'otherCLParams'} if $$parameters{'otherCLParams'};
122
   open SENDMAIL, "|$commandLine" or die "Could not open [$commandLine]: $!\n";
123
   print SENDMAIL $report;
124
   close SENDMAIL;
125
} # sendReport
126
 
73 rodolico 127
sub sendViaSendmail {
128
   my ( $parameters, $report ) = @_;
129
   my @header;
130
   push @header, 'To: ' . qq/$$parameters{'mailTo'}/;
131
   push @header, 'From: ' . qq/$$parameters{'mailFrom'}/;
132
   push @header, 'Subject: ' . qq/$$parameters{'mailSubject'}/;
133
   $report = join( "\n", @header ) . "\n\n" . $report . "\n.\n";
75 rodolico 134
   open SENDMAIL, "|/usr/sbin/sendmail $$parameters{mailTo}" or die "could not open sendmail: $!\n";
73 rodolico 135
   print SENDMAIL $report;
136
   close SENDMAIL;
137
} 
138
 
139
 
140
 
141
 
32 rodolico 142
sub individualReport {
143
   my $data = shift;
144
   my @report;
145
 
146
   for my $key ( 'Device ID', 'Run Time', 'Files', 'Size (M)', 'Deleted', 'Skipped', 'Traffic (M)' ) {
147
      push @report, "$key: " . $$data{$key} unless $key eq 'mailto';
148
   }
149
   return join( "\n", @report );
150
}
151
 
73 rodolico 152
 
153
 
23 rodolico 154
########################################################################
155
# Main Program
156
########################################################################
157
 
158
# Read anything passed on STDIN to prepend to e-mail
159
while ( my $line = <> ) {
160
   $message .= $line;
161
}
162
 
163
my $configuration = &loadConfig( $confFileName, @searchPaths );
164
die "Could not find configuration file $confFileName in " . join( ', ', @searchPaths) . "\n" unless $configuration;
165
 
166
my $dbh = DBI->connect('DBI:mysql:' . $$configuration{'database'}{'database'},
167
                       $$configuration{'database'}{'databaseUsername'}, 
168
                       $$configuration{'database'}{'databasePassword'} )
169
         || die "Could not connect to database: $DBI::errstr";
170
 
171
my $sth = $dbh->prepare( $count );
172
$sth->execute();
173
my $results = $sth->fetchrow_hashref();
174
$message .= "$results->{Count} reports in last 24 hours\n";
175
$sth->finish();
176
 
25 rodolico 177
$results = $dbh->selectall_hashref( $missing, 'ID' );
178
if ( %$results ) {
179
   $message .= '='x40 . "\nWARNING, no backup reports for the following machines\n" . '='x40 . "\n";
180
   foreach my $device ( keys %$results ) {
181
      $message .= sprintf( "%6d\t%s\n", $device, $results->{$device}->{name} );
182
   }
183
   $message .= '='x40 . "\n\n";
184
}
185
 
32 rodolico 186
# we're going to reuse the mail configuration for the individual reports
187
# if any, so let's save the mailTo and mailSubject
188
my $saveMailTo = $$configuration{'sendReport'}->{'mailTo'};
189
my $saveSubject = $$configuration{'sendReport'}->{'mailSubject'};
190
 
23 rodolico 191
$results = $dbh->selectall_hashref( $query, 'ID' );
25 rodolico 192
$message .= 'Device  Device Name                               Run Time  Files Size (M) Delete   Skip     xfer' . "\n" . '='x40 . "\n";
79 rodolico 193
# following sorts the results by the name ('source'} field
194
foreach my $id ( sort { $results->{$a}->{'Source'} cmp $results->{$b}->{'Source'} } keys %$results ) {
25 rodolico 195
   $message .= sprintf( "%7u %-40s %9s %6u %8.2f %6u %6u %8.2f\n",
196
                  $results->{$id}->{'Device ID'} , 
197
                  $results->{$id}->{'Source'} , 
198
                  $results->{$id}->{'Run Time'} ,
199
                  $results->{$id}->{'Files'} ,
200
                  $results->{$id}->{'Size (M)'} ,
201
                  $results->{$id}->{'Deleted'} ,
202
                  $results->{$id}->{'Skipped'} ,
203
                  $results->{$id}->{'Traffic (M)'}
204
               );
32 rodolico 205
   if ( $results->{$id}->{'mailto'} ) {
206
      $$configuration{'sendReport'}->{'mailTo'} = $results->{$id}->{'mailto'};
207
      $$configuration{'sendReport'}->{'mailSubject'} = 'Backup Report for ' . $results->{$id}->{'Source'};
73 rodolico 208
      if ( $$configuration{'sendReport'}{'emailScript'} ) {
209
         &sendReport( $$configuration{'sendReport'}, &individualReport( $results->{$id} ) );
210
      } else {
211
         &sendViaSendmail( $$configuration{'sendReport'}, &individualReport( $results->{$id} ) );
212
      }
32 rodolico 213
   }
214
 
23 rodolico 215
}
216
$dbh->disconnect();
32 rodolico 217
 
218
# reset mailTo and MailSubject to original values
219
$$configuration{'sendReport'}->{'mailTo'} = $saveMailTo;
220
$$configuration{'sendReport'}->{'mailSubject'} = $saveSubject;
221
 
222
 
223
#print $message;
73 rodolico 224
if ( $$configuration{'sendReport'}{'emailScript'} ) {
225
   &sendReport( $$configuration{'sendReport'}, $message );
226
} else {
227
   &sendViaSendmail( $$configuration{'sendReport'}, $message );
228
}
229
 
23 rodolico 230
1;