Subversion Repositories computer_asset_manager_v1

Rev

Rev 27 | Rev 73 | Go to most recent revision | Show entire file | Ignore whitespace | Details | Blame | Last modification | View Log | RSS feed

Rev 27 Rev 32
Line 1... Line 1...
1
#! /usr/bin/perl -w
1
#! /usr/bin/perl -w
2
 
2
 
-
 
3
# Version 0.9 20160713 RWR
-
 
4
# Added ability to have individual owners receive a summary report
-
 
5
 
3
use DBI;
6
use DBI;
4
use Cwd 'abs_path';
7
use Cwd 'abs_path';
5
#use Data::Dumper;
8
#use Data::Dumper;
6
use File::Basename;
9
use File::Basename;
7
use YAML::XS;
10
use YAML::XS;
8
 
11
 
9
 
12
 
10
 
13
 
11
my $VERSION = '0.8';
14
my $VERSION = '0.9';
12
my $MY_DIRECTORY = abs_path(dirname(__FILE__) );
15
my $MY_DIRECTORY = abs_path(dirname(__FILE__) );
13
my $message; # the message to send
16
my $message; # the message to send
14
# query to find all reports in last 24 hours. Assumes the script
17
# query to find all reports in last 24 hours. Assumes the script
15
# runs at the same time each day
18
# runs at the same time each day
16
my $query = qq|select 
19
my $query = qq|select 
-
 
20
                  backups.responsible_party 'mailto',
17
                  backups_run.backups_run_id 'ID',
21
                  backups_run.backups_run_id 'ID',
18
                  device.device_id 'Device ID',
22
                  device.device_id 'Device ID',
19
                  device.name 'Source',
23
                  device.name 'Source',
20
                  timediff( backups_run.end_time,backups_run.start_time ) 'Run Time',
24
                  timediff( backups_run.end_time,backups_run.start_time ) 'Run Time',
21
                  backups_run.transferred_count 'Files',
25
                  backups_run.transferred_count 'Files',
Line 118... Line 122...
118
   open SENDMAIL, "|$commandLine" or die "Could not open [$commandLine]: $!\n";
122
   open SENDMAIL, "|$commandLine" or die "Could not open [$commandLine]: $!\n";
119
   print SENDMAIL $report;
123
   print SENDMAIL $report;
120
   close SENDMAIL;
124
   close SENDMAIL;
121
} # sendReport
125
} # sendReport
122
 
126
 
-
 
127
sub individualReport {
-
 
128
   my $data = shift;
-
 
129
   my @report;
-
 
130
   
-
 
131
   for my $key ( 'Device ID', 'Run Time', 'Files', 'Size (M)', 'Deleted', 'Skipped', 'Traffic (M)' ) {
-
 
132
      push @report, "$key: " . $$data{$key} unless $key eq 'mailto';
-
 
133
   }
-
 
134
   return join( "\n", @report );
-
 
135
}
-
 
136
 
123
########################################################################
137
########################################################################
124
# Main Program
138
# Main Program
125
########################################################################
139
########################################################################
126
 
140
 
127
# Read anything passed on STDIN to prepend to e-mail
141
# Read anything passed on STDIN to prepend to e-mail
Line 150... Line 164...
150
      $message .= sprintf( "%6d\t%s\n", $device, $results->{$device}->{name} );
164
      $message .= sprintf( "%6d\t%s\n", $device, $results->{$device}->{name} );
151
   }
165
   }
152
   $message .= '='x40 . "\n\n";
166
   $message .= '='x40 . "\n\n";
153
}
167
}
154
 
168
 
-
 
169
# we're going to reuse the mail configuration for the individual reports
-
 
170
# if any, so let's save the mailTo and mailSubject
-
 
171
my $saveMailTo = $$configuration{'sendReport'}->{'mailTo'};
-
 
172
my $saveSubject = $$configuration{'sendReport'}->{'mailSubject'};
-
 
173
 
155
$results = $dbh->selectall_hashref( $query, 'ID' );
174
$results = $dbh->selectall_hashref( $query, 'ID' );
156
$message .= 'Device  Device Name                               Run Time  Files Size (M) Delete   Skip     xfer' . "\n" . '='x40 . "\n";
175
$message .= 'Device  Device Name                               Run Time  Files Size (M) Delete   Skip     xfer' . "\n" . '='x40 . "\n";
157
foreach my $id ( keys %$results ) {
176
foreach my $id ( keys %$results ) {
158
   $message .= sprintf( "%7u %-40s %9s %6u %8.2f %6u %6u %8.2f\n",
177
   $message .= sprintf( "%7u %-40s %9s %6u %8.2f %6u %6u %8.2f\n",
159
                  $results->{$id}->{'Device ID'} , 
178
                  $results->{$id}->{'Device ID'} , 
Line 163... Line 182...
163
                  $results->{$id}->{'Size (M)'} ,
182
                  $results->{$id}->{'Size (M)'} ,
164
                  $results->{$id}->{'Deleted'} ,
183
                  $results->{$id}->{'Deleted'} ,
165
                  $results->{$id}->{'Skipped'} ,
184
                  $results->{$id}->{'Skipped'} ,
166
                  $results->{$id}->{'Traffic (M)'}
185
                  $results->{$id}->{'Traffic (M)'}
167
               );
186
               );
-
 
187
   if ( $results->{$id}->{'mailto'} ) {
-
 
188
      $$configuration{'sendReport'}->{'mailTo'} = $results->{$id}->{'mailto'};
-
 
189
      $$configuration{'sendReport'}->{'mailSubject'} = 'Backup Report for ' . $results->{$id}->{'Source'};
-
 
190
      &sendReport( $$configuration{'sendReport'}, &individualReport( $results->{$id} ) );
-
 
191
   }
-
 
192
      
168
}
193
}
169
$dbh->disconnect();
194
$dbh->disconnect();
-
 
195
 
-
 
196
# reset mailTo and MailSubject to original values
-
 
197
$$configuration{'sendReport'}->{'mailTo'} = $saveMailTo;
-
 
198
$$configuration{'sendReport'}->{'mailSubject'} = $saveSubject;
-
 
199
 
-
 
200
 
170
print $message;
201
#print $message;
171
&sendReport( $$configuration{'sendReport'}, $message );
202
&sendReport( $$configuration{'sendReport'}, $message );
172
 
203
 
173
1;
204
1;