Limit number of calls per trunk

Long story short, I am trying to set up a trunk where asterisk should not send more than 100 calls to it per day. After 100 calls, let it respond with 503 Service Unavailable and fail over to the next trunk.

Is there any sample script that can accomplish this? I would have no idea what to do.


You will need to use DB()

something like this macro will work - i have not tested it but you get the idea:

exten => s,1,Set(MAXCALLS=500) ; == Set the max calls on this trunk below ==
exten => s,n,Set(TOTALCALLS_TRUNK_1=${DB(TRUNKS/1/totalcalls)})
exten => s,n,GotoIf($[${TOTALCALLS_TRUNK_1} < ${MAXCALLS}]?candial)
exten => s,n,Congestion(10)
exten => s,n,Hangup ;  == Or do what ever you want here. ==

exten => s,n(candial),NoOp()
exten => s,n,Set(DB(TRUNKS/1/totalcalls)=${TOTALCALLS_TRUNK_1}+1)
exten => s,n,Dial(SIP/${MACRO_EXTEN}@trunkcontext)  
; == Where ${MACRO_EXTEN} will be the number you dialled. == 
; == You can use [trunkcontext] or IP or whatever. ==

Now call this macro in your dial plan

exten => _X,1,Macro(dialout-trunk-1)

Then of course you will need to rest the DB value every night. So make a cron job like this:

asterisk -xr 'database put TRUNKS/1 totalcalls 0'

As you can see for trunk 2 3 4 etc you can just reuse the same logic.

Personally i would not try and get it to return with a 503, just handle the fail over in the code. You may just confuse the hell out of the device. If you want it to respond on the 100’th call, then not again, you can store the trunk name or whatever reference in the DB.