haproxy 통계를 수행할 스크립트를 작성한다.(/usr/local/bin/haproxy-stat.sh, chmod 755)
#!/bin/sh
# Script basat de https://collectd.org/wiki/index.php/Plugin:haproxy-stat.sh
# Parametres de haproxy consultables a http://cbonte.github.com/haproxy-dconv/configuration-1.4.html#9.2
#
#
# Us:
# LoadPlugin exec
# <Plugin exec>
# Exec "haproxy:haproxy" "/etc/collectd.d/haproxy-stat.sh" "-s" "/var/run/haproxy-stat"
# </Plugin>
#
# Novembre de 2012
# Toni Comerma
sock='/var/lib/haproxy/stats'
host="$(hostname -f)"
pause=10
while getopts "h:p:s:" c; do
case $c in
s) sock=$OPTARG;;
*) echo "Usage: $0 [-s <sockfile>]";;
esac
done
host="${COLLECTD_HOSTNAME:-$host}"
pause="${COLLECTD_INTERVAL:-$pause}"
INTERVAL=$pause
while [ $? -eq 0 ]; do
time="$(date +%s)"
echo 'show stat' | socat - UNIX-CLIENT:$sock \
|while IFS=',' read pxname svname qcur qmax scur smax slim stot bin bout dreq dresp ereq econ eresp wretr wredis status weight act bck chkfail chdown lastchg downtime qlimit pid iid sid throttle lbtot tracked type rat
e 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
do
if [ "$svname" != "svname" -a ! -z "$svname" ]
then
if [ "$svname" = 'FRONTEND' ]
then
echo "PUTVAL $host/haproxy-$pxname-$svname/gauge-req_rate interval=$INTERVAL $time:${req_rate:-0}"
echo "PUTVAL $host/haproxy-$pxname-$svname/counter-ereq interval=$INTERVAL $time:${ereq:-0}"
fi
echo "PUTVAL $host/haproxy-$pxname-$svname/bytes-bin interval=$INTERVAL $time:${bin:-0}"
echo "PUTVAL $host/haproxy-$pxname-$svname/bytes-bout interval=$INTERVAL $time:${bout:-0}"
echo "PUTVAL $host/haproxy-$pxname-$svname/gauge-scur interval=$INTERVAL $time:${scur:-0}"
echo "PUTVAL $host/haproxy-$pxname-$svname/gauge-rate interval=$INTERVAL $time:${rate:-0}"
if [ "$svname" != 'FRONTEND' ]
then
echo "PUTVAL $host/haproxy-$pxname-$svname/counter-eresp interval=$INTERVAL $time:${eresp:-0}"
echo "PUTVAL $host/haproxy-$pxname-$svname/counter-econ interval=$INTERVAL $time:${econ:-0}"
fi
fi
done
만약, socat 패키지가 설치되어있지 않다면 설치한다.
#yum install socat
설정 파일에서 exec 모듈을 활성화한다.
#vi /etc/collectd.conf
LoadPlugin exec
<Plugin exec>
Exec "haproxy:haproxy" "/usr/local/bin/haproxy-stat.sh" "-s" "/var/lib/haproxy/stats"
# Exec "user:group" "/path/to/exec"
# NotificationExec "user:group" "/path/to/exec"
</Plugin>
/var/lib/haproxy/stats 파일의 소유권을 haproxy로 변경한다.
#chown haproxy.haproxy /var/lib/haproxy/stats
collectd 를 재실행한다.
2013-12-30
#!/bin/sh
sock='/var/lib/haproxy/stats'
host="${COLLECTD_HOSTNAME}"
pause="${COLLECTD_INTERVAL:-10}"
while getopts "h:p:s:" c; do
case $c in
h) host=$OPTARG;;
p) pause=$OPTARG;;
s) sock=$OPTARG;;
*) echo "Usage: $0 [-h <hostname>] [-p <seconds>] [-s <sockfile>]";;
esac
done
while [ $? -eq 0 ]; do
time="$(date +%s)"
echo 'show stat' | socat - UNIX-CLIENT:$sock \
|while IFS=',' read pxname svname qcur qmax scur smax slim stot bin bout dreq dresp ereq econ eresp wretr wredis status weight act bck chkfail chdown 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; do
[ "$svname" != 'BACKEND' ] && continue
echo "PUTVAL $host/haproxy/haproxy_backend-$pxname $time:${stot:-0}:${econ:-0}:${eresp:-0}"
done
sleep $pause
done
collectd.conf 의 내용을 아래와 같이 수정
Exec "haproxy:haproxy" "/etc/collectd/haproxy-stat.sh" "-s" "/var/lib/haproxy/stats" "-h" "logy.kr" "-p" "10"
마지막으로 /usr/share/collectd/types.db 에 아래의 내용을 추가함
haproxy_backend stot:COUNTER:0:U, econ:COUNTER:0:U, eresp:COUNTER:0:U
만약, 서버/클라이언트 형태로 collectd 를 사용하고 있다면 서버의 types.db 에도 해당 항목을 추가한다.
'서버 > 리눅스' 카테고리의 다른 글
nginx load balancer 구축기 - Cent OS (0) | 2013.05.28 |
---|---|
MySQL Cluster 7.1 설치기 - CentOS 5.x (0) | 2013.05.28 |
HAProxy 로 MySQL 로드밸런싱 구축하기 - CentOS 5.x (0) | 2013.05.24 |
phpMyAdmin 설치 및 복수 MySQL 서버 연동 - CentOS 5.x (0) | 2013.05.24 |
최신 FFmpeg 설치하기 - CentOS 5.x (0) | 2013.05.21 |
댓글