Using Zabbix to monitor Asterisk SIP Trunks
I'm currently running a small Asterisk Server in my office which has a few SIP Trunks via a couple of providers, SIP Gate and Skype Connect. The connection from the Asterisk Server to the Internet is more complex than it should be due to the environment I have for testing various network configurations. This can lead to the Asterisk Server loosing contact with the SIP Trunk providers. Skype Connect being the most likely to take a drive at any time. I need to know if this happens so I can fix anything I've broken or be aware of external problems that leave my Asterisk Server disconnected.
I'm going to assume that any Asterisk Server to be monitored has a Zabbix Agent installed and connectivity between a Zabbix Server and the Zabbix Agent has been verified and is working. In my particular case I'm using a couple of ADSL connections via a pfSense Server running a Zabbix Proxy that then talks to a Zabbix Agent running on an Asterisk Server.
I'm not going to cover pfSense or the Zabbix Proxy running on that box, as these are transparent to the process of monitoring the Asterisk Server.
The first and most important thing to get in place is the configuration that tells the Zabbix Agent how to query the Asterisk SIP Trunk state. This relies on the Asterisk Command Line Interface (CLI) that allows commands to be sent to Asterisk and the Zabbix Agent's "UserParameter" configuration (/usr/local/etc/zabbix_agentd.conf) which specifies these command and how to interpret the command output.
In my case, I've configured the Zabbix Agent to send the following command to the Asterisk Server to query the Asterisk SIP Registry;
asterisk -r -x 'sip show registry'
Which gives the following output (usernames removed!).
A "State" of "Registered" is what I'm looking for to indicate that a SIP Trunk that is working. I take any other state as not working. There is a slight wrinkle in this logic in that the Asterisk Server will re-register the SIP Trunk every so often. So the SIP Trunk can be in a "State" of "Registration sent" for a short period of time. From the command output, the "State" for a particular "Username" needs to be extracted. The following Zabbix Agent configuration statement does this by sending the query command (asterisk -r -x 'sip show registry'), finding the line for the requested "Username" (grep $1 - $1 is replaced with the first parameter of the Zabbix Server Key asterisk.sip_user) and then cutting out the "State" (cut -c 71-91) which is returned to the Zabbix Server.
UserParameter=asterisk.sip_user[*],asterisk -r -x 'sip show registry' | grep $1 | cut -c 71-91
The configuration examples below are for just one SIP Trunk. This can be duplicated for any number of SIP Trunks.
At the Zabbix Server a "Host Item" needs to be configured for the "Host" describing the Asterisk Server. Here is a snipped of the "Host Item" configuration where "nnnnnnnn" has to be replaced with the SIP Username (usually a number) for the SIP Trunk (as supplied by your SIP provider).
I've also configured a "Trigger" within the "Host" configuration so I can be alerted if a SIP Trunk fails. Here is a snippet of the "Trigger" configuration where "nnnnnnnn" has be be replaced, as above. The "Trigger Expression" looks at the last 10 values to see if ALL of them are not equal to "Registered". I am sampling the SIP Trunk state every 30 seconds, so this corresponds to a time of 300 seconds or 5 minutes. Thus if the SIP Trunk state is not "Registered" for a period of 5 minutes, the trigger is fired. This allows for the re-registrations mentioned before.
I also created entries on a "Map" (see the "CommuniG8 Office" map above) so I could easily view the Asterisk Server's SIP Trunk state. The right hand side of the "Map", has "Map Elements" of Type "Trigger" where the "Trigger" is the one created above. Using the following "Label" (with "nnnnnnnn" substituted as before) allows the "Map" to show the last sampled state;
Trunk - Skype Connect
Thanks for reading.
Richard Gate, CommuniG8 Ltd.