Split sip call trunk to two different destinations

Is it possible to split sip trunk call to two different destinations eg. 50% calls to one destination and 50% to another

exten => 345687,n,Dial(SIP/458686@test_new1)
exten => 345687,n,Dial(SIP/458687@test_new2)

if possible please tell me how to do it.
thank you.

Being a trunk isn’t relevant here, and isn’t a term that SIP uses. You have different user, as well as domain, parts.

One way is with a queue, with an appropriate strategy.

Another way is with a global variable that you increment for each call and test for odd or even… You can probably tolerate race conditions, but if not, you may have to use locks around the increment and test.

If this is a new system, please consider chan_psjip, instead of chan_sip, as support for the latter is limited.

The channel variable ‘uniqueid’ consists of the ‘EPOCH’ (seconds since Unix time 0), a period, and the number of channels created since this instance of Asterisk was started. You could ‘even/odd’ this part and get a pretty good distribution depending on other channel creation activity.

Although this is probably safe, even in the long term, one isn’t actually supposed to know the internal structure of unique IDs.

I Tried this but didn’t work call got routed to one directions only and not the other


exten => 345687,n,GotoIf($["${GLOBAL(MYGLOBALVAR)}" = “0”]?dial1)
exten => 345687,n,GotoIf($["${GLOBAL(MYGLOBALVAR)}" = “1”]?dial2)
exten => 345687,n(dial1),Dial(SIP/458686@test_new1)
exten => 345687,n,Set(GLOBAL(MYGLOBALVAR)=1)
exten => 345687,n,Hangup()
exten => 345687,n(dial2),Dial(SIP/458686@test_new2)
exten => 345687,n,Set(GLOBAL(MYGLOBALVAR)=0)
exten => 345687,n,Hangup()

All calls went to SIP/458686@test_new1
but not into SIP/458686@test_new2

what did i do wrong? Please help.

you can use condition like this:

  • if seconds div 2 =0 then go to dest 1
  • else go to dest 2

(Please wrap code and console snippets in preformatted text tags so the forum doesn’t mung special characters like quotes and dollar signs.)

Not so much wrong, as not understanding.

When the call exits, the dial() application does not (usually) return to the next priority in the dialplan. Execution usually continues at the ‘h’ extension. Thus, your set()'s are never executed.

This default behavior can be changed with the ‘g’ option.

You can modify your dialplan so the set() is executed before the dial() or you can add the ‘g’ option. Personally, I’d go with the set() before the dial(). It seems more obvious and it may save the next guy a trip to the dox to figure out what ‘g’ does.

A few other suggestions on your dialplan:

  1. Use same = n for a lot of reasons. Clarity, reliability, debugging, maintainability, etc.
  2. Add some whitespace to ease reading and maintenance like:
        same = n(dial2),                set(GLOBAL(MYGLOBALVAR)=0)
        same = n,                       dial(sip/7604683867@vitel-outbound,30,r)
        same = n,                       hangup()
  1. Start your context with something to help with debugging like:
        same = n,                       verbose(1,[${EXTEN}@${CONTEXT}!${MYGLOBALVAR}])
  1. Use a more descriptive variable name like DISTRIBUTOR
  2. The GLOBAL() function is not needed to read the global variable value unless you also have a channel variable of the same name. After thinking about it for a couple billion nanoseconds, keep it in so it is more obvious to the next guy and everything won’t fall down if somebody hacks in a channel variable of the same name for some other purpose.
  3. The first gotoif() is unnecessary.

(Note that several of these suggestions are just my personal preferences.)

So if I were writing this dialplan using a global variable, it would look something like:

; distribute calls between destinations
        exten = 345687,1,               verbose(1,[${EXTEN}@${CONTEXT}!${DISTRIBUTOR}])
        same = n,                       gotoif($["${GLOBAL(DISTRIBUTOR)}" = "1"]?destination1)

; destination 0
        same = n,                       set(GLOBAL(DISTRIBUTOR)=1)
        same = n,                       dial(SIP/458686@test_new1)
        same = n,                       hangup()

; destination 1
        same = n(destination1),         set(GLOBAL(DISTRIBUTOR)=0)
        same = n,                       dial(SIP/458686@test_new2)
        same = n,                       hangup()

For 2 completely different approaches, take a look at Asterisk send calls to SIP trunks with round-robin call distribution from 11 years ago!

1 Like