Check if current sessions with Socket

Hi

I am using unix socket to change the state of nodes on Haproxy for automation. I am able to put it into the following states maint,drain and ready using the below command.

echo “set server $FRONTEND/$NODE state drain” | nc -U /var/lib/haproxy/stats

How do I check if the node is fully drained from command line?

Hi Isa,

Setting a server to DRAIN state removes the server from load balancing. That is the existing connections are drained and no new connections are established on the server. In order to conclude that a server has been completely drained, we need to refer to the HAProxy stats properties related to sessions and connections.

You may use below command to retrieve HAProxy stats in CSV over UNIX Socket:

[root@ha3 opt]# echo “show stat” |nc -U /var/lib/haproxy/stats

OUTPUT:
#pxname,svname,qcur,qmax,scur,smax,slim,stot,bin,bout,dreq,dresp,ereq,econ,eresp,wretr,wredis,status,weight,act,bck,chkfail,chkdown,lastchg,downtime,qlimit,pid,iid,sid,throttle,lbtot,tracked,type,rate,rate_lim,rate_max,check_status,check_code,check_duration,hrsp_1xx,hrsp_2xx,hrsp_3xx,hrsp_4xx,hrsp_5xx,hrsp_other,hanafail,req_rate,req_rate_max,req_tot,cli_abrt,srv_abrt,comp_in,comp_out,comp_byp,comp_rsp,lastsess,last_chk,last_agt,qtime,ctime,rtime,ttime,
fe_keystone,FRONTEND,0,12,2000,7147,2976789,15461054,0,0,0,OPEN,1,2,0,0,0,0,5938,0,28157,46,31,7002,0,0,5938,35236,0,0,0,0,
be_keystone,op1.example.com,0,0,0,7,28234,2395882,13976630,0,0,0,0,0,UP,1,1,0,1,1,4,5346,1,3,1,28104,2,0,819,L4OK,0,0,28157,46,31,0,0,0,0,0,1193,0,1,8,9,
be_keystone,BACKEND,0,0,0,7,200,35236,2976789,15461054,0,0,7002,0,0,0,UP,1,1,0,4,4,5343,1,3,0,28104,1,0,5938,0,28157,46,31,7002,0,0,0,0,0,0,0,1193,0,1,8,9,
stats,FRONTEND,0,0,2000,0,0,0,0,0,0,OPEN,1,4,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
stats,BACKEND,0,0,0,0,200,0,0,0,0,0,0,0,0,0,UP,0,0,0,0,11537,0,1,4,0,0,1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,-1,0,0,0,0,

Out of all the stats properties retrieved above, the relevant ones in this case are rate, req_rate and scur.
where,

  • rate : number of sessions per second over last elapsed second. This value keeps increasing if we start hitting the target server in a loop and the value decreases to zero when the server stops accepting new connections.
  • req_rate : HTTP requests per second over last elapsed second. This value should also go down to zero as load balancer would stop forwarding new request to the draining node.
  • scur : number of current sessions on the server. This value too starts decreasing to zero once the server is put into drain state.

[root@ha3 opt]# echo “show stat” |nc -U /var/lib/haproxy/stats| cut -d ‘,’ -f 1,2,5,18,34
#pxname,svname,scur,status,rate
fe_keystone,FRONTEND,7,OPEN,0
be_keystone,op1.example.com,7,UP,741
be_keystone,BACKEND,7,UP,741
stats,FRONTEND,0,OPEN,0
stats,BACKEND,0,UP,0

[root@ha3 opt]# echo “show stat” |nc -U /var/lib/haproxy/stats| cut -d ‘,’ -f 1,2,5,18,34
#pxname,svname,scur,status,rate
fe_keystone,FRONTEND,0,OPEN,0
be_keystone,op1.example.com,0,DRAIN,0
be_keystone,BACKEND,0,DOWN,0
stats,FRONTEND,0,OPEN,0
stats,BACKEND,0,UP,0

Therefore, in order to conclude that a server has been completely drained, you need to ensure that the value for rate, req_rate and scur is all zero.

Hope this is helpful !

Thanks , this is exact what I needed