I have this script that merges log files. It reads the list of dirs from a file and looks for a certain log file and merges all the content and saves to another file (in a separate directory)
While the script itself is sound and is working on my sandbox, it goes "Out of Memory!" on the machine I am trying to run this script.
Can I optimize the script any further? Or is there some other way to do the same more effectively?
#!/usr/bin/perl
###################### Globals #####################################
+####
%keydirs; ## Hash for use in tracking directories
@d;
@ed;
$profile = `\. ~/.profile`; # Load user profile
$log_for_days = eval(86400 * 91); ## 91 is the number of days for rete
+ntion
###################### Globals #####################################
+####
&date_calc;
&get_dirs;
foreach $dir1_(@d)
{
chomp $dir1_;
@dir_ = split(/\//,$dir1_);
$num = eval(@dir_ - 1);
$logfile = "@dir_[$num].$myLog";
$delfile = "@dir_[$num].$delTime";
if(-e "$dir1_/logs/$delfile")
{
$del_old_recs = `rm -f $dir1_/logs/$delfile`;
}
chop $dir1_;
get_ldat($dir1_);
open(ARCLOG,">$dir1_/logs/$logfile");
print ARCLOG "@data1";
close(ARCLOG);
undef @data1;
}
############################ Subroutines ###########################
+####
sub date_calc
{
($sec,$min,$hour,$mday,$mon,$year,$wday,$yday,$isdst) = localtime(time
+ - 86400);
$syear = $year+1900;
$smon = sprintf '%02d',$mon+1;
$smday = sprintf '%02d',$mday;
$shour = sprintf '%02d', $hour;
$smin = sprintf '%02d', $min;
$ssec = sprintf '%02d', $sec;
$myTime = "$smon/$smday/$syear $shour:$smin:$ssec";
$myLog = "$syear"."$smon"."$smday";
($fsec,$fmin,$fhour,$fmday,$fmon,$fyear,$fwday,$fyday,$fisdst) = local
+time(time);
$fsyear = $fyear+1900;
$fsmon = sprintf '%02d',$fmon+1;
$fsmday = sprintf '%02d',$fmday;
$fshour = sprintf '%02d', $fhour;
$fsmin = sprintf '%02d', $fmin;
$fssec = sprintf '%02d', $fsec;
$fmyTime = "$fsmon/$fsmday/$fsyear";
($dsec,$dmin,$dhour,$dmday,$dmon,$dyear,$dwday,$dyday,$disdst) = local
+time(time - $log_for_days);
$delyear = $dyear+1900;
$delmon = sprintf '%02d',$dmon+1;
$delmday = sprintf '%02d',$dmday;
$delhour = sprintf '%02d',$dhour;
$delmin = sprintf '%02d',$dmin;
$delsec = sprintf '%02d',$dsec;
$delTime = "$delyear"."$delmon"."$delmday";
}
sub get_dirs
{
open(DIRFILE,"</opt/scripts/dirs") || die "Cannot open file for readin
+g:$^E\n";
@directories = <DIRFILE>;
close(DIRFILE);
foreach $directory(@directories)
{
next unless $directory !~ /\#/ig;
chomp $directory;
@dire = split(/(encrypt|decrypt|\:)/,$directory);
if(! exists($keydirs{$dire[2]}))
{
$keydirs{$dire[2]} = 1;
@d = (@d,$dire[2]);
}
}
}
sub get_ldat
{
$dirtoget = "@_";
chomp $dirtoget;
if(-e "$dirtoget/encrypt")
{
@encdir = `ls -Af $dirtoget/encrypt 2>&1`;
}
else
{
@encdir = "";
}
foreach $endir(@encdir)
{
chomp $endir;
if($endir !~ /\./)
{
if(-e "$dirtoget/encrypt/$endir/sample.log")
{
@data = `cat $dirtoget/encrypt/$endir/sample.log`;
$cnt=0;
$dcnt= eval(@data - 1);
foreach $row(@data)
{
if($row !~ /$fmyTime/i)
{
$cnt++;
}
else
{
last;
}
}
$cnt = eval($cnt - 1);
$icnt=0;
for($icnt=0;$icnt<= $cnt;$icnt++)
{
push @data1,$data[$icnt];
}
$cnt = eval($cnt + 1);
for($icnt2=$cnt;$icnt2<=$dcnt;$icnt2++)
{
push @data2,$data[$icnt2];
}
open(LOG,">$dirtoget/encrypt/$endir/sample.log");
print LOG "@data2";
close(LOG);
}
}
undef $cnt;
undef $dcnt;
undef @data;
undef $row;
undef $icnt;
undef $irow;
undef $irow2;
undef @data2;
}
if(-e "$dirtoget/decrypt/sample.log")
{
@data = `cat $dirtoget/decrypt/sample.log`;
$cnt=0;
$dcnt=@data;
foreach $row(@data)
{
if($row !~ /$fmyTime/i)
{
$cnt++;
}
else
{
last;
}
}
$icnt=0;
for($icnt=0;$icnt< $cnt;$icnt++)
{
push @data1,$data[$icnt];
}
for($icnt2=$cnt;$icnt2<=$dcnt;$icnt2++)
{
push @data2,$data[$icnt2];
}
open(LOG,">$dirtoget/decrypt/sample.log");
print LOG "@data2";
close(LOG);
}
undef $cnt;
undef $dcnt;
undef @data;
undef $row;
undef $icnt;
undef $irow;
undef $irow2;
undef @data2;
}