Convert HAProxy Statistics To A Python Dictionary

HAProxy provides both GUI and unix socket file. Through them, we can explore lots of critical info, especially HAProxy Statistics.

More importantly, how to read and understand the key output? Furthermore, make good use of it.

Convert HAProxy Statistics To A Python Dictionary

HAProxy is a very popular load balancer and reverse proxy service at both L7 layer(HTTP) and L4 layer(TCP). With easy setup, we can get haproxy stats page in GUI.[1]

1.1 Understand HAProxy Stats Page

Understand HAProxy Stats GUI

From HAProxy GUI, we know below meaningful metrics:

  • Normal http/https visit hits. Poll the data periodically. We can have a clear understanding for the history and the trend. Pretty much close to google analytics. Right?
  • Exceptions In Server Side. If we have a noticeable number of failures, raise alarms immediately.
  • Slow Response. Mostly we shall see very few slow responses. Better add monitoring items for this.
  • Errors In Client Side. Could be different reasons. e.g, being attacked by malicious users, users are calling API with old data format, etc.

1.2 Interactive With Unix Socket File

The same metrics in GUI can be retrieved via unix socket[2]. hmm… yes, the same. Just a bit complicated.

# Figure out file path of haproxy socket file
# grep sock /etc/haproxy/haproxy.cfg

root@denny# echo "show stat" | nc -U /var/run/haproxy/admin.sock
# pxname,svname,qcur,qmax,scur,smax,slim,stot,bin,bout,dreq,...

1.3 Convert Socket Output Into A Python Dictionary

The first line of above command output is a list of field names. Following that, each line is a record of data.

Wrap up a python module in github. GitHub

Convert HAProxy Statistics To A Python Dictionary

If you can’t measure it, you can’t manage it. – Peter Drucker.

Next step: Put effective monitoring and pull meaningful metrics about haproxy.

More Reading: Generate Common DB Data Report By ELK.



PRs Welcome

Blog URL:

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.