Is there a way to view the current state (Master/Backup) using a command line inside a server instance?



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:

# notify.sh

echo $1 $2 is in $3 state > /var/run/keepalive.$1.$2.state

And a get state script like:

# getstate.sh

cat /var/run/keepalive.*.*.state
  • Nice solution! But what if you wanted to check the status without any state transition? Is it possible without using a patch? – Ben Dec 9 '13 at 1:19
  • You mean if initial startup state is equal to current state? I guess you could just write out the /var/run/keepalive.*.*.stat file as part of your startup script. – dmourati Dec 9 '13 at 1:44
  • I mean, if the keepalived is currently running. How can I determine the current running state without having a startup script? – Ben Dec 9 '13 at 2:06
  • Try the above, I think it will work as is. – dmourati Dec 9 '13 at 21:06

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.

  • As of this post, this does NOT work on CentOS 7 from the native repositories. Yum in the repo is 1.2.13. This has been solved in later versions if you want to compile it. – jnovack Oct 8 '15 at 15:56

If you issue the command:

journalctl -u keepalived

it has the state listed:

Jul 12 13:45:52 vmt007 Keepalived_vrrp[14335]: VRRP_Instance(VI_INT) Entering MASTER STATE

on the slave side you can see:

Jul 12 13:45:51 vmt008 Keepalived_vrrp[3569]: VRRP_Instance(VI_INT) Entering BACKUP STATE

Dumping current state can be done by sending USR2 signal to keepalived parent process:

kill -USR2 `cat /var/run/keepalived.pid`

See result in /tmp/keepalived.stats.

Note: if SELinux is used (CentOS 7), it does not allow writing to this file. You can get around this with this preparation:

touch /tmp/keepalived.stats
chmod go+w /tmp/keepalived.stats
semanage fcontext -a -t keepalived_var_run_t /tmp/keepalived.stats
restorecon /tmp/keepalived.stats

Now try sending the signal again.


With version 1.3.0, keepalived added a DBus interface, that can be enabled with the enable_dbus option in the global_defs block of the config file (However, the interface must be enabled with the --enable-dbus build option during configure, which might not be the case, if you are using a binary package.).

You can access the DBus interface with any DBus client/library (e.g. dbus-send, gdbus, qdbus). I'm using systemd's busctl as an example here, because it has a very nice interface:

# busctl tree org.keepalived.Vrrp1
      │ └─/org/keepalived/Vrrp1/Instance/eth0
      │   └─/org/keepalived/Vrrp1/Instance/eth0/1
      │     └─/org/keepalived/Vrrp1/Instance/eth0/1/IPv4

Two interfaces are available, the global org.keepalived.Vrrp1.Vrrp on /org/keepalived/Vrrp1/Vrrp:

# busctl introspect org.keepalived.Vrrp1 /org/keepalived/Vrrp1/Vrrp org.keepalived.Vrrp1.Vrrp
.CreateInstance           method    ssuu      -            -
.DestroyInstance          method    s         -            -
.PrintData                method    -         -            -
.PrintStats               method    -         -            -
.ReloadConfig             method    -         -            -
.VrrpReloaded             signal    -         -            -
.VrrpStarted              signal    -         -            -
.VrrpStopped              signal    -         -            -

And foreach VRRP instance, the org.keepalived.Vrrp1.Instance interface on paths according to this template /org/keepalived/Vrrp1/Instance/<interface>/<virtual-router-id>/<ip-family> org.keepalived.Vrrp1.Instance. For an IPv4 VRRP instance with id 1 on eth1 named my-instance, we get the following:

# busctl introspect org.keepalived.Vrrp1 /org/keepalived/Vrrp1/Instance/eth0/1/IPv4 org.keepalived.Vrrp1.Instance
NAME                          TYPE      SIGNATURE RESULT/VALUE   FLAGS
.SendGarp                     method    -         -              -
.Name                         property  s         "my-instance"  emits-change
.State                        property  (us)      2 "Master"     emits-change
.VrrpStatusChange             signal    u         -              -

To get the state of this particular instance, we can use the following command

# busctl get-property org.keepalived.Vrrp1 /org/keepalived/Vrrp1/Instance/br_vrrp/67/IPv4  org.keepalived.Vrrp1.Instance State
(us) 2 "Master"

The returned property is a STRUCT, with state code and the human readable-name. The states are 0 "Init", 1 "Backup", 2 "Master", 3 "Fault", 4 "Goto master" and 98 "Goto fault". The last three states are internal and official RFC 2338 states.

If you're using multiple keepalived processes and set the instance option or if you're using the network namespace feature with the namespace option, the path prefix changes /org/keepalived/Vrrp1/<namespace>/<interface>.


I use this simple line:

service keepalived status | tail -1

Sorry, but this won't work if any errors occur on the last line.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.