Thursday, August 9, 2012

How to setup failover for multiple SIP Trunks?

 

Problem Statement:
Note: ITSP stands for Internet Telephony Service Provider
Inbound call stands for call coming from ITSP to Asterisk
Outboud call stands for call from Asterisk to ITSP
ServerA and ServerB stands for Asterisk server at location A and B.
I have also made an assumption that you know how to install asterisk and configure SIP Peers/Trunks. 

I have two SIP Trunks (Trunk_A and Trunk_B) from ITSP coming into two Asterisk servers at different physical location.

Inbound call is not an issue. ITSP periodically checks if servers are available. If ServerA dies, incoming call comes only to serverB. If ITSP sees that serverA is alive, ITSP starts sending call to ServerA. [Note: No manual intervention involved here]

I would like to achieve something similar to above in Asterisk for outbound calls. I want Asterisk to periodically check if trunks are alive or not. If TrunkA fails, it shouldn't send the call to TrunkA. Once TrunkA is alive, it should start sending call to it. 

Problem Solution:

I did the following to address my problem

SIP configuration in ServerA

file: /etc/asterisk/sip.conf
----
-----
-----
[sip_trunkA-out]
type = peer
fromdomain = serverA_IP_address
host = ITSP_A_IP_address
qualify=yes
qualifyfreq=30 ; Qualification: How often to check for the host to be up in seconds
; and reported in milliseconds with sip show settings.
; Set to low value if you use low timeout for NAT of UDP sessions
qualifygap=60 ; Number of milliseconds between each group of peers being qualified



Dial plan in ServerA

file:/etc/asterisk/extensions.conf

exten => _XXXXXXXXXX,1,Verbose(1,--- Dialing out from Trunk A)
  same => n,Set(NETWORKSTATUS=${SIPPEER(sip_trunkA-out,status)})
  same => n,Gotoif($[NETWORKSTATUS=UNREACHABLE]?trunkB:trunkA)
  same => n(trunkA),Dial(SIP/sip_trunkA-out/${EXTEN:1})
  same => n,Hangup
  same => n(trunkB),Dial(SIP/ServerB/${EXTEN})
  same => n,Hangup



Do the similar SIP configuration and dialplan on ServerB.

Dialplan Logic: If TrunkA is unreachable from ServerA, use TrunkB through ServerB. Similarly if TrunkB is unreachable from ServerB, use TrunkA through ServerA. SIPPEER() function is used to retrieve the status of SIP Trunk/Peer.
Please also note that you might have to customize the dialplan to meet your requirement

I have posted this solution at asterisk.org forum as well.