#! /usr/bin/php -q 20071102 * Copyright (c) Open Source Solutions Ltd. (http://www.opensolutions.ie/) * * Permission is hereby granted, free of charge, to any person obtaining a copy * of this software and associated documentation files (the "Software"), to deal * in the Software without restriction, including without limitation the rights * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell * copies of the Software, and to permit persons to whom the Software is * furnished to do so, subject to the following conditions: * * The above copyright notice and this permission notice shall be included in * all copies or substantial portions of the Software. * * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN * THE SOFTWARE. * */ define( "VERSION", 0.01 ); // Make sure we don't run indefinitly ini_set( 'max_execution_time', '55' ); // Don't mess up the output with warnings/errors ini_set( 'display_errors', false ); define( "STATUS_OK", 0 ); define( "STATUS_WARNING", 1 ); define( "STATUS_CRITICAL", 2 ); define( "STATUS_UNKNOWN", 3 ); //////////////////////////////////////// // PARSE COMMAND LINE ARGUMENTS //////////////////////////////////////// $i = 1; if( $argc == 1 ) { printUsage(); exit( STATUS_UNKNOWN ); } // defaults $warning = 2; $critical = 1; $num_pris = 0; $port = 5038; $username = ''; $password = ''; $host = ''; $timeout = 10; $verbose = false; $required_params = 3; $required_params_got = 0; while( $i < $argc ) { if( $argv[$i][0] != '-' ) { $i++; continue; } switch( $argv[$i][1] ) { case 'h': case '?': printUsage(); exit( STATUS_OK ); break; case 'V': printVersion(); exit( STATUS_OK ); break; case 'v': $verbose = true; $i++; break; case 'n': $num_pris = $argv[$i+1]; $i++; break; case 'U': $username = $argv[$i+1]; $i++; $required_params_got++; break; case 'P': $password = $argv[$i+1]; $i++; $required_params_got++; break; case 'c': $critical = $argv[$i+1]; $i++; break; case 'w': $warning = $argv[$i+1]; $i++; break; case 't': $timeout = $argv[$i+1]; $i++; break; case 'H': $host = $argv[$i+1]; $i++; $required_params_got++; break; case '-': case 'h': case '?': printHelp(); exit( STATUS_OK ); break; default: $i++; } } if( $required_params != $required_params_got ) { echo "Host, Username and Password are required."; printUsage(); exit( STATUS_CRITICAL ); } //////////////////////////////////////// // Talk to Asterisk //////////////////////////////////////// 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 ); } if( strpos( $asttext, "Permission denied" ) !== false ) { echo "Asterisk manager permission denied for Command."; exit( STATUS_CRITICAL ); } //////////////////////////////////////// // Check the answer //////////////////////////////////////// /* * We expect one of: * PRI span 1/0: Provisioned, In Alarm, Down, Active * PRI span 1/0: Provisioned, Up, Active */ $lines = array(); $count = 0; $pri_down = 0; $segments = explode( "\n", $asttext ); foreach( $segments as $segment ) { if( substr( $segment, 0, 8 ) == "PRI span" ) { $lines[$count]['identity'] = substr( $segment, strpos( $segment, ':' ) - 3, 3 ); if( strpos( $segment, "Provisioned, Up, Active" ) !== FALSE ) $lines[$count]['up'] = 1; else if( strpos( $segment, "Up, Active" ) !== FALSE ) $lines[$count]['up'] = 1; else { $lines[$count]['up'] = 0; $lines[$count]['err'] = $segment; $pri_down++; } $count++; } } $status = STATUS_OK; $msg = ''; //////////////////////////////////////// // Decide on test result //////////////////////////////////////// if( $count == 0 ) { echo "No PRIs found"; if( $verbose ) echo "\n$asttext\n\n"; exit( STATUS_UNKNOWN ); } if( $count != $num_pris ) { $msg .= "Number of PRIs found ($count) not equal to number expected by command line argument ($num_pris). "; if( $status < STATUS_WARNING ) $status = STATUS_WARNING; } if( $pri_down == 0 ) { $msg .= "All " . count( $lines ) . " PRI(s) found are up and provisioned. "; if( $status < STATUS_OK ) $status = STATUS_OK; } else { $msg .= "$pri_down of " . count( $lines ) . " PRIs down. "; foreach( $lines as $line ) { if( $line['up'] == 0 ) $msg .= "{$line['err']} "; } if( $num_pris - $pri_down <= $critical ) $status = STATUS_CRITICAL; else if( $num_pris - $pri_down <= $warning && $status < STATUS_WARNING ) $status = STATUS_WARNING; } echo $msg; if( $verbose ) echo "\n$asttext\n\n"; exit( $status ); function printUsage() { global $argv; echo <<