📜 ⬆️ ⬇️

Monitoring Asterisk Trunks with Zabbix Low-Level Detection

Foreword


Last year, the company in which I work began to actively switch to IP telephony. For these purposes, the FreePBX distribution was used. There was practically no experience with telephony, so there were many problems. In total, we use 2 FreePBX servers, approximately 30 trunks on each. Some trunks do not behave very well and sometimes lose registration. Therefore, it became necessary to monitor the status of registration in order to learn about idle telephony before users.
To monitor the IT-infrastructure, we have long been using Zabbix. It is a tedious, tedious and uninteresting task to set up data elements for each trunk (about 60). In addition, you must constantly monitor the list of trunks in monitoring and update it. Therefore, it was decided to use one of the most interesting features of this monitoring system - low-level detection. So what has been done.

Setting up Zabbix server


Creating a Zabbix template

In the Zabbix web panel, go to Settings-> Templates, and create an empty template. Let's call it, for example, Asterisk Trunks Discovery. Go to the template editing, and go to the "Detection" section. There we create 2 detection rules, one with the asterisk.discovery [ips] key - it will monitor the IP addresses of the telephony providers, the second with the asterisk.discovery [trunks] key - it will monitor the registration. These keys are invented, you can use whatever you like, the main thing is that they do not overlap with the built-in ZAbbix keys.

image

Prototype data items

