Simple Linux and UNIX system monitoring with ping command and scripts
You can simply monitor your remote system hosted some remote IDC. There may be tons of causes for which system goes out the network.
Understanding ping command
ping is one of the basic and nifty command to test network connection. For example you can send 4 ping request to cyberciti.biz with following command:
$ ping -c 4 cyberciti.biz
$ ping -c 4 router
Output:
PING router (192.168.1.254) 56(84) bytes of data. 64 bytes from router (192.168.1.254): icmp_seq=1 ttl=64 time=1.02 ms 64 bytes from router (192.168.1.254): icmp_seq=2 ttl=64 time=0.824 ms 64 bytes from router (192.168.1.254): icmp_seq=3 ttl=64 time=0.826 ms 64 bytes from router (192.168.1.254): icmp_seq=4 ttl=64 time=0.843 ms --- router ping statistics --- 4 packets transmitted, 4 received, 0% packet loss, time 3012ms rtt min/avg/max/mdev = 0.824/0.880/1.027/0.085 ms
Just see output '--- router ping statistics ---', 4 received is key to verify that 4 packets send and received successfully. You can extract 4 received with the following command:
$ ping -c4 router | grep 'received'
Output:
4 packets transmitted, 4 received, 0% packet loss, time 3012ms
Next logical step is to just get number 4 received using awk command:
$ ping -c4 router | grep 'received' | awk -F',' '{ print $4}'
Output:
4 received
Last step is to just get 4 number and remove received word:
$ ping -c4 router | grep 'received' | awk -F',' '{ print $2}' | awk '{ print $1}'
Output:
4
Shell script to system monitoring with ping command
Now you know how to obtained received packets, it is time to automate entire process with simple script. Here is sample script (download link):
#!/bin/bash HOSTS="cyberciti.biz theos.in router" COUNT=4 for myHost in $HOSTS do count=$(ping -c $COUNT $myHost | grep 'received' | awk -F',' '{ print $2 }' | awk '{ print $1 }') if [ $count -eq 0 ]; then # 100% failed echo "Host : $myHost is down (ping failed) at $(date)" fi done
You can download complete working script which sends an email alert here.
Setup Cron job
In order to run this script every 30 minutes (or as per your requirements), you need to install a script as cron job:
$ chmod +x /path/to/monitorHost
Install the monitorHost script as crontab using the editor:
$ crontab -e
Append the following cronjob entry:
# Monitor remote host every 30 minutes using monitorHost
30 * * * * /home/vivek/bin/monitorHost
Save and close the file.
You may also be interested in other helpful articles:
- Howto: Ubuntu Linux convert DHCP network configuration to static IP configuration
- Ubuntu Linux view the status of my network Interfaces/card
- Ajax based DNS query service
- How to Access Network When Everything Else is Blocked
- Testing Connectivity with fping and send mail if any hosts are unreachable
Discussion on This Article:
Leave a Reply
We encourage your comments, and suggestions. But please stay on topic, be polite, and avoid spam. Thank you very much for stopping by our site!
Tags: grep command, monitor lan with ping command, packet loss, ping command, ping cron job, ping request, ping statistics, seq command, shell script



This is the script I’m using for the same thing:
case `ping -qnc 1 google.com 2>&1` in
*’100% packet loss’*)
echo “The network is DOWN.”
exit 1
;;
esac
One way or around…
Nice, though I think a ( got dropped before *’100% packet loss’*) ?
No, ( is not missing. Try it:
http://pastebin.ca/102156
Kotnik,
Short and sweet code
Hie
this is an ingenious script:)
thanks to share it
I have just a little question.
I didn’t find “Email ID”
can you help me to find it please??
thanks for your help
Go here
HTH
You can skip the awk stuff by using cut like this:
ping -c4 localhost | grep ‘received’ | cut -f2 -d’,’ |cut -f2 -d’ ‘
it also returns 4
TonyK
Yes you can always use another set of commands to do the same thing. Coding is like that… heh
Appreciate your post!
I commonly use this. Just another way to do it.
if [[ $(ping -q -c 3 10.1.1.1) == @(*100% packet loss*) ]]; then
echo “$host is down”
else
echo “$host is alive”
fi
can someone show me a script I could cron every 15 min or so, that would email ‘root’ each time a computer comes up on the LAN or goes off the LAN, giving the ip address and the windows computer name?