Link to home
Start Free TrialLog in
Avatar of j2
j2Flag for Sweden

asked on

ping script

I need a script (shell or perl) that pings two hosts every X seconds, and if either of those hosts are not reachable / not answering logs this. Preferably in one file per pinged host.

( i need to find out just how much downtime our X-line really has right now, i have a feeling it is worse then our TelCo tells us )
Avatar of jyu_88
jyu_88

directly from 'perldoc Net::Ping'
           use Net::Ping;

           $p = Net::Ping->new();
           print "$host is alive.\n" if $p->ping($host);
           $p->close();

           $p = Net::Ping->new("icmp");
           foreach $host (@host_array)
           {
               print "$host is ";
               print "NOT " unless $p->ping($host, 2);
               print "reachable.\n";
               sleep(1);
           }
           $p->close();

           $p = Net::Ping->new("tcp", 2);
           while ($stop_time > time())
           {
               print "$host not reachable ", scalar(localtime()), "\n"
                   unless $p->ping($host);
               sleep(300);
           }

---------cut--------cut------

It should be pretty easy to modify the example to your content. For details, read the  module's man page by 'perldoc Net::Ping'
Avatar of j2

ASKER

I need working code. Lets just say i am a better net admin then i am a coder ;)

ive gotten this far, unfortunately it doesnt log anything :(

#!/bin/csh -f

set IP=( 192.71.220.10 212.181.140.105 10.10.10.10 192.168.0.74 )

set LOGFILE="ping.log"
set ZZZ=2

echo "Started ping log at:   `date`" >! ${LOGFILE}
echo "Targets: $IP" >> ${LOGFILE}
echo " " >> ${LOGFILE}

# Go until Ctrl-C or killed
while 1

  foreach target ( ${IP} )
     ping -c1 -qn ${target} > /dev/null

     if( ${status} == 1 ) then
        echo "`date`: ${target} is not reachable" >> ${LOGFILE}
     endif
   end

   sleep ${ZZZ}

end
when I run your script on a Linux box, it worked just fine. The content of the log is:

/users/csfa/zyu/824p$ csh -x t.csh
/users/csfa/zyu/824p$ cat ping.log
Started ping log at:   Tue Feb 29 20:43:00 EST 2000
Targets: 192.71.220.10 212.181.140.105 10.10.10.10 192.168.0.74

Tue Feb 29 20:43:11 EST 2000: 10.10.10.10 is not reachable
Tue Feb 29 20:43:21 EST 2000: 192.168.0.74 is not reachable
Tue Feb 29 20:43:33 EST 2000: 10.10.10.10 is not reachable  

The fact you don't have log entries may be because that the list of hosts are available on your site?
Have you considered mrtg? I seem to remember a contributed example that monitored connectivity and produced a nice graph of same. The homepage is http://ee-staff.ethz.ch/~oetiker/
Avatar of j2

ASKER

mrtg would be usable, however, there are about a gazillion addons.. care to be more specific? :=)
You're right there are a gazillion. I'll go look and see which one I used.
I used "ping-probe" when I had a similar problem and you can get it (and a couple of other cute things) from ftp://ftp.pwo.de/pub/pwo/mrtg.
Avatar of j2

ASKER

Aha.. to tell you the truth.. i have no idea how to set mrtg up, i have it installed, but how do i write a config file?
Yeah, it can be confusing... Tell you what, if you can give me a little time I'll set up mrtg and ping-probe (taking notes) let you know what I did.
Avatar of j2

ASKER

Would be wonderful.. ill add a bucket of points for that favour when you do :)
I think I've pretty much got it. Since you posted this in Linux networking is it fair to assume that's where you'll set it up to monitor the WAN links? Do you have any routers that you'd like notes for how to set them up for monitoring?
Avatar of j2

ASKER

All i have is a single linuxbox connected to to a WAN device over am ethercard. The wav device keeps going down.. much more s then the operator says it does. So this is what i would like to monitor. So no routers per se. Just a RH box.
ASKER CERTIFIED SOLUTION
Avatar of jlevie
jlevie

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Have you had a chance to try the above?
Avatar of j2

ASKER

Sorry, no. I got sent to England :) I just got home, so ill set this up this weekend.
Okay, I was just wondering...
Avatar of j2

ASKER

Question here, this will "poll" the link every five mins? But i need to go down to something with a 1 or 2 second poll interval.
Avatar of j2

ASKER

Adjusted points from 50 to 150
Avatar of j2

ASKER

.and all i get is

