使用者工具

網站工具


tech:zabbix:haproxy

HAProxy 納入 Zabbix 監控

  • Prerequisites
    • Zabbix Server >= 2.x
    • Zabbix Frontend >= 2.x
    • HAProxy >= 1.3
    • Socat

@HAProxy Server 安裝程序

  • 新增 userparameter_haproxy.conf
    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
  • 新增 haproxy_discovery.sh
    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
  • /usr/local/bin/haproxy_discovery.sh 依據實際狀況更改了原本 info.sock 定義
    • /var/run/haproxy/info.sock → /var/lib/haproxy/stats
  • 重起 zabbix-agent
    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"}]}]

@Zabbix Server 設定程序

參考網址

tech/zabbix/haproxy.txt · 上一次變更: 2015/05/25 19:35 由 jonathan