{"id":927,"date":"2023-12-28T15:36:05","date_gmt":"2023-12-28T15:36:05","guid":{"rendered":"https:\/\/james-batchelor.com\/?p=927"},"modified":"2023-12-28T15:36:05","modified_gmt":"2023-12-28T15:36:05","slug":"asterisk-monitoring-over-snmp","status":"publish","type":"post","link":"https:\/\/james-batchelor.com\/index.php\/2023\/12\/28\/asterisk-monitoring-over-snmp\/","title":{"rendered":"Asterisk Monitoring Over SNMP"},"content":{"rendered":"\n<p>The aim of deploying Zabbix and adding SNMP to Gentoo was to gain better insight on how an Asterisk PBX was performing.<\/p>\n\n\n\n<p>Last of the hurdles was to get data from Asterisk in order to send to Zabbix, however the traditional way of loading the res_snmp.so module in Asterisk was not available, as while the PBX in question utilises Asterisk, its buried under proprietary licensing and a non-standard api, therefore being unable to either add the SNMP module or to query it.<\/p>\n\n\n\n<p>If you are experiencing a similar situation, here is how to extract some stats from Asterisk 16 using SNMP, but without the SNMP module\u2026<\/p>\n\n\n<div class=\"wp-block-image\">\n<figure class=\"aligncenter size-full\"><a href=\"https:\/\/james-batchelor.com\/wp-content\/uploads\/2023\/12\/Screenshot-2023-12-23-184013.png\"><img loading=\"lazy\" decoding=\"async\" width=\"1204\" height=\"325\" src=\"https:\/\/james-batchelor.com\/wp-content\/uploads\/2023\/12\/Screenshot-2023-12-23-184013.png\" alt=\"\" class=\"wp-image-928\"\/><\/a><\/figure><\/div>\n\n\n<!--more-->\n\n\n\n<p>Despite not being able edit Asterisk code directly, SSH access and the Asterisk CLI is available, so can run commands to return a snapshot of PBX activity.<\/p>\n\n\n\n<p>The following commands can be run to output some valuable info:<\/p>\n\n\n\n<p><strong>asterisk -rx &#8216;pjsip show endpoints&#8217;<\/strong> returns total number and current online endpoints, as well as reported user agent of device.<\/p>\n\n\n\n<p><strong>asterisk -rx &#8216;core show channels verbose&#8217;<\/strong> gives us a list of active channels and current point in an inbound call flow, along with a counter of completed calls.<\/p>\n\n\n\n<p><strong>asterisk -rx &#8216;pjsip show channelstats&#8217;<\/strong> is similar to \u2018show channels verbose\u2019, but includes the codec in use, helpful to determine if the system is transcoding.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">Scripts<\/h3>\n\n\n\n<p>To get and output just the info required, a script can be created to parse the command output and list each value on a new line.<\/p>\n\n\n\n<p>Create the three files below and paste the code in.<br>In this example I saved to \/usr\/local\u2026\u2026<\/p>\n\n\n\n<p>asterisk_chan.sh<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>command_output=$(asterisk -rx 'core show channels')\n\nactive=$(echo \"$command_output\" | grep -oE '&#91;0-9]+ active channels' | grep -oE '&#91;0-9]+')\nprocessed=$(echo \"$command_output\" | grep -oE '&#91;0-9]+ calls processed' | grep -oE '&#91;0-9]+')\n\necho \"$active\"\necho \"$processed\"<\/code><\/pre>\n\n\n\n<p>asterisk_chanstat.sh<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>command_output=$(asterisk -rx 'pjsip show channelstats')\n\nopus=$(echo \"$command_output\" | grep 'opus' | (wc -l))\ng722=$(echo \"$command_output\" | grep 'g722' | (wc -l))\nalaw=$(echo \"$command_output\" | grep 'alaw' | (wc -l))\nulaw=$(echo \"$command_output\" | grep 'ulaw' | (wc -l))\ng729=$(echo \"$command_output\" | grep 'ulaw' | (wc -l))\ntotal=$(echo \"$command_output\" | grep -oP 'Objects found: \\K\\d+' || echo 0)\n\necho \"$opus\"\necho \"$g722\"\necho \"$alaw\"\necho \"$ulaw\"\necho \"$g729\"\necho \"$total\"<\/code><\/pre>\n\n\n\n<p>asterisk_endpoint.sh<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>command_output=$(asterisk -rx 'pjsip show endpoints')\n\navail=$(echo \"$command_output\" | grep 'Avail' | (wc -l))\ntotal=$(( $(echo \"$command_output\" | grep -c 'Endpoint') - 1 ))\n\necho \"$avail\"\necho \"$total\"<\/code><\/pre>\n\n\n\n<p>Saved, they need to be made executable, add the execute flag to each file\u2026<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>chmod +x \/usr\/local\/asterisk_chan.sh\nchmod +x \/usr\/local\/asterisk_chanstat.sh\nchmod +x \/usr\/local\/asterisk_endpoint.sh<\/code><\/pre>\n\n\n\n<h3 class=\"wp-block-heading\">SNMP extend_sh<\/h3>\n\n\n\n<p>SNMP has an extension feature to add and poll your own custom data, to take advantage of this the snmp.d.conf file need to include reference and path to the scripts. At the bottom of snmpd.conf, add the following lines\u2026<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>extend-sh asterisk_chan \/usr\/local\/share\/snmp\/rps\/asterisk_chan.sh\nextend-sh asterisk_chanstat \/usr\/local\/share\/snmp\/rps\/asterisk_chanstat.sh\nextend-sh asterisk_endpoint \/usr\/local\/share\/snmp\/rps\/asterisk_endpoint.sh<\/code><\/pre>\n\n\n\n<p>Restart snmpd to load changes\u2026<\/p>\n\n\n\n<p>Gentoo:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>\/etc\/init.d\/snmpd restart<\/code><\/pre>\n\n\n\n<p>RHEL:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>systemctl restart snmpd<\/code><\/pre>\n\n\n\n<h3 class=\"wp-block-heading\">Testing<\/h3>\n\n\n\n<p>Using snmpget command locally or on your collector machine, test that its working..<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>snmpget -c }your-community} -v 2c {ip-address} 'NET-SNMP-EXTEND-MIB::nsExtendOutLine.\"asterisk_chan\".1'<\/code><\/pre>\n\n\n\n<p>Where <em>\u201casterisk_chan\u201d<\/em> is the name of the extend_sh created in snmpd.conf file, and <em>.1<\/em> on the end collates to the line of script output, <em>.2<\/em> returns the second line and so on.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">Add to monitoring<\/h3>\n\n\n\n<p>When adding to your monitoring system, in this example Zabbix, use the command listed in the testing phase to grab the values\u2026<\/p>\n\n\n<div class=\"wp-block-image\">\n<figure class=\"aligncenter size-full\"><a href=\"https:\/\/james-batchelor.com\/wp-content\/uploads\/2023\/12\/Screenshot-2023-12-23-183809.png\"><img loading=\"lazy\" decoding=\"async\" width=\"672\" height=\"251\" src=\"https:\/\/james-batchelor.com\/wp-content\/uploads\/2023\/12\/Screenshot-2023-12-23-183809.png\" alt=\"\" class=\"wp-image-929\"\/><\/a><\/figure><\/div>\n\n\n<p>Logging and monitoring is now enabled for the Asterisk service&#8230;<\/p>\n\n\n<div class=\"wp-block-image\">\n<figure class=\"aligncenter size-full\"><a href=\"https:\/\/james-batchelor.com\/wp-content\/uploads\/2023\/12\/Screenshot-2023-12-23-184140.png\"><img loading=\"lazy\" decoding=\"async\" width=\"1202\" height=\"405\" src=\"https:\/\/james-batchelor.com\/wp-content\/uploads\/2023\/12\/Screenshot-2023-12-23-184140.png\" alt=\"\" class=\"wp-image-930\"\/><\/a><\/figure><\/div>","protected":false},"excerpt":{"rendered":"<p>The aim of deploying Zabbix and adding SNMP to Gentoo was to gain better insight on how an Asterisk PBX was performing. Last of the hurdles was to get data from Asterisk in order to send to Zabbix, however the traditional way of loading the res_snmp.so module in Asterisk was not available, as while the &hellip; <a href=\"https:\/\/james-batchelor.com\/index.php\/2023\/12\/28\/asterisk-monitoring-over-snmp\/\" class=\"more-link\">Continue reading<span class=\"screen-reader-text\"> &#8220;Asterisk Monitoring Over SNMP&#8221;<\/span><\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[5,247],"tags":[252,381,380,276,379,367],"class_list":["post-927","post","type-post","status-publish","format-standard","hentry","category-servers","category-voip","tag-asterisk","tag-logging","tag-monitoring","tag-pbx","tag-snmp","tag-snmpd"],"_links":{"self":[{"href":"https:\/\/james-batchelor.com\/index.php\/wp-json\/wp\/v2\/posts\/927","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/james-batchelor.com\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/james-batchelor.com\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/james-batchelor.com\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/james-batchelor.com\/index.php\/wp-json\/wp\/v2\/comments?post=927"}],"version-history":[{"count":2,"href":"https:\/\/james-batchelor.com\/index.php\/wp-json\/wp\/v2\/posts\/927\/revisions"}],"predecessor-version":[{"id":932,"href":"https:\/\/james-batchelor.com\/index.php\/wp-json\/wp\/v2\/posts\/927\/revisions\/932"}],"wp:attachment":[{"href":"https:\/\/james-batchelor.com\/index.php\/wp-json\/wp\/v2\/media?parent=927"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/james-batchelor.com\/index.php\/wp-json\/wp\/v2\/categories?post=927"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/james-batchelor.com\/index.php\/wp-json\/wp\/v2\/tags?post=927"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}