shell监控脚本-监控网络

shell监控脚本-监控网络

注意:请先参考 shell监控脚本-准备工作,监控脚本rhel5/6 下测试正常,其它版本的linux 系统请自行测试
#监控网络

  
  
  
  
  1. cat chk_ping.sh

  2. #!/bin/bash

  3. #

  4. #script_name:chk_ping.sh

  5. #check network

  6. #

  7. #last update 20130320 by dongnan

  8. #bbs# http://bbs.ywwd.net/

  9. #blog# http://dngood.blog.51cto.com

  10. #lost

  11. # ping -f -c 20  202.013.191.31

  12. #PING 202.013.191.31 (202.11.191.31) 56(84) bytes of data.

  13. #....................

  14. #--- 202.013.191.31 ping statistics ---

  15. #20 packets transmitted, 0 received, 100% packet loss, time 236ms

  16. #delay

  17. # ping -f -c 20 199.192.152.2

  18. #PING 199.192.152.2 (199.192.152.2) 56(84) bytes of data.                    

  19. #--- 199.192.152.2 ping statistics ---

  20. #20 packets transmitted, 20 received, 0% packet loss, time 227ms

  21. #rtt min/avg/max/mdev = 266.758/268.957/272.593/1.787 ms, pipe 20, ipg/ewma 11.959/269.829 ms

  22. #

  23. #variables

  24. ping=/bin/ping

  25. delay_limit=100

  26. loss_limit=20

  27. sh_dir=/root/sh/

  28. crondir=${sh_dir}crontab

  29. source ${sh_dir}CONFIG

  30. user=`id -u`

  31. hosts="$ALLHOSTS"

  32. #main

  33. #root权限

  34. if [ "$user" -ne 0 ];then

  35.    echo "must root !!"

  36.    exit 1

  37. fi

  38. #主循环遍历机器

  39. for HOST in $hosts ;do

  40. flag_ping_file="$crondir/log/$HOST.ping"

  41. error_log="$crondir/log/ping_error.log"

  42. #true

  43. if ! $ping -W 2 -f -c 10 $HOST > $crondir/log/ping.log 2>&1;then

  44.    grep "packet loss" $crondir/log/ping.log >$crondir/log/ping.loss

  45.    #返回值0,定义delay 与 loss 变量

  46. loss=`awk '{print $6}' $crondir/log/ping.loss | awk -F% '{print $1}'`

  47.    #loss 大于规定限制,则发送报警邮件

  48.    if [ "$loss" -ge "$loss_limit" ];then

  49.        if [ ! -f $flag_ping_file ];then

  50.            #sms

  51.            #for mobile in $MOBILES;do

  52.                #echo "Monitor to $HOST delay $delay'ms;$loss% packet loss" | /usr/local/bin/gammu --sendsms TEXT "$mobile" -unicode

  53.            #done

  54.            #mail

  55.            for mail in $MAILS;do

  56.                echo "Monitor to $HOST $loss% packet loss" | mail -s "$HOST lost" $mail

  57.            done

  58.            #log

  59.            date +'%F %T' >>$error_log

  60.            cat $crondir/log/ping.log >>$error_log

  61.            #生成flag文件

  62.            echo "$HOST network error" >$flag_ping_file

  63.        fi

  64.    fi

  65. #false  

  66. else

  67.    #loss 小于规定限制,则发送解除报警邮件

  68.    if [ -f $flag_ping_file ];then

  69.        #sms

  70.        #for mobile in $MOBILES;do

  71.            #echo "$HOST network ok"|/usr/local/bin/gammu --sendsms TEXT "$mobile" -unicode

  72.        #done

  73.        #mail

  74.        for mail in $MAILS;do

  75.            echo "$HOST network ok" | mail -s "$HOST network ok" $MAILS

  76.        done

  77.        #delete flag

  78.        rm -f $flag_ping_file

  79.    fi

  80. #  

  81. fi

  82. #

  83. done

结束
更多请:  
linux 系统运维  37275208  
vmware 虚拟化  166682360

你可能感兴趣的:(网络,shell,脚本,ping,监控)