HAproxy: Run script on health check change
I would probably do this with an external check script and use this to control the rise and fall values. Then when you meet the rise or fall value you can run your own script. Set the rise and fall in your haproxy config to be 1 and change the check to external
external-check command ping.sh
server s1_a 1.2.3.4:3600 check inter 5s fall 1 rise 1
server s2_b 1.2.3.5:3600 check backup
timeout queue 60s
timeout server 60s
timeout connect 60s
Then as a rough example with an external ping check
#!/bin/bash
PATH=/usr/local/sbin:/usr/local/bin:/sbin:/bin:/usr/sbin:/usr/bin:/root/bin
# Set the maximum time in seconds to allow ping to run, 1-10 is a sensible range.
TMEOUT=1
# Interval between pings in seconds, 0.2-1 is a sensible range.
INT=1
# Number of pings to send.
NUM=1
RIP=$(echo ${3}))
VIP=$(echo ${1})
#Optionally provides source IP for Ping
SRC=
if [ "$SRC" != '' ]; then
SRC_IP="-I $SRC"
fi
if ping ${SRC_IP} -n -w${TMEOUT} -i${INT} -c${NUM} ${RIP} >& /dev/null; then
COUNTER="$(cat count)"
if [COUNTER != 0]; then
//run up script here
fi
echo 0 >> count
exit ${?}
else
COUNTER="$(cat count)"
if [COUNTER >= 3]; then
//run down script here
fi
COUNTER=$[COUNTER + 1]
echo COUNTER >> count
exit ${?}
fi
You may also be able to do this with lua but I've not looked at that. I've done a couple of other options for email alerts like polling the stats socket or hacking haproxy which could be adapted but the external check will probably work best here. However if you wish to try them out you can find them here https://www.loadbalancer.org/blog/3-ways-to-send-haproxy-health-check-email-alerts/ .