vi /etc/zabbix/zabbix_agentd.d/userparameter_haproxy.conf
# # Discovery Rule # # HAProxy Frontend, Backend and Server Discovery rules UserParameter=haproxy.list.discovery[*],/usr/local/bin/haproxy_discovery.sh $1 $2 # Frontend / Backend current sessions UserParameter=haproxy.stat.bin[*],echo "show stat" | socat $1 stdio | grep "^$2,$3" | cut -d, -f9 UserParameter=haproxy.stat.bout[*],echo "show stat" | socat $1 stdio | grep "^$2,$3" | cut -d, -f10 UserParameter=haproxy.stat.scur[*],echo "show stat" | socat $1 stdio | grep "^$2,$3" | cut -d, -f5 UserParameter=haproxy.stat.status[*],echo "show stat" | socat $1 stdio | grep "^$2,$3" | cut -d, -f18 | cut -d\ -f1
vi /usr/local/bin/haproxy_discovery.sh
#!/bin/bash
#
# Get list of Frontends and Backends from HAPROXY
# Example: ./haproxy_discovery.sh [/var/run/haproxy/info.sock] FRONTEND|BACKEND|SERVERS
# First argument is optional and should be used to set location of your HAPROXY socket
# Second argument is should be either FRONTEND, BACKEND or SERVERS, will default to FRONTEND if not set
#
# !! Make sure the user running this script has Read/Write permissions to that socket !!
#
## haproxy.cfg snippet
# global
# stats socket /run/haproxy/info.sock mode 666 level user
#HAPROXY_SOCK="/var/run/haproxy/info.sock"
HAPROXY_SOCK="/var/lib/haproxy/stats"
[ -n "$1" ] && echo $1 | grep -q ^/ && HAPROXY_SOCK="$(echo $1 | tr -d '\040\011\012\015')"
get_stats() {
echo "show stat" | socat ${HAPROXY_SOCK} stdio 2>/dev/null | 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,"'{"{#BACKEND_NAME}":"'$backend'","{#SERVER_NAME}":"'$server'"}'
done
done
echo '{"data":['${serverlist#,}']}'
exit 0
;;
*) END="FRONTEND" ;;
esac
for frontend in $(get_stats | grep "$END" | cut -d, -f1 | uniq); do
felist="$felist,"'{"{#'${END}'_NAME}":"'$frontend'"}'
done
echo '{"data":['${felist#,}']}'
chmod a+x /usr/local/bin/haproxy_discovery.sh
service zabbix-agent restart
sudo zabbix_agentd -t haproxy.list.discovery[BACKEND]
haproxy.list.discovery[BACKEND] [t|{"data":[{"{#BACKEND_NAME}":"stats-back"},{"{#BACKEND_NAME}":"pxc-back"},{"{#BACKEND_NAME}":"pxc-onenode-back"}]}]