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 () { 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; }