#!/bin/sh
####################
#服务器ping测试程序
#James Qi 2013
#循环检测各台服务器,在指定的ping次数中丢失的必须小于某个设定值,如果大于则说明有问题,发邮件通知报警
#在/etc/crontab中设定本程序的执行周期
####################
#
#参数设置
COUNT=100 #每台服务器测试ping的次数
MAX=90 #其中最多无法ping通的次数
HOST=(#服务器列表,IP地址或者域名
.140.x.x
.140.xx.xx
.140.xxx.xxx
falcon000.startdedicated.com
eagle000.startdedicated.com
hawk000.startdedicated.com
)
#循环检测
for ipadd in "${HOST[@]}"
do
timing=date +%Y/%m/%d/%H:%M:%S
ping $ipadd -c $COUNT > 1ping.log
losspag=grep "packet loss" 1ping.log |awk '{print $6}' |sed 's/%//g'
if [ $losspag -ge $MAX ] ;
then
echo $timing > tmp.log
echo -n $ipadd >> tmp.log
echo -n "packet loss is more than $MAX of $COUNT">> tmp.log
cat /root/tmp.log | mail -s "$ipadd ping packet loss is more then $MAX of $COUNT" [email protected]
else
rm -f tmp.log
fi
done

在/etc/crontab中设置:

/6 * root sh /root/checkping.sh &

也就是每6分钟执行checkping.sh一次

#!/bin/sh
COUNT=100
MAX=90
HOST=(
192.168.0.128
)
for ipadd in "${HOST[@]}"
do
timing=date +%Y/%m/%d/%H:%M:%S
ping $ipadd -c $COUNT > 1ping.log
losspag=grep "packet loss" 1ping.log |awk '{print $6}' |sed 's/%//g'
if [ $losspag -ge $MAX ] ;
then
echo $timing > tmp.log
echo -n $ipadd >> tmp.log
echo -n "packet loss is more than $MAX of $COUNT">> tmp.log
cat /root/tmp.log | mail -s "$ipadd ping packet loss is more then $MAX of $COUNT" [email protected]
else
rm -f tmp.log
fi
done

更多资讯,请关注微信公众号 Big Data课堂

Linux系统检查服务器ping值脚本_第1张图片