in reply to MRTG script should be easy
Let's see.... could a module do the trick? A search on ping on search.cpan.org returns... Net::Ping! This should both give you a convenient way to get the data in Perl, without having to resort to parsing the output of an external program, and solve portability problems if/when you want to use this tool in a different environment.
After installing Net::Ping, a quick glance at the docs, installing Time::Hires, here is a (tested) piece of code that should work:
#!/usr/bin/perl -w use strict; use Net::Ping; my $NB_PING=5; my $host="dslreports.com"; my( $ok, $nok, $total_time)=(0,0,0); # use the icmp protocol to emulate the unix ping # note that this implies that the script should be # run as root (or suid root) my $p = Net::Ping->new( "icmp"); $p->hires(); # needs Time::Hires foreach (1..$NB_PING) { my ($ret, $duration, $ip) = $p->ping($host, 3); # 3 is the time ou +t in sec if( $ret) { $ok++; $total_time+= $duration * 1000; } else { $nok++; } } $p->close(); # we don't want division by 0 errors do we? my $average= $ok ? int($total_time / $ok) : 0; my $lost= int ( ($nok / ( $ok + $nok)) * 100); print "$lost%\n$average\n0\n0\n";
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re: Re: MRTG script should be easy
by zengargoyle (Deacon) on May 20, 2003 at 22:59 UTC |