Skip to content


nagios增加监控网卡速率插件check_ethspeed.sh

服务器上线时间长了,网线可能会老化或接触不良导致达不到工作速率.
增加个nagios插件随机监控网卡速率

参阅:linux查看和改变网卡工作速率

cd /opt/nagios/libexec
vi check_ethspeed.sh

  1. #!/bin/bash
  2. #########################################################################
  3. #
  4. # File: check_ethspeed.sh
  5. # Description: Nagios check plugins to check eth speed in *nix.
  6. # Language: GNU Bourne-Again SHell
  7. # Version: 1.0.1
  8. # Date: 2015-1-23
  9. # Author: C1g
  10. # Bog: http://blog.C1gStudio.com
  11. # Note: Allow nagios to run ethtool commands
  12. # visudo
  13. # #Defaults    requiretty
  14. # nagios ALL=NOPASSWD:/sbin/ethtool
  15. #
  16. #########################################################################
  17.  
  18. path=/bin:/sbin:/usr/bin:/usr/sbin:/usr/local/bin:/usr/local/sbin:~/bin
  19. export PATH
  20. STATE_OK=0
  21. STATE_WARNING=1
  22. STATE_CRITICAL=2
  23. STATE_UNKNOWN=3
  24.  
  25. warn_num=100
  26. critical_num=10
  27. eth=eth0
  28.  
  29. usage(){
  30.   echo -e "Usage: $0 -i|--interface interface -w|--warning warning threshold -c|--critical critical threshold"
  31.   echo -e "Example:"
  32.   echo -e "$0 -i eth0 -w 100 -c 10"
  33.  
  34. }
  35. select_arg(){
  36. if [ $# -eq 0 ];then
  37.   return 1
  38. fi
  39. until [ $# -eq 0 ];do
  40.   case $1 in
  41.     -i|--interface)
  42.       [ $# -lt 2 ] && return 1
  43.       if ! cat /var/log/dmesg |grep $2 >/dev/null 2>&1;then
  44.           return 1
  45.       fi
  46.       eth=$2
  47. shift 2
  48.       ;;
  49.     -w|--warning)
  50.       [ $# -lt 2 ] && return 1
  51.       if ! echo $2 |grep -E -q "^[1-9][0-9]*$";then
  52.         return 1
  53.       fi
  54.       warn_num=$2
  55. shift 2
  56.       ;;
  57.     -c|--critical)
  58.       [ $# -lt 2 ] && return 1
  59.       if ! echo $2 |grep -E -q "^[1-9][0-9]*$";then
  60.         return 1
  61.       fi
  62.       critical_num=$2
  63. shift 2
  64.       ;;
  65.     *)
  66. return 1
  67.      ;;
  68.   esac
  69. done
  70. return 0
  71. }
  72.  
  73. select_arg $@
  74. [ $? -ne 0 ] && usage && exit $STATE_UNKNOWN
  75.  
  76. #echo "warn :$warn_num"
  77. #echo "critical :$critical_num"
  78.  
  79. if [ $critical_num -gt $warn_num ];then
  80.     usage
  81.     exit $STATE_UNKNOWN
  82. fi
  83.  
  84. #ethtool $eth| grep Speed | grep -o  '[0-9]\+'
  85. #kernel >=2.6.33
  86. #cat /sys/class/net/$eth/speed
  87. total=`sudo /sbin/ethtool $eth |grep Speed:|awk '{print $2}' |awk -F 'Mb' '{print $1}'`
  88. if [ $total = Unknown! ];then
  89.     echo "UNKNOWN STATE $eth maybe not working!"
  90.     exit $STATE_UNKNOWN
  91. elif [ $total -gt $warn_num ];then
  92.     echo "$eth OK - Speed: $total Mb/s |$eth=$total;$warn_num;$critical_num;0"
  93.     exit $STATE_OK
  94. elif [ $total -le $warn_num -a $total -gt $critical_num ];then
  95.     echo "$eth WARNING - Speed: $total Mb/s |$eth=$total;$warn_num;$critical_num;0"
  96.     exit $STATE_WARNING
  97. elif [ $total -le $critical_num ];then
  98.     echo "$eth CRITICAL - Speed: $total Mb/s |$eth=$total;$warn_num;$critical_num;0"
  99.     exit $STATE_CRITICAL
  100. else
  101.     echo "UNKNOWN STATE"
  102.     exit $STATE_UNKNOWN
  103. fi

下载check_ethspeed.sh

chown nagios:nagios check_ethspeed.sh
chmod 775 check_ethspeed.sh

运行ethtool命令需root权限
visudo

  1. Defaults    requiretty

注释这一行

添加nagios用户无需密码运行ethtool权限

  1. nagios  ALL=NOPASSWD:/sbin/ethtool

客户端nrpe增加监控命令
echo ‘command[check_ethspeed2]=/opt/nagios/libexec/check_ethspeed.sh -i eth2 -w 100 -c 10’ >> /opt/nagios/etc/nrpe.cfg

重启nrpe
kill `cat /var/run/nrpe.pid`
/opt/nagios/bin/nrpe -c /opt/nagios/etc/nrpe.cfg -d

监控端增加监控服务
vi c1gstudio.cfg

  1. define service{
  2.         use                             local-service,srv-pnp         ; Name of service template to use
  3.         host_name                       c1gstudio
  4.         service_description             check_ethspeed eth2
  5.         check_command                   check_ethspeed!eth2!100!10
  6. notifications_enabled 0
  7.         }

重启nagios
/etc/init.d/nagios reload

check_ethspeed

参阅:http://blog.c1gstudio.com/archives/1748

Posted in Nagios.

Tagged with , , .


No Responses (yet)

Stay in touch with the conversation, subscribe to the RSS feed for comments on this post.



Some HTML is OK

or, reply to this post via trackback.