📜 ⬆️ ⬇️

Asterisk in examples: channel balancing

More and more organizations choose for telephony not astronomically expensive, terribly intricate and limited in functionality ready ATC, but modern, extensible and absolutely free software that can be installed on any Linux distribution. The most famous and widely used solution for telephony based on Linux is, of course, Asterisk .

Unfortunately for system administrators, Asterisk is not far from corporate PBXs in terms of ease of setup. Of course, Asterisk can, perhaps, everything that is possible to imagine, but the price for this is not a trivial setting.

During my work with Asterisk, I had a lot of different configuration examples. Fully digital faxes with the ability to send from any application in one click, intelligent recording of calls, all sorts of things with IVR, etc. etc. There will be interest - I will post it in time.
')
In the same post I would like to share a system of simple balancing of outgoing connections based on the “weight” of the channel . The simplest example of why this may be needed is calls through the usual SIM of several operators with unlimited tariffs. All operators have a certain maximum value of minutes that can be spoken for free at a tariff per month. Therefore, I would like to distribute outgoing calls on SIM cards in a certain proportion.

It is assumed that you can configure Asterisk in a basic way, know how to use dialplan, etc. To write another article for beginners, a la how to put Asterisk, it would be foolish.

On the other hand, a full-fledged solution to the problem of balancing based on the number of minutes implies setting up a database with statistics on the duration of conversations, AGI scripts, and other rather complex and heavy narratives. I will describe a simpler, though not completely comprehensive solution, which, nevertheless, has proven itself very well.

So: there are two SIP lines, each leads to its GSM channel. Tele2 and Megaphone operators. On Tele2, we have 300 minutes, on a megaphone - only 150. Accordingly, it is necessary that Tele2 received twice as many calls. In addition, an outgoing call must go through an unoccupied SIM card, and if both channels are busy, then the caller must be informed about this by a kind aunt, who must also offer to wait for the release.

So, in sip.conf there is something like this line description:

; ################################ ; GSM  ; ################################ [gsm-lines](!) deny=0.0.0.0/0 permit=10.42.42.42/32 type=friend secret=******* qualify=yes host=dynamic callcounter=yes ;    DEVICE_STATE call-limit=1 ;  1    group = 1 context = from-gsm ;     insecure=invite canreinvite=no nat=no ;  [gsmline1](gsm-lines) ; Tele2 [gsmline2](gsm-lines) 

The important parameters here are callcounter , call-limit and context . I think everything is clear with them.

Actually, in extensions.conf the specified context is described as:

 ;   GSM  [from-gsm] ;  ( ) exten => +79310000000,1,Set(GROUP(gsm)=public) ;  ,     exten => +79310000000,n,Goto(to-internal,queue,1) ; Tele2 exten => +79520000000,1,Set(GROUP(gsm)=public) ;  ,     exten => +79520000000,n,Goto(to-internal,queue,1) 

Of course, you will have other names for the extensions (I used the numbers of the corresponding phones to make it easier). They are configured on the GSM gateway in the SIP connection settings for each channel.

Here it is important for us that for each incoming call a public group is established in the gsm category. This is necessary to calculate the current number of busy channels.

Now the most interesting is the outgoing context:

 ;    [to-gsm] ; ,     exten => _89XX.,1,GotoIf($["${GROUP_COUNT(public@gsm)}" >= "2"]?noline) ;    ... exten => _89XX.,n,Set(GROUP(gsm)=public) ;  ,     ; Ok,  -     ? exten => _89XX.,n,Set(PR=${EXTEN:1:3}) exten => _89XX.,n,GotoIf($[$["${PR}"="921"] | $["${PR}"="931"] | $["${PR}"="929"]]?prefer-megafon) ;    : ;  Tele2   300  ,   - 150. ;    ,     300 -    tele2,  -  exten => _89XX.,n,Set(BALANCE=${RAND(0,450)}) exten => _89XX.,n,GotoIf($[${BALANCE}<=300]?prefer-tele2:prefer-megafon) ;  Tele2  ,      exten => _89XX.,n(prefer-tele2),GotoIf($["${DEVICE_STATE(SIP/gsmline2)}" = "NOT_INUSE"]?tele2:megafon) ;    ... exten => _89XX.,n(prefer-megafon),GotoIf($["${DEVICE_STATE(SIP/gsmline1)}" = "NOT_INUSE"]?megafon:tele2) ;        . exten => _89XX.,n(tele2),Dial(SIP/gsmline2/${EXTEN},120,Tg) exten => _89XX.,n,Goto(after-dial,${EXTEN},1) ;   -  exten => _89XX.,n(megafon),Dial(SIP/gsmline1/${EXTEN},120,Tg) exten => _89XX.,n,Goto(after-dial,${EXTEN},1) ;   -  ;     exten => _89XX.,n(noline),Set(__CALLED_GSM_NUM=${EXTEN}) ;    exten => _89XX.,n,Goto(ivr-gsm,no-line,1) ;          exten => h,1,Goto(after-dial,h,1) 

In general, there are comments for all operations. For the distribution itself is responsible tricky trick with random. I’ll say right away - in practice, this very approximate solution for balancing showed results no worse than a full-fledged database with the calculation of the duration of each call. You do buy free minutes with a reserve, I hope? Those. you just need to roughly maintain a balance, which with success makes the proposed solution.

The after-dial context is needed for post-call processing. There can be any actions with the recording, the same calculation of the duration, etc. For this article, this is all irrelevant.

But ivr-gsm is of some interest:

 ; IVR  GSM  [ivr-gsm] ;         exten => no-line,1,Background(no-gsm-line,,custom) exten => no-line,n,Wait(10) ;     -     exten => no-line,n,GotoIf($["${CALLED_GSM_NUM}" != ""]?to-gsm,${CALLED_GSM_NUM},1) ;  -    exten => no-line,n,Hangup() 

In the no-gsm-line file, the girl should say in a pleasant voice something like the following: All outgoing lines are busy. You can wait or try to call back later.

In principle, the dialplan proposed above simply expands to more lines. Just after checking DEVICE_STATE, if the desired line is busy, it should be sent to a certain extension, which will simply look for the first unoccupied one.

If the article is in demand - I will try to continue the series and post, for example, the configuration for digital fax.

Source: https://habr.com/ru/post/180445/


All Articles