use Net::Ping; use vars qw($STATUSMSG); my @ips = qw (255.255.255.x 255.255.255.x 255.255.255.x 255.255.255.x +); sub pinger { my ($host,$timeout) = @_; $timeout = 5 unless $timeout; my $p = Net::Ping->new("icmp"); my $res = $p->ping($host,$timeout); $p->close(); return $res; } foreach my $ip (@ips) { $ip = Function::Stats->new( label => $ip , # The name of the inst +ance latest => 10, # Keep track of the l +ast 10 calls. coderef => sub { pinger($ip,3) }, # The code t +o execute # boolean basically lets you preprocess the return + from coderef. boolean => sub { my $r = shift; $STATUSMSG = $r ? "$ip is up" : "$ip +is down!"; $r; } ); } # Yes infinite loops may be hazardous to your health. I know... while (1){ foreach my $ip (@ips){ print "Host: $ip\tInterval:",$ip->iterations,"\n"; # Print +the name and current iteration my $res = $ip->execute; # Call the function + print $STATUSMSG,"\n"; # Print the status if ($ip->current_avg_failed > 0.49){ # Connection g +etting bad. print "\tConnection lacking for $ip... ",$ip->current_avg_ +failed,"% failure\n"; print $ip->dumpstats("\t"); } if ($ip->current_avg_runtime > 3){ print "Connection slow for $ip... ",$ip->current_avg_runti +me," seconds\n"; } print "-" x 30 ,"\n" ; sleep(15); } } __END__ # Sample output of dumpstats ------------------------------ Summary 255.255.255.255 Succeded: 72 Failed: 6 Iterations: 78 ------------------------------ Current Runtime Avg%: 0.861059010028839 seconds Current Success Avg%: 0.7000 Current Failure Avg%: 0.3000 ------------------------------ Overall Runtime Avg%: 0.226245646293347 seconds Overall Success Avg%: 0.9231 Overall Failure Avg%: 0.0769
In reply to OO lifetime function stats mod, is this useful? by shotgunefx
| For: | Use: | ||
| & | & | ||
| < | < | ||
| > | > | ||
| [ | [ | ||
| ] | ] |