#! /usr/bin/php description = preg_replace("/ {2,}/"," ",$matches[1]); $a->alarms = $matches[2]; $a->irq = $matches[3]; $a->bpviol = $matches[4]; $a->crc4 = $matches[5]; $a->fra = $matches[6]; $a->codi = $matches[7]; $a->options = $matches[8]; $a->lbo = $matches[9]; $aDahdi[] = $a; } } $count = count($aDahdi); $retCode = STATUS_OK; $alarms = "Alarms: "; foreach($aDahdi as $d) { if ($d->alarms != "OK") { $alarms .= " {$d->description}({$d->alarms})"; $retCode = STATUS_CRITICAL; } } if ($retCode == STATUS_OK) print "OK - No alarms among $count dahdi\n"; else print "CRITICAL - $alarms\n"; return $retCode; } function sip_show_channels($arr) { global $warning; global $critical; // Peer User/ANR Call ID Format Hold Last Message // 201.33.209.194 3904383132 1393637-3478951 0x100 (g729) No Rx: ACK $channels = array(); foreach($arr as $line) { if (preg_match("/^([^ ]+) +([^ ]+) +([^ ]+) +([^ ]+) +([^ ]+) +([^ ]+) +([^ ]+) +([^ ]+) *$/", $line, $matches)) { $c = new StdClass(); $c->peer = $matches[1]; $c->useranr = $matches[2]; $c->callid = $matches[3]; $c->format = $matches[4]." ".$matches[5]; $c->hold = $matches[6]; $c->lastMessage = $matches[7]." ".$matches[8]; $channels[] = $c; } } $count = count($channels); if ($count >= $critical) { print "CRITICAL - $count sip channels | channels=$count;$warning;$critical;0;\n"; return STATUS_CRITICAL; } elseif ($count >= $warning) { print "WARNING - $count sip channels | channels=$count;$warning;$critical;0;\n"; return STATUS_WARNING; } print "OK - $count sip channels | channels=$count;$warning;$critical;0;\n"; return STATUS_OK; } function pri_show_spans($arr) { $spans = array(); foreach($arr as $line) { if (preg_match("/^(PRI .+):(.+)$/", $line, $matches)) { $s = new StdClass(); $s->id = $matches[1]; $s->state = $matches[2]; $spans[] = $s; } } $count = count($spans); if ($count == 0) { print "CRITICAL - Unable to find spans\n"; return STATUS_CRITICAL; } $retCode = STATUS_OK; $outStr = "Spans with problem: "; foreach($spans as $s) { if (strstr($s->state,"Down") !== false || strstr($s->state,"Alarm")) { $retCode = STATUS_CRITICAL; $outStr .= " $s->id($s->state)"; } } if ($retCode == STATUS_OK) { print "OK - All $count spans verified sucessfully\n"; } else { print $outStr."\n"; } return $retCode; } function core_show_channels($arr) { $channels = array(); $first = true; foreach($arr as $line) { if (preg_match("/^([^ ]+) +([^ ]+) +([^ ]+) +([^ ]+) +$/",$line,$matches)) { if ($first) { $first = false; continue; } $c = new StdClass(); $c->channel = $matches[1]; $c->location = $matches[2]; $c->state = $matches[3]; $c->application = $matches[4]; $channels[] = $c; } } $count = count($channels); if ($count == 0) { print "CRITICAL - Unable to find channels\n"; return STATUS_CRITICAL; } $retCode = STATUS_OK; $outStr = "Channels with problem: "; foreach($channels as $c) { if ($c->state != "Up" && $c->state != "Ring") { $retCode = STATUS_CRITICAL; $outStr .= " ".$c->channel; } } if ($retCode == STATUS_OK) { print "OK - All $count channels in Up or Ring state\n"; } else { print $outStr."\n"; } return $retCode; } function printVersion() { global $argv; printf( "{$argv[0]} (Nagios Plugin) %0.2f\n", VERSION ); } function connectAndRun() { global $host; global $port; global $errno; global $errstr; global $timeout; global $command; global $verbose; global $username; global $password; $asttext = ""; if ($verbose) echo "Connect and run - $command\n"; if (!$command) { print "You must provide a valid command with -C argument, please view below.\n"; printHelp(); exit( STATUS_UNKNOWN ); } if( ( $astsock = @fsockopen( $host, $port, $errno, $errstr, $timeout ) ) === false ) { echo "Could not connect to Asterisk manager: $errstr\n"; 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: $command\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.\n"; exit( STATUS_CRITICAL ); } if( strpos( $asttext, "Permission denied" ) !== false ) { echo "Asterisk manager permission denied for Command.\n"; exit( STATUS_CRITICAL ); } $aRet = array(); $aRet = explode("\n",$asttext); return $aRet; } function printHelp() { global $argv, $timeout, $port, $valid_commands; echo <<