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

Discover 50 of the latest mobile performance statistics with the Ultimate Guide to Digital Experience Monitoring, brought to you in partnership with Catchpoint.

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

haproxy_stats_gui.jpg

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,...
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,...

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).

haproxy_convert.jpg

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.

Footnotes:

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

Is your APM strategy broken? This ebook explores the latest in Gartner research to help you learn how to close the end-user experience gap in APM, brought to you in partnership with Catchpoint.

Topics:
haproxy ,python

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.

SEE AN EXAMPLE
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.
Subscribe

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

{{ parent.tldr }}

{{ parent.urlSource.name }}