Asterisk Failover (Virtual IP) solution

I have been searching for a solution that allows for a cluster of Asterisk servers to co-operate and fail over as necessary. Usually with the original configuration of Asterisk, if configured correctly, will restart itself automatically should the instance crashes. This results in somewhat of a resilient server that recovers itself, the downside is that most of the time all the calls will have been disconnected. That may seem alright for a small company, but for larger organization this could mean hundreds of customer service calls getting disconnected. Also, if the server experience some type of hardware (HD, CPU, PS) failure there’s no chance that the server will come back up within a short time frame. In a perfect world, the cluster would keep track of all concurrent calls and transfer them over to a different server soon after the failure before the SIP session times out and disconnect all calls. I have yet to find a solution that will work like that for Asterisk. However, I’ve found a solution that will allows for a failover in case of a hardware failure. Yes, calls will get disconected but at least the organization will be able to continue to function until the affected server is recovered.

CREDIT: Credit goes to Gregg Hansen, creator of this solution and author of http://www.thiscoolsite.com

Requirements:
1) nmap
2) arping
3) download the two scripts located of Gregg’s site:  flip1405_primaryflip1405_secondaryor copy and paste the scripts at the end of this page

Steps (copy and pasted from Gregg’s Site):
1) Create a cron.every1 folder on each server, copy flip1405_primary and flip1405_secondary to their respective servers.
2) Make sure you edit the files with your IP addressing scheme. I commented what you need to find and replace.
3) Setup shared-keys so the servers can copy between each other without user intervention. (
Setup Login without password)
4) Schedule a “rsync -avz /etc/asterisk/* root@secondaryIP:/etc/asterisk”
To replicate the configs from the primary to the secondary every 30 minutes or so (another cronjob).
You also can use the same command (diff dir) to replicate all other files, sounds, VM, etc.)

This works out so you can reboot your primary box, the secondary will take over for the time being. As soon as the primary is up, the secondary will resume slave/standby mode.

SCRIPTS:
flip1405_primary

#!/bin/sh
#FLIP1405 - Failover Server Solution
#RUN ON PRIMARY ASTERISK SERVER
#AUTHOR-GREGG HANSEN 20080208
#DEPENDENCIES: nmap, arping
#
#Do a find and replace on the following IP with your Virtual IP
#192.168.5.30  =  VIRTUAL IP

STATUS=$(nmap -p 5060 -sU 127.0.0.1 | awk '{print $2}' | grep open)                     #if Local Asterisk up = 'open|filtered'
PRIMARYIP=$(ifconfig eth0:1 | grep 192.168.5.30 | awk '{print $2}' | sed 's/addr://g')   #if primary owns virtual = '192.168.5.30' 
VIRTUALIP=$(nmap -sP 192.168.5.30 | grep down | awk '{print $4}')                      #if virtual is not pingable = 'down.'

if [ "$STATUS" == "open|filtered" ] ; then  ###is primary asterisk up?   
	if [ "$PRIMARYIP" != "192.168.5.30" ] ; then  ###does primary not own virtual ip?
		if [ "$VIRTUALIP" == "down." ] ; then  ###is the virtual IP not pingable?
		       ifconfig eth0:1 192.168.5.30/24 up
			arping -U -c 5 -I eth0 192.168.5.30  ###Gratuitous ARP request
		fi
	fi
else
	service asterisk start
	ifconfig eth0:1 down
fi

flip1405_secondary

#!/bin/sh
#FLIP1405 - Failover Server Solution
#RUN ON SECONDARY ASTERISK SERVER
#AUTHOR-GREGG HANSEN 20080208
#DEPENDENCIES: nmap, arping
#Find and Replace the following IPs with your IP scheme:
#192.168.5.30  = Virtual IP
#192.168.5.31  = Real IP Server 1

PRISTATUS=$(nmap -p 5060 -sU 192.168.5.31 | awk '{print $2}' | grep open)                     ###if Primary Asterisk up = 'open|filtered'
SECSTATUS=$(nmap -p 5060 -sU 127.0.0.1 | awk '{print $2}' | grep open)			    ###if Secondary Asterisk up = 'open|filtered'
PRIMARYIP=$(ifconfig eth0:1 | grep 192.168.5.30 | awk '{print $2}' | sed 's/addr://g')   ###if local owns Virtual = '192.168.5.30' 
VIRTUALIP=$(nmap -sP 192.168.5.30 | grep down | awk '{print $4}')                      ###if Virtual not pingable = 'down.'

if [ "$PRISTATUS" != "open|filtered" ] ; then   ###is primary asterisk down?
	if [ "$SECSTATUS" == "open|filtered" ] ; then     ###is secondary asterisk up?     
		if [ "$PRIMARYIP" != "192.168.5.30" ] ; then   ###does secondary not own virtual ip?
			if [ "$VIRTUALIP" == "down." ] ; then  ###is the virtual IP not pingable?
		       ifconfig eth0:1 192.168.5.30/24 up
			arping -U -c 5 -I eth0 192.168.5.30   ###Gratuitous ARP request
			fi
		fi

	else
		service asterisk start
	fi
else
	if [ "$SECSTATUS" == "open|filtered" ] ; then ###primary is up, is secondary up? (there can be only one!)
		service asterisk stop
	else
 			if [ "$PRIMARYIP" == "192.168.5.30" ] ; then ###primary is up, do we still have the virtual ip?
			ifconfig eth0:1 down
			fi
	fi
fi

 

Leave a Reply

Please log in using one of these methods to post your comment:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

This site uses Akismet to reduce spam. Learn how your comment data is processed.