forked from anapsix/zabbix-haproxy
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathhaproxy_discovery.sh
More file actions
executable file
·32 lines (28 loc) · 938 Bytes
/
haproxy_discovery.sh
File metadata and controls
executable file
·32 lines (28 loc) · 938 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
#!/bin/bash
#
# Get list of Frontends and Backends from HAPROXY
#
HAPROXY_SOCK=$(echo $1 | grep -Po "^/[^\s]+" || echo "/var/run/haproxy/info.sock")
#echo "DEBUG: HAPROXY_SOCK: $HAPROXY_SOCK" >&2
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 -P "^${backend}"',(?!BA)' | 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 $(echo "show stat" | socat ${HAPROXY_SOCK} stdio 2>/dev/null | grep -v "#" | grep "$END" | cut -d, -f1 | uniq); do
felist="$felist,"'{"{#'${END}'_NAME}":"'$frontend'"}'
done
echo '{"data":['${felist#,}']}'