talas-group/04_INFRA_DEPLOIEMENT/Ansible/roles/haproxy/files/haproxy_discovery.sh
senke 66471934af Initial commit: Talas Group project management & documentation
Knowledge base of ~80+ markdown files across 14 domains (00-13),
Logseq graph, hardware design files (KiCAD), infrastructure configs,
and talas-wiki static site.

Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
2026-04-04 20:10:41 +02:00

36 lines
1.1 KiB
Bash
Executable file

#!/bin/bash
# Ansible managed
# modified from https://raw.githubusercontent.com/anapsix/zabbix-haproxy/master/haproxy_discovery.sh
# Get list of Frontends and Backends from HAPROXY
# Example: ./haproxy_discovery.sh FRONTEND|BACKEND|SERVERS
# the argument should be either FRONTEND, BACKEND or SERVERS, will default to FRONTEND if not set
HAPROXY_SOCK="/run/haproxy/monitoring.sock"
query_stats() {
echo "show stat" | socat ${HAPROXY_SOCK} stdio 2>/dev/null
}
get_stats() {
echo "$(query_stats)" | grep -v "^#"
}
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 -e '{ "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 -e '{ "data": [ '${felist#,}']}'