We have a monitoring system to check network devices, sometimes we got an alert saying the remote device is down. We check the remote device and it’s working fine. To ensure that our monitoring working properly, we create an external script to monitor system, then we want to compare the result between 2 systems. I finally come with this script (ping.sh) , it will continuously ping the remote server, if it’s down, it will write a log.
#!/bin/bash
hostname="my_default_hostname";
if [[ -n $1 ]]
then
hostname=$1;
fi
while true;
do
date;
fping -c3 $hostname ;
if [[ $? -ne 0 ]]
then
echo "============System [$hostname] is down============"
d=`date`;
echo "$d System [$hostname] is down " >>log.txt
traceroute $hostname >>log.txt 2>&1
fi
sleep 1;
done
I want to run this script in background so i run it like this:
nohup ./ping.sh www.myserver.com &
The script is just the basic only, you can do a lot of customization such as format the log.