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