tye,
Thank you very much for the insight. I have "borrowed" a great deal of your code and came up with the following:
#!/usr/bin/perl -w
use strict;
use Time::Local;
chdir "/var/spool/wt400/gateways/$ARGV[0]" or exit;
mkdir "capture", 0755 unless (-d "capture");
my $Dir = $ARGV[1];
my $ListTime = 0;
my $BufferSize = 64 * 1024;
my $MaxLen = 0;
my %Traps;
my @GrepList;
my $GrepString;
my $Counter = 1;
my $Size;
my $Prev;
my $Now;
my $NF;
while (1) {
if ($Counter > 20 || ! @GrepList) {
if ( (stat("traplist.$Dir"))[9] gt $ListTime ) {
$ListTime = (stat(_))[9];
open (LIST,"traplist.$Dir");
while (<LIST>) {
next if ($_ =~ /^Created\t\tExpires/ || $_ =~ /^ *$/);
my @Fields = split "\t" , $_;
my($mon, $day, $year, $hour, $min) = split ?[-/:]? , $Fields[1];
my $Expiration = timelocal(0, $min, $hour, $day, $mon -1, $year +
+ 100);
$Traps{"$Fields[6]"} = [ $Expiration,$Fields[2],$Fields[5],$Fields
+[7] ];
}
close (LIST);
}
}
@GrepList = ();
$Now = time;
foreach my $trap (keys %Traps) {
push @GrepList,$Traps{$trap}[3] unless (($Traps{$trap}[0] < $Now &&
+$Traps{$trap}[1]) || $trap eq "SIZE");
}
map { $MaxLen = length($_) if length($_) > $MaxLen } @GrepList;
$BufferSize = 2 * $MaxLen if ($BufferSize < 2 * $MaxLen);
if (exists $Traps{"SIZE"} && $Traps{"SIZE"}[1]) {
$Size = $Traps{"SIZE"}[2] unless ($Traps{"SIZE"}[0] < $Now && $Traps
+{"SIZE"}[2] > 0);
}
exit unless (@GrepList || $Size);
$GrepString = join '|', map quotemeta $_, @GrepList;
$GrepString = qr/($GrepString)/i;
if ($Dir eq "out") {
@ARGV = <out/do*>;
}
elsif ($Dir eq "in") {
@ARGV = <in/di*>;
}
else {
@ARGV = <out/do* in/di*>
}
if (@ARGV) {
$/=\$BufferSize;
$Prev= "";
while (<>) {
$_ =~ tr/\n//d;
if(($Prev.$_) =~ /$GrepString/) {
($NF = "$ARGV-$+") =~ s/^.*\///;
rename $ARGV , "capture/$NF";
close (ARGV);
$Prev = "";
}
if (eof) {
$Prev = "";
}
else {
$Prev = substr($_,-$MaxLen);
}
}
}
$/ = "\n";
++$Counter;
sleep 3
}
This provides 10X more functionality the original shell script did.
I would appreciate any advice on how it could be made to go faster and still be efficient.
L~R |