Monitoring HAProxy with Zabbix

HAProxy is a great and reliable load-balancer that offer unparalleled performance and versatility.
Monitoring connection rates and state of backend servers of HAProxy is relativly simple, via it's build-in web interface (see haproxy demo) or local stats socket. However these interfaces do not provide trends, historical data, etc..
Gathering such data is not difficult with some scripting skills and your favorite monitoring solution such as Nagios, Ganglia, Zabbix, etc.. Adding frontends, backends and all backend servers manually is kind of tedious, though.
Zabbix's powerful discovery and template engines makes it possible to automatically discover frontends, backends and backends servers, without manual configuration.
Inspired by pg_monz for PostgreSQL / Zabbix, I've made a not-too complicated discovery script and a template that discoveres endpoints and tracks some basic statistics: session rate, bytes in and out, status. Monitoring of other paramaters can be easily implemented.
It serves my purpose well and may help few other Zabbix users.

Latest instructions, discovery script and template - available at my Github repo.

Update

  • 09/2015: Now stats are gathered via script that caches the results, to avoid hitting HAProxy socket too many times.
    Even if you have 1 frontend, 1 backend and 4 backend servers, you'd be gathering (!) 372 measurements = 62 [stats] x 6 [endpoints]. With caching, the socket is hit only once in TTL (5 minutes by default)

Prerequisites

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

Instructions

  1. Place userparameter_haproxy.conf into /etc/zabbix/zabbix_agentd.d/ directory, assuming you have Include set in zabbix_agend.conf, like so:

    ### Option: Include
    # You may include individual files or all files in a directory in the configuration file.
    # Installing Zabbix will create include directory in /usr/local/etc, unless modified during the compile time.
    #
    # Mandatory: no
    # Default:
    Include=/etc/zabbix/zabbix_agentd.d/
    
  2. Place haproxy_discovery.sh into /usr/local/bin/ directory and make sure it's executable (sudo chmod +x /usr/local/bin/haproxy_discovery.sh)

  3. Import haproxy_zbx_template.xml template via Zabbix Web UI interface (provided by zabbix-frontend-php package)
  4. Configure HAProxy control socket

    • Configure HAProxy to listen on /run/haproxy/info.sock
    • or set custom socket path in checks (set {$HAPROXY_SOCK} template macro to your custom socket path)
    • or update userparameter_haproxy.conf and haproxy_discovery.sh with your socket path

      # haproxy.conf snippet
      # haproxy read-only non-admin socket
      ## (user level permissions are required, admin level will work as well, though not necessary)
      global
        stats socket /run/haproxy/info.sock  mode 666 level user
      
  5. Verify on server with HAProxy installed:

    anapsix@lb1:~$ sudo zabbix_agentd -t haproxy.list.discovery[FRONTEND]
    haproxy.list.discovery[FRONTEND]  [t|{"data":[{"{#FRONTEND_NAME}":"http-frontend"},{"{#FRONTEND_NAME}":"https-frontend"}]}]
    
    
    anapsix@lb1:~$ sudo zabbix_agentd -t haproxy.list.discovery[BACKEND]
    haproxy.list.discovery[BACKEND]  [t|{"data":[{"{#BACKEND_NAME}":"www-backend"},{"{#BACKEND_NAME}":"api-backend"}]}]
    
    
    anapsix@lb1:~$ sudo zabbix_agentd -t haproxy.list.discovery[SERVERS]
    haproxy.list.discovery[SERVERS]  [t|{"data":[{"{#BACKEND_NAME}":"www-backend","{#SERVER_NAME}":"www01"},{"{#BACKEND_NAME}":"www-backend","{#SERVER_NAME}":"www02"},{"{#BACKEND_NAME}":"www-backend","{#SERVER_NAME}":"www03"},{"{#BACKEND_NAME}":"api-backend","{#SERVER_NAME}":"api01"},{"{#BACKEND_NAME}":"api-backend","{#SERVER_NAME}":"api02"},{"{#BACKEND_NAME}":"api-backend","{#SERVER_NAME}":"api03"}]}]
    
  6. Add hosts with HAProxy installed to just imported Zabbix HAProxy template.

  7. Wait for discovery.. Frontend(s), Backend(s) and Server(s) should show up under Host Items.
    An easy way to see all data is via Overview (make sure to pick right Group, one of the "HAProxy" applications and select Data as Type)

License

MIT License

The MIT License (MIT)

Copyright (c) 2015-2019 "Anastas Dancha <[email protected]>"

Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.