Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
open-falcon agent haproxy plugin
#!/bin/bash
#requires: socat
#author: fangpsh
#关于采集的Haproxy指标和报警阈值设置请参考:https://github.com/iask/haproxymon
set -eu
STATS_SOCKET="/var/lib/haproxy/stats"
HOST=`cat /etc/hostname`
DATE=`date +%s`
tmp_info="/tmp/haproxy_info"
tmp_stat="/tmp/haproxy_stat"
echo -n "["
##haproxy info
echo "show info" |sudo socat /var/lib/haproxy/stats stdio > $tmp_info 2>/dev/null
echo -n "{\"metric\": \"Haproxy_CurrConns\", \"endpoint\": \"$HOST\", \"tags\": \"\", \"value\": `grep -E '^CurrConns:' $tmp_info |cut -d ' ' -f 2`,\"timestamp\": $DATE, \"counterType\": \"GAUGE\", \"step\": 30}"
echo -n ",{\"metric\": \"Haproxy_ConnRate\", \"endpoint\": \"$HOST\", \"tags\": \"\", \"value\": `grep -E '^ConnRate:' $tmp_info |cut -d ' ' -f 2`,\"timestamp\": $DATE, \"counterType\": \"GAUGE\", \"step\": 30}"
echo -n ",{\"metric\": \"Haproxy_Tasks\", \"endpoint\": \"$HOST\", \"tags\": \"\", \"value\": `grep -E '^Tasks:' $tmp_info |cut -d ' ' -f 2`,\"timestamp\": $DATE, \"counterType\": \"GAUGE\", \"step\": 30}"
echo -n ",{\"metric\": \"Haproxy_SessRate\", \"endpoint\": \"$HOST\", \"tags\": \"\", \"value\": `grep -E '^SessRate:' $tmp_info |cut -d ' ' -f 2`,\"timestamp\": $DATE, \"counterType\": \"GAUGE\", \"step\": 30}"
echo -n ",{\"metric\": \"Haproxy_Run_queue\", \"endpoint\": \"$HOST\", \"tags\": \"\", \"value\": `grep -E '^Run_queue:' $tmp_info |cut -d ' ' -f 2`,\"timestamp\": $DATE, \"counterType\": \"GAUGE\", \"step\": 30}"
echo -n ",{\"metric\": \"Haproxy_Idle_pct\", \"endpoint\": \"$HOST\", \"tags\": \"\", \"value\": `grep -E '^Idle_pct:' $tmp_info |cut -d ' ' -f 2`,\"timestamp\": $DATE, \"counterType\": \"GAUGE\", \"step\": 30}"
##haproxy stat
echo "show stat" |sudo socat /var/lib/haproxy/stats stdio > $tmp_stat 2>/dev/null
for line in `grep -vE '#|FRONTEND|BACKEND' $tmp_stat | sed '/^$/d'`;
do
server=`echo $line |cut -d ',' -f 1,2 |sed 's/,/_/'`
status=`echo $line |cut -d ',' -f 18`
if [ $status == 'UP' ];
then
status_code=0
else
status_code=1
fi
scur=`echo $line |cut -d ',' -f 5`
qcur=`echo $line |cut -d ',' -f 3`
rate=`echo $line |cut -d ',' -f 34`
econ=`echo $line |cut -d ',' -f 14`
qtime=`echo $line |cut -d ',' -f 59`
ctime=`echo $line |cut -d ',' -f 60`
echo -n ",{\"metric\": \"Haproxy_Status\", \"endpoint\": \"$HOST\", \"tags\": \"server=$server\", \"value\": $status_code,\"timestamp\": $DATE, \"counterType\": \"GAUGE\", \"step\": 30}"
echo -n ",{\"metric\": \"Haproxy_Scur\", \"endpoint\": \"$HOST\", \"tags\": \"server=$server\", \"value\": $scur,\"timestamp\": $DATE, \"counterType\": \"GAUGE\", \"step\": 30}"
echo -n ",{\"metric\": \"Haproxy_Qcur\", \"endpoint\": \"$HOST\", \"tags\": \"server=$server\", \"value\": $qcur,\"timestamp\": $DATE, \"counterType\": \"GAUGE\", \"step\": 30}"
echo -n ",{\"metric\": \"Haproxy_Rate\", \"endpoint\": \"$HOST\", \"tags\": \"server=$server\", \"value\": $rate,\"timestamp\": $DATE, \"counterType\": \"GAUGE\", \"step\": 30}"
echo -n ",{\"metric\": \"Haproxy_Econ\", \"endpoint\": \"$HOST\", \"tags\": \"server=$server\", \"value\": $econ,\"timestamp\": $DATE, \"counterType\": \"GAUGE\", \"step\": 30}"
echo -n ",{\"metric\": \"Haproxy_Qtime\", \"endpoint\": \"$HOST\", \"tags\": \"server=$server\", \"value\": $qtime,\"timestamp\": $DATE, \"counterType\": \"GAUGE\", \"step\": 30}"
echo -n ",{\"metric\": \"Haproxy_Ctime\", \"endpoint\": \"$HOST\", \"tags\": \"server=$server\", \"value\": $ctime,\"timestamp\": $DATE, \"counterType\": \"GAUGE\", \"step\": 30}"
done
for line in `grep -E 'FRONTEND' $tmp_stat | sed '/^$/d'`;
do
server=`echo $line |cut -d ',' -f 1,2 |sed 's/,/_/'`
ereq=`echo $line |cut -d ',' -f 13`
dreq=`echo $line |cut -d ',' -f 11`
echo -n ",{\"metric\": \"Haproxy_Ereq\", \"endpoint\": \"$HOST\", \"tags\": \"server=$server\", \"value\": $ereq,\"timestamp\": $DATE, \"counterType\": \"GAUGE\", \"step\": 30}"
echo -n ",{\"metric\": \"Haproxy_Dreq\", \"endpoint\": \"$HOST\", \"tags\": \"server=$server\", \"value\": $dreq,\"timestamp\": $DATE, \"counterType\": \"GAUGE\", \"step\": 30}"
done
echo -n "]"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.