haproxy本身提供了很好的stats页面来展示其性能指标,不过我们需要将这些集中到zabbix监控中去。
可以通过自定义key的方式,也可以通过snmp的方式。
就部署来说,还是使用snmp方式更容易一些。
haproxy的官方源码中包含了社区贡献者Krzysztof Piotr
Oledzki提供的haproxy.pl脚本。
这个脚本可以通过读取haproxy socket的方式,将对应指标转换成snmp的OID节点。
脚本支持GET命令(snmpget)和GETNEXT命令(snmpwalk),正式监控推荐使用GET命令。
因为GET非常快,而GETNEXT则比较慢。
下面流水账一下安装步骤:
1. 安装snmp相关软件
]# yum install net-snmp net-snmp-perl net-snmp-utils
2. 安装haproxy.pl脚本
]# wget
http://www.haproxy.org/download/contrib/netsnmp-perl/haproxy.pl
]# cp haproxy.pl /etc/snmp/
]# grep -q "disablePerl false" /etc/snmp/snmpd.conf || echo
"disablePerl false" >> /etc/snmp/snmpd.conf
]# echo "perl do '/etc/snmp/haproxy.pl';" >>
/etc/snmp/snmpd.conf
]# vi /etc/snmp/snmpd.conf
加入如下一行:
===> view systemview
included
.1.3.6.1.4.1.29385.106
]# /etc/init.d/snmpd start
3. haproxy.pl给出的snmp信息
haproxy可以输出很多统计信息,例如info,stats,errors,sess等,我们比较关注如下两种:
一种是stats,包含了frontend,backend,server相关的各种指标计数器值,例如某个frontend上的连接数等。
一种是info,包含了haproxy进程本身的一些相关信息,例如haproxy版本号,PID,当前连接数, session
rates等。
haproxy.pl支持种OID,分别对应stats和info。
具体每个指标的含义,请参考官方文档的9.1章节。
- 1.3.6.1.4.1.29385.106.1: 从stats中获取一个变量
使用方法:
1.3.6.1.4.1.29385.106.1.$type.$field.$iid.$sid
- type为下列值之一:
0)
frontend
1)
backend
2)
server
- field为下列值之一:
0..32) CSV格式变量
10001) index
10002) unique name
- iid 是proxy
id
- sid 是service id (sid):
0是frontends和backends, >= 1对应的是server
- 1.3.6.1.4.1.29385.106.2: 从info中获取一个变量
使用方法:
1.3.6.1.4.1.29385.106.2.$req.$varnr
-
req为下列值之一:
0) 获取变量名
1) 获取变量值
应用举例:
- 获取fontend列表(type: 0),并带有状态信息 (field: 17):
$ snmpbulkwalk -c public -v2c 192.168.0.1
1.3.6.1.4.1.29385.106.1.0.17
SNMPv2-SMI::enterprises.29385.106.1.0.17.1.0 = STRING:
"OPEN"
SNMPv2-SMI::enterprises.29385.106.1.0.17.47.0 = STRING:
"OPEN"
- 获取backend列表(type: 1),并带有index (field: 10001):
$ snmpbulkwalk -c public -v2c 192.168.0.1
1.3.6.1.4.1.29385.106.1.1.10001
SNMPv2-SMI::enterprises.29385.106.1.1.10001.1.0 = STRING:
"1.0"
SNMPv2-SMI::enterprises.29385.106.1.1.10001.1100.0 = STRING:
"1100.0"
SNMPv2-SMI::enterprises.29385.106.1.1.10001.1101.0 = STRING:
"1101.0"
SNMPv2-SMI::enterprises.29385.106.1.1.10001.1200.0 = STRING:
"1200.0"
SNMPv2-SMI::enterprises.29385.106.1.1.10001.1201.0 = STRING:
"1201.0"
SNMPv2-SMI::enterprises.29385.106.1.1.10001.1300.0 = STRING:
"1300.0"
SNMPv2-SMI::enterprises.29385.106.1.1.10001.1400.0 = STRING:
"1400.0"
SNMPv2-SMI::enterprises.29385.106.1.1.10001.1401.0 = STRING:
"1401.0"
SNMPv2-SMI::enterprises.29385.106.1.1.10001.1500.0 = STRING:
"1500.0"
(...)
- 获取所有的backend server列表(type: 2),并带有unique name (field:
10002):
$ snmpbulkwalk -c public -v2c 192.168.0.1
1.3.6.1.4.1.29385.106.1.2.10002
SNMPv2-SMI::enterprises.29385.106.1.2.10002.1100.1001 =
STRING: "backend1/s2"
SNMPv2-SMI::enterprises.29385.106.1.2.10002.1100.1002 =
STRING: "backend1/s5"
SNMPv2-SMI::enterprises.29385.106.1.2.10002.1100.1003 =
STRING: "backend1/s6"
SNMPv2-SMI::enterprises.29385.106.1.2.10002.1101.1001 =
STRING: "backend2/s9"
SNMPv2-SMI::enterprises.29385.106.1.2.10002.1101.1002 =
STRING: "backend2/s10"
SNMPv2-SMI::enterprises.29385.106.1.2.10002.1200.1001 =
STRING: "backend3/s8"
(...)
- 获取proxy 4300的backend server列表(type: 2),并带有weight值 (field:
18):
$ snmpbulkwalk -c public -v2c 192.168.0.1
1.3.6.1.4.1.29385.106.1.2.18.4300
SNMPv2-SMI::enterprises.29385.106.1.2.18.4300.1001 = STRING:
"40"
SNMPv2-SMI::enterprises.29385.106.1.2.18.4300.1002 = STRING:
"25"
SNMPv2-SMI::enterprises.29385.106.1.2.18.4300.1003 = STRING:
"40"
SNMPv2-SMI::enterprises.29385.106.1.2.18.4300.1012 = STRING:
"80"
- 获取所有索引为47的proxy[sid.iid: 47.0 (proxy #47)]的fronted(type:
1)的session的计数器(field: 7), :
snmpget -c public -v2c 192.168.0.1
enterprises.29385.106.1.0.7.47.0
SNMPv2-SMI::enterprises.29385.106.1.0.7.47.0 = STRING:
"1014019"
- 获取所有可用变量列表(req: 0):
$ snmpbulkwalk -c public -v2c 192.168.0.1
1.3.6.1.4.1.29385.106.2.0
SNMPv2-SMI::enterprises.29385.106.2.0.0 = STRING: "Name"
SNMPv2-SMI::enterprises.29385.106.2.0.1 = STRING:
"Version"
SNMPv2-SMI::enterprises.29385.106.2.0.2 = STRING:
"Release_date"
SNMPv2-SMI::enterprises.29385.106.2.0.3 = STRING:
"Nbproc"
SNMPv2-SMI::enterprises.29385.106.2.0.4 = STRING:
"Process_num"
SNMPv2-SMI::enterprises.29385.106.2.0.5 = STRING: "Pid"
SNMPv2-SMI::enterprises.29385.106.2.0.6 = STRING:
"Uptime"
SNMPv2-SMI::enterprises.29385.106.2.0.7 = STRING:
"Uptime_sec"
SNMPv2-SMI::enterprises.29385.106.2.0.8 = STRING:
"Memmax_MB"
SNMPv2-SMI::enterprises.29385.106.2.0.9 = STRING:
"Ulimit-n"
SNMPv2-SMI::enterprises.29385.106.2.0.10 = STRING:
"Maxsock"
SNMPv2-SMI::enterprises.29385.106.2.0.11 = STRING:
"Maxconn"
SNMPv2-SMI::enterprises.29385.106.2.0.12 = STRING:
"CurrConns"
- 获取变量Uptime_sec的值(varnr: 7)的值(req: 1):
$ snmpget -c public -v2c 192.168.0.1
1.3.6.1.4.1.29385.106.2.1.7
SNMPv2-SMI::enterprises.29385.106.2.1.7 = STRING: "18761"
参考文档:
https://fossies.org/linux/haproxy/contrib/netsnmp-perl/README
http://blog.chinaunix.net/uid-28718792-id-3598804.html
http://makandracards.com/makandra/36727-get-haproxy-stats-informations-via-socat
http://haproxy.tech-notes.net/9-2-unix-socket-commands/
http://www.liquidweb.com/kb/how-to-install-and-configure-snmp-on-centos/