Thursday, October 3, 2019

Asterisk Balancing outbound calls across multiple random trunks

This code will send calls out trunks via a random generator

HOW IT WORKS
It adds a random generator and then will send calls across multiple trunks
(GUI) PBX -> Outbound Routes
Open the route you want to apply this random trunk
1: Under "Trunk Sequence" you want the first route to be the one if the time limit is NOT exceeded
2: The next route is the one ff the time IS exceeded. (you can have as many more as you want)
Make sure those changes are saved
Now open up /etc/asterisk/extensions_additional.conf
Find your route in the dialplan. it will look SOMETHING like the one below. Yours maybe different heading and number of trunks, so just use mine as an example only, modify your file.
This is mine, and i have TWO Destinations

[outrt-2] ; randomtrunk
include => outrt-2-custom
exten => _.,1,Macro(user-callerid,LIMIT,EXTERNAL,)
exten => _.,n,Set(MOHCLASS=${IF($["${MOHCLASS}"=""]?default:${MOHCLASS})})
exten => _.,n,Set(_NODEST=)
exten => _.,n,Gosub(sub-record-check,s,1(out,${EXTEN},))
exten => _.,n,Macro(dialout-trunk,1,${EXTEN},,off)
exten => _.,n,Macro(dialout-trunk,2,${EXTEN},,off)
exten => _.,n,Macro(outisbusy,)
;--== end of [outrt-2] ==--;

Copy and paste the entire section
open up /etc/asterisk/extensions_override_issabelpbx.conf  (if you are using a different deployment "extensions_custom.conf" should work as well
Go to the bottom of the file, and PASTE it

Now under the line with "GOSUB" put this value in

exten => _.,n,Set(randomtrunk=${RAND(1,2)});

The 1 and 2 signify the low and high number.  So if you have 2 trunks, it would be 1 & 2.  If you had 4, it would be 1 & 4.

These are the trunks are called. 
In my example two trunks are called, and if they do not work, "outisbusy" is run which is "all circuits are busy" message. You'll notice in my example it has "..trunk, 1, $ ..." and followed by "... trunk, 2, $ ...".
exten => _.,n,Macro(dialout-trunk,1,${EXTEN},,off)
exten => _.,n,Macro(dialout-trunk,2,${EXTEN},,off)
exten => _.,n,Macro(outisbusy,)
;--== end of [outrt-2] ==--;

It IS possible that, although mine are in numeric order, yours may not be. So just make note of that when you see the modification example
Change the first line to look like this instead. Just note if your 1 or 2 values ​​are in a different order and adjust accordingly.

execif($["${randomtrunk}" = "X"]?......

exten => _.,n,execif($["${randomtrunk}" = "1"]?macro(dialout-trunk,1,${EXTEN},,off))
exten => _.,n,execif($["${randomtrunk}" = "2"]?macro(dialout-trunk,2,${EXTEN},,off))


Just change the "X" value to be the match to one of your randomly generated values.

Heres what it will look like when you are done.

[outrt-2] ; randomtrunk
include => outrt-2-custom
exten => _.,1,Macro(user-callerid,LIMIT,EXTERNAL,)
exten => _.,n,Set(MOHCLASS=${IF($["${MOHCLASS}"=""]?default:${MOHCLASS})})
exten => _.,n,Set(_NODEST=)
exten => _.,n,Gosub(sub-record-check,s,1(out,${EXTEN},))

exten => _.,n,Set(randomtrunk=${RAND(1,2)});
exten => _.,n,execif($["${randomtrunk}" = "1"]?macro(dialout-trunk,1,${EXTEN},,off))
exten => _.,n,execif($["${randomtrunk}" = "2"]?macro(dialout-trunk,2,${EXTEN},,off))

exten => _.,n,Macro(outisbusy,)
;--== end of [outrt-2] ==--;

From the command line type
asterisk -rx 'core reload'
Test and try!
Please note that once this code is in place, you can't change the trunk order in the GUI for this outbound route. This code will over-ride the GUI for that. So if you DO need to make changes, you can make the change the GUI, but you will need to re-perform these custom modifications for it to work.
Anyone who is trying this, and you should only do this in a test server if you aren't familiar, but if you DO have problems and its messing up your PBX, in the "extensions_override_issabelpbx.conf" file. You can just erase these changes and save / reload the file and your pBX will revert to its original config for this rule.

No comments:

Post a Comment