I have a number of Asterisk implementations that I keep an eye on that have multiple PRI connections. Knowing if and when they ever go down has the obvious benefits of alerting me to a problem in near real time. But besides that, it allows my customers and I to verify SLAs, track and log issues, etc.
To this end, I have written a Nagios plugin which queries Asterisk’s manager interface and executes the pri show spans
CLI command (this is Asterisk 1.4 by the way). The script then parses the output to ascertain whether a PRI is up or not.
The actual code to connect to the manager interface and execute the query is simply:
if( ( $astsock = fsockopen( $host, $port, $errno, $errstr, $timeout ) ) === false ) { echo "Could not connect to Asterisk manager: $errstr"; exit( STATUS_CRITICAL ); } fputs( $astsock, "Action: Login\r\n"); fputs( $astsock, "UserName: $username\r\n"); fputs( $astsock, "Secret: $password\r\n\r\n"); fputs( $astsock, "Action: Command\r\n"); fputs( $astsock, "Command: pri show spans\r\n\r\n"); fputs( $astsock, "Action: Logoff\r\n\r\n"); while( !feof( $astsock ) ) { $asttext .= fread( $astsock, 8192 ); } fclose( $astsock ); if( strpos( $asttext, "Authentication failed" ) !== false ) { echo "Asterisk manager authentication failed."; exit( STATUS_CRITICAL ); }
This plugin is hard coded to English and expects to find Provisioned, Up, Active
for a good PRI. For example, the Asterisk implementations that support the pri show spans
command that I have access to return one of:
PRI span 1/0: Provisioned, In Alarm, Down, Active
PRI span 3/0: Provisioned, Up, Active
PRI span 2/0: Up, Active
I’m actually running a slightly older version of Nagios at the moment, version 1.3. To integrate the plugin, first add the following command definition to an appropriate existing or new file under /etc/nagios-plugings/config/
:
define command{ command_name check_asterisk_pri command_line /usr/lib/nagios/plugins/check_asterisk_pri.php \\ -H $HOSTADDRESS$ -U $ARG1$ -P $ARG2$ -w $ARG3$ \\ -c $ARG4$ -n $ARG5$ }
where $ARG1$
is the Asterisk manager username and $ARG2$
is the password. $ARG3$
and $ARG4$
are the warning and critical thresholds respectively whereby if the number of available PRIs reaches one of these values, the appropriate error condition will be set. Lastly, $ARG5$
is the number of PRIs the plugin shouldexpect to find.
NB: the command_line
line above is split for readability but it should all be on the one line.
Now create a test for a host in an appropriate file in /etc/nagios/config/
:
define service{ use core-service host_name hostname.domain.ie service_description Asterisk PRIs check_command check_asterisk_pri!user!pass!2!1!4 }
Ensure that your Nagios server has permissions to access the Asterisk server via TCP on the Asterisk manager port (5038 by default). If on a public network, this should be done via stunnel or a VPN for security reasons.
Lastly, you’ll need a user with the appropriate permissions and host allow statements in your Asterisk configuration (/etc/asterisk/manager.conf
):
[username] secret = password deny=0.0.0.0/0.0.0.0 permit=1.2.3.4/255.255.255.255 read = command write = command
The next version may include support for BRI and Zap FXO ports also. I also plan on a Cacti plug in to show the channels on each PRI (up – on a call, down, etc). In any case, updates will be posted here.
The plug in can be download from: http://www.opensolutions.ie/misc/check_asterisk_pri.php.txt
UPDATED 20/03/2012: Aterisk 1.8.9 takes out the word “Provisioned” in “pri show spans”. Thanks to Shane O’Cain.