Solved

ping script

Posted on 2000-02-29
33
3,821 Views
Last Modified: 2008-03-10
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 )
0
Comment
Question by:j2
  • 17
  • 14
  • 2
33 Comments
 
LVL 3

Expert Comment

by:jyu_88
ID: 2570949
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'
0
 
LVL 12

Author Comment

by:j2
ID: 2571019
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
0
 
LVL 3

Expert Comment

by:jyu_88
ID: 2571148
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?
0
 
LVL 40

Expert Comment

by:jlevie
ID: 2585071
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/
0
 
LVL 12

Author Comment

by:j2
ID: 2585120
mrtg would be usable, however, there are about a gazillion addons.. care to be more specific? :=)
0
 
LVL 40

Expert Comment

by:jlevie
ID: 2585240
You're right there are a gazillion. I'll go look and see which one I used.
0
 
LVL 40

Expert Comment

by:jlevie
ID: 2585406
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.
0
 
LVL 12

Author Comment

by:j2
ID: 2593247
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?
0
 
LVL 40

Expert Comment

by:jlevie
ID: 2593312
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.
0
 
LVL 12

Author Comment

by:j2
ID: 2593333
Would be wonderful.. ill add a bucket of points for that favour when you do :)
0
 
LVL 40

Expert Comment

by:jlevie
ID: 2610988
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?
0
 
LVL 12

Author Comment

by:j2
ID: 2611187
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.
0
 
LVL 40

Accepted Solution

by:
jlevie earned 200 total points
ID: 2615722
Okay, here tis. I had it ready and tested yesterday, but couldn't get to EE all last evening. I don't know if it was a routing problem or a problem with the EE site.

Notes for installing Mrtg to monitor a WAN point-to-point link
with ping probe. I usually put the mrtg stuff directly in an
htdocs subdir, /opt/Apache/htdocs/netmon on my systems.

Dependancies as of 9 Mar 00

mrtg-2.8.12
  ping-probe-1.9.0 (copied to contrib/ping-probe)


1) Configure, build and install mrtg

   > cd mrtg-2.8.12
   > ./configure
   > make
   > mkdir -p /opt/Apache/htdocs/netmon/run /opt/Apache/htdocs/netmon/images
   > cp run/* /opt/Apache/htdocs/netmon/run
   > cp -r images/*.png /opt/Apache/htdocs/netmon/images

2) Install ping-probe
   
   > cd contrib/ping-probe/mrtg-ping-probe-1.9.0
   > make man
   > cp  mrtg-ping-probe.1 /usr/local/man/man1
   > cp mrtg-ping-probe /opt/Apache/htdocs/netmon/run

3) Make a minimal mrtg config file for ping probe. Make sure to use
   the IP address of something at the far end so that dns problems
   don't show up as a loss of connectivity. I've also told ping to
   only user numeric addresses. The config looks like:

WorkDir: /opt/Apache/htdocs/netmon
IconDir: /opt/Apache/htdocs/netmon/images

Title[wan-link]: DC NT server
PageTop[wan-link]: Ping times over wan-link
MaxBytes[wan-link]: 50
AbsMax[wan-link]:200
Options[wan-link]: gauge
Target[wan-link]: `/opt/Apache/htdocs/netmon/run/mrtg-ping-probe -o -n 192.168.196.100`
YLegend[wan-link]: round trip time
ShortLegend[wan-link]: ms

   You may want to twiggle "MaxBytes" and "AbsMAX". "MaxBytes should be
   a bit more than what you'd expect to see on a quiet, normally operating
   link. I set "AbsMax" about four times what I set "MaxBytes" to so that
   excessive ping times will still be graphed.

4) Run mrtg from the command line. The first two times there will be
   ignorable warnings as it creates all the necessary files. Be patient
   it takes a bit to get the data.

   > cd /opt/Apache/netmon/run
   > ./mrtg mrtg.cfg

   Add a line to root's cron the run mrtg every 5 minutes, like
   
   0-59/5 * * * * /opt/Apache/htdocs/netmon/run/mrtg /opt/Apache/htdocs/netmon/run/mrtg.cfg

5) If you look in /opt/Apache/htdocs/netmon there'll be some files named
   wan-link.*, amoung them is wan-link.html. The rest is left as an
   an exercise for the reader.
0
 
LVL 40

Expert Comment

by:jlevie
ID: 2625235
Have you had a chance to try the above?
0
 
LVL 12

Author Comment

by:j2
ID: 2625267
Sorry, no. I got sent to England :) I just got home, so ill set this up this weekend.
0
 
LVL 40

Expert Comment

by:jlevie
ID: 2625293
Okay, I was just wondering...
0
Top 6 Sources for Identifying Threat Actor TTPs

Understanding your enemy is essential. These six sources will help you identify the most popular threat actor tactics, techniques, and procedures (TTPs).

 
LVL 12

Author Comment

by:j2
ID: 2637355
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.
0
 
LVL 12

Author Comment

by:j2
ID: 2637390
Adjusted points from 50 to 150
0
 
LVL 12

Author Comment

by:j2
ID: 2637391
.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.
0
 
LVL 40

Expert Comment

by:jlevie
ID: 2638250
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.
0
 
LVL 12

Author Comment

by:j2
ID: 2638921
I tried to apply your configs to the RedHat version of mrtg.. ill redo that :)
0
 
LVL 40

Expert Comment

by:jlevie
ID: 2639847
Ahh, that would probably explain it. I'm going to look for my fast pinger today.
0
 
LVL 12

Author Comment

by:j2
ID: 2718979
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?
0
 
LVL 12

Author Comment

by:j2
ID: 2718982
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
0
 
LVL 40

Expert Comment

by:jlevie
ID: 2718983
What happens if you simply run "home/httpd/html/mrtg/run/mrtg-ping-probe -o -n 195.54.96.76" from the command line?
0
 
LVL 12

Author Comment

by:j2
ID: 2718991
[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? :)
0
 
LVL 12

Author Comment

by:j2
ID: 2718992
but it IS building stuff... look at

http://animal.mupp.net/mrtg/
0
 
LVL 40

Expert Comment

by:jlevie
ID: 2719009
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.
0
 
LVL 40

Expert Comment

by:jlevie
ID: 2719016
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.
0
 
LVL 12

Author Comment

by:j2
ID: 2719022
Adjusted points from 150 to 200
0
 
LVL 12

Author Comment

by:j2
ID: 2719023
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 :)
0
 
LVL 12

Author Comment

by:j2
ID: 2719028
found the answer to 1. RH had stuck it in its "own" /etc/crontab.
0
 
LVL 40

Expert Comment

by:jlevie
ID: 2719822
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.
0

Featured Post

How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

Join & Write a Comment

I have seen several blogs and forum entries elsewhere state that because NTFS volumes do not support linux ownership or permissions, they cannot be used for anonymous ftp upload through the vsftpd program.   IT can be done and here's how to get i…
Note: for this to work properly you need to use a Cross-Over network cable. 1. Connect both servers S1 and S2 on the second network slots respectively. Note that you can use the 1st slots but usually these would be occupied by the Service Provide…
This demo shows you how to set up the containerized NetScaler CPX with NetScaler Management and Analytics System in a non-routable Mesos/Marathon environment for use with Micro-Services applications.
You have products, that come in variants and want to set different prices for them? Watch this micro tutorial that describes how to configure prices for Magento super attributes. Assigning simple products to configurable: We assigned simple products…

705 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

17 Experts available now in Live!

Get 1:1 Help Now