Web Hosting Talk







View Full Version : Perl Script - Timeout


mainarea
02-06-2005, 12:08 PM
I'm having a problem with Cacti & ping graphs. Here's the script:
#!/usr/bin/perl

$ping = `ping -c 1 $ARGV[0] | grep icmp_seq`;
$ping =~ m/(.*time=)(.*) (ms|usec)/;

print $2;

The problem with the code is that if one of the hosts that the script is pinging is down, it stops (since it's still waiting for a response) and doesn't go onto other hosts, therefore leaving gaps in the graphs.

Is there any way that I could add an argument to say that if ping doesn't respond within 2 seconds, to print 0 as the result? Otherwise, it would display the result (which is the 2 current lines of code).

- Matt

fastduke
02-06-2005, 03:24 PM
I'm not really getting how this script goes on to a next host? I am not getting to see the whole picture or what? where is the code that creates your graph? Why is their a gap in your graph?

this will just say "0" rather than nothing at all which perl sees as FALSE anyhow.

if(!$2){ print "0"; } else { print $2; }


and since you are using an external program inside this script... gets you a timeout in 2

$ping = `ping -W 2 -c 1 $ARGV[0] | grep icmp_seq`;

mainarea
02-06-2005, 03:33 PM
I'm not really getting how this script goes on to a next host? I am not getting to see the whole picture or what? where is the code that creates your graph? Why is their a gap in your graph?
I'm using Cacti to create graphs and pull the results. Basically, it goes in order & calls up this perl script for each host it needs to check. Then it records the result, and queries the next host. For example, it will execute the following & record the response:

/path/to/pingscript.pl www.google.com
/path/to/pingscript.pl www.site2.com
/path/to/pingscript.pl www.site3.com
/path/to/pingscript.pl www.site4.com

If site2.com doesn't respond, it just hangs there & doesn't move on though. There's a gap in all of the graphs for site2.com, site3.com, and site4.com because it was unable to record a result.

and since you are using an external program inside this script... gets you a timeout in 2

$ping = `ping -W 2 -c 1 $ARGV[0] | grep icmp_seq`;
Should be a lowercase w, not sure how I missed the timeout option for ping... I'll go test it out & see what I can do.

- Matt

mainarea
02-06-2005, 03:40 PM
This works:

#!/usr/bin/perl

$ping = `ping -c 1 -w 2 $ARGV[0] | grep icmp_seq`;
$ping =~ m/(.*time=)(.*) (ms|usec)/;

if(!$2){ print "0"; } else { print $2; }

Thanks!

- Matt