#!/usr/bin/php 
<?php
/*
#
#    Program : check_e1t1
#            :
#     Author : Eng. Jorge Valenzuela <jorge.valenzuela@linuxpowerup.com>
#            :
#    Purpose : Nagios plugin to monitor de availability of E1/T1 on asterisk/elastik 
#            : servers
#            :
# Parameters : --help -h
#            : --version -V
#            : -p <number of port to be monitored>, <number of port to be monitored>, ... 
#            :
#    Returns : Standard Nagios status_* 
#      Notes :
#============:==============================================================
#        1.0 : 20/05/2011
#            : Initial coding
#            :
#
# Shamelessly stolen from other Nagios plugins.
#
*/
$cmd = "/usr/sbin/dahdi_scan";  /////  dahdi_scan command path /////
	if($argc == 1 || $argc > 3)
	{
	print_usage();
	exit(3);
	}
	if($argc == 2)
	{
		if($argv[1] == "--help" || $argv[1] == "-h")
		{
		print_help();
		exit(0);
		}
		elseif($argv[1] == "--version" || $argv[1] == "-V")
		{	
		print_version();
		exit(0);
		}
		else
		{
		print_usage();
		exit(0);
		}

	}

	if($argc == 3)
	{
	$valid = 0;
		if($argv[1]=="-p")	$valid = 1;
		
	$regularArg="/[a-zA-Z]+$/";
	$numMatch = preg_match_all($regularArg, $argv[2], $matchings, PREG_OFFSET_CAPTURE);
		if(!$numMatch)		$valid = $valid + 1;
	
		if ($valid == 2)	monitorea($cmd,$argv);
		else			print_usage();
	}

function print_version()
{
echo "check_e1t1 version 1.0\n";
}
function print_help()
{
echo "\nNagios plugin to monitor E1/T1 ports on asterisk";
echo "\nhttp://www.linuxpowerup.com/en/How-to-install-Nagios-NRPE-to-monitor-an-E1-T1-on-a-remote-elastix-server\n";
echo "\ncheck_e1t1";
echo "\t-p 1,3,4\t-ports to be monitored";
echo "\n\t\t-h --help\t-print this help";
echo "\n\t\t-V --version\t-print the version of this plugin\n\n";
}
function print_usage()
{
print_help();
}
function monitorea($path,$argv)
{
$configE1 = array();
$E1pos = array();
$configE1 = split(",",$argv[2]);
$numconfigE1 = count($configE1);
$cadena = `$path`;
$patronLines = "/.*\\n/";
$patronPorts = "/\[[[:digit:]]\]/";
$numLines = preg_match_all($patronLines, $cadena, $coincidencias, PREG_OFFSET_CAPTURE);
$numPorts = preg_match_all($patronPorts, $cadena, $matchPorts, PREG_OFFSET_CAPTURE);
	if($numLines)
	{
	$status=0;
	$mensaje="";
	$nagiosports="";
	$count=0;
	$countp=0;
		for($lines=0;$lines<$numLines;$lines++)
		{
			for($i=0;$i<$numPorts;$i++)
			{
			$p=$i+1;
			$port="[".$p."]";
			$coincidencias[0][$lines][0]=trim($coincidencias[0][$lines][0]);
				if ($port == $coincidencias[0][$lines][0])
				{
				$lines=$lines+2;
				$alarm="alarms=OK\n";
					for($b=0;$b<$numconfigE1;$b++)
					{
						if ($p == $configE1[$b])
						{
						
							if($coincidencias[0][$lines][0]==$alarm)
							{
								if($countp>0)
								{
								$nagiosports.=",";
								}
							$nagiosports.=$port;
							$countp=$countp+1;	
							}
							else 
							{
							$status=1;
							$alarmstatus = chop($coincidencias[0][$lines][0]);
								if($count>0)
								{
								$mensaje.="/";
								}
							$mensaje.="port[$p]-" . $alarmstatus;
							$count=$count+1;
							}
						}
					}
				}		
			}
    		}
	}
	else
	{
	exit(3);
	}
	if ($status==1)
	{
        echo $mensaje;
	exit(2);
	}
	if ($status==0)
	{
	print("E1/T1 OK - ".$nagiosports."\n");
	exit;
	}
}
?>