Monday, March 19, 2012

How to set the concurrent calls limit on SIP trunk in Asterisk?

Have you ever wanted to setup the concurrent calls limit on SIP trunk in Asterisk System? Ok, then you are in the right place to find your answers.


Scenario:

Suppose Server2 is from third party and we don't have a control over it. So, it can send any amount of calls to Server1. Our job is to limit the number of concurrent calls from Server2 to Server1. Thus, we will be working on Server1 to achieve our goal.


Method1:

If you are using Asterisk system, you might have already known that SIP Peer is also know as SIP trunk.
There is an easy way to set it up in SIP trunk/peer configuration using call-limit parameter.  (Warning!! This parameter is deprecated and Asterisk strongly discourages to use it)

#vi sip.conf
   context=mydefaultContext

   [SIPTrunkFORserver2]
   type=peer
   fromdomain=server1_IP_add
   host=server2_IP_add
   outboundproxy=server2_IP_add
   call-limit=100    
;Here I am limiting to 100 max concurrent calls thru' this trunk 
   context=FromServer2Trunk   
;Call from this trunk lands to [FromServer2Trunk] context

Pros: It's easy to implement
Cons: It's deprecated parameter, so it may not be available in future versions of Asterisk. (Luckily it is available in 1.8). I found that it requires Asterisk Restart when you want to remove call-limit parameter.


Method2 (preferred):

Looking at cons of using call-limit, I decided not to use it even though I found it to be really easy implementation. Similar goal can be achieved via the use of GROUP() and GROUP-COUNT() functions available in Asterisk Dialplan. You don't have to make any adjustments on SIP Trunk configuration for call limit (which  is good thing).

GROUP() function defines the trunk group
GROUP_COUNT() function returns the number of concurrent calls on the given trunk group


#vi sip.conf
#SIP configuration on Server1
   context=mydefaultContext

   [SIPTrunkFORserver2]
   type=peer
   fromdomain=server1_IP_add
   host=server2_IP_add
   outboundproxy=server2_IP_add
   call-limit=100    
   context=FromServer2Trunk   
;Call from this trunk lands to [FromServer2Trunk] context
 We are directing the call from server2 to [FromServer2Trunk] context in our dialplan.

#vi extensions.conf
#Asterisk dialplan on Server1

   [FromServer2Trunk]
   exten => _X.,1,Verbose(1,***** Server2  dialing ${EXTEN} *******)
        same => n,Set(GROUP()=server2Trunkgroup)
        same => n,Verbose(1,**** Number of concurrent calls are ${GROUP_COUNT(server2Trunkgroup)})
        same => n,GotoIf($[${GROUP_COUNT(server2Trunkgroup)} > 100]?999)
        same => n,Dial(SIP/telco/${EXTEN})

        same => 999,Verbose(1,***Number of concurrent calls are ${GROUP_COUNT(server2Trunkgroup)}  over limit)
        same => n,Set(DIALSTATUS=CHANUNAVAIL)

Since we are directing only the calls from Server2 to [FromServer2Trunk] context in our dialplan in Server1, we can safely use GROUP() function to define the name of the trunk group associated with the incoming calls from Server2. I named it as server2Trunkgroup

Now we can use GROUP_COUNT(server2Trunkgroup) to count the number of concurrent-calls flowing in to Server1 from Server2. We can use GotoIf statement to compare the concurrent calls with threshold value (say 100 in my case). If number of concurrent calls are less than threshold value, accept the call, otherwise drop the call with channel status: CHANUNAVAIL. 

You can implement more sophisticated logic to achieve your goal. 
I hope this helped you. Good Luck!

5 comments:

  1. SIP Trunk could be used to work with Voice over IP solution in business establishments. This is a solution that utilizes existing wired PBX models for communications for linking traditional telephone networks to external environment through the net

    ReplyDelete
  2. Can you please tell me how can i limit the call length in outbound call. please tell me the full procedure.
    Regards,
    Mansur

    ReplyDelete
  3. I hope Call limits deprecated, so you can utilize GROUP_COUNT method

    ReplyDelete