Here's the script in its entirety (running on ActiveState Perl 5.6.1 on an NT4 box, if it matters):
use strict;
use Getopt::Long;
use Net::Ping;
use Time::HiRes qw( gettimeofday tv_interval );
#vars
my %args;
my @hosts;
#option defaults
my $timeout = 3;
my $yawn = 2;
my $infile = 'pinger_time_hostlist.txt';
my $outfile = 'pinger_time_results.txt';
my $help;
my $ok = GetOptions (
'wait=i' => \$timeout,
'sleep=i' => \$yawn,
'inputfile=s' => \$infile,
'outputfile=s' => \$outfile,
'help' => \$help
);
usage() if ! $ok || $help;
#go!
open OUT, ">>$outfile" or die "Can't open $outfile for writing: $!";
select OUT;
$|=1;
open(STDERR, ">&OUT");
open IN, "$infile" or die "Can't open $infile: $!";
while (<IN>) {
chomp;
s/\s*#.*//;
push @hosts, $_ unless /^$/;
}
close IN;
my $p = Net::Ping->new("icmp",$timeout,32);
while (1) {
foreach my $host (@hosts) {
my @times;
for (1..4) {
my $t0 = [gettimeofday];
my $alive = $p->ping($host);
my $t1 = tv_interval ($t0, [gettimeofday]);
push @times,
$alive ? # was ping successful?
int($t1 * 1000) : # if so, record interval
$timeout * 1000; # if not, record timeout val
}
my ($sec, $min, $hr, $day, $month, $year) = (localtime)[0..5];
my $timestamp = sprintf("%04d-%02d-%02d,%02d:%02d:%02d",
$year + 1900, $month + 1, $day, $hr, $min, $sec);
print "$timestamp,$host,", join ( ',', @times ), "\n";
}
sleep $yawn;
}
$p->close();
sub usage {
(my $msg = <<" MSG") =~ s/^\s{3}//mg;
Usage: $0 [-w timeout] [-s sleep] [-i inputfile]
[-o outputfile] [-h]
Options:
-w timeout Timeout in seconds to wait for each reply
(default $timeout seconds)
-s sleep Sleep time in seconds to pause after pinging
through hostlist (default $yawn seconds)
-i inputfile File containing list of hosts to ping
(default $infile)
-o outputfile Output log file (default $outfile)
-h Displays this help
MSG
die $msg;
}
The input file is just a list of hosts separated by newlines. Comments begin with #. This really seems like a nice script... er, if it worked reliably. :-)
I suspect this is being caused by some transient network condition or occurrence. (I wrote this thing because we're having trouble with our Qwest (ugh) VPNs.) However, like you, I can't force the error, and I can't figure out how it could cause the behavior I'm seeing. I'm starting to think this is <gulp> a bug in Perl or one of the modules I'm calling.
Something might be happening to $p itself. I haven't tried moving this definition inside one of the loops, but that might be next. It's such a sporadic thing, though -- I've been running it since yesterday with no error, but I saw the problem four times between Friday and Monday. Aargh!
|