Kunaljain's Weblog

October 30, 2007

System monitoring with ping command and script

Filed under: Linux Tips & Tricks — Kunal Jain @ 18:04

Ping is the basic command to test the network connection

ping -c 4 http://www.mydomain.com

output:

PING http://www.mydomain.com (67.155.205.190) 56(84) bytes of data.

64 bytes from mydomain.com (67.155.205.190): icmp_seq=1 ttl=46 time=277 ms

64 bytes from mydomain.com (67.155.205.190): icmp_seq=2 ttl=46 time=273 ms

64 bytes from mydomain.com (67.155.205.190): icmp_seq=3 ttl=46 time=272 ms

64 bytes from mydomain.com (67.155.205.190): icmp_seq=4 ttl=46 time=274 ms

http://www.mydomain.com ping statistics —

4 packets transmitted, 4 received, 0% packet loss, time 3007ms

rtt min/avg/max/mdev = 272.229/274.482/277.306/1.954 ms

4 received is just to verify that 4 packets send and received successfully. we extract 4 received with the following command

ping -c 4 http://www.mydomain.com | grep received

Output:

4 packets transmitted, 4 received, 0% packet loss, time 3007ms

Next step is to get number 4 received

$ping -c 4 http://www.mydomain.com | grep received | awk -F’,’ ‘{ print $2 }’

Output:

4 received

Next step is to get number 4

$ping -c 4 http://www.mydomain.com | grep received | awk -F’,’ ‘{ print $2 }’ | awk ‘{ print $1 }’

Output:

4

Shell script monitoring system with ping command

HOST=”www.mydomain.com”

COUNT=4

echo “Running, please wait…”;

for TheHost in $HOST

do

count=$(ping -c $COUNT $TheHost | grep received | awk -F ‘,’ ‘{ print $2 }’ | awk ‘{ print $1}’)

if [ $count -eq 0 ]; then

echo “$(date) – $TheHost ping Failed”

else

echo “$(date) – $TheHost ping success”

fi

done

Now you can set this script with your other cron jobs as per your desire

Advertisements

Leave a Comment »

No comments yet.

RSS feed for comments on this post. TrackBack URI

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

Create a free website or blog at WordPress.com.

%d bloggers like this: