我有这样的设置,其中几个应用程序正在通过负载平衡器(HAproxy)并访问 MySQL 数据库。
Several applications -> HAproxy -> MySQL database
我想知道哪个特定应用程序正在访问数据库。当我在数据库上执行 show processlist 时,它会显示 HAproxy 的 IP(当然)。然后我尝试在 haproxy 和 mysql 数据库上执行 lsof,但它没有帮助我(或者我只是不知道如何正确解释结果)。最后我检查了 haproxy 日志,但没有显示任何内容。
谢谢。
答案1
我还想找到一种方法来将 haproxy 连接追溯到各个机器上的特定进程。这个简单的黑客程序会在 haproxy 机器上查找您为其提供的主机。然后它会转到源机器并查找到后端数据库的连接。
最基本的部分是echo 'show sess' | socat stdio unix-connect:/tmp/haproxy
在 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