Monks I stand before you once again seeking your wisdom. I have recently written a script that will be uised to archive several reports from multiple sites to a central location. The script seems to function well enough, but I am having a problem with part of the reporting. The script generates a report that lets me know if a report wasn't copied over and which sites it couldn't copy the reports from. The report displays the first missed report properly, but then keeps tacking on the sites from the previous reports after that. For example.
I am having problems spotting where the problem is. I am sure I am overlookign something obvious. Also it seems like there should be an easier way to handle copying over the reports so any suggestions on just overall improving the code would be very appreciated. Here is the code.
#!/usr/bin/perl -w
use strict;
use Time::Local;
sub _init {
my $config_dir = '/config/dir';
my $config_file = 'report_copy.cfg';
my $site_file = 'sites.cfg';
my $report_dir = '/report/dir';
my ($day, $month, $year) = (localtime)[3,4,5];
my $log_date = sprintf("%04d%02d%02d", $year + 1900, $month + 1
+, $day);
return $config_dir, $config_file, $site_file, $report_dir, $day
+, $log_date;
}
sub _read_cfg {
my ($config_dir, $config_file, $site_file) = @_;
my (%reports, %sites);
open IN, "$config_dir/$config_file"
or die "Error: Could not open $config_dir/$config_file f
+or reading: $!\n";
while (<IN>) {
chomp;
my ($name, $type, $day, $site_locations) = split;
$reports{$name} = "$type:$day:$site_locations";
}
close IN
or warn "Error: Could not close $config_dir/$config_file
+: $!\n";
open IN, "$config_dir/$site_file"
or die "Error: Could not open $config_dir/$site_file for
+ reading: $!\n";
while (<IN>) {
chomp;
my ($site, $folder) = split /=/;
$sites{$site} = $folder;
}
close IN
or warn "Error: Could not close $config_dir/$site_file:
+$!\n";
return \(%reports, %sites);
}
sub _get_reports {
my ($report_dir, $day, $log_date, $reports, $sites) = @_;
my (@site_array, %missed_reports);
for (sort keys %$reports) {
my ($type, $report_day, $site_locations) = split /:/, $r
+eports->{$_};
if ($site_locations eq 'All') {
for my $site (sort keys %$sites) {
push @site_array, $site;
}
} else {
@site_array = split /,/, $site_locations;
}
if ($type eq 'daily') {
_copy_report($_, $report_dir, $log_date, $sites,
+\@site_array);
} elsif ($type eq 'monthly' && $report_day eq $day) {
_copy_report($_, $report_dir, $log_date, $sites,
+\@site_array);
} else {
delete $reports->{$_};
}
my $file_check;
my $problem = "";
for my $site_folder (@site_array) {
$file_check = 0;
my $folder = $sites->{$site_folder};
for my $filename (</disk2/$folder/reports/*>) {
$file_check = 1 if $filename =~ /$_.*\.$lo
+g_date/;
}
if ($file_check == 1) {
next;
} else {
$problem .= "$site_folder ";
$missed_reports{$_} = $problem;
}
}
delete $reports->{$_};
}
return \%missed_reports;
}
sub _copy_report {
my ($report_name, $report_dir, $log_date, $sites, $site_array)
+= @_;
for my $site (@$site_array) {
my $result = `scp host.$site:$report_dir/${report_name}*
+.$log_date /disk2/$sites->{$site}/reports`;
}
}
sub _print_report {
my ($missed_reports, $log_date) = @_;
open OUT, ">> /disk2/reports/report_copy.$log_date"
or die "Error: Could not open /disk2/reports/report_copy
+.$log_date: $!\n";
print OUT "The following files were not copied over properly:\n
+\n";
for (sort keys %$missed_reports) {
print OUT "$_\t$missed_reports->{$_}\n";
}
close OUT
or warn "Error: Could not close /disk2/reports/report_co
+py.$log_date: $!\n";
}
my ($config_dir, $config_file, $site_file, $report_dir, $day, $log_dat
+e) = _init;
my ($reports, $sites) = _read_cfg($config_dir, $config_file, $site_fil
+e);
my $missed_reports = _get_reports($report_dir, $day, $log_date, $repor
+ts, $sites);
_print_report($missed_reports, $log_date);
Again any suggestions you might have would be greatly appreciated. Sorry the code is so long. Hopefully it is readable. Thanks.