Hello Monks. I am hoping some of you can take a look at a program recently wrote and maybe make some suggestions on how it can be improved. The function of the program is to transfer copies of several reports from several sites each day. The program is run every 15 minutes out of a cron job between midnight and eight am.
Essentially it looks to see if the reports are completed at each site and if they are copies them over. I am using ssh commands to do the listings and copies. One of my concerns is that I am generating a ton of SSH connections and I would like to find a good way to cut that down. My Perl skills are improving, but are still a long way from being very good. So please be gentle. Any suggestions you might have would be greatly appreciated. Thank you. Below is a copy of the code.
use strict;
use Time::Local;
sub _init_values {
my ($seconds, $minutes, $hours, $day, $month, $year) = localtim
+e;
my $date = sprintf("%04d%02d%02d", $year + 1900, $month + 1, $d
+ay);
my $report_dir = "/reports/directory";
my $config_dir = "/config/directory";
return $date, $report_dir, $config_dir, $hours;
}
sub _get_list {
my ($date, $report_dir, $config_dir, $hours) = @_;
my %reports;
if ($hours == 00) {
open IN, "$config_dir/report_master_list" or die "$!";
while (<IN>) {
chomp;
$reports{$_}++;
}
close IN or warn "$!";
_get_reports($date, $report_dir, $config_dir, \%reports)
+;
} elsif ($hours == 08) {
open IN, "$config_dir/report_temp_list" or die "$!";
while (<IN>) {
chomp;
$reports{$_}++;
}
close IN or warn "$!";
open OUT, ">> /disk2/daily_report" or die "$!";
print OUT "\n\nDaily Reports That Were Unable To Be Tran
+sferred\n";
print OUT "---------------------------------------------
+---\n\n";
for (sort keys %reports) {
print OUT "$_\n";
}
print OUT "\n\n";
unlink "$config_dir/report_temp_list" or warn "$!";
} else {
open IN, "$config_dir/report_temp_list" or die "$!";
while (<IN>) {
chomp;
$reports{$_}++;
}
close IN or warn "$!";
_get_reports($date, $report_dir, $config_dir, \%reports)
+;
}
}
sub _get_reports {
my ($date, $report_dir, $config_dir, $reports) = @_;
my %site_map = qw(
site1 fullsite1
site2 fullsite2
site3 fullsite3
site4 fullsite4
site5 fullsite5
site6 fullsite6
);
for (sort keys %$reports) {
my ($report_name, $site) = split /\./;
my @files = `ssh host.$site ls $report_dir`;
for my $file (@files) {
chomp $file;
if ($file =~ /$report_name/ && $file =~ /$date/)
+{
my $status = `ssh host.$site cat $report_d
+ir/$file`;
if ($status =~ /End of Report/) {
my $transfer = `scp host.$site:$rep
+ort_dir/$file /disk2/$site_map{$site}/Reports`;
delete $reports->{$_};
}
}
}
}
open OUT, "> $config_dir/report_temp_list" or die "$!";
for (sort keys %$reports) {
print OUT "$_\n";
}
}
my ($date, $report_dir, $config_dir, $hours) = _init_values;
_get_list($date, $report_dir, $config_dir, $hours);
Thanks again for any help you can offer. Sorry if some of the long lines make the script kind of hard to read.
-Prime