zabbix添加对haproxy的监控
HAProxy提供高可用性、负载均衡以及基于TCP和HTTP应用的代理,支持虚拟主机,它是免费、快速并且可靠的一种解决方案。HAProxy本身提供一个web页面可以显示haproxy的各种信息,方便管理员查看系统状态等。此次我采用socat(socat是netcat的扩展实现)这款开源软件通过haproxy的socks配合zabbix实现haproxy系统的状态监控。
一、安装socat
wget http://www.dest-unreach.org/socat/download/socat-1.7.3.0.tar.gztar xzf socat-1.7.3.0.tar.gzcd socat-1.7.3.0./configuremake && make installwhich socat #安装完自动会在/usr/local/bin 创建执行程序/usr/local/bin/socat二、开启haproxy sock
编辑haproxy配置文件在global下添加如下:global stats socket /tmp/haproxy.sock mode 600 level admin# 重启haproxy
/etc/init.d/haproxy restartHaproxy 信息
[root@sdtw02 socat-1.7.3.0]# echo "show info" |socat /tmp/haproxy.sock stdio
Name: HAProxyVersion: 1.5.19Release_date: 2016/12/25Nbproc: 1Process_num: 1Pid: 20816Uptime: 1d 21h14m40sUptime_sec: 162880Memmax_MB: 0Ulimit-n: 400034Maxsock: 400034Maxconn: 200000Hard_maxconn: 200000CurrConns: 0CumConns: 45CumReq: 45MaxSslConns: 0CurrSslConns: 0CumSslConns: 0Maxpipes: 0PipesUsed: 0PipesFree: 0ConnRate: 0ConnRateLimit: 0MaxConnRate: 2SessRate: 0SessRateLimit: 0MaxSessRate: 2SslRate: 0SslRateLimit: 0MaxSslRate: 0SslFrontendKeyRate: 0SslFrontendMaxKeyRate: 0SslFrontendSessionReuse_pct: 0SslBackendKeyRate: 0SslBackendMaxKeyRate: 0SslCacheLookups: 0SslCacheMisses: 0CompressBpsIn: 0CompressBpsOut: 0CompressBpsRateLim: 0ZlibMemUsage: 0MaxZlibMemUsage: 0Tasks: 7Run_queue: 1Idle_pct: 100node: 210-61-161-118.hinet-ip.hinet.netdescription:[root@sdtw02 socat-1.7.3.0]# echo "show stat" |socat /tmp/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,comp_in,comp_out,comp_byp,comp_rsp,lastsess,last_chk,last_agt,qtime,ctime,rtime,ttime,admin_stats,FRONTEND,,,0,1,10,13,619,2481,0,0,11,,,,,OPEN,,,,,,,,,1,2,0,,,,0,0,0,2,,,,0,0,0,11,2,0,,0,2,13,,,0,0,0,0,,,,,,,,admin_stats,BACKEND,0,0,0,1,1,2,619,2481,0,0,,2,0,0,0,UP,0,0,0,,0,163002,0,,1,2,0,,0,,1,0,,1,,,,0,0,0,0,2,0,,,,,0,0,0,0,0,0,-1,,,0,0,0,0,im_access_6666,FRONTEND,,,0,1,200000,31,6617,8443,0,0,0,,,,,OPEN,,,,,,,,,1,3,0,,,,0,0,0,1,,,,,,,,,,,0,0,0,,,0,0,0,0,,,,,,,,im_access_6666,im01,0,0,0,1,100000,16,3473,4235,,0,,0,0,0,0,UP,1,1,0,0,0,163002,0,,1,3,1,,16,,2,0,,1,L4OK,,0,,,,,,,0,,,,2,0,,,,,4285,,,0,1,0,3116,im_access_6666,im02,0,0,0,1,100000,15,3144,4208,,0,,0,0,0,0,UP,1,1,0,0,0,163002,0,,1,3,2,,15,,2,0,,1,L4OK,,0,,,,,,,0,,,,0,0,,,,,4885,,,0,1,0,3778,im_access_6666,BACKEND,0,0,0,1,20000,31,6617,8443,0,0,,0,0,0,0,UP,2,2,0,,0,163002,0,,1,3,0,,31,,1,0,,1,,,,,,,,,,,,,,2,0,0,0,0,0,4285,,,0,1,0,6804,看到这里大家都明白了,我们可以通过“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、添加.sh文件到/usr/bin目录下
# cd /usr/bin# cat ckey.ha.bin.sh #!/bin/bashexport PATH=/usr/lib64/qt-3.3/bin:/usr/local/sbin:/usr/local/bin:/sbin:/bin:/usr/sbin:/usr/bin:/root/binecho "show stat" |socat /tmp/haproxy.sock stdio|grep "\<$1\>"|grep -v "^FRONTEND"|grep -v "^BACKEND"|awk -F "," '{print $9}'
# cat ckey.ha.bout.sh
#!/bin/bashexport PATH=/usr/lib64/qt-3.3/bin:/usr/local/sbin:/usr/local/bin:/sbin:/bin:/usr/sbin:/usr/bin:/root/binecho "show stat" |socat /tmp/haproxy.sock stdio|grep "\<$1\>"|grep -v "^FRONTEND"|grep -v "^BACKEND"|awk -F "," '{print $10}'
# cat ckey.ha.downtime.sh
#!/bin/bashexport PATH=/usr/lib64/qt-3.3/bin:/usr/local/sbin:/usr/local/bin:/sbin:/bin:/usr/sbin:/usr/bin:/root/binecho "show stat" |socat /tmp/haproxy.sock stdio|grep "\<$1\>"|grep -v "^FRONTEND"|grep -v "^BACKEND"|awk -F "," '{print $25}'
# cat ckey.ha.lastchk.sh
#!/bin/bashexport PATH=/usr/lib64/qt-3.3/bin:/usr/local/sbin:/usr/local/bin:/sbin:/bin:/usr/sbin:/usr/bin:/root/binecho "show stat" |socat /tmp/haproxy.sock stdio|grep "\<$1\>"|grep -v "^FRONTEND"|grep -v "^BACKEND"|awk -F "," '{print $38}'
# cat ckey.ha.queue.sh
#!/bin/bashexport PATH=/usr/lib64/qt-3.3/bin:/usr/local/sbin:/usr/local/bin:/sbin:/bin:/usr/sbin:/usr/bin:/root/binecho "show stat" |socat /tmp/haproxy.sock stdio|grep "\<$1\>"|grep -v "^FRONTEND"|grep -v "^BACKEND"|awk -F "," '{print $3}'
# cat ckey.ha.session.sh
#!/bin/bashexport PATH=/usr/lib64/qt-3.3/bin:/usr/local/sbin:/usr/local/bin:/sbin:/bin:/usr/sbin:/usr/bin:/root/binecho "show stat" |socat /tmp/haproxy.sock stdio|grep "\<$1\>"|grep -v "^FRONTEND"|grep -v "^BACKEND"|awk -F "," '{print $5}'
# cat ckey.ha.status.sh
#!/bin/bashexport PATH=/usr/lib64/qt-3.3/bin:/usr/local/sbin:/usr/local/bin:/sbin:/bin:/usr/sbin:/usr/bin:/root/binecho "show stat" |socat /tmp/haproxy.sock stdio|grep "\<$1\>"|grep -v "^FRONTEND"|grep -v "^BACKEND"|awk -F "," '{print $18}'
# 建立发现规则
key shell如下:(这里有一点需要注意,就是haproxy.sock这个文件的权限问题,默认是644,单纯执行这个shell没问题,但如果需要zabbix用户去获取返回值的时候就会提示权限不足,所以我直接将haproxy.sock设置成了666,也可以设置visudo,我为了方便直接修改了这个文件权限)# cat 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/binprintf '{\n'printf '\t"data":[\n'export LINE=$(echo "show stat" |socat /tmp/haproxy.sock stdio|grep -v "^#"|grep -v "^admin_stats"|awk -F "," '{print $2}'|grep -v "^FRONTEND"|grep -v "^BACKEND"|grep -v "^$"|wc -l)export N=0#printf '\t\t{\n'for i in $(echo "show stat" |socat /tmp/haproxy.sock stdio|grep -v "^#"|grep -v "^admin_stats"|awk -F "," '{print $2}'|grep -v "^FRONTEND"|grep -v "^BACKEND"); do printf "\t\t\t{\"{#SNAME}\":\"$i\"}"; N=$(($N+1)) if [ $N -eq $LINE ]; then printf '\n' else printf ',\n' fi done;printf ']}\n' 3、定义key配置文件[root@sdtw02 src]# vim /etc/zabbix/zabbix_agentd.d/HaProxy_status.confUserParameter=has.discovery,/usr/bin/has.discovery.shUserParameter=ckey.ha.bin[*],/usr/bin/ckey.ha.bin.sh $1UserParameter=ckey.ha.bout[*],/usr/bin/ckey.ha.bout.sh $1UserParameter=ckey.ha.downtime[*],/usr/bin/ckey.ha.downtime.sh $1UserParameter=ckey.ha.lastchk[*],/usr/bin/ckey.ha.lastchk.sh $1UserParameter=ckey.ha.queue[*],/usr/bin/ckey.ha.queue.sh $1UserParameter=ckey.ha.session[*],/usr/bin/ckey.ha.session.sh $1UserParameter=ckey.ha.status[*],/usr/bin/ckey.ha.status.sh $1重启zabbix客户端
# /etc/init.d/zabbix-agent restart4.在web上操作主机添加模板
在zabbix-server端测试:[root@u04zbx01 ~]# zabbix_get -s 1.1.1.1 -p 10050-k has.discovery{ "data":[2017/11/17 17:06:38 socat[8231] E open("/usr/local/haproxy/haproxy.sock", 02002, 0666): Permission denied2017/11/17 17:06:38 socat[8241] E open("/usr/local/haproxy/haproxy.sock", 02002, 0666): Permission denied]}修改权限即可
[root@sdtw02 src]# chmod 666 /tmp/haproxy.sock[root@u04zbx01 ~]# zabbix_get -s 1.1.1.1 -p 10050 -k has.discovery
{ "data":[ {"{#SNAME}":"im01"}, {"{#SNAME}":"im02"}]}sed -i "s#/usr/local/haproxy/haproxy.sock#/tmp/haproxy.sock#g" *.sh
监控模板zbx_haproxy_templates.xml
3.0 2017-11-17T06:48:38Z Templates Template HaProxy Service Template HaProxy Service Templates NetWork Status ha.vserver.discovery 0 has.discovery 1800 0 0 0 0 0 0 30 ha.vserver.bps.in $1 0 1 ckey.ha.bin[{#SNAME}] 60 90 365 0 0 bps 1 0 0 0 6 0 0 0 NetWork ha.vserver.bps.out $1 0 1 ckey.ha.bout[{#SNAME}] 60 90 365 0 0 bps 1 0 0 0 8 0 0 0 NetWork ha.vserver.downtime $1 0 0 ckey.ha.downtime[{#SNAME}] 60 90 365 0 3 s 0 0 0 0 1 0 0 0 Status ha.vserver.lastchk $1 0 0 ckey.ha.lastchk[{#SNAME}] 60 90 365 0 3 0 0 0 0 1 0 0 0 Status HTTP response status code ha.vserver.queue $1 0 0 ckey.ha.queue[{#SNAME}] 60 90 365 0 0 0 0 0 0 1 0 0 0 NetWork ha.vserver.session $1 0 0 ckey.ha.session[{#SNAME}] 60 90 365 0 3 0 0 0 0 1 0 0 0 NetWork ha.vserver.status $1 0 0 ckey.ha.status[{#SNAME}] 60 90 0 0 4 0 0 0 0 1 0 0 0 Status {Template HaProxy Service:ckey.ha.bin[{#SNAME}].max(#5)}>2000000 Ha.Vserver {#SNAME} Inbound Flyde over 2Mbps 0 2 0 {Template HaProxy Service:ckey.ha.lastchk[{#SNAME}].last(,2m)}<>200 Ha.Vserver {#SNAME} is Check Failed 0 4 0 {Template HaProxy Service:ckey.ha.status[{#SNAME}].strlen(,2m)}<>2 Ha.Vserver {#SNAME} is Down 0 4 0 Ha.Vserver {#SNAME} is Check Failed {Template HaProxy Service:ckey.ha.lastchk[{#SNAME}].last(,2m)}<>200 {Template HaProxy Service:ckey.ha.bout[{#SNAME}].max(#5)}>2000000 Ha.Vserver {#SNAME} Outbound Flyde over 2Mbps 0 2 0 Ha.Vserver.DownTime {#SNAME} 900 200 0.0000 100.0000 1 1 0 1 0 0.0000 0.0000 0 0 0 0 0 0 009999 0 7 0 Template HaProxy Service ckey.ha.downtime[{#SNAME}] Ha.Vserver.LastCheck {#SNAME} 900 200 0.0000 100.0000 1 1 0 1 0 0.0000 0.0000 0 0 0 0 0 0 00DD00 0 2 0 Template HaProxy Service ckey.ha.lastchk[{#SNAME}] Ha.Vserver.NetWrok Flow {#SNAME} 900 200 0.0000 100.0000 1 1 0 1 0 0.0000 0.0000 0 0 0 0 0 0 1A7C11 0 7 0 Template HaProxy Service ckey.ha.bin[{#SNAME}] 1 0 F63100 0 7 0 Template HaProxy Service ckey.ha.bout[{#SNAME}] Ha.Vserver.NetWrok Session_Queue {#SNAME} 900 200 0.0000 100.0000 1 1 0 1 0 0.0000 0.0000 0 0 0 0 0 0 BB00BB 0 2 0 Template HaProxy Service ckey.ha.queue[{#SNAME}] 1 0 0000CC 0 2 0 Template HaProxy Service ckey.ha.session[{#SNAME}] HTTP response status code 100 Continue 101 Switching Protocols 102 Processing 200 OK 201 Created 202 Accepted 203 Non-Authoritative Information 204 No Content 205 Reset Content 206 Partial Content 207 Multi-Status 208 Already Reported 226 IM Used 300 Multiple Choices 301 Moved Permanently 302 Found 303 See Other 304 Not Modified 305 Use Proxy 306 Switch Proxy 307 Temporary Redirect 308 Permanent Redirect/Resume Incomplete 400 Bad Request 401 Unauthorized 402 Payment Required 403 Forbidden 404 Not Found 405 Method Not Allowed 406 Not Acceptable 407 Proxy Authentication Required 408 Request Timeout 409 Conflict 410 Gone 411 Length Required 412 Precondition Failed 413 Payload Too Large 414 Request-URI Too Long 415 Unsupported Media Type 416 Requested Range Not Satisfiable 417 Expectation Failed 418 I'm a Teapot 419 Authentication Timeout 420 Method Failure/Enhance Your Calm 421 Misdirected Request 422 Unprocessable Entity 423 Locked 424 Failed Dependency 426 Upgrade Required 428 Precondition Required 429 Too Many Requests 431 Request Header Fields Too Large 440 Login Timeout 444 No Response 449 Retry With 450 Blocked by Windows Parental Controls 451 Unavailable for Legal Reasons/Redirect 494 Request Header Too Large 495 Cert Error 496 No Cert 497 HTTP to HTTPS 498 Token Expired/Invalid 499 Client Closed Request/Token Required 500 Internal Server Error 501 Not Implemented 502 Bad Gateway 503 Service Unavailable 504 Gateway Timeout 505 HTTP Version Not Supported 506 Variant Also Negotiates 507 Insufficient Storage 508 Loop Detected 509 Bandwidth Limit Exceeded 510 Not Extended 511 Network Authentication Required 520 Unknown Error 598 Network Read Timeout Error 599 Network Connect Timeout Error