How to count number of channels used by the SIP trunk

Generally SIP trunk providers provide SIP trunk with some fix number of channels on it. Means we can run those much calls only simultaneously. Suppose the SIP trunk has a limit of 100 channels and all 100 calls are running right now then we can not dial the next call until the 100 concurrent calls are running. Once the call completed the channel becomes free. For example 2 calls completed and 98 are still running, in this case we have two channels free so we are able to send/receive two more calls.

So, while we are making calls via Auto dialer, click to call or via dialpad we need to know how many calls are currently running. By knowing this we can have an idea about how much more calls we can produce now. (I mean, [Available channels = Total channels – Running calls channels])

For this we can run the below python script to get currently available channels on the SIP trunk.

import os
import subprocess
import uuid

#provide your trunk name available on Asterisk
trunk_name = “kamtrunk”
total_channels = 100

x = str(uuid.uuid4())
#print(x)
cmd = os.system(‘asterisk -rx “core show channels” | grep ‘+trunk_name+’ >> /tmp/’+x+’.txt’)
#data = os.system(‘wc -l /tmp/mylog.txt’)
data = subprocess.check_output(‘wc -l /tmp/’+x+’.txt’, shell=True)
data = data.decode()
y = data.split(” “)
print(“Number of used channels: “+y[0])
available_channels = total_channels-int(y[0])
print(“Available Channels: “+str(available_channels))
os.system(‘rm /tmp/’+x+’.txt’)

 

Above is the python code that is able to count used channels and available channels on particular trunk. You need to specify variables trunk_name and total_channels as per your trunk’s specification. For example in my case the trunk’s name is kamtrunk available on Asterisk and the total capacity of channels on this trunk is 100 channels. Here, basically we run the “core show channels” command to grep channels on particular trunk name. Then we count the channels via writing channels on temp text file and via command wc -l. At the end we print the number of used and available channels and then remove the temp text file.

Below is the script output when I had all channels available and when one channel was in use.

Now suppose you have 3-4 SIP trunks available with different names then you may need to call this script separately with passing different SIP trunk names. Means for separate trunks there will be separate values of used and available channels. This logic can help us to reduce the call failure ratio due to all capacity of channels occupied on the trunk. I mean we can know how many channels are available now and that much calls only we will produce next. Till the all channels occupied we will not generate more calls.

Comments

No comments yet. Why don’t you start the discussion?

Leave a Reply

Your email address will not be published. Required fields are marked *