Asterisk: round-robin dial out through trunks

Imagine that we have 3 trunks for dial out and we must balance outgoing calls in random order. This could be done with the RAND function. Module func_rand.so must be compiled and loaded.

sip.conf:

[trunk1](trunk_preset)
host=10.10.10.11

[trunk2](trunk_preset)
host=10.10.10.12

[trunk3](trunk_preset)
host=10.10.10.13

extensions.conf:

exten => _79XXXXXXXXX,1,NoCDR()
    same => n,Dial(SIP/trunk${RAND(1,3)}/${EXTEN})
    same => n(hang),Hangup()

You may say – well, OK, but what if my trunks are named not like [trunk1], [trunk2], [trunkN] and I can not substitute the last symbol in Dial application?
In this case we can write a list of trunk names (which you configured already in sip.conf) in txt file, for example rrtrunks.txt:

teliasonera-out
deutschetelekom-out
level3-out

Ensure that a ‘sort’ command is available in your *NIX system. Usually it’s a part of coreutils. Older versions don’t suit us, because they don’t have the ‘-R’ flag which means sort randomly. Newer versions have.

Then load func_shell.so and use it in your dialplan. It can execute a shell command and use its output in dialplan.

exten => _79XXXXXXXXX,1,NoCDR()
    same => n,Dial(SIP/${SHELL(sort -R /etc/asterisk/rrtrunks.txt | head -1)}/${EXTEN},20)
    same => n,Hangup()

It means that we sort randomly our list of trunks and get the first one each time.

Tags: , , , , , , , ,

Comments are closed.