1753460 Members
4717 Online
108794 Solutions
New Discussion юеВ

Re: scripting help

 
SOLVED
Go to solution
lawrenzo
Trusted Contributor

scripting help

Hello Friends,

I have been asked to write a script that will constantly ping 14 devices ( shop tills ) and record the response time,host address and actual time.

Because this will create a huge logfile I have suggested we run the ping and only alert when the response is over a certian limit.

I have no problem with writing the ping check and limits however I am struggling to get my head around how I can ping 14 devices simaltaniously from one script - any ideas?

cheers
hello
5 REPLIES 5
Peter Godron
Honored Contributor
Solution

Re: scripting help

Hi,
how about:
#!/usr/bin/sh
/usr/sbin/ping server1 &
/usr/sbin/ping server2 &
..
/usr/sbin/ping server14 &


run this script and redirect output
./x.sh > x.log
Hemmetter
Esteemed Contributor

Re: scripting help

Hi lawrenzo

What about:

( ping $HOST1 &
ping $HOST2 &
ping $HOST3 &
) | do_something


rgds
HGH


Peter Nikitka
Honored Contributor

Re: scripting help

Hi,

a flexible way for me seems to be to write
- one script, monitoring one host supplied as parameter, printing an alert after n unsuccesful attempts to reach this host
- another script, calling script one for all hosts to monitor in a loop as background processes, like

trap 'kill $pidlist' '1 2 3 15' # to be customized
for h in $allhosts
do
./one $h &
pidlst="$pidlst $!"
done

So you have control over your child processes via $pidlist.

mfG Peter
The Universe is a pretty big place, it's bigger than anything anyone has ever dreamed of before. So if it's just us, seems like an awful waste of space, right? Jodie Foster in "Contact"
Victor Fridyev
Honored Contributor

Re: scripting help

Hi,

You can use the subroutine:
#################################################
spng() {
P=$(/etc/ping $1 64 1 |
awk 'NR==3||NR==4 {if($7=="100%") {printf("NO");exit}}
NR==5 {printf("OK") }')
if [ $P != "OK" ]; then
return 1
else
return 0
fi
}
#################################################
cat listfile | while read ADDR; do
if spng $ADDR; then
:
# The host answers
else

(/etc/ping $ADDR 1500 10
fi
done > logfile

BTW, I'm not sure that ping is a good tool for the purpose.

HTH
Entities are not to be multiplied beyond necessity - RTFM
Bill Hassell
Honored Contributor

Re: scripting help

Well, the first issue is that ping is a low level test and will succeed even when the machine is not working correctly. Secondly, ping does NOT return a failed exit code if the ping does not respond. It will return a failure code if the IP address or hostname is wrong but a ping to a reachable host will exit with zero status whether the ping was successful or not.

So you must process the messages reported by ping. Also, ping may hang under some conditions so you'll need to put the ping in the background, then monitor the status every second to see if the ping completes. Naturally, you need to ping just once to see if the host responds -- using multiple pings per host makes parsing tricky. If the ping doesn't complete, kill the ping process and report on the problem.

I have attached a simple script that will take an unlimited list of IP or hostnames (comments ignored) and reports on success or failure. You'll need to modify the script for reporting.

As mentioned, ping is a trivial test for functionality. pings may be fine but the system has big problems. That's why you need a separate monitoring system that queries the system about overall status -- HP OpenView products can do this as well as freeware like Nagios. These products cover both the network health as well as system health.


Bill Hassell, sysadmin