4、添加插件至nagios
(1)、修改commands.cfg,添加
##haproxy
define command{
command_name check_haproxy
command_line /opt/nagios/libexec/check_haproxy.sh -s $ARG1$ -m $ARG2$ -w $ARG3$ -C $ARG4$
}
(2)、修改服务配置文件,我是在本机监控故只改localhost.cfg,添加以下监控
##haproxy check
define service{
use local-service ; Name of service template to use
host_name localhost
service_description haproxy-Session-cur
check_command check_haproxy!/var/lib/haproxy/stats!rate!10!20
notifications_enabled 0
}
define service{
use local-service ; Name of service template to use
host_name localhost
service_description haproxy-Errors-req
check_command check_haproxy!/var/lib/haproxy/stats!ereq!3!5
notifications_enabled 0
}
define service{
use local-service ; Name of service template to use
host_name localhost
service_description haproxy-Errors-con
check_command check_haproxy!/var/lib/haproxy/stats!econ!3!5
notifications_enabled 0
}
define service{
use local-service ; Name of service template to use
host_name localhost
service_description haproxy-Errors-resp
check_command check_haproxy!/var/lib/haproxy/stats!eresp!3!5
notifications_enabled 0
}
(3)、添加进nrpe.cfg,这些报警阈值是为了方便测试设得比较小,根据实际应用更改
command[check_haproxy]=/usr/bin/sudo /opt/nagios/libexec/check_haproxy.sh -s /var/lib/haproxy/stats -m rate --warning 10 --critical 20
command[check_haproxy]=/usr/bin/sudo /opt/nagios/libexec/check_haproxy.sh -s /var/lib/haproxy/stats -m ereq --warning 3 --critical 5
command[check_haproxy]=/usr/bin/sudo /opt/nagios/libexec/check_haproxy.sh -s /var/lib/haproxy/stats -m econ --warning 3 --critical 5
command[check_haproxy]=/usr/bin/sudo /opt/nagios/libexec/check_haproxy.sh -s /var/lib/haproxy/stats -m eresp --warning 3 --critical 5
重启nagios服务,登陆web界面,已经可以看到监控项了:

现在来测试下是否能够正常报警,使用siege测试:
./bin/siege -c 50 -n 50 -t 30 http://192.168.1.250/stat.php
,等几分钟看界面,已经有报警了:

完毕
附:通过curl访问haproxy状态页面来获取数据脚本:
#########################################################################
# File Name: check_haproxy-url.sh
# Author: jc
# Created Time: 2014年03月01日 星期六 17时27分03秒
#########################################################################
#!/bin/bash
ST_OK=0
ST_WR=1
ST_CR=2
ST_UK=3
url_check=1
print_help() {
echo " --url|-u)"
echo " haproxy监控页面的链接"
echo " -m/--mode)"
echo " haproxy的状态项,如:rate,ereq,econ,eresp,status"
echo " -n/--no-url-check|)"
echo " 是否检查haproxy的url:1检测(默认),0不检查"
exit $ST_UK
}
while test -n "$1"; do
case "$1" in
-help|-h)
print_help
exit $ST_UK
;;
--mode|-m)
mode=$2
shift
;;
--no-url-check|-n)
url_check=0
;;
--url|-u)
hap_url=$2
shift
;;
--warning|-w)
warning=$2
shift
;;
--critical|-c)
critical=$2
shift
;;
*)
echo "Unknown argument: $1"
print_help
exit $ST_UK
;;
esac
shift
done
#check_url() {
#curl -o /tmp/stats.csv $url >/dev/null 2>&1
#if [ -s stats.csv ]
#then
#con=1
#else
#con=2
#fi
#}
curl -o /tmp/stats.csv $hap_url >/dev/null 2>&1
ch_val() {
if [ -z $val ]
then
val=0
else
val=$val
fi
}
get_val() {
case $mode in
rate)
val=`cat /tmp/stats.csv | grep web1 | awk -F '<tr class=' '{print $3}' | awk -F '</td><td>' '{print $5}'`
ch_val
;;
ereq)
val=`cat /tmp/stats.csv | grep web1 | awk -F '<tr class=' '{print $3}' | awk -F '</td><td>' '{print $16}'`
ch_val
;;
econ)
val=`cat /tmp/stats.csv | grep web1 | awk -F '<tr class=' '{print $3}' | awk -F '</td><td>' '{print $17}'| awk -F '<' '{print $1}'`
ch_val
;;
eresp)
val=`cat /tmp/stats.csv | grep web1 | awk -F '<tr class=' '{print $3}' | awk -F '</td><td>' '{print $18}'`
ch_val
;;
# status)
# val=`cat /tmp/stats.csv | grep web1 | awk -F '<tr class=' '{print $3}' | awk -F '</td><td>' '{print $21}'`
# ch_val
# ;;
*)
echo "mode输入错误,请重新输入"
;;
esac
}
out_pr() {
output="haproxy is running. $mode: $val"
}
get_stat() {
if [ $val -ge $warning ] && [ $val -lt $critical ]
then
echo "WARNING - ${output}"
exit $ST_WR
elif [ "$val" -ge "$critical" ]
then
echo "CRITICAL - ${output}"
exit $ST_CR
else
echo "OK - ${output}"
exit $ST_OK
fi
}
#if [ $url_check -eq 1 ]
#then
# check_url
# if [ "$con" -eq 2 ]
# then
# echo "ERR!!无法访问haproxy的状态页面,请确认url输入正确!"
# exit $ST_CR
# fi
#fi
get_val
out_pr
get_stat
Nagios 的详细介绍:请点这里
Nagios 的下载地址:请点这里
相关阅读:
网络监控器Nagios全攻略 http://www.linuxidc.com/Linux/2013-07/87067.htm
Nagios搭建与配置详解 http://www.linuxidc.com/Linux/2013-05/84848.htm
Nginx环境下构建Nagios监控平台 http://www.linuxidc.com/Linux/2011-07/38112.htm
在RHEL5.3上配置基本的Nagios系统(使用Nagios-3.1.2) http://www.linuxidc.com/Linux/2011-07/38129.htm
CentOS 5.5+Nginx+Nagios监控端和被控端安装配置指南 http://www.linuxidc.com/Linux/2011-09/44018.htm
Ubuntu 13.10 Server 安装 Nagios Core 网络监控运用 http://www.linuxidc.com/Linux/2013-11/93047.htm
