How to setup failover for multiple SIP Trunks from ITSP?

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

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.

Inbound call is not an issue. ITSP is smart to send call to us in Round Robin fashion (first incoming call on serverA and second incoming call on serverB). If serverA dies, incoming call comes only to serverB. ITSP periodically pings serverA and 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. I would like to setup serverA such that it will send calls to TrunkA and TrunkB in Round Robin fashion(first call to TrunkA and second call to TrunkB). If TrunkA fails, it shouldn’t send the call to TrunkA. I also want Asterisk to periodically ping TrunkA to check if trunk is alive or not. Once TrunkA is alive, it should start sending call to it.

I am wondering if you guys came across this issue. It would be great if you can provide any resources or assistance.

Use qualify to detect the loss of a trunk. Use a global variable to track which one you last used, when both are available. Analyse the HANGUPCAUSE to determine whether calls fail due to a down trunk or a more distant problem.

Ask support questions in Asterisk Support!

Thank you David. Your answer helped to resolve the problem.
I did the following to address my problem

file: /etc/asterisk/sip.conf


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
; Default: 60
qualifygap=60 ; Number of milliseconds between each group of peers being qualified
; Default: 100


same => n,Set(NETWORKSTATUS=${SIPPEER(sip_trunkA-out,status)})
same => n,Gotoif($[NETWORKSTATUS=UNREACHABLE]?101:)
same => n,Dial(SIP/sip_trunkA-out/${EXTEN:1})
same => 101,Dial(SIP/sip_trunkB-out/${EXTEN:1})

[Logic: If TrunkA is unreachable, go to TrunkB;
Please also note that you might have to customize the dialplan to meet your requirement]

I hope this will help someone else who is looking the solution for the similar problem.

Hey David, Same issue was with me, I was searching for the answer… Thanks to post it here. I hope you will help me if I need.