[ZBX-19344] Asterisk Template Incorrect Detection of Active Channels per Trunk Created: 2021 May 09  Updated: 2024 Apr 10  Resolved: 2021 May 31

Status: Closed
Project: ZABBIX BUGS AND ISSUES
Component/s: Templates (T)
Affects Version/s: None
Fix Version/s: 5.0.13rc1, 5.4.1rc1, 6.0.0alpha1, 6.0 (plan)

Type: Problem report Priority: Trivial
Reporter: Oleg Gawriloff Assignee: Maxim Chudinov (Inactive)
Resolution: Fixed Votes: 0
Labels: None
Remaining Estimate: Not Specified
Time Spent: Not Specified
Original Estimate: Not Specified

Issue Links:
Duplicate
is duplicated by ZBX-19492 Asterisk Template Incorrect Reload Tr... Closed
Team: Team INT
Sprint: Sprint 76 (May 2021)
Story Points: 1

 Description   

For detection of active channels per trunk following JS code is used:

response = Ami.request(url, 'command&command=core%20show%20channels');
channels = response.body;
var fields = {
 active_channels: 'active channels?',
 active_calls: 'active calls?',
 calls_processed: 'calls? processed'
};

 

So first - channels is filled with result of 'core show channels' command. After that for each trunk active channels is searched in 'channels' with:

asterisk.pjsip.trunks.forEach(function (trunk) {
 var active_channels = channels.match(new RegExp(' PJSIP/' + trunk.ObjectName.split('/')[0], 'g'));
 trunk.active_channels = (active_channels === null) ? 0 : active_channels.length;
 });

 

Problem is that 'core show channels' output channel name is limited to 20 characters (including PJSIP/):

Action: Command
Command: core show channels

Response: Success
Message: Command output follows
Output: Channel Location State Application(Data)
Output: PJSIP/1000-0000004c 141@from-internal:1 Ringing AppDial((Outgoing Line))
Output: *PJSIP/trunk_+3754478* s@macro-dial:24 Ring Dial(PJSIP/1000/sip:[email protected]
Output: 2 active channels
Output: 1 active call
Output: 48 calls processed

 

But trunk name is longer than that:

Action: PJSIPShowEndpoints

Response: Success
EventList: start
Message: A listing of Endpoints follows, presented as EndpointList events

Event: EndpointList
ObjectType: endpoint
*ObjectName: trunk_+375447889910*

<...skip...>

DeviceState: Not in use

 

So, if trunk naming is 'trunk_'+ full number - Active channels is always zero.

If we use core show channels concise

We receive following with full channel name:

freepbxtst*CLI> core show channels concise
PJSIP/1000-00000052!from-internal!141!1!Ringing!AppDial!(Outgoing Line)!1000!!!3!8!!1620540014.84
*PJSIP/trunk_+375447889917-00000051*!macro-dial!s!24!Ring!Dial!PJSIP/1000/sip:[email protected]:42034;ob,15,HhtrQ(NO_ANSWER)M(auto-blkvm)b(func-apply-sipheaders^s^1),!+375296521507!!!3!9!!1620540013.83

 

Also if trunk name contains reserved characters (+ in my case) in must be escaped.

Following patch fixes that (not tested with IAX2):

--- asterisk.js.orig 2021-02-01 18:27:04.004398291 +0300
+++ asterisk.js 2021-05-09 09:53:01.097621580 +0300

@@ -81,6 +82,10 @@
 }
 };

+function escapeRegExp(text) {
+ return text.replace(/[-[\]{}()*+?.,\\^$|#\s]/g, '\\$&');
+}
+
 function block2Object(text) {
 var fields = [],
 dict = {};
@@ -138,7 +143,7 @@
 }
 });
 asterisk.sip.trunks.forEach(function (trunk) {
- var active_channels = channels.match(new RegExp(' SIP/' + trunk.ObjectName.split('/')[0], 'g'));
+ var active_channels = full_channels.match(new RegExp(escapeRegExp('SIP/' + trunk.ObjectName.split('/')[0]), 'g'));
 trunk.active_channels = (active_channels === null) ? 0 : active_channels.length;
 });
 }
@@ -166,7 +171,7 @@
 });
 asterisk.iax.online = asterisk.iax.total - asterisk.iax.offline;
 asterisk.iax.trunks.forEach(function (trunk) {
- var active_channels = channels.match(new RegExp('( IAX2/' + trunk.ObjectName.split('/')[0] +
+ var active_channels = full_channels.match(new RegExp('( IAX2/' + trunk.ObjectName.split('/')[0] +
 '| IAX2/' + trunk.ObjectUsername + ')', 'g'));
 trunk.active_channels = (active_channels === null) ? 0 : active_channels.length;
 });
@@ -187,11 +192,19 @@
 });
 asterisk.pjsip.available = asterisk.pjsip.total - asterisk.pjsip.unavailable;
 asterisk.pjsip.trunks.forEach(function (trunk) {
- var active_channels = channels.match(new RegExp(' PJSIP/' + trunk.ObjectName.split('/')[0], 'g'));
+ var active_channels = full_channels.match(new RegExp(escapeRegExp('PJSIP/' + trunk.ObjectName.split('/')[0]), 'g'));
 trunk.active_channels = (active_channels === null) ? 0 : active_channels.length;
 });
 }

@@ -230,6 +243,9 @@
 calls_processed: 'calls? processed'
 };

+response = Ami.request(url, 'command&command=core%20show%20channels%20concise');
+full_channels = response.body;
+
 Object.keys(fields).forEach(function (field) {
 var match = channels.match('(\\d+) ' + fields[field]);
 if (match !== null && typeof match[1] !== 'undefined') {

 



 Comments   
Comment by Maxim Chudinov (Inactive) [ 2021 May 17 ]

Hello barzog.
What Asterisk version are you using?
Could you try the updated template? https://git.zabbix.com/projects/ZBX/repos/zabbix/browse/templates/tel/asterisk_http?at=refs%2Fheads%2Ffeature%2FZBX-19344-5.0

Comment by Oleg Gawriloff [ 2021 May 17 ]

Asterisk version is 16.9.0.

Tested, works as expected.

 

 

Comment by Maxim Chudinov (Inactive) [ 2021 May 27 ]

Available in:

Generated at Wed Apr 30 06:47:17 EEST 2025 using Jira 9.12.4#9120004-sha1:625303b708afdb767e17cb2838290c41888e9ff0.