Over a million developers have joined DZone.

Convert HAProxy Statistics to a Python Dictionary

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

· Performance Zone

See Gartner’s latest research on the application performance monitoring landscape and how APM suites are becoming more and more critical to the business, brought to you in partnership with AppDynamics.

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 the HAProxy stats page in GUI.1

Understand HAProxy Stats Page


From the HAProxy GUI, we know the 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.

Interactive With Unix Socket File

The same metrics in the GUI can be retrieved via the UNIX socket2. Hmm… yes, it really is 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,...

Convert Socket Output Into A Python Dictionary

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

Wrap up a Python module (original in GitHub).


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

Next step: Add effective monitoring and pull meaningful metrics about HAProxy.

More ReadingGenerate Common DB Data Report By ELK.


1 www.datadoghq.com/blog/how-to-collect-haproxy-metrics/
2 http://cbonte.github.io/haproxy-dconv/1.6/management.html#9.2

The Performance Zone is brought to you in partnership with AppDynamics.  See Gartner’s latest research on the application performance monitoring landscape and how APM suites are becoming more and more critical to the business.


Published at DZone with permission of Denny Zhang, DZone MVB. See the original article here.

Opinions expressed by DZone contributors are their own.

The best of DZone straight to your inbox.

Please provide a valid email address.

Thanks for subscribing!

Awesome! Check your inbox to verify your email so you can start receiving the latest in tech news and resources.

{{ parent.title || parent.header.title}}

{{ parent.tldr }}

{{ parent.urlSource.name }}