shell监控脚本-监控网络
注意:请先参考 shell监控脚本-准备工作,监控脚本在 rhel5/6 下测试正常,其它版本的linux 系统请自行测试
#监控网络
-
cat chk_ping.sh
-
#!/bin/bash
-
#
-
#script_name:chk_ping.sh
-
#check network
-
#
-
#last update 20130320 by dongnan
-
#bbs# http://bbs.ywwd.net/
-
#blog# http://dngood.blog.51cto.com
-
#lost
-
# ping -f -c 20 202.013.191.31
-
#PING 202.013.191.31 (202.11.191.31) 56(84) bytes of data.
-
#....................
-
#--- 202.013.191.31 ping statistics ---
-
#20 packets transmitted, 0 received, 100% packet loss, time 236ms
-
#delay
-
# ping -f -c 20 199.192.152.2
-
#PING 199.192.152.2 (199.192.152.2) 56(84) bytes of data.
-
#--- 199.192.152.2 ping statistics ---
-
#20 packets transmitted, 20 received, 0% packet loss, time 227ms
-
#rtt min/avg/max/mdev = 266.758/268.957/272.593/1.787 ms, pipe 20, ipg/ewma 11.959/269.829 ms
-
#
-
#variables
-
ping=/bin/ping
-
delay_limit=100
-
loss_limit=20
-
sh_dir=/root/sh/
-
crondir=${sh_dir}crontab
-
source ${sh_dir}CONFIG
-
user=`id -u`
-
hosts="$ALLHOSTS"
-
#main
-
#root权限
-
if [ "$user" -ne 0 ];then
-
echo "must root !!"
-
exit 1
-
fi
-
#主循环遍历机器
-
for HOST in $hosts ;do
-
flag_ping_file="$crondir/log/$HOST.ping"
-
error_log="$crondir/log/ping_error.log"
-
#true
-
if ! $ping -W 2 -f -c 10 $HOST > $crondir/log/ping.log 2>&1;then
-
grep "packet loss" $crondir/log/ping.log >$crondir/log/ping.loss
-
#返回值0,定义delay 与 loss 变量
-
loss=`awk '{print $6}' $crondir/log/ping.loss | awk -F% '{print $1}'`
-
#loss 大于规定限制,则发送报警邮件
-
if [ "$loss" -ge "$loss_limit" ];then
-
if [ ! -f $flag_ping_file ];then
-
#sms
-
#for mobile in $MOBILES;do
-
#echo "Monitor to $HOST delay $delay'ms;$loss% packet loss" | /usr/local/bin/gammu --sendsms TEXT "$mobile" -unicode
-
#done
-
#mail
-
for mail in $MAILS;do
-
echo "Monitor to $HOST $loss% packet loss" | mail -s "$HOST lost" $mail
-
done
-
#log
-
date +'%F %T' >>$error_log
-
cat $crondir/log/ping.log >>$error_log
-
#生成flag文件
-
echo "$HOST network error" >$flag_ping_file
-
fi
-
fi
-
#false
-
else
-
#loss 小于规定限制,则发送解除报警邮件
-
if [ -f $flag_ping_file ];then
-
#sms
-
#for mobile in $MOBILES;do
-
#echo "$HOST network ok"|/usr/local/bin/gammu --sendsms TEXT "$mobile" -unicode
-
#done
-
#mail
-
for mail in $MAILS;do
-
echo "$HOST network ok" | mail -s "$HOST network ok" $MAILS
-
done
-
#delete flag
-
rm -f $flag_ping_file
-
fi
-
#
-
fi
-
#
-
done
结束
更多请:
linux 系统运维 37275208
vmware 虚拟化 166682360
本文转自 dongnan 51CTO博客,原文链接:http://blog.51cto.com/dngood/1163556