Skip to content


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

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

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

cd /opt/nagios/libexec
vi check_ethspeed.sh

#!/bin/bash
#########################################################################
#
# File: check_ethspeed.sh
# Description: Nagios check plugins to check eth speed in *nix.
# Language: GNU Bourne-Again SHell
# Version: 1.0.1
# Date: 2015-1-23
# Author: C1g
# Bog: http://blog.C1gStudio.com
# Note: Allow nagios to run ethtool commands
# visudo
# #Defaults requiretty
# nagios ALL=NOPASSWD:/sbin/ethtool
#
#########################################################################

path=/bin:/sbin:/usr/bin:/usr/sbin:/usr/local/bin:/usr/local/sbin:~/bin
export PATH
STATE_OK=0
STATE_WARNING=1
STATE_CRITICAL=2
STATE_UNKNOWN=3

warn_num=100
critical_num=10
eth=eth0

usage(){
echo -e “Usage: $0 -i|–interface interface -w|–warning warning threshold -c|–critical critical threshold”
echo -e “Example:”
echo -e “$0 -i eth0 -w 100 -c 10”

}
select_arg(){
if [ $# -eq 0 ];then
return 1
fi
until [ $# -eq 0 ];do
case $1 in
-i|–interface)
[ $# -lt 2 ] && return 1
if ! cat /var/log/dmesg |grep $2 >/dev/null 2>&1;then
return 1
fi
eth=$2
shift 2
;;
-w|–warning)
[ $# -lt 2 ] && return 1
if ! echo $2 |grep -E -q “^[1-9][0-9]*$”;then
return 1
fi
warn_num=$2
shift 2
;;
-c|–critical)
[ $# -lt 2 ] && return 1
if ! echo $2 |grep -E -q “^[1-9][0-9]*$”;then
return 1
fi
critical_num=$2
shift 2
;;
*)
return 1
;;
esac
done
return 0
}

select_arg $@
[ $? -ne 0 ] && usage && exit $STATE_UNKNOWN

#echo “warn :$warn_num”
#echo “critical :$critical_num”

if [ $critical_num -gt $warn_num ];then
usage
exit $STATE_UNKNOWN
fi

#ethtool $eth| grep Speed | grep -o ‘[0-9]\+’
#kernel >=2.6.33
#cat /sys/class/net/$eth/speed
total=`sudo /sbin/ethtool $eth |grep Speed:|awk ‘{print $2}’ |awk -F ‘Mb’ ‘{print $1}’`
if [ $total = Unknown! ];then
echo “UNKNOWN STATE $eth maybe not working!”
exit $STATE_UNKNOWN
elif [ $total -gt $warn_num ];then
echo “$eth OK – Speed: $total Mb/s |$eth=$total;$warn_num;$critical_num;0”
exit $STATE_OK
elif [ $total -le $warn_num -a $total -gt $critical_num ];then
echo “$eth WARNING – Speed: $total Mb/s |$eth=$total;$warn_num;$critical_num;0”
exit $STATE_WARNING
elif [ $total -le $critical_num ];then
echo “$eth CRITICAL – Speed: $total Mb/s |$eth=$total;$warn_num;$critical_num;0”
exit $STATE_CRITICAL
else
echo “UNKNOWN STATE”
exit $STATE_UNKNOWN
fi

下载check_ethspeed.sh

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

运行ethtool命令需root权限
visudo

Defaults requiretty

注释这一行

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

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

define service{
use local-service,srv-pnp ; Name of service template to use
host_name c1gstudio
service_description check_ethspeed eth2
check_command check_ethspeed!eth2!100!10
notifications_enabled 0
}

重启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.