For each detection rule, you need to create prototypes of the data elements, which in fact will collect data about each trunk. For the rule asterisk.discovery [trunks], we use the data element with the key asterisk.registry [{# TRUNKNAME}]. The key name, again, is not important, only its #TRUNKNAME parameter is important. Everything else can be seen in the picture.
')
image

For the asterisk.discovery [ips] rule, we used data elements from the standard shabolona for ping, replacing the host name with the macro {#TRUNKIP}. The picture is clearly visible.

image

It remains only to attach the template to the right servers. On this, with the setting Zabbix finish.

Configuring Asterisk Servers


Create a folder for scripts / usr / scripts. We copy into it the trunk_disc.php trunk detection script and the reg_mon.php trunk monitoring script (yes, they are in php, and they are creepy, because they are snapped up and didn’t finish). The scripts themselves:
Script trunk_disc.php
#!/usr/bin/php <?php error_reporting(1); exec("sudo asterisk -rx 'sip show registry'", $tr_list); foreach($tr_list as $line) { $ar_line = null; $ar_line[] = trim(substr($line, 0, 40)); $ar_line[] = trim(substr($line, 47, 19)); $ar_line = array_diff($ar_line, array('')); $ar_line = array_combine(array(0,1), $ar_line); $trunks[] = $ar_line['1']; $ip = explode(":", $ar_line['0']); if (!in_array($ip['0'], $ips)) { $ips[] = $ip['0']; } } $trunks[0] = ''; $ips[0] = ''; $trunks = array_diff($trunks, array('')); $ips = array_diff($ips, array('')); function getJson($items, $name) { $first = 1; print "{\n"; print "\t\"data\":[\n\n"; foreach ($items as $item) { if (!$first) { print "\t,\n" ; $first = 0; } print "\t{\n"; print "\t\t\"{#$name}\":\"$item\"\n"; print "\t},\n"; } print "\n\t]\n"; print "}\n"; } if ($argv[1] == 'trunks') { getJson($trunks, "TRUNKNAME"); } elseif ($argv[1] == 'ips') { getJson($ips, "TRUNKIP"); } else { print "error"; } 


Script reg_mon.php
 #!/usr/bin/php <?php error_reporting(0); $find = "*".$argv[1]."*"; exec("sudo asterisk -rx 'sip show registry'", $tr_list); $filter = preg_grep($find, $tr_list); foreach($filter as $line) { if (preg_match("*Registered*", $line) & preg_match($find, $line)) { exit('1'); } else { exit('0'); } } exit('0') 


In the Zabbix agent configuration file, add:

 UserParameter=asterisk.registry[*],/usr/scripts/reg_mon.php $1 UserParameter=asterisk.discovery[*], /usr/scripts/trunk_disc.php $1 

And restart the agent Zabbixa.

Check


To check the operation, we log in to the Zabbix-server console. Run the command
 zabbix_get -s -k asterisk.discovery[trunks] 

{ "data":[ { "{#TRUNKNAME}":"trunk1" }, { "{#TRUNKNAME}":"trunk2" }, { "{#TRUNKNAME}":"trunk3" }, { "{#TRUNKNAME}":"trunk4" }, { "{#TRUNKNAME}":"trunk5" }, { "{#TRUNKNAME}":"trunk5" }, ...

trunk1,trunk2 .. zabbix_get -s -k asterisk.discovery[ips]
:
{ "data":[ { "{#TRUNKIP}":"213.141.252.17" }, { "{#TRUNKIP}":"188.187.255.6" }, { "{#TRUNKIP}":"sip.pctel.ru" }, ...

. , zabbix_get -s -k asterisk.registry[trunk1] 1, 0. , . - Zabbix .


, . , , .. Zabbix . , , . .
zabbix_get -s -k asterisk.discovery[trunks]

{ "data":[ { "{#TRUNKNAME}":"trunk1" }, { "{#TRUNKNAME}":"trunk2" }, { "{#TRUNKNAME}":"trunk3" }, { "{#TRUNKNAME}":"trunk4" }, { "{#TRUNKNAME}":"trunk5" }, { "{#TRUNKNAME}":"trunk5" }, ...

trunk1,trunk2 .. zabbix_get -s -k asterisk.discovery[ips]
:
{ "data":[ { "{#TRUNKIP}":"213.141.252.17" }, { "{#TRUNKIP}":"188.187.255.6" }, { "{#TRUNKIP}":"sip.pctel.ru" }, ...

. , zabbix_get -s -k asterisk.registry[trunk1] 1, 0. , . - Zabbix .


, . , , .. Zabbix . , , . .
 zabbix_get -s -k asterisk.discovery[trunks] 

{ "data":[ { "{#TRUNKNAME}":"trunk1" }, { "{#TRUNKNAME}":"trunk2" }, { "{#TRUNKNAME}":"trunk3" }, { "{#TRUNKNAME}":"trunk4" }, { "{#TRUNKNAME}":"trunk5" }, { "{#TRUNKNAME}":"trunk5" }, ...

trunk1,trunk2 .. zabbix_get -s -k asterisk.discovery[ips]
:
{ "data":[ { "{#TRUNKIP}":"213.141.252.17" }, { "{#TRUNKIP}":"188.187.255.6" }, { "{#TRUNKIP}":"sip.pctel.ru" }, ...

. , zabbix_get -s -k asterisk.registry[trunk1] 1, 0. , . - Zabbix .


, . , , .. Zabbix . , , . .
zabbix_get -s -k asterisk.discovery[trunks]

{ "data":[ { "{#TRUNKNAME}":"trunk1" }, { "{#TRUNKNAME}":"trunk2" }, { "{#TRUNKNAME}":"trunk3" }, { "{#TRUNKNAME}":"trunk4" }, { "{#TRUNKNAME}":"trunk5" }, { "{#TRUNKNAME}":"trunk5" }, ...

trunk1,trunk2 .. zabbix_get -s -k asterisk.discovery[ips]
:
{ "data":[ { "{#TRUNKIP}":"213.141.252.17" }, { "{#TRUNKIP}":"188.187.255.6" }, { "{#TRUNKIP}":"sip.pctel.ru" }, ...

. , zabbix_get -s -k asterisk.registry[trunk1] 1, 0. , . - Zabbix .


, . , , .. Zabbix . , , . .

zabbix_get -s -k asterisk.discovery[trunks]

{ "data":[ { "{#TRUNKNAME}":"trunk1" }, { "{#TRUNKNAME}":"trunk2" }, { "{#TRUNKNAME}":"trunk3" }, { "{#TRUNKNAME}":"trunk4" }, { "{#TRUNKNAME}":"trunk5" }, { "{#TRUNKNAME}":"trunk5" }, ...

trunk1,trunk2 .. zabbix_get -s -k asterisk.discovery[ips]
:
{ "data":[ { "{#TRUNKIP}":"213.141.252.17" }, { "{#TRUNKIP}":"188.187.255.6" }, { "{#TRUNKIP}":"sip.pctel.ru" }, ...

. , zabbix_get -s -k asterisk.registry[trunk1] 1, 0. , . - Zabbix .


, . , , .. Zabbix . , , . .

zabbix_get -s -k asterisk.discovery[trunks]

{ "data":[ { "{#TRUNKNAME}":"trunk1" }, { "{#TRUNKNAME}":"trunk2" }, { "{#TRUNKNAME}":"trunk3" }, { "{#TRUNKNAME}":"trunk4" }, { "{#TRUNKNAME}":"trunk5" }, { "{#TRUNKNAME}":"trunk5" }, ...

trunk1,trunk2 .. zabbix_get -s -k asterisk.discovery[ips]
:
{ "data":[ { "{#TRUNKIP}":"213.141.252.17" }, { "{#TRUNKIP}":"188.187.255.6" }, { "{#TRUNKIP}":"sip.pctel.ru" }, ...

. , zabbix_get -s -k asterisk.registry[trunk1] 1, 0. , . - Zabbix .


, . , , .. Zabbix . , , . .

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


All Articles