So, with a friday and a saturday worth of work on my home automation move here is what is complete.

  1. install server os on vm
  2. install openhab and all bindings currently in use
  3. move openhab configs over to new vm
  4. shutdown old openhab-pi
  5. configure raspberry pi with virtualhere server to share usb
  6. configure new vm server to connect to raspberry pi to communicate with z-wave stick
  7. install keepalived on new server
  8. configure virtual ip as my new primary ip for openhab access
  9. configure scripts to run to start openhab and connect to shared usb
  10. clone server to secondary vm for failover
  11. reconfigure keepalived to make second box slave
  12. test failover

So let me show you my keepalived settings and my scripts.

/etc/keepalived/keepalived.conf

 1     state MASTER
 2     interface eth0
 3     virtual_router_id 220
 4     priority 150
 5     notify /usr/local/sbin/notify-keepalived.sh
 6     advert_int 1
 7     authentication {
 8           auth_type PASS
 9           auth_pass fakepass
10     }
11     virtual_ipaddress {
12         192.168.2.90
13     }
14}

 

See the “notify” line?    That script is pretty simple.

 1TYPE=$1
 2NAME=$2
 3STATE=$3
 4case $STATE in
 5        "MASTER") sleep 30
 6                  /usr/local/sbin/usb-connect.sh
 7                  /usr/sbin/service openhab start;;
 8        "BACKUP") /usr/sbin/service openhab stop
 9                  /usr/local/sbin/usb-disconnect.sh;;
10        "FAULT")  /usr/sbin/service openhab stop
11                  /usr/local/sbin/usb-disconnect.sh
12                  exit 0
13                  ;;
14        *)        /sbin/logger "unknown state"
15                  exit 1
16                  ;;
17esac

 

So what that does it every time there is a keepalived state change it notifies that script.   That script then runs additional scripts based on the state.   So when it goes to “MASTER” or at boot time (which is why i have to put that sleep statement in there) it runs usb-connect.sh.  Which just has a couple commands

1sleep 10
2/sbin/vhclient -t "USE,4294967409"

I’ll walk you through this one.

  1. runs the virtualhere usb client.
  2. waits a few seconds so the client can detect shared usb on the network
  3. sends a command to the running client “-t = command” specifying to “USE” the device with the id “4294967409”

The usb-disconnect.sh is a single line!

Thats it.  Just shutdown the client.   So now when the box boots up openhab1 becomes “MASTER” for openhab.  It then executes the scripts to connect to the shared USB.  Then starts openhab.

Once the second box is in place, all the same scripts and all will be put in place with 1 single change.

 

 1     state MASTER
 2     interface eth0
 3     virtual_router_id 220
 4     priority 200
 5     notify /usr/local/sbin/notify-keepalived.sh
 6     advert_int 1
 7     authentication {
 8           auth_type PASS
 9           auth_pass fakepass
10     }
11     virtual_ipaddress {
12         192.168.2.90
13     }
14}

Notice line 5, the priority is a higher number than in openhab1.  This means that when the boxes communicate they will negotiate who gets to be master.   Then either box can start openhab and whoever is running openhab gets the USB z-wave stick.