生产环境中有HAproxy服务器,虽然HAproxy可以通过配置statas页面监控HAproxy的状态,但还是希望能集中在Zabbix中监控。因为Zabbix并没有自带的HAproxy监控模板,所以只能自定义监控项去监控HAproxy。
zabbix客户端
HAproxy提供了另一种方法——通过socket来显示HAproxy的状态。通过修改配置文件haproxy.cfg开启socket文件并开放权限为666,如下所示。
# vim /etc/haproxy/haproxy.cfg
global
log 127.0.0.1 local0
chroot /var/lib/haproxy
pidfile /var/run/haproxy.pid
maxconn 30000
user haproxy
group haproxy
daemon
tune.bufsize 128000
stats socket /var/run/haproxy/info.sock mode 666 level user
tune.ssl.default-dh-param 2048
...
# systemctl restart Haproxy
除之外,还要为/var/tmp/haproxy_stats.cache.lock、/var/tmp/haproxy_stats.cache开放zabbix用户的rwx权限
setfacl -m u:zabbix:rwx /var/tmp/haproxy_stats.cache.lock
setfacl -m u:zabbix:rwx /var/tmp/haproxy_stats.cache
首先我们看下通过socket文件我们能看到什么信息
echo "show stat"|socat /var/run/haproxy/info.sock stdio
# pxname,svname,qcur,qmax,scur,smax,slim,stot,bin,bout,dreq,dresp,ereq,econ,eresp,wretr,wredis,status,weight,act,bck,chkfail,chkdown,lastchg,downtime,qlimit,pid,iid,sid,throttle,lbtot,tracked,type,rate,rate_lim,rate_max,check_status,check_code,check_duration,hrsp_1xx,hrsp_2xx,hrsp_3xx,hrsp_4xx,hrsp_5xx,hrsp_other,hanafail,req_rate,req_rate_max,req_tot,cli_abrt,srv_abrt,admin_stats,FRONTEND,,,0,1,2000,2918,1779980,133971311,0,0,0,,,,,OPEN,,,,,,,,,1,1,0,,,,0,0,0,1,,,,0,2918,0,0,0,0,,0,1,2918,,,
admin_stats,BACKEND,0,0,0,0,2000,0,1779980,133971311,0,0,,0,0,0,0,UP,0,0,0,,0,176433,0,,1,1,0,,0,,1,0,,0,,,,0,0,0,0,0,0,,,,,0,0,
test,FRONTEND,,,0,2,2000,5402,1267880,26563174,0,0,0,,,,,OPEN,,,,,,,,,1,2,0,,,,0,0,0,3,,,,0,5402,0,0,0,0,,0,3,5402,,,
test,test1,0,0,0,1,,2701,632860,13110551,,0,,0,0,0,0,UP,3,1,0,1,0,176433,0,,1,2,1,,2701,,2,0,,2,L7OK,200,3,0,2701,0,0,0,0,0,,,,0,1,
test,test2,0,0,0,1,,2701,635020,13452623,,0,,0,0,0,0,UP,3,1,0,0,0,176433,0,,1,2,2,,2700,,2,0,,2,L7OK,200,3,0,2701,0,0,0,0,0,,,,0,0,
以下是部分属性的解释
pxname 组名
svname 服务器名
qcur 当前队列
qmax 最大队列
scur当前会话用户
smax最大会话用户
slim会话限制
stot会话总量
bin入口流量
bout出口流量
dreq拒绝的请求
dresp拒绝的响应
ereq错误请求
econ错误链接
eresp错误响应
wretr警告重试次数
wredis
status服务器状态
weight权重
act是否活动Y/N
bck备用
chkfail检查宕机0
chkdown检查宕机0
lastchg最后检查
downtime宕机时间
qlimit队列限制
通过socket的输出,我们可以很清晰地看出每个属性都用" , "隔开了,我们只需要通过awk就能提取出我们想要的数据。
HAproxy中有frontend、backend、server三种角色,我们可以通过socket看到这三种角色的状态信息,可以通过以下脚本获得每个这三种角色的名称
# vim /usr/local/bin/haproxy_discovery.sh
HAPROXY_SOCK="/var/run/haproxy/info.sock"
[ -n "$1" ] && echo $1 | grep -q ^/ && HAPROXY_SOCK="$(echo $1 | tr -d '\040\011\012\015')"
then
HAPROXY_STATS_IP="$1"
QUERYING_METHOD="TCP"
fi
QUERYING_METHOD="${QUERYING_METHOD:-SOCKET}"
query_stats() {
if [[ ${QUERYING_METHOD} == "SOCKET" ]]; then
echo "show stat" | socat ${HAPROXY_SOCK} stdio 2>/dev/null
elif [[ ${QUERYING_METHOD} == "TCP" ]]; then
echo "show stat" | nc ${HAPROXY_STATS_IP//:/ } 2>/dev/null
fi
}
get_stats() {
echo "$(query_stats)" | grep -v "^#"
}
[ -n "$2" ] && shift 1
case $1 in
B*) END="BACKEND" ;;
F*) END="FRONTEND" ;;
S*)
for backend in $(get_stats | grep BACKEND | cut -d, -f1 | uniq); do
for server in $(get_stats | grep "^${backend}," | grep -v BACKEND | cut -d, -f2); do
serverlist="$serverlist,\n"'\t\t{\n\t\t\t"{#BACKEND_NAME}":"'$backend'",\n\t\t\t"{#SERVER_NAME}":"'$server'"}'
done
done
echo -e '{\n\t"data":[\n'${serverlist#,}']}'
exit 0
;;
*) END="FRONTEND" ;;
esac
for frontend in $(get_stats | grep "$END" | cut -d, -f1 | uniq); do
felist="$felist,\n"'\t\t{\n\t\t\t"{#'${END}'_NAME}":"'$frontend'"}'
done
echo -e '{\n\t"data":[\n'${felist#,}']}'
编写提取数据的脚本,以下脚本我只提取了backend和server的部分状态信息
# vim /usr/local/bin/zabbix_haproxy.sh
#!/bin/bash
sock="/var/run/haproxy/info.sock"
case $2 in
#backend入口流量
backendbin)
echo "show stat"|socat $sock stdio| awk -F, -v var=$1 '$1==var && $2=="BACKEND"{print $9}';;
#backend出口流量
backendbout)
echo "show stat"|socat $sock stdio| awk -F, -v var=$1 '$1==var && $2=="BACKEND"{print $10}';;
#backend当前队列
backendqcur)
echo "show stat"|socat $sock stdio| awk -F, -v var=$1 '$1==var && $2=="BACKEND"{print $3}';;
#backend最大当前队列
backendqmax)
echo "show stat"|socat $sock stdio| awk -F, -v var=$1 '$1==var && $2=="BACKEND"{print $4}';;
#backend当前会话用户
backendscur)
echo "show stat"|socat $sock stdio| awk -F, -v var=$1 '$1==var && $2=="BACKEND"{print $5}';;
#backend最大当前会话用户
backendsmax)
echo "show stat"|socat $sock stdio| awk -F, -v var=$1 '$1==var && $2=="BACKEND"{print $6}';;
#backend最大当前会话用户限制
backendslimit)
echo "show stat"|socat $sock stdio| awk -F, -v var=$1 '$1==var && $2=="BACKEND"{print $7}';;
#backend会话总数
backendstotal)
echo "show stat"|socat $sock stdio| awk -F, -v var=$1 '$1==var && $2=="BACKEND"{print $8}';;
#backend状态
backendstatus)
echo "show stat"|socat $sock stdio| awk -F, -v var=$1 '$1==var && $2=="BACKEND"{print $18}';;
#server入口流量
serverbin)
echo "show stat"|socat $sock stdio| awk -F, -v var=$1 '$2==var{print $9}';;
#server出口流量
serverbout)
echo "show stat"|socat $sock stdio| awk -F, -v var=$1 '$2==var{print $10}';;
#server当前队列
serverqcur)
echo "show stat"|socat $sock stdio| awk -F, -v var=$1 '$2==var{print $3}';;
#server最大当前队列
serverqmax)
echo "show stat"|socat $sock stdio| awk -F, -v var=$1 '$2==var{print $4}';;
#server当前会话用户
serverscur)
echo "show stat"|socat $sock stdio| awk -F, -v var=$1 '$2==var{print $5}';;
#server最大当前会话用户
serversmax)
echo "show stat"|socat $sock stdio| awk -F, -v var=$1 '$2==var{print $6}';;
#server最大当前会话用户限制
serverslimit)
echo "show stat"|socat $sock stdio| awk -F, -v var=$1 '$2==var{print $7}';;
#server会话总数
serverstotal)
echo "show stat"|socat $sock stdio| awk -F, -v var=$1 '$2==var{print $8}';;
#server状态
serverstatus)
echo "show stat"|socat $sock stdio| awk -F, -v var=$1 '$2==var{print $18}'
这两个脚本放到/usr/local/bin/下,并赋予执行权限
-rwxr-xr-x. 1 root root 1920 Apr 19 18:14 haproxy_discovery.sh
-rwxr-xr-x. 1 root root 2281 Apr 21 11:28 zabbix_haproxy.sh
在/etc/zabbix/zabbix_agent.d/目录下创建一个自定义key文件userparameter_haproxy.conf,然后重启zabbix-agent服务
# vim /etc/zabbix/zabbix_agentd.d/userparameter_haproxy.conf
...
# HAProxy Frontend, Backend and Server Discovery rules
UserParameter=haproxy.list.discovery[*],/usr/local/bin/haproxy_discovery.sh $1 $2
# support legacy way
UserParameter=haproxy.stat.qcur[*],/usr/local/bin/zabbix_haproxy.sh $1 $2
UserParameter=haproxy.stat.qmax[*],/usr/local/bin/zabbix_haproxy.sh $1 $2
UserParameter=haproxy.stat.scur[*],/usr/local/bin/zabbix_haproxy.sh $1 $2
UserParameter=haproxy.stat.smax[*],/usr/local/bin/zabbix_haproxy.sh $1 $2
UserParameter=haproxy.stat.slim[*],/usr/local/bin/zabbix_haproxy.sh $1 $2
UserParameter=haproxy.stat.bin[*],/usr/local/bin/zabbix_haproxy.sh $1 $2
UserParameter=haproxy.stat.bout[*],/usr/local/bin/zabbix_haproxy.sh $1 $2
UserParameter=haproxy.stat.status[*],/usr/local/bin/zabbix_haproxy.sh $1 $2
UserParameter=haproxy.stat.stotal[*],/usr/local/bin/zabbix_haproxy.sh $1 $2
# systemctl restart zabbix-agent
zabbix服务端
创建完监控模板,在主机上直接调用这个模板就可以了