我有这样的设置,其中几个应用程序正在通过负载平衡器(HAproxy)并访问MySQL数据库。
Several applications -> HAproxy -> MySQL database
我想知道哪个特定的应用程序正在访问数据库。 当我在数据库上做了一个show processlist时,它显示了HAproxy的IP(当然)。 然后我试图在haproxy和mysql数据库上做lsof,但是它并没有帮助我(或者我不知道如何正确解释结果)。 最后我检查了haproxy日志,但没有显示任何东西。
谢谢。
我也在寻找一种方法来跟踪haproxy连接到各种机器上的特定进程。 这个笨拙的小hackery看起来haproxy机器上的一个主机,你给它。 然后它转到源机器并查find后端数据库的连接。
最基本的部分是运行echo 'show sess' | socat stdio unix-connect:/tmp/haproxy echo 'show sess' | socat stdio unix-connect:/tmp/haproxy haproxy服务器本身上的echo 'show sess' | socat stdio unix-connect:/tmp/haproxy
#!/bin/bash # comments here: haproxy='<your haproxy target>' if [ $# -ne 1 ] then echo "" echo "usage: db_trace_haproxy_sessions.sh <hostname>" echo "looks at connections from <hostname> to load balanced database and traces them back to processes" echo "" exit fi ip=`host $1 | awk '{print $4}' 2>/dev/null` if [ "$ip" == "found:" ] then echo "" echo "couldn't resolve $1" echo "" exit fi IFS=" " for i in `ssh $haproxy "echo 'show sess' | socat stdio unix-connect:/tmp/haproxy | grep $ip"` do # output example: #0x2cd2780: proto=tcpv4 src=10.210.50.104:50791 fe=mysql be=mysql srv=db302 ts=04 age=1d11m calls=5 rq[f=909202h,l=0,an=00h,rx=7h59m,wx=,ax=] rp[f=109202h,l=0,an=00h,rx=7h59m,wx=,ax=] s0=[7,18h,fd=32,ex=] s1=[7,18h,fd=52,ex=] exp=7h47m echo "" db=`echo $i | awk '{print \$6}' | cut -d "=" -f2` src=`echo $i | awk '{print \$3}' | cut -d "=" -f2` echo "Source Connection: $src ... connected to $db" ssh $1 "ps=\`netstat -naltp | grep $src | awk '{print \$7}' | cut -d "/" -f1\`; ps aux | grep \" \$ps \" | grep -v grep" done; echo ""
输出如下所示:
db_trace_haproxy_sessions.sh appserver Source Connection: 10.210.50.103:37114 ... connected to database319 root 19300 1.0 0.0 53944 16180 pts/0 S+ Feb19 16:02 /usr/bin/perl somePerlScript.pl Source Connection: 10.210.50.103:37115 ... connected to database142 root 19301 5.4 0.0 53940 16160 pts/1 D+ Feb19 81:41 someapachethread