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,...
frontend-http,FRONTEND,,,0,1,2000,14,3738,82593,0,0,3,,,,,OP...
frontend-https,FRONTEND,,,0,1,2000,6,410,150635,0,0,2,,,,,OP...
backend-http,127.0.0.1,0,0,0,1,4000,5,767,597,,0,,0,0,0,0,UP...
backend-http,BACKEND,0,0,0,1,200,5,767,597,0,0,,0,0,0,0,UP,1...
backend-https,127.0.0.1,0,0,0,1,4000,3,263,130057,,0,,0,0,0,...
backend-https,BACKEND,0,0,0,1,200,3,263,130057,0,0,,0,0,0,0,...

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.

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

linkedin
github
slack

PRs Welcome

Blog URL: https://www.dennyzhang.com/haproxy_stats


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.