<?
/*
#============================================================================
# Program: Monitoring-Setup.php
# Programmer: Remo Rickli
#
# DATE		COMMENT
# -----------------------------------------------------------
# 05/06/05	initial version.
# 01/03/06	bulk add with auto dependencies
# 17/03/06	new SQL query support
# 25/05/07	minor SQL and cosmetic improvements
# 12/07/07	new location scheme
# 04/09/07	implemented CSS scheme
# 20/09/07	implemented language support
# 28/09/07	Various improvements
*/

error_reporting(E_ALL ^ E_NOTICE);

include_once ("inc/header.php");
include_once ('inc/libdev.php');
include_once ('inc/libmon.php');

$_GET = sanitize($_GET);
$gen = isset($_GET['gen']) ? $_GET['gen'] : "";
$mon = isset($_GET['mon']) ? $_GET['mon'] : "";
$loc = isset($_GET['loc']) ? $_GET['loc'] : "";
$crm = isset($_GET['crm']) ? $_GET['crm'] : "";
$cad = isset($_GET['cad']) ? $_GET['cad'] : "";
$nrm = isset($_GET['nrm']) ? $_GET['nrm'] : "";
$nad = isset($_GET['nad']) ? $_GET['nad'] : "";
$dev = isset($_GET['dev']) ? $_GET['dev'] : "";
$dep = isset($_GET['dep']) ? $_GET['dep'] : "";
$dpo = isset($_GET['dpo']) ? $_GET['dpo'] : "";
$ars = isset($_GET['ars']) ? $_GET['ars'] : "";
$clc = isset($_GET['clc']) ? $_GET['clc'] : "";
?>
<h1>Monitoring Setup</h1>
<form method="get" action="<?=$self?>.php" name="lflt">
<table class="content"><tr class="<?=$modgroup[$self]?>1">
<th width=80><a href="<?=$self?>.php"><img src="img/32/sys.png"></a></th>
<th>
<?=$loclbl?> 
<input type="text" name="loc" value="<? echo $loc?>" size="40">
<select size=1 name="sl" onchange="document.lflt.loc.value=document.lflt.sl.options[document.lflt.sl.selectedIndex].value">
<option value=""><?=$sellbl?> ->
<?
$link	= @DbConnect($dbhost,$dbuser,$dbpass,$dbname);
$query	= GenQuery('devices','s','name,location','location');
$res	= @DbQuery($query,$link);
if($res){
	while( ($d = @DbFetchRow($res)) ){
		$devs[] = $d[0];
		$l = explode($locsep, $d[1]);
		$lopt[$l[0]][$l[1]]++;
	}
	@DbFreeResult($res);
}else{
	print @DbError($link);
}
foreach(array_keys($lopt) as $r){
?><option value="<?=TopoLoc($r)?>" style="color:red"><?=$r?>

<?
	foreach(array_keys($lopt[$r]) as $c){
?><option value="<?=TopoLoc($r,$c)?>"><?=$c?> (<?=$lopt[$r][$c]?>)
<?
	}
}
?>
</SELECT>
</th>
<th>
<input type="checkbox" name="ars"> Auto <?=$parlbl?> &nbsp;&nbsp;
<input type="submit" value="<?=$addbtn?>" name="mon" onclick="return confirm('Monitor matching devices?')" >
</th>
<th width=80>
<input type="submit" value="<?=$shobtn?>" name="gen">
</th>
</tr></table></form><p>
<?
if($crm){
	$query	= GenQuery('monitoring','d','','','',array('device'),array('='),array($crm) );
	if( !@DbQuery($query,$link) ){echo "<h4>".DbError($link)."</h4>";}else{echo "<h5>$delbtn $crm ok</h5>";}
}elseif($cad){
	$query	= GenQuery('monitoring','i','','','',array('device','status','depend','sms','mail','lastchk','uptime','lost','ok'),'',array($cad,'0','none','0','0','0','0','0','0') );
	if( !@DbQuery($query,$link) ){echo "<h4>".DbError($link)."</h4>";}else{echo "<h5>$updlbl $cad ok</h5>";}
}elseif($nrm){
	$query	= GenQuery('monitoring','u','device',$dev,'',array($nrm),'',array('0') );
	if( !@DbQuery($query,$link) ){echo "<h4>".DbError($link)."</h4>";}else{echo "<h5>$delbtn $dev $nrm ok</h5>";}
}elseif($nad){
	$query	= GenQuery('monitoring','u','device',$dev,'',array($nad),'',array('1') );
	if( !@DbQuery($query,$link) ){echo "<h4>".DbError($link)."</h4>";}else{echo "<h5>$updlbl $dev $nad ok</h5>";}
}elseif($clc){
	$query	= GenQuery('monitoring','u','device',$clc,'',array('lost'),'',array('0') );
	if( !@DbQuery($query,$link) ){echo "<h4>".DbError($link)."</h4>";}else{echo "<h5>$updlbl $clc $loslbl ok</h5>";}
}elseif($dep or $dpo){
	if(!$_GET['dep']){$dep = $dpo;}
	if (in_array($dep, $devs) or $dep == 'none') {
		$query	= GenQuery('monitoring','u','device',$dev,'',array('depend'),'',array($dep) );
		if( !@DbQuery($query,$link) ){echo "<h4>".DbError($link)."</h4>";}else{echo "<h5>$updlbl $dev $parlbl = $dep</h5>";}
	}else{
		echo "0 $vallbl";
	}
}
if($gen or $mon){
?>
<table class="content"><tr class="<?=$modgroup[$self]?>2">
<th width=80><a href="<?=$self?>.php"><img src="img/32/dev.png"><br>Device</th>
<th><img src="img/32/glob.png"><br><?=$loclbl?></th>
<th><img src="img/32/find.png"><br>Info</th>
<th><img src="img/32/clock.png"><br>Uptime</th>
<th><img src="img/32/neti.png"><br><?=$parlbl?></th>
<th><img src="img/32/say.png"><br><?=$msglbl?></th></tr>

<?
	$query	= GenQuery('devices','s','name,location,snmpversion,icon','','',array('location'),array('regexp'),array($loc) );
	$res	= @DbQuery($query,$link);
	if($res){
		while( ($d = @DbFetchRow($res)) ){
			$dloc[$d['0']] = $d['1'];
			$dsnm[$d['0']] = $d['2'];
			$dico[$d['0']] = $d['3'];
		}
		@DbFreeResult($res);
	}else{
		print @DbError($link);
	}
	$uloc = rawurlencode($loc);
	$row = 0;
	foreach ($dico as $dv => $ico){
		if ($row % 2){$bg = "txta"; $bi = "imga";}else{$bg = "txtb"; $bi = "imgb";}
		$row++;
		$ud = rawurlencode($dv);

		$query	= GenQuery('monitoring','s','*','','',array('device'),array('='),array($dv) );
		$res	= @DbQuery($query,$link);
		if($res){
			if (@DbNumRows($res) == 1){
				$m = @DbFetchRow($res);
				@DbFreeResult($res);
			}else{
				$m = array();
			}
		}else{
			print @DbError($link);
		}

		$query	= GenQuery('links','s','*','','',array('device'),array('='),array($dv) );
		$res	= @DbQuery($query,$link);
		$neb	= array();
		if($res){
			while( ($l = @DbFetchRow($res)) ){
				$neb[$l[3]] = $l[4];
			}
			@DbFreeResult($res);
		}else{
			print @DbError($link);
		}

		if($mon and !$m[0] and $dsnm[$dv]){
			$adep = 'none';
			if(count(array_keys($neb) ) == 1 and $ars){
				$adep = key($neb);
			}
			$query	= GenQuery('monitoring','i','','','',array('device','status','depend','sms','mail','lastchk','uptime','lost','ok'),'',array($dv,'0',$adep,'0','0','0','0','0','0') );
			if( !@DbQuery($query,$link) ){echo "<h4 align=center>".DbError($link)."</h4>";}else{echo "<h5>$dv $updlbl OK</h5>";$m[0] = $dv;$m[2] = $adep;}
		}

		list($statbg,$stat) = StatusBg(1,$m[0],$m[1],$bi);
		echo "<tr class=\"$bg\"><th class=\"$statbg\">\n";
		echo "<a href=\"Devices-Status.php?dev=$ud\"><img src=\"img/dev/$ico.png\" title=\"$stat\"></a><p>\n";
		echo "<b>$dv</b></th><td>$dloc[$dv]</td><td>";
		if ($m['5']){
			echo "<a href=\"?loc=$uloc&clc=$ud&gen=1\"><img align=right src=\"img/16/star.png\" title=\"$delbtn $loslbl\"></a> <b>$m[7]/$m[8]</b> $loslbl<br>\n";
			echo "$laslbl: <b>". date($datfmt,$m[5]) . "</b> ";
		}
		echo "</td><th>";
		if($m[0]){
			echo "<a href=\"?loc=$uloc&crm=$ud&gen=1\"><img src=\"img/16/bchk.png\" title=\"-Check\"></a>";
		}elseif($dsnm["$dv"]){
			echo "<a href=\"?loc=$uloc&cad=$ud&gen=1\"><img src=\"img/16/bcls.png\"  title=\"+Check\"></a>";
		}else{
			echo "<img src=\"img/16/bstp.png\"  title=\"no SNMP available!\"></a>";
		}
		echo "</th>\n";
?>
<td nowrap>
<form method="get" action="<?=$self?>.php">
<input type="hidden" name="dev" value="<?=$dv?>">
<input type="hidden" name="loc" value="<?=$loc?>">
<input type="hidden" name="gen" value="update">
<input type="text" name="dpo" size=12 value="<?=$m[2]?>" onchange="this.form.submit();" title="type in device name to override link based dependency">
<SELECT size=1 name="dep" onchange="this.form.submit();" title="set link based dependency">
<OPTION VALUE=""><?=$sellbl?>
<OPTION VALUE="none">---
<?
		if($neb){
			foreach ($neb as $nen => $nif){
				echo "<OPTION VALUE=\"$nen\">$nen ($nif)\n";
			}
		}
		echo "</select></form></td>\n";
		echo "<th>";
		if($m[3]){echo "<a href=\"?loc=$uloc&dev=$ud&nrm=sms&gen=up\"><img src=\"img/16/mobil.png\" title=\"-SMS\"></a>";}
		else{echo "<a href=\"?loc=$uloc&dev=$ud&nad=sms&gen=up\"><img src=\"img/16/bcls.png\" title=\"+SMS\"></a>";}
		if($m[4]){echo "<a href=\"?loc=$uloc&dev=$ud&nrm=mail&gen=up\"><img src=\"img/16/mail.png\" title=\"-Mail\"></a>";}
		else{echo "<a href=\"?loc=$uloc&dev=$ud&nad=mail&gen=up\"><img src=\"img/16/bcls.png\" title=\"+Mail\"></a>";}
		echo "</th></tr>\n";
	}
	?>
</table>
<table class="content">
<tr class="<?=$modgroup[$self]?>2"><td><?=$row?> Devices (<?=$query?>)</td></tr>
</table>
	<?
}
include_once ("inc/footer.php");
?>
