GrepBoard/Manuals
605 manuals ·

Read HAProxy server status from the stats socket with show stat

·Tested on Ubuntu 24.04 · HAProxy 2.8.16 · socat 1.8.0.0 · Python 3.12 (test backends)·Test scope ↓·8 steps

Query the HAProxy stats socket with show stat, read status, check_status and lastchg per server, and see how they change when a server goes DOWN.

The log only records the moment a server drops out; show stat tells you where things stand now. The output below comes from HAProxy 2.8.16 with three servers: s1 has its process stopped, s2 is healthy, and s3 is disabled in the config. Only the socket path was changed, to /run/haproxy/admin.sock. For the 503 after every server is out, see this post; for the reason: strings in the log, see this one.

[1] Check that global has a stats socket line. The default haproxy.cfg in the Ubuntu package already has it.

/etc/haproxy/haproxy.cfg
global    stats socket /run/haproxy/admin.sock mode 660 level admin    stats timeout 30s

level admin also allows commands that disable and enable servers; mode is the socket file's permission. Section 9.3 of the management manual shows how to listen on a TCP port too, and warns that it is dangerous, so this post sticks to the UNIX socket.

[2] Send one line with socat. The socket handles one line, answers, and closes the connection. Cut the columns with cut.

console
# echo "show stat" | socat stdio unix-connect:/run/haproxy/admin.sock | cut -d, -f1,2,18,24,37,38,39
output
# pxname,svname,status,lastchg,check_status,check_code,check_durationweb,FRONTEND,OPEN,,,,app,s1,DOWN,29,L4CON,,0app,s2,UP,20,L7OK,200,2app,s3,MAINT,59,,,app,BACKEND,UP,20,,,

[3] Watch the column numbers. The CSV list in manual section 9.1 counts pxname as 0, while cut -f starts at 1: the manual's 17. status is -f18. If that gets confusing, pick columns by the names in the header line.

sh
#!/bin/sh# pick.sh pxname,svname,status,...echo "show stat" | socat stdio unix-connect:/run/haproxy/admin.sock | awk -F, -v want="$1" 'NR==1 { sub(/^# /,""); for(i=1;i<=NF;i++) idx[$i]=i; n=split(want,w,","); next }NF { line=""; for(j=1;j<=n;j++) line=line (j>1?",":"") $(idx[w[j]]); print line }'
console
$ ./pick.sh pxname,svname,status,lastchg,check_status,check_code,last_chk
output
web,FRONTEND,OPEN,,,,app,s1,DOWN,29,L4CON,,Connection refusedapp,s2,UP,20,L7OK,200,app,s3,MAINT,59,,,app,BACKEND,UP,20,,,

[4] The columns to read on a server line (manual section 9.1):

ColumnMeaning
statusUP · DOWN · NOLB · MAINT …
lastchgseconds since the last UP↔DOWN transition
check_statusresult of the last check — L4CON connection problem · L4TOUT timeout · L7OK · L7STS bad response status · L7TOUT …
check_codestatus code from a layer 7 check
last_chkcheck contents or the error text
chkdownnumber of UP→DOWN transitions

The manual says that while a check is running, check_status shows the last known value prefixed with * . A disabled server (s3) has empty check columns and shows MAINT.

[5] Watch the order in which values change on the way to DOWN. s1 has inter 1s fall 2 rise 2. Stop its backend process while the script below runs. It repeats the query every second and prints a few columns (the leading number is the time; column numbers are 1-based, as in cut).

sh
#!/bin/sh# watch.sh <server> — one line per secondwhile :; do  echo "show stat" | socat stdio unix-connect:/run/haproxy/admin.sock |    awk -F, -v s="$1" -v t="$(date +%T)" '$2==s { print t, $2, "|" $18 "|", "lastchg=" $24, "chk=" $37, "last_chk=" $57, "chkfail=" $22, "chkdown=" $23 }'  sleep 1done
console
$ sh watch.sh s1
output
14:57:27 s1 |UP| lastchg=7 chk=L7OK last_chk= chkfail=0 chkdown=014:57:28 s1 |UP 1/2| lastchg=8 chk=L4CON last_chk=Connection refused chkfail=1 chkdown=014:57:29 s1 |DOWN| lastchg=1 chk=L4CON last_chk=Connection refused chkfail=2 chkdown=114:57:30 s1 |DOWN| lastchg=2 chk=L4CON last_chk=Connection refused chkfail=2 chkdown=1

On the second line one check has failed: check_status is already L4CON, but status is still UP 1/2 and lastchg has not reset (it keeps counting up). At the fall-th failure the state becomes DOWN, lastchg goes back to 1 and chkdown becomes 1. When a server comes back it passes through DOWN 1/2 once and turns UP at the rise-th success. The manual does not explain 1/2; it is read here only as what it looked like, an intermediate state counting toward fall or rise.

[6] To narrow the output, use show stat <backend> <type> <sid>. type is 1 frontends, 2 backends, 4 servers, and values can be added; sid -1 means all. A trailing up keeps only UP servers and no-maint drops MAINT ones.

console
# echo "show stat app 4 -1" | socat stdio unix-connect:/run/haproxy/admin.sock | cut -d, -f1,2,18
output
# pxname,svname,statusapp,s1,DOWNapp,s2,UPapp,s3,MAINT

[7] show servers state app shows the same servers as space-separated numbers. Pick values with awk. The column names are on the second line after #; below are srv_name, srv_op_state, srv_admin_state, srv_time_since_last_change and srv_check_status.

console
# echo "show servers state app" | socat stdio unix-connect:/run/haproxy/admin.sock | awk 'NR>2{print $4,$6,$7,$10,$11}'
output
s1 0 0 29 8s2 2 0 20 15s3 0 5 59 1

Per the manual, srv_op_state is 0 stopped, 1 starting, 2 running, 3 stopping, and srv_admin_state is a bit mask. The 5 on s3 is 0x01 (forced into maintenance by command) plus 0x04 (maintenance from the configuration). srv_check_status is a number, not L4CON, and this section has no table for the numbers. When you need the check result by name, use show stat.

[8] A level admin socket can also take a server out and put it back.

console
# echo "disable server app/s2" | socat stdio unix-connect:/run/haproxy/admin.sock# echo "show stat" | socat stdio unix-connect:/run/haproxy/admin.sock | awk -F, '$2=="s2"{print $18,$24}'MAINT 0# echo "enable server app/s2" | socat stdio unix-connect:/run/haproxy/admin.sock

In show servers state, srv_admin_state for s2 was 1 at that point, and one second after enable the server was UP again.

Analyze your HAProxy log in the browser ↗ — nothing is uploaded.

#haproxy#stats-socket#health-check#socat