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.
Join the DZone community and get the full member experience.Join For Free
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 socket 2 . 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).
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 reading : generate common db data report by elk .
footnotes :1 www.datadoghq.com/blog/how-to-collect-haproxy-metrics/
Published at DZone with permission of Denny Zhang, DZone MVB. See the original article here.
Opinions expressed by DZone contributors are their own.