#!/bin/sh
set -x
#FLIP1405 - Failover Server Solution
#RUN ON PRIMARY ASTERISK SERVER
#ORIGINAL AUTHOR GREGG HANSEN 20080208 &lt;hansen.gregg@gmail.com&gt;
#MODIFIED BY GREGORY BOEHNLEIN 20090201 &lt;damin@nacs.net&gt;
#DEPENDENCIES: nmap, arping

# Version 1.0 - 2009-03-25
# - Consolidated Master/Slave scripts into a single script
# - Converted hardcoded interface / IP configuration to variable based
# - Forced Asterisk to issue a "reload" to bind to the floating IP
# - Consolidated external rsync-replicate script to be self-contained

# If set to "1" this is the Master server.
# If commented out, or set to anything else, this will act as if it is a slave
MASTER="1"

# The Master and Slave IP Addresses for Replication / Testing
MASTERIP="172.16.3.12"
SLAVEIP="172.16.3.13"

# The IP address that will float between Master and Slave
FLOAT="172.16.3.10"

# The device on which the floating interface exists
DEVICE="eth0"

# The specific interface alias on the device
IFACE="$DEVICE:1"

# Main Code
if [ "$MASTER" == "1" ] ; then
#if Local Asterisk up = 'open|filtered'
STATUS=$(nmap --system-dns -p 4569 -sU 127.0.0.1 | awk '{print $2}' | grep open)
#if primary owns virtual = '207.166.192.51'
PRIMARYIP=$(/sbin/ifconfig "$IFACE" | grep "$FLOAT" | awk '{print $2}' | sed 's/addr://g')
#if virtual is not pingable = 'down.'
VIRTUALIP=$(nmap --system-dns -sP "$FLOAT" | grep down | awk '{print $4}')

# Configuration File Replication from Master to Slave
rsync -avzr --rsh=ssh /etc/asterisk/ root@$SLAVEIP:/etc/asterisk/
rsync -avzr --rsh=ssh /var/spool/asterisk/voicemail root@$SLAVEIP:/var/spool/asterisk
rsync -avzr --rsh=ssh /var/lib/asterisk/moh root@$SLAVEIP:/var/lib/asterisk
rsync -avzr --rsh=ssh /var/lib/asterisk/sounds root@$SLAVEIP:/var/lib/asterisk
rsync -avzr --rsh=ssh /var/www/html root@$SLAVEIP:/var/www
rsync -avzr --rsh=ssh /var/www/db root@$SLAVEIP:/var/www
rsync -avzr --rsh=ssh /tftpboot root@$SLAVEIP:/
#rsync -avzr --rsh=ssh /usr/src root@$SLAVEIP:/usr/src

#/root/rsync_replicate &gt; /dev/null 2&gt; /dev/null
if [ "$STATUS" == "open|filtered" ] ; then  ###is primary asterisk up?
if [ "$PRIMARYIP" != "$FLOAT" ] ; then  ###does primary not own virtual ip?
if [ "$VIRTUALIP" == "down." ] ; then  ###is the virtual IP not pingable?
/sbin/ifconfig $IFACE $FLOAT/24 up
/sbin/arping -U -c 5 -I $DEVICE $FLOAT  ###Gratuitous ARP request
/sbin/service asterisk restart
/sbin/service httpd restart
/sbin/service ntpd restart

fi
fi
else
/sbin/service asterisk start
/sbin/service httpd restart
/sbin/service ntpd restart
/sbin/ifconfig $IFACE down
fi
else # We must be running as Slave node
###if Primary Asterisk up = 'open|filtered'
PRISTATUS=$(nmap --system-dns -p 4569 -sU $MASTERIP | awk '{print $2}' | grep open)
###if Secondary Asterisk up = 'open|filtered'
SECSTATUS=$(nmap --system-dns -p 4569 -sU 127.0.0.1 | awk '{print $2}' | grep open)
###if local owns Virtual = '207.166.192.51'
PRIMARYIP=$(/sbin/ifconfig "$IFACE" | grep "$FLOAT" | awk '{print $2}' | sed 's/addr://g')
###if Virtual not pingable = 'down.'
VIRTUALIP=$(nmap --system-dns -sP "$FLOAT" | grep down | awk '{print $4}')

if [ "$PRISTATUS" != "open|filtered" ] ; then   ###is primary asterisk down?
if [ "$SECSTATUS" == "open|filtered" ] ; then     ###is secondary asterisk up?
if [ "$PRIMARYIP" != "$FLOAT" ] ; then   ###does secondary not own virtual ip?
if [ "$VIRTUALIP" == "down." ] ; then  ###is the virtual IP not pingable?
/sbin/ifconfig $IFACE $FLOAT/24 up
/sbin/arping -U -c 5 -I $DEVICE $FLOAT   ###Gratuitous ARP request
/sbin/service asterisk restart
/sbin/service httpd restart
/sbinservice ntpd restart
fi
fi
else
/sbin/service asterisk start
/sbin/service httpd restart
/sbin/service ntpd restart
fi
else
if [ "$SECSTATUS" == "open|filtered" ] ; then ###primary is up, is secondary up? (there can be only one!)
/sbin/service asterisk stop
else
echo
if [ "$PRIMARYIP" == "$FLOAT" ] ; then
# If the Primary is up but we still own the Virtual IP, shut it down
/sbin/ifconfig $IFACE down
fi
fi
fi
fi
