2 编写shell脚本实现lvs监控
2.1 监控脚本
Nagios里面没有现成的监控lvs的状态脚本,所以须要去网上找一个简单的监控脚本check_lvs.sh,copy到/usr/lib/nagios/plugins/文件夹,赋予nagios权限,脚本内容例如以下:
#!/bin/bash
# http://www.ohlinux.com/archives/632/
# add by tim on 20140613
USAGE_Method=\"$(basename $0)[-h|--hostname] <Free ip or hostname> [-w|--warning] <Free integer> [-c|--critical] <Free integer>\"
USAGE_Value=\"warning value must be small than critical value: `basename $0` $*\"
STATE_OK=0
STATE_WARNING=1
STATE_CRITICAL=2
STATE_UNKNOWN=3
if [ $# -lt 4 ];then
echo
echo \"Usage: $USAGE_Method\"
echo
exit 0
fi
while [ $# -gt 0 ];
do
case \"$1\" in
-w|--warning)
shift
warning=$1
;;
-c|--critical)
shift
critical=$1
;;
esac
shift
done
if [[ $warning == $critical || $warning -gt $critical ]]
then
#echo $warning
#echo $critical
echo \"$USAGE_Value\"
echo \"Usage: $USAGE_Method\"
exit 0
fi
ACT_COUNT=0
Inactive_count=0
stat1=`sudo ipvsadm | grep http | grep Route|wc -l`
if [ $stat1 -ne 0 ];then
for NUM in `sudo ipvsadm | grep http | grep Route | awk \'{print $5}\'`
do
ACT_COUNT=$(($ACT_COUNT+ $NUM))
done
for NUM in `sudo ipvsadm | grep http | grep Route | awk \'{print $6}\'`
do
Inactive_count=$(($Inactive_count+ $NUM))
done
else
echo \" stat1:$stat1, lvs critical,lvs is down now.\"
exit 3
fi
if [[ \"$ACT_COUNT\" -gt \"$critical\" ]]
then
echo \"critical - lvs connetion is : $ACT_COUNT active\"
exit 2
fi
if [[ \"$ACT_COUNT\" -gt \"$warning\" && \"$ACT_COUNT\" -lt \"$critical\" ]]
then
echo \"warning - lvs connetions is : $ACT_COUNT active\"
exit 1
fi
if [[ \"$ACT_COUNT\" -lt \"$warning\" || $ACT_COUNT == 0 ]]
then
echo \"LVS OK - LVS is running (conn: $ACT_COUNT active, $Inactive_count inactive)|active=$ACT_COUNT;69999;99999;0; inactive=$Inactive_count;69999;99999;0;\"
exit 0
fi
2.2 nrpe.cfg里面配置例如以下
Vim /etc/nagios/nrpe.cfg,在里面加入一行check_lvs命令:
command[check_lvs]=/usr/lib/nagios/plugins/check_lvs -w 300 -c 600 之后重新启动nrpe
[iyunv@/root/nagios/check_lvs ~]# service nrpe restart;
Shutting down nrpe: [确定]
Starting nrpe: [确定]
[iyunv@/root/nagios/check_lvs ~]#service nrpe restart;