[Haproxy] Lệnh view haproxy status bằng command line sử dụng socket

Hướng dẫn thao tác xem haproxy status bằng command line

1. Các bạn cài đặt gói nc

yum install nc -y

2. Lệnh xem haproxy status

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,agent_status,agent_code,agent_duration,check_desc,agent_desc,check_rise,check_fall,check_health,agent_rise,agent_fall,agent_health,addr,cookie,mode,algo,conn_rate,conn_rate_max,conn_tot,intercepted,dcon,dses,wrew,connect,reuse,cache_lookups,cache_hits,srv_icur,src_ilim,qtime_max,ctime_max,rtime_max,ttime_max,eint,idle_conn_cur,safe_conn_cur,used_conn_cur,need_conn_est,uweight,agg_server_check_status,-,ssl_sess,ssl_reused_sess,ssl_failed_handshake,h2_headers_rcvd,h2_data_rcvd,h2_settings_rcvd,h2_rst_stream_rcvd,h2_goaway_rcvd,h2_detected_conn_protocol_errors,h2_detected_strm_protocol_errors,h2_rst_stream_resp,h2_goaway_resp,h2_open_connections,h2_backend_open_streams,h2_total_connections,h2_backend_total_streams,
stats,FRONTEND,,,1,1,8000,1,385,24117,0,0,0,,,,,OPEN,,,,,,,,,1,2,0,,,,0,1,0,1,,,,0,1,0,0,0,0,,1,1,1,,,0,0,0,0,,,,,,,,,,,,,,,,,,,,,http,,1,1,1,1,0,0,0,,,0,0,,,,,,,0,,,,,,,-,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
stats,BACKEND,0,0,0,0,800,0,385,24117,0,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,1,0,0,0,2,0,,1,5,3,,0,,2,0,,0,* 
INI,,,,,,,,,,,,,0,0,,,,,-1,,,0,0,0,0,,,,Initializing,,2,3,2,,,,172.20.28.27:443,,tcp,,,,,,,,0,0,0,,,0,,0,0,0,0,0,0,0,0,1,1,,-,0,0,0,,,,,,,,,,,,,,
bkhttps,BACKEND,0,0,0,0,800,0,0,0,0,0,,0,0,0,0,UP,2,2,0,,0,2,0,,1,5,0,,0,,1,0,,0,,,,,,,,,,,,,,0,0,0,0,0,0,-1,,,0,0,0,0,,,,,,,,,,,,,,tcp,leastconn,,,,,,,0,0,0,,,,,0,0,0,0,0,,,,,2,0,-,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,

Như vậy hệ thống sẽ trả về 1 chuỗi rất khó nhìn để chọn lọc và chia thành từng cột dễ nhìn hơn các bạn có thể chạy lệnh sau

echo "show stat" | nc -U /var/lib/haproxy/stats | cut -d "," -f 1,2,5-11,18,24,27,30,36,50,37,56,57,62 | column -s, -t

Output

pxname 
 svname    scur  smax  slim  stot  bin   bout    dreq  status  lastchg  pid  throttle  rate_max  check_status  cli_abrt  lastsess  last_chk  ttime
stats     FRONTEND  1     1     8000  1     7315  462640  0     OPEN             1              1                     
stats     BACKEND   0     0     800   0     7315  462640  0     UP      98       1              0                       0       4                   181
http      FRONTEND  0     0     8000  0     0     0       0     OPEN             1              0                       
fthttps   FRONTEND  0     0     8000  0     0     0       0     OPEN             1              0                          
bkhttps   sv30      0     0           0     0     0             DOWN    96       1              0         L4TOUT        0         -1                  0
bkhttps   sv26      0     0           0     0     0             DOWN    96       1              0         * L4TOUT      0         -1                  0
bkhttps   sv27      0     0           0     0     0             DOWN    95       1              0         * L4TOUT      0         -1                  0
bkhttps   BACKEND   0     0     800   0     0     0       0     DOWN    95       1              0                       0         -1

Nếu các bạn muốn xem liên tục thì kết họp thêm lệnh watch nhé mỗi 2s nó sẽ refesh cho bạn 1 lần

watch 'echo "show stat" | nc -U /var/lib/haproxy/stats | cut -d "," -f 1,2,5-11,18,24,27,30,36,50,37,56,57,62 | column -s, -t'

Chúc các bạn thành công

Bình luận