shell 以编程方式(SH脚本)检查HAProxy也路由哪个服务器的流量

7kqas0il  于 2023-11-21  发布在  Shell
关注(0)|答案(1)|浏览(157)

我有2个HAProxy服务器与Keepalived配置与他们,并有2个后端服务器的流量由HAProxy路由,我写一个shell脚本,将同步后端服务器之间的目录基于服务器当前设置为使用osync活动.(HA配置为主动-被动,切换到可用的服务器时,当前的一个去了)。问题是我找不到任何资源或方式,可以编程告诉我哪个服务器目前正在积极接受交通。

bfhwhh0e

bfhwhh0e1#

我最近在休息后重新开始工作,并成功地完成了它。

Haproxy文件配置

为了实现这一点,我们首先需要在haproxy配置文件的global部分中指定这些配置,从而在haproxy中启用stats套接字级别的日志。请参阅本指南HAProxy Stats Socket,以了解有关stats套接字的更多信息。

global

 log /dev/log local0

 log /dev/log local1 notice

 stats socket /var/lib/haproxy/haproxy.sock mode 600 level admin

 stats timeout 2m

字符串
确保没有策略阻止haproxy从套接字访问日志,例如selinux。请注意,在某些情况下,haproxy.sock文件可能位于不同的路径,因此您可能需要进行调查。

Shell脚本

现在,进入激动人心的部分。启用stats套接字后,您可以查询haproxy以显示其正在执行的操作的当前状态,包括所有服务器和流量状态。
您可以使用此命令查看它:

echo "show stat" | socat /var/run/haproxy.sock stdio


echo "show info" | socat /var/run/haproxy.sock stdio


确保根据您的目录调整haproxy.sock的路径。两个命令产生不同的输出,但出于我们的目的,我们将重点关注'show stat'命令。
RESPONSE=$(echo“show stat”|socat unix-connect:$SOCKET stdio)

PRIMARY_8080_STATUS=$(echo "$RESPONSE" | awk -F',' '/primary_8080/ {print $18}')
PRIMARY_8082_STATUS=$(echo "$RESPONSE" | awk -F',' '/primary_8082/ {print $18}')

if [ "$PRIMARY_8080_STATUS" == "UP" ] && [ "$PRIMARY_8082_STATUS" == "UP" ]; then
    echo "All Traffic is currently routed to Primary Orchestrator" 
fi

显示状态输出:

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,

stats,FRONTEND,,,2,2,2000,85,1262591,48945969,0,0,82,,,,,OPEN,,,,,,,,,1,2,0,,,,0,0,0,2,,,,0,2079,0,82,0,0,,0,3,2161,,,0,0,0,0,,,,,,,,,,,,,,,,,,,,,http,,0,2,85,2079,0,0,
stats,BACKEND,0,0,0,0,200,0,1262591,48945969,0,0,,0,0,0,0,UP,0,0,0,,0,5391,,,1,2,0,,0,,1,0,,0,,,,0,0,0,0,0,0,,,,0,0,0,0,0,0,0,1,,,0,0,0,2676,,,,,,,,,,,,,,http,roundrobin,,,,,,,
8080,FRONTEND,,,0,6,2000,14,426300,927680,0,0,0,,,,,OPEN,,,,,,,,,1,3,0,,,,0,0,0,3,,,,,,,,,,,0,0,0,,,0,0,0,0,,,,,,,,,,,,,,,,,,,,,tcp,,0,3,14,,0,0,
8082,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,,,,,,,,,,,,,,,,,,,,,tcp,,0,0,0,,0,0,
8080,primary_8080,0,0,0,0,,0,0,0,,0,,0,0,0,0,UP,1,1,0,7,3,2667,2578,,1,5,1,,0,,2,0,,0,L4OK,,0,,,,,,,,,,,0,0,,,,,-1,,,0,0,0,0,,,,Layer4 check passed,,2,3,4,,,,192.168.1.206:8080,,tcp,,,,,,,,
8080,backup_8080,0,0,0,6,,14,426300,927680,,0,,0,0,0,0,UP,1,0,1,0,0,5391,0,,1,5,2,,14,,2,0,,3,L4OK,,0,,,,,,,,,,,0,0,,,,,3770,,,0,0,0,6336,,,,Layer4 check passed,,2,3,4,,,,192.168.1.211:8080,,tcp,,,,,,,,
8080,BACKEND,0,0,0,6,200,14,426300,927680,0,0,,0,0,0,0,UP,1,1,1,,0,5391,0,,1,5,0,,14,,1,0,,3,,,,,,,,,,,,,,0,0,0,0,0,0,3770,,,0,0,0,6336,,,,,,,,,,,,,,tcp,roundrobin,,,,,,,
8082,primary_8082,0,0,0,0,,0,0,0,,0,,0,0,0,0,UP,1,1,0,2,0,5391,0,,1,6,1,,0,,2,0,,0,L4OK,,0,,,,,,,,,,,0,0,,,,,-1,,,0,0,0,0,,,,Layer4 check passed,,2,3,4,,,,192.168.1.206:8082,,tcp,,,,,,,,
8082,backup_8082,0,0,0,0,,0,0,0,,0,,0,0,0,0,UP,1,0,1,0,0,5391,0,,1,6,2,,0,,2,0,,0,L4OK,,0,,,,,,,,,,,0,0,,,,,-1,,,0,0,0,0,,,,Layer4 check passed,,2,3,4,,,,192.168.1.211:8082,,tcp,,,,,,,,
8082,BACKEND,0,0,0,0,200,0,0,0,0,0,,0,0,0,0,UP,1,1,1,,0,5391,0,,1,6,0,,0,,1,0,,0,,,,,,,,,,,,,,0,0,0,0,0,0,-1,,,0,0,0,0,,,,,,,,,,,,,,tcp,roundrobin,,,,,,,


在此输出中,您可以看到已设置的服务器的名称。对于每个特定的服务器,它都有自己的一组统计信息。在特定服务器的信息末尾,您会发现单词'up'或'down'。这是我们需要的信息。

结论

这就是我们如何以编程方式检索有关哪个服务器运行或关闭的信息。这里有无数的可能性,这一切都取决于你想玩它的程度。

相关问题