in reply to Alarm Implemented in Win32?

I use the Net::Ping in NT everyday. I wrote a small system monitoring package that checks hosts via ping, telnet, http, and dbi. The script just runs a loop and checks different hosts with different methods, logs any failures, and pages me if it's down more than once.

Here is the relevant ping code. This works on NT with the activestate perl and the Net::Ping package installed with the ppm tool. I had no problems with alarm. The only problem I've had with Net::Ping is that it requires root when running in unix...

#!/usr/bin/perl @pinglist=("www.yahoo.com", "www.thisisntreal.com", "wlindsey.bbcg.com +"); use Net::Ping; sub test_ping; foreach $h (@pinglist) { if (test_ping($h)) { print "[p (ok) $h]"; } else { print "[p <NO> +$h]"; } print "\n"; } sub test_ping { # test_ping($hostname) my $hostname=shift; my $p=Net::Ping->new(icmp,5); return $p->ping($h,5); $p->close; } # end test_ping

Replies are listed 'Best First'.
RE: Re: Alarm Implemented in Win32?
by buzzcutbuddha (Chaplain) on May 19, 2000 at 16:43 UTC
    Elwarren, I implemeneted your code and then I looked at it and realized that I could get
    the same functionality at half the speed.

    UPDATE: errr...I should say at twice the speed...oops, typing faster than my brain
    I thought you might be interested.
    Here is the code:
    #!/usr/bin/perl @pinglist=('10.17.1.60', '10.17.1.61', '10.17.1.62', '10.17.1.63'); use Net::Ping; my $p = Net::Ping->new(icmp, 5); foreach $h (@pinglist) { if ($p->ping($h, 5)) { print "[p (ok) $h]"; } else { print "[p <NO +> $h]"; } print "\n"; } $p->close;
    And the benchmarking results:
    H:\>benchingping.pl Benchmark: timing 5000 iterations of inline, modular... inline: 63 wallclock secs (22.26 usr + 8.72 sys = 30.98 CPU) @ 16 +1.37/s (n= 5000) modular: 104 wallclock secs (38.02 usr + 23.94 sys = 61.95 CPU) @ 8 +0.71/s (n= 5000)
    Cool, huh? Thanks again!

      That's good to know. I never really went back to optimize the code, I was happy enough that I could run it on my NT machine at work. We had a server that kept crashing at different times, usually when nobody was looking :-) I threw this together to ping it every 5 minutes and it sort grew into a tool that did a few different checks. I put all the code into functions that handle creating the objects, connecting, checking the results, cleaning up and then returning a pass or fail. Each connection type has it's own special quirks hidden behind the function, so I can just as easily drop in a test_ping($hostname) as I could test_http($hostname)

      For a script that sits idle and wakes up every 5 minutes it's an acceptable slowdown ;-)

      Oh, and it ended up being a bad ups on the server...

RE: Re: Alarm Implemented in Win32?
by buzzcutbuddha (Chaplain) on May 19, 2000 at 15:33 UTC
    Hot Damn! That did it. I guess the PingEcho function still uses Alarm, but if
    you use the Ping Object Method, then it works. Good to know for future reference!
    Thanks!