[root@animal mrtg]# mrtg ./mrtg.cfg
Could not get any data from external command '/home/httpd/html/mrtg/mrtg-ping-probe-1.9.0/mrtg-ping-probe -o -n 192.168.196.100'
Maybe the external command did not even start. (Illegal seek)

[root@animal mrtg]# mrtg ./mrtg.cfg
Could not get any data from external command '/home/httpd/html/mrtg/mrtg-ping-probe-1.9.0/mrtg-ping-probe -o -n 192.168.0.254'
Maybe the external command did not even start. (Illegal seek)

[root@animal mrtg]#

Those are two machines on my local network.
Actually a 1-2 sec pinger isn't really a problem. I've done one that did that for use with mrtg. The mrtg updates were only every five minutes, and the data shown was the percentage of pings sent for outbound and the percentage of replies received for inbound. I'll go dredge up the code (it's on a backup tape) and cook up another example.

Now as to the failure. I'm guessing that you didn't follow the recipe. At least the paths look suspiciously like you didn't. I suspect that you didn't make the "run" directory and copy the mrtg executables and the mrtg-ping-probe code tho that dir. Don't unpack the sources into your htdocs area, put 'em somplace else and set up a "run" dir in the htdocs area like I did in the example.
Avatar of j2

ASKER

I tried to apply your configs to the RedHat version of mrtg.. ill redo that :)
Ahh, that would probably explain it. I'm going to look for my fast pinger today.
Avatar of j2

ASKER

GAH, i still havent been able to fix this... here is my setup

WorkDir: /home/httpd/html/mrtg

IconDir: /home/httpd/html/mrtg/images

Title[wan-link]: Darkface

PageTop[wan-link]: Ping times over Wan-link

MaxBytes[wan-link]: 50

AbsMax[wan-link]: 200

Options[wan-link]: gauge

Target[wan-link]: `/home/httpd/html/mrtg/run/mrtg-ping-probe -o -n 195.54.96.76`

YLegend[wan-link]: round trip time

ShortLegend[wan-link]: ms

The paths are correct. and the mrtg binary resides in /usr/bin/mrtg  Running mrtg /home/httpd/html/mrtg/mrtg.cfg still gives me

Could not get any data from external command '/home/httpd/html/mrtg/run/mrtg-ping-probe -o -n 195.54.96.76'
Maybe the external command did not even start. (Illegal seek)


Any hints?
Avatar of j2

ASKER

GAH, it was an incorrect path to perl that got me... now i get

Line 19 in CFG file does not make sense

which config file? in mrtg.cfg my line 19 is

WorkDir: /home/httpd/html/mrtg
What happens if you simply run "home/httpd/html/mrtg/run/mrtg-ping-probe -o -n 195.54.96.76" from the command line?
Avatar of j2

ASKER

[root@animal mrtg-ping-probe-1.9.0]# /home/httpd/html/mrtg/run/mrtg-ping-probe -o -n 195.54.96.76
48
27
[root@animal mrtg-ping-probe-1.9.0]#

what does that mean? :)
Avatar of j2

ASKER

but it IS building stuff... look at

http://animal.mupp.net/mrtg/
That means that the ping probe runs and returns the avg RTT & the minimum RTT. So I'd suspect something is wrong with the mrtg in /usr/bin. Is that the one from the download or the RedHat copy? Also, mrtg probably has to be run by root to have write privs to where it stores its data.
Yep, it is working. I suspect that mrtg isn't seeing exactly what it expects from pring-probe. It doesn't happen on any of my installations, so I'm at a bit of a loss as to why it happens here.
Avatar of j2

ASKER

Adjusted points from 150 to 200
Avatar of j2

ASKER

Ok, that error also dissapeared, so now it works. I have but two questions.

1. How do i kill it? it ISNT in my crontab, and yet it gets run every 5 minutes (not in atq either). :)

2. Did you have a clue to how to design something that fires every 5 seconds or so?

And yeah, you will definitely get an A on thsi, even if you dont have the answers to above :)
Avatar of j2

ASKER

found the answer to 1. RH had stuck it in its "own" /etc/crontab.
Are you adverse to installing a perl module? I couldn't find my "fast pinger" easily so I've mostly re-created it, but it does require downloading & installing a perl module. It's slightly more complex than just using ping-probe, as it requires a separate task running to do pings every few seconds to determine link status, which mrtg can use to generate a graph of pings sent/received every 5 minutes (that being the normal granularity of mrtg).

What I do is to ping every n seconds with a continuously running perl script, which updates a file in /tmp every 5 minutes with the number of pings sent, the number received, avg RTT, and max RTT. A module for mrtg reads the data for plotting. The plot will clearly show any line outages, as the ping return (plotted as incoming data rate) will spike down for line outages.