Technical - POSSA/freepbx-trunk-balancing GitHub Wiki
General Technical Description
The way this module works:
Database: asterisk, table: trunkbalance. Store the different 'balanced trunk' and rules that you are creating in the module page.
Each 'balanced trunk' is also creating a custom trunk in freepbx that has its name starting with BAL_ and the destination being 'Balancedtrunk/name of the balanced trunk'. The destination is not used for routing, but just as reminder when displayed in the trunk selection in your outbound route.
When you reload, Freepbx will add in extensions_additional.conf the line exten => s,n,AGI(trunkbalance.php,${ARG1}) in the [macro-dialout-trunk].
Every outbound call going through the [macro-dialout-trunk] (i.e. any call except dundi and enum) will then be evaluated by the trunkbalance.php agi.
/agi-bin/trunkbalance.php :
First check if the trunk used is one of the 'balanced trunk', if not the agi returns to the macro without doing anything. If the trunk is one of the 'balanced trunk', then the rules are evaluated in the following order. time condition: test the conditions if a time group was selected for this 'balanced trunk'.
ratio rule: if the ratio is >1, pick a random number between 1 and the ratio. If the random is one, then the rule passed.
number of calls: pool the asteriskcrdrb database, table crd and request the number of 'ANSWERED' outbound calls made on the destination real trunk since the last billing date [or the beginning of the cdr if no date is entered] and matching the rules entered in 'balanced trunk' filter. If this number is below the max allowed by the rule, the rule passed.
number of different calls: same as the previous rule, except that we add a DISTINCT clause in the sql query. Ver 1.0.0 has logic flaw with this approach, if the outbound call matches one of the distinct numbers in the search result, call should be allowed regardless if whether maximum is reached. See ticket #9
duration of calls: sum the billing time of calls made on the destination real trunk since the last billing date [or the beginning of the cdr if no date is entered] and matching the rules entered in 'balanced trunk' filter. If this number is below the max allowed by the rule, the rule passed.
expiration date: check if the today's date is before the expiration date.
If a rule is entered as 0, it is not evaluated. If all the rules passed, the channel trunk number will be changed to the destination real trunk and the agi will return to the macro. The call will then progress as if it was made directly on the real trunk.
If the rule failed, the channel trunk number will not be changed, and the macro will ended with a channel unavailable as this balanced trunk does not go anywhere. The call will then progress to the next trunk listed in the outbound route.