Skip to content

Instantly share code, notes, and snippets.

@james-huston
Last active February 7, 2018 13:21
Show Gist options
  • Save james-huston/42186969e4f984a70b8a to your computer and use it in GitHub Desktop.
Save james-huston/42186969e4f984a70b8a to your computer and use it in GitHub Desktop.
HAProxy stats to StackDriver using a shell script
# run this every minute from cron as a quick and dirty solution to get some
# haproxy stats on each of your listeners over to stackdriver
instance=$(curl -s "http://169.254.169.254/latest/meta-data/instance-id")
# while [ 1 ]; do
cp /dev/null /tmp/hastats.json
echo 'show stat' | socat - UNIX-CLIENT:/tmp/haproxy.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 '{"timestamp": '"$(date +%s)"', "proto_version": 1, "data": [
{ "name":"haproxy::'$pxname'::req_rate", "value":'${rate:-0}', "instance":"'${instance}'", "collected_at":'"$(date +%s)"' },
{ "name":"haproxy::'$pxname'::scur", "value":'${scur:-0}', "instance":"'${instance}'", "collected_at":'"$(date +%s)"' }
]}' >> /tmp/hastats.json
echo $'Sending data'
cat /tmp/hastats.json
curl -X POST \
-H "x-stackdriver-apikey: <your_key_here>" \
-H "Content-Type: application/json" \
--data @/tmp/hastats.json \
https://custom-gateway.stackdriver.com/v1/custom
echo $'\n'
done
# sleep 10
#done
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment