PRI failover trunk

Hi guys!
Does anyone have some experience or kind of “best practice” how to configure a failover trunk over PRI networks?
F.e. I have 2 port Dgium card and two PSTN providers, and both of them can handle calls to same destinations, but one of them should be used only if the first provider is out of service.
My only idea is to do some checks in dialplan like:

exten => _9X.,1,Dial(DAHDI/g1/${EXTEN})
exten => _9X.,n,GotoIf($["${DIALSTATUS}" = "CHANUNAVAIL"]?20)
exten => _9X.,n,GotoIf($["${DIALSTATUS}" = "CONGESTION"]?20)
exten => _9X.,n,GotoIf($["${DIALSTATUS}" = "BUSY"]?20)
exten => _9X.,n,GotoIf($["${DIALSTATUS}" = "CANCEL"]?20) 

exten => _9X.,20,Dial(DAHDI/g2/${EXTEN})

Is there any more accurate way? Or may be I should check some other DIALSTATUS values?
And advice will be appreciated.

You will never get CANCEL in that context, as the channel will have gone down.

${HANGUPCAUSE} will give you the ISDN clearing cause code, which is about as much detail as you will get. For technologies other than ISDN, Asterisk will try to generate a code that best matches the native code. Recent versions have mechanisms for finding the native code, for SIP.

Some of your conditions will trigger for all circuits busy, and if that is all you want, simply putting them all in the same group should be enough.

Thankds, david55.
I’ve looked through the information about ${HANGUPCAUSE}: … angupcause

Seems that I’ll have to find all “normal” ${HANGUPCAUSE} values and route call to a backup provider in case ${HANGUPCAUSE} is not equal to “normal” values.