Page 1 of 1

HowTo ping a Lot of hosts for up|down

Posted: Sat Mar 24, 2012 8:17 pm
by Habitual
Couldn't find one that worked readily, so I made one.
This script is by NO means conclusive or as complete as it could be (needs an alternate check method (Hint Hint Nudge Nudge Wink Wink)... icmp "may" be blocked on some hosts and that will fail in the script and come back as "down", that's not the script's fault. :) )

Step1:
Make list of hosts (actual or FQDN or a mash of both...)

Code: Select all

grid1
grid2
c9Zabbix
C9_Prod_Bacula
c9gluster-nas1
c9gluster-nas2
Cirrhus9_Prod_sFTP
Cirrhus9_Prod_Wiki
MikeJ_WinServer
Cirrhus9_JJ_LAMP
cirrhus9b
webdev
Icinga/Cacti
drupal3
varnish3
remotedb2
nfs
web1
web2
rsgrid1
rsgrid2
rsgrid3
save it as say ~/Documents/ping.list

Step2:
Edit /etc/hosts as root and add your IP/hosts details:

Code: Select all

xx.xxx.246.198  grid1
xx.xxx.224.34   grid2
xx.xxx.224.62   c9Zabbix
xx.xxx.224.35   C9_Prod_Bacula
xx.xxx.224.53   c9gluster-nas1
xx.xxx.224.54   c9gluster-nas2
xx.xxx.224.36   Cirrhus9_Prod_sFTP
xx.xxx.224.38   Cirrhus9_Prod_Wiki
xx.xxx.224.47   MikeJ_WinServer 
xx.xxx.224.45   Cirrhus9_JJ_LAMP
xx.xxx.181.212  c9a
xx.xxx.139.254 	cirrhus9b
xx.xxx.207.18   webdev
xx.xxx.171.149  Icinga/Cacti
xx.xxx.121.73   drupal3
xx.xxx.121.78   varnish3
xx.xxx.155.196  remotedb2
xx.xxx.131.124  nfs
xx.xxx.130.234  web1
xx.xxx.131.126  web2
xx.xxx.143.152  rsgrid1
xx.xxx.128.55   rsgrid2
xx.xxx.27.48    rsgrid3
Step2:
Create this .sh script and add:

Code: Select all

#!/bin/bash
# Written by John Jones of cirrhus9.com

is_alive_ping()
{
  if   
  ping -c 1 $1 > /dev/null ; then
  [ $? -eq 0 ] && echo $i is up
  else
  echo $i \""may\"" be down
  fi
}

for i in $(cat ~/Documents/ping.listt)
do
is_alive_ping $i 
done
#EOF
save it as say ~/bin/ping_hosts.sh

Code: Select all

chmod 700 ~/bin/ping_hosts.sh
Now run it:

Code: Select all

~/bin/ping_hosts.sh
Output:

Code: Select all

grid1 is up
grid2 is up
c9Zabbix is up
C9_Prod_Bacula is up
c9gluster-nas1 "may" be down
c9gluster-nas2 "may" be down
Cirrhus9_Prod_sFTP is up
Cirrhus9_Prod_Wiki is up
MikeJ_WinServer is up
Cirrhus9_JJ_LAMP is up
cirrhus9b is up
webdev is up
Icinga/Cacti is up
drupal3 is up
varnish3 is up
remotedb2 is up
nfs is up
web1 is up
web2 is up
rsgrid1 is up
rsgrid2 is up
rsgrid3 is up
Other more elegant solutions may exist (That's my standard disclaimer folks)
This code is just me hacking for my own creative ends while serving a need.

Enjoy.

Re: HowTo ping a Lot of hosts for up|down

Posted: Sun Mar 25, 2012 2:03 am
by Pilosopong Tasyo
Improvement: concurrent ping

I wrote a similar function last year (for my cybercafé timer project). It's slightly different from your version, though. What I did was I put the ping function in its own script, then called that script from within the for loop of the monitoring script. The main difference being, the ping script gets put in the background within the for loop. And in doing so, it pings all hosts at the same time instead of pinging them sequentially. The result I get is far lesser time it takes to compile a report. :D

Apart from the -c parameter, I also used the -w parameter to force a timeout.

sample ping.list

Code: Select all

google.com
cnn.com
192.168.254.50
yahoo.com
facebook.com
globe.com.ph
192.168.254.254
smart.com.ph
linuxmint.com
wikipedia.org
getofftheweb.com
ipchicken.com
192.168.254.100
ping-pong.sh

Code: Select all

#!/bin/sh

COUNT=1  # only need a one-time ping
DEADLINE=2  # forced timeout in seconds
HOST=$1

ping -c $COUNT -w $DEADLINE $HOST 1>/dev/null 2>/dev/null
SUCCESS=$?

if [ $SUCCESS -eq 0 ]
then
  echo "$HOST has replied"
else
  echo "$HOST didn't reply"
fi
check-server-status.sh

Code: Select all

#!/bin/sh

SERVER_LIST=`cat ping.list`

echo
echo "Sequential ping"
echo "---------------"
START=`date +%s`
for SERVER in $SERVER_LIST
do
  ./ping-pong.sh $SERVER
done
END=`date +%s`

DURATION=`expr $END - $START`
echo "Done.  Sequential ping took a total of $DURATION seconds to finish."

echo
echo "Concurrent ping"
echo "---------------"
START=`date +%s`
for SERVER in $SERVER_LIST
do
  ./ping-pong.sh $SERVER &
done
wait
END=`date +%s`

DURATION=`expr $END - $START`
echo "Done.  Concurrent ping took a total of $DURATION seconds to finish."
Running the test produced the following output:

Code: Select all

administrator@dg31pr ~/Public $ ./check-server-status.sh 

Sequential ping
---------------
google.com has replied
cnn.com didn't reply
192.168.254.50 has replied
yahoo.com has replied
facebook.com has replied
globe.com.ph didn't reply
192.168.254.254 has replied
smart.com.ph has replied
linuxmint.com has replied
wikipedia.org has replied
getofftheweb.com didn't reply
ipchicken.com has replied
192.168.254.100 didn't reply
Done.  Sequential ping took a total of 12 seconds to finish.

Concurrent ping
---------------
192.168.254.254 has replied
192.168.254.50 has replied
google.com has replied
yahoo.com has replied
smart.com.ph has replied
ipchicken.com has replied
facebook.com has replied
wikipedia.org has replied
linuxmint.com has replied
192.168.254.100 didn't reply
getofftheweb.com didn't reply
globe.com.ph didn't reply
cnn.com didn't reply
Done.  Concurrent ping took a total of 2 seconds to finish.
administrator@dg31pr ~/Public $
Two seconds versus twelve. :!:

Re: HowTo ping a Lot of hosts for up|down

Posted: Sun Mar 25, 2012 11:58 am
by Habitual
Thank you very much.

+1