View Current State of Keepalived
Solution 1:
You could use the notify command to write out a state file.
# for ANY state transition.
# "notify" script is called AFTER the
# notify_* script(s) and is executed
# with 3 arguments provided by keepalived
# (ie don’t include parameters in the notify line).
# arguments
# $1 = "GROUP"|"INSTANCE"
# $2 = name of group or instance
# $3 = target state of transition
# ("MASTER"|"BACKUP"|"FAULT")
notify /path/notify.sh
The create a notify script like:
#!/bin/bash
# notify.sh
echo $1 $2 is in $3 state > /var/run/keepalive.$1.$2.state
And a get state script like:
#!/bin/bash
# getstate.sh
cat /var/run/keepalive.*.*.state
Solution 2:
Reading the current status through SNMP has proven the most reliable for me. To enable this you have to start keepalived with snmp support:
- add -x to the deamon options (see /etc/sysconfig/keepalived on RedHat based systems)
and install snmpd.
You can then reliably query the status via
snmpget -Oq -Ov -v2c -cpublic localhost KEEPALIVED-MIB::vrrpInstanceState.1
It can also be done throug the notify scripts, but these don't always fire, leaving the state file out of sync with reality.