I have set up a haproxy configuration with a backend with two servers looking like that:
...
default
option log-health-checks
...
mailers mta
mailer smtp1 127.0.0.1:25
...
backend s_api
balance roundrobin
option httpchk GET /sites?site=q&limit=1
http-check expect rstatus (2|3)[0-9][0-9]
server s1_a 1.2.3.4:3600 check inter 5s fall 4 rise 1
server s2_b 1.2.3.5:3600 check backup
timeout queue 60s
timeout server 60s
timeout connect 60s
email-alert mailers mta
email-alert level notice
email-alert from [email protected]
email-alert to [email protected]
I now get noticed via email when a server gets down and up again. What I would like to archive is to start a local bash script when a server is going up and another one when the server is going down.
How could that be done?
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
Then as a rough example with an external ping check
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/ .