这是一个HAProxy的zabbix监控模板,HAProxy提供高可用性、负载均衡以及基于TCP和HTTP应用的代理,支持虚拟主机,它是免费、快速并且可靠的一种解决方案。HAProxy本身提供一个web页面可以显示haproxy的各种信息,方便管理员查看系统状态等。此次我采用socat(socat是netcat的扩展实现)这款开源软件通过haproxy的socks配合zabbix实现haproxy系统的状态监控。
一、安装socat
[root@haproxy01 ~]# wget http://www.dest-unreach.org/socat/download/socat-1.7.3.0.tar.bz2
[root@haproxy01 ~]# tar xzf socat-1.7.3.0.tar.gz
[root@haproxy01 ~]# cd socat-1.7.3.0
[root@haproxy01 ~]# ./configure
[root@haproxy01 ~]# make
[root@haproxy01 ~]# make install
[root@haproxy01 ~]# which socat ###安装完自动会在/usr/local/bin 创建执行程序。
/usr/local/bin/socat
二、开启haproxy sock
编辑haproxy配置文件在global下添加如下:
global
stats socket /usr/local/haproxy/haproxy.sock
重启haproxy
Haproxy 信息
[root@haproxy01 ~]# echo "show info" |socat /usr/local/haproxy/haproxy.sock stdio
Name: HAProxy
Version: 1.4.24
Release_date: 2013/06/17
Nbproc: 1
Process_num: 1
Pid: 3499
Uptime: 2d 1h00m47s
Uptime_sec: 176447
Memmax_MB: 0
Ulimit-n: 131102
Maxsock: 131102
Maxconn: 65535
Maxpipes: 0
CurrConns: 18
PipesUsed: 0
PipesFree: 0
Tasks: 34
Run_queue: 1
node: HAProxy01
description: haproxy server 01
Haproxy 状态
[root@CMHAProxy01 ~]# echo "show stat" |socat /usr/local/haproxy/haproxy.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,
test,BACKEND,0,0,0,2,2000,5402,1267880,26563174,0,0,,0,0,0,0,UP,6,2,0,,0,176433,0,,1,2,0,,5401,,1,0,,3,,,,0,5402,0,0,0,0,,,,,0,1,
看到这里大家都明白了,我们可以通过“show stat”返回的信息,过滤出我们想要得到的监控字段数据。这里我们主要取qcur(queue数量)、scur(session数量)、bin(入站流量)、bout(出站流量)、status(后端服务器状态)、downtime(宕机总时长)、check_status(健康检测状态),自定义key收集这些数据制作相应的item,如果你想监控更多信息可以取更多的字段数据。
大家看到这些字段数据使用”,”分隔,非常方便我们用awk来过滤数据,具体自定义key见代码。首先,我们需要建立一个HAProxy模版,然后建立发现规则,来发现这些后端服务器,这样每次haproxy添加服务的时候,zabbix就会自动发现这些后端服务器、自动建立item、自动建立Graph、自动建立Trigger,一切全部自动完成。
三、Step by Step
1、建立一个haproxy模版
2、建立发现规则
key shell如下:(这里有一点需要注意,就是haproxy.sock这个文件的权限问题,默认是644,单纯执行这个shell没问题,但如果需要zabbix用户去获取返回值的时候就会提示权限不足,所以我直接将haproxy.sock设置成了666,也可以设置visudo,我为了方便直接修改了这个文件权限)
[root@haproxy01 ~]# cat /usr/bin/has.discovery.sh
!/bin/bash
export PATH=/usr/lib64/qt-3.3/bin:/usr/local/sbin:/usr/local/bin:/sbin:/bin:/usr/sbin:/usr/bin:/root/bin
printf '{\n'
printf '\t"data":[\n'
export LINE=2}'|grep -v "^FRONTEND"|grep -v "^BACKEND"|grep -v "^$"|wc -l) ##(这里我过滤调admin_stats web管理台、FRONTEND前端、BACKEND后端 得到的是所有后端服务器的列表)
export N=0
printf '\t\t{\n'
for i in 2}'|grep -v "^FRONTEND"|grep -v "^BACKEND");
do printf "\t\t\t{"{#SNAME}":"((N -eq $LINE ]; ## 最后一行是不能加","的,所以需要if判断一下是否到了最后一行。
then
printf '\n'
else
printf ',\n'
fi
done;
printf ']}\n'
执行结果如下:(zabbix的发现规则就是要返回json格式的数据)
[root@haproxy01 haproxy]# sh /usr/bin/has.discovery.sh
{
"data":[
{"{#SNAME}":"test1"},
{"{#SNAME}":"test2"},
{"{#SNAME}":"testapi1"},
{"{#SNAME}":"test51"},
{"{#SNAME}":"test52"},
{"{#SNAME}":"testclient1"},
{"{#SNAME}":"testclient2"},
{"{#SNAME}":"testmobile1"},
{"{#SNAME}":"testmobile2"}
]}
3、建立自定义key
- Queue数量[qcur]
key shell如下:
[root@haproxy01 haproxy]# cat /usr/bin/ckey.ha.queue.sh
!/bin/bash
export PATH=/usr/lib64/qt-3.3/bin:/usr/local/sbin:/usr/local/bin:/sbin:/bin:/usr/sbin:/usr/bin:/root/bin
echo "show stat" |socat /usr/local/haproxy/haproxy.sock stdio|grep "<3}' ## 重点就是通过awk取哪个字段的数据
- Session数量[scur]
[root@haproxy01 haproxy]# cat /usr/bin/ckey.ha.session.sh
!/bin/bash
export PATH=/usr/lib64/qt-3.3/bin:/usr/local/sbin:/usr/local/bin:/sbin:/bin:/usr/sbin:/usr/bin:/root/bin
echo "show stat" |socat /usr/local/haproxy/haproxy.sock stdio|grep "<5}'
- 虚拟服务器入站流量[bin]
[root@haproxy01 haproxy]# cat /usr/bin/ckey.ha.bin.sh
!/bin/bash
export PATH=/usr/lib64/qt-3.3/bin:/usr/local/sbin:/usr/local/bin:/sbin:/bin:/usr/sbin:/usr/bin:/root/bin
echo "show stat" |socat /usr/local/haproxy/haproxy.sock stdio|grep "<9}'
- 虚拟服务器出站流量[bout]
[root@haproxy01 haproxy]# cat /usr/bin/ckey.ha.bout.sh
!/bin/bash
export PATH=/usr/lib64/qt-3.3/bin:/usr/local/sbin:/usr/local/bin:/sbin:/bin:/usr/sbin:/usr/bin:/root/bin
echo "show stat" |socat /usr/local/haproxy/haproxy.sock stdio|grep "<10}'
- 后端服务器状态[status]
[root@haproxy01 haproxy]# cat /usr/bin/ckey.ha.status.sh
!/bin/bash
export PATH=/usr/lib64/qt-3.3/bin:/usr/local/sbin:/usr/local/bin:/sbin:/bin:/usr/sbin:/usr/bin:/root/bin
echo "show stat" |socat /usr/local/haproxy/haproxy.sock stdio|grep "<18}'
- 宕机总时长[downtime]
[root@haproxy01 haproxy]# cat /usr/bin/ckey.ha.downtime.sh
!/bin/bash
export PATH=/usr/lib64/qt-3.3/bin:/usr/local/sbin:/usr/local/bin:/sbin:/bin:/usr/sbin:/usr/bin:/root/bin
echo "show stat" |socat /usr/local/haproxy/haproxy.sock stdio|grep "<25}'
- 健康检测状态[check_status]
[root@haproxy01 haproxy]# cat /usr/bin/ckey.ha.lastchk.sh
!/bin/bash
export PATH=/usr/lib64/qt-3.3/bin:/usr/local/sbin:/usr/local/bin:/sbin:/bin:/usr/sbin:/usr/bin:/root/bin
echo "show stat" |socat /usr/local/haproxy/haproxy.sock stdio|grep "<38}'
定义key
[root@haproxy01 haproxy]# cat /etc/zabbix/zabbix_agentd.d/HaProxy_status.conf
UserParameter=has.discovery,/usr/bin/has.discovery.sh
UserParameter=ckey.ha.bin[],/usr/bin/ckey.ha.bin.sh 1
UserParameter=ckey.ha.downtime[],/usr/bin/ckey.ha.downtime.sh 1
UserParameter=ckey.ha.queue[],/usr/bin/ckey.ha.queue.sh 1
UserParameter=ckey.ha.status[],/usr/bin/ckey.ha.status.sh $1
重启zabbix-agent验证一下:
[root@zabbixproxy01 ~]# zabbix_get -s 192.168.1.100 -k has.discovery
{
"data":[
{"{#SNAME}":"test1"},
{"{#SNAME}":"test2"},
{"{#SNAME}":"testapi1"},
{"{#SNAME}":"test51"},
{"{#SNAME}":"test52"},
{"{#SNAME}":"testclient1"},
{"{#SNAME}":"testclient2"},
{"{#SNAME}":"testmobile1"},
{"{#SNAME}":"testmobile2"}
]}
[root@zabbixproxy01 ~]# zabbix_get -s 192.168.1.100 -k ckey.ha.status[test1]
UP
其余的key验证效果大同小异,这里就不浪费篇幅一一列举了。
4、创建Graph prototypes
- 网络流量图
- Session
- 健康检测状态
- 宕机总时间
生成的Graphs效果图
5、创建Trigger prototypes
- inbound网络流量最近5分钟内超过2Mbps
- outbound网络流量最近5分钟内超过2Mbps
- 后端服务器健康检测异常
- 后端服务器宕机
这里设置一下依赖,因为服务器是否宕机依赖于健康检测状态
自定义key shell
http://opensgalaxy.oss-cn-beijing.aliyuncs.com/images/zabbix_haproxy/zabbix_haproxy.tar.gz
导出的haproxy模版
http://opensgalaxy.oss-cn-beijing.aliyuncs.com/images/zabbix_haproxy/zbx_export_templates.xml
作者:ASIH
来源:CSDN
原文:https://blog.csdn.net/hanzheng260561728/article/details/52819253
版权声明:本文为博主原创文章,转载请附上博文链接!