<?
/*
#============================================================================
# Program: Reports-Monitoring.php
# Programmer: Remo Rickli
#
# DATE		COMMENT
# -----------------------------------------------------------
# 15/06/05	initial version.
# 20/03/06	new SQL query support
# 05/07/06	uptime report
# 25/07/07	implemented new location scheme
# 04/09/07	implemented CSS scheme
# 20/09/07	implemented language support
# 03/10/07	Optimized availability
*/

error_reporting(E_ALL ^ E_NOTICE);

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

$_GET = sanitize($_GET);
$rep = isset($_GET['rep']) ? $_GET['rep'] : array();
$lim = isset($_GET['lim']) ? $_GET['lim'] : 10;
$ord = isset($_GET['ord']) ? "checked" : "";
?>
<h1>Monitoring Reports</h1>
<form method="get" action="<?=$self?>.php">
<table class="content"><tr class="<?=$modgroup[$self]?>1">
<th width=80><a href="<?=$self?>.php"><img src="img/32/dpie.png"></a></th>
<th><?=$sellbl?></th>
<th>
<select multiple name="rep[]" size=4>
<OPTION value="dav" <?=(in_array("dav",$rep))?"selected":""?>>Device <?=$avalbl?>
<OPTION value="lav" <?=(in_array("lav",$rep))?"selected":""?>><?=$loclbl?> <?=$avalbl?>
<OPTION value="mss" <?=(in_array("mss",$rep))?"selected":""?>><?=$stslbl?> <?=$msglbl?>
<OPTION value="tup" <?=(in_array("tup",$rep))?"selected":""?>>Uptime <?=$stslbl?>
</SELECT></th>
</th>
<th>
<SELECT size=1 name="lim">
<? selectbox("limit",$lim);?>
</SELECT> <?=$limlbl?>
</th>
<th>
<INPUT type="checkbox" name="ord" <?=($ord)?"checked":""?> title="Reverse or order by levels in Message Sources"> <?=$altlbl?>
</th>
</SELECT></th>

<th width=80><input type="submit" name="do" value="<?=$shobtn?>"></th>
</tr></table></form><p>
<?
if($rep){

$link	= @DbConnect($dbhost,$dbuser,$dbpass,$dbname);
$query	= GenQuery('monitoring');
$res	= @DbQuery($query,$link);
if($res){
	$nmon = 0;
	$mavl	= array();
	$topup	= array();
	while( ($m = @DbFetchRow($res)) ){
		if($m[8]){
			$mavl[$m[0]] = (1 - $m[7] / $m[8]) * 100;
		}else{
			$mavl[$m[0]] = 0;
		}
		$topup[$m[0]] = $m[6];
		$nmon++;
	}
	@DbFreeResult($res);
}else{
	print @DbError($link);
	die;
}
$query	= GenQuery('devices','s','name,ip,type,location,contact,icon');
$res	= @DbQuery($query,$link);
if($res){
	$ndev = 0;
	while( ($d = @DbFetchRow($res)) ){
		$dip[$d[0]]  = long2ip($d[1]);
		$dtyp[$d[0]] = $d[2];
		$dcon[$d[0]] = $d[4];
		$dico[$d[0]] = $d[5];
		$l = explode($locsep, $d[3]);
		$dcty["$l[0]$locsep$l[1]"]++;
		$dbld["$l[0]$locsep$l[1]$locsep$l[2]"]++;
		if($mavl[$d[0]]){
			$mcty["$l[0]$locsep$l[1]"]++;
			$mbld["$l[0]$locsep$l[1]$locsep$l[2]"]++;
			$acty["$l[0]$locsep$l[1]"] = ($acty["$l[0]$locsep$l[1]"] + $mavl[$d[0]])/$mcty["$l[0]$locsep$l[1]"];
			$abld["$l[0]$locsep$l[1]$locsep$l[2]"] = ($abld["$l[0]$locsep$l[1]$locsep$l[2]"] + $mavl[$d[0]])/$mbld["$l[0]$locsep$l[1]$locsep$l[2]"];
		}
	}
	@DbFreeResult($res);
}else{
	print @DbError($link);
	die;
}

if ( in_array("dav",$rep) ){
?>
<h2>Device <?=$avalbl?></h2>
<table class="content"><tr class="<?=$modgroup[$self]?>2">
<th colspan=2  width=20%><img src="img/32/dev.png"><br>Device</th>
<th><img src="img/32/umgr.png"><br><?=$conlbl?></th>
<th><img src="img/32/bup.png"><br><?=$avalbl?></th>
</tr>
<?
	if($ord){
		ksort($mavl);
	}else{
		asort($mavl);
	}
	$row = 0;
	foreach ($mavl as $dv => $av){
		if ($row % 2){$bg = "txta"; $bi = "imga";}else{$bg = "txtb"; $bi = "imgb";}
		$row++;
		$img  = $dico[$dv];
		$ud   = rawurlencode($dv);
		$dbar = Bar($av,-99);
		echo "<tr class=\"$bg\"><th class=\"$bi\">\n";
		echo "<a href=Devices-Status.php?dev=$ud><img src=\"img/dev/$img.png\" title=\"$dtyp[$dv]\"><p></a>$dv</th>\n";
		echo "<td><a href=telnet://$dip[$dv]>$dip[$dv]</td><td>$dcon[$dv]</td><td>$dbar".sprintf("%01.2f",$av)." %</td></tr>\n";
		if($row == $lim){break;}

	}
	?>
</table>
<table class="content" >
<tr class="<?=$modgroup[$self]?>2"><td><?=$row?> monitored, <?=$nmon?> devices in total</td></tr>
</table>
	<?
}

if ( in_array("lav",$rep) ){
?>
<h2><?=$loclbl?> <?=$avalbl?></h2>

<table class="full fixed"><tr><td class="helper">

<table class="content"><tr class="<?=$modgroup[$self]?>2">
<th width=20%><img src="img/32/glob.png"><br>City</th>
<th><img src="img/32/bup.png"><br><?=$avalbl?></th>
</tr>
<?
	if($ord){
		ksort($acty);
	}else{
		asort($acty);
	}
	$row = 0;
	foreach (array_keys($acty) as $c){
		$l = explode($locsep, $c);
		if ($row % 2){$bg = "txta"; $bi = "imga";}else{$bg = "txtb"; $bi = "imgb";}
		$row++;
		$img  = CtyImg($dcty[$c]);
		$cbar = Bar($acty[$c],-99);
		$ur   = rawurlencode($l[0]);
		$uc   = rawurlencode($l[1]);
		echo "<tr class=\"$bg\"><th class=\"$bi\">\n";
		echo "<a href=Monitoring-Health.php?reg=$ur&cty=$uc><img src=\"img/$img.png\" title=\"$mcty[$c] of $dcty[$c] devices monitored\"></a><p>$l[0]-$l[1]</th>\n";
		echo "<td>$cbar ". sprintf("%01.2f",$acty[$c])."%</td></tr>\n";
		if($row == $lim){break;}
	}
	?>
</table>
<table class="content" >
<tr class="<?=$modgroup[$self]?>2"><td><?=$row?> Cities, <?=$nmon?> monitored devices</td></tr>
</table>

</td><td class="helper">

<table class="content"><tr class="<?=$modgroup[$self]?>2">
<th  width=20%><img src="img/32/glob.png"><br>Building</th>
<th><img src="img/32/bup.png"><br><?=$avalbl?></th>
</tr>

<?
	if($ord){
		ksort($abld);
	}else{
		asort($abld);
	}
	$row = 0;
	foreach (array_keys($abld) as $b){
		$l = explode($locsep, $b);
		$ur = rawurlencode($l[0]);
		$uc = rawurlencode($l[1]);
		$ub = rawurlencode($l[2]);
		if ($row % 2){$bg = "txta"; $bi = "imga";}else{$bg = "txtb"; $bi = "imgb";}
		$row++;
		$img  = BldImg($dbld[$b],$bld);
		$cbar = Bar($abld[$b],-99);
		echo "<tr class=\"$bg\"><th class=\"$bi\">\n";
		echo "<a href=Monitoring-Health.php?reg=$ur&cty=$uc&bld=$ub><img src=\"img/$img.png\" title=\"$mbld[$b] of $dbld[$b] devices monitored\"></a><p>$l[1]-$l[2]</th>\n";
		echo "<td>$cbar ".sprintf("%01.2f",$abld[$b])."%</td></tr>\n";
		if($row == $lim){break;}
	}
	?>
</table>
<table class="content" >
<tr class="<?=$modgroup[$self]?>2"><td><?=$row?> Buildings, <?=$nmon?> monitored devices</td></tr>
</table>

</td></tr></table>

	<?
}

if ( in_array("mss",$rep) ){
?>
<h2><?=$stslbl?> <?=$msglbl?></h2>
<table class="content"><tr class="<?=$modgroup[$self]?>2">
<th width=10%><img src="img/32/say.png"><br><?=$srclbl?></th>
<th width=20%><img src="img/32/umgr.png"><br><?=$conlbl?></th>
<th colspan=2><img src="img/32/say.png"><br><?=$msglbl?></th>
</tr>
<?
	$msgord = ($ord)? "level desc" : "source";
	$query	= GenQuery('messages','g','source,level',$msgord);
	$res	= @DbQuery($query,$link);
	if($res){
		while( ($s = @DbFetchRow($res)) ){
			$source{$s[0]}{$s[1]} = $s[2];
		}
		@DbFreeResult($res);
	}else{
		print @DbError($link);
		die;
	}
	$row = 0;
	foreach (array_keys($source) as $s){
		if ($row % 2){$bg = "txta"; $bi = "imga";}else{$bg = "txtb"; $bi = "imgb";}
		$row++;
		$us  = rawurlencode($s);
		echo "<tr class=\"$bg\"><th class=\"$bi\">\n";
		if( isset($dico[$s]) ){
			echo "<a href=Devices-Status.php?dev=$us>\n";
			echo "<img src=\"img/dev/$dico[$s].png\" title=\"$dtyp[$dv]\"></a>\n";
			echo "<p>$s</th><td align=center>$dcon[$s]</td><td>\n";
		}else{
			echo "<a href=\"Nodes-List.php?ina=ip&opa==&sta=$s\"><img src=\"img/32/cubs.png\"></a><p>$s</th><td align=center>-</td><td>\n";
		}
		$tmsg = 0;
		foreach (array_keys($source[$s]) as $lvl){
			$nmsg = $source[$s][$lvl];
			$tmsg += $nmsg;
			$mbar = Bar($nmsg,0);
			echo "<img src=\"img/16/$mico[$lvl].png\" title=\"$mlvl[$lvl]\"> $mbar \n";
			echo "<a href=Monitoring-Messages.php?ina=source&opa==&sta=$us&cop=AND&inb=level&opb==&stb=$lvl>$nmsg</a> $mlvl[$lvl]<br>\n";
		}
		$tbar = Bar($tmsg,0);
		echo "</td><td>$tbar <a href=Monitoring-Messages.php?ina=source&opa==&sta=$us>$tmsg</a> total</td></tr>\n";
		if($row == $lim){break;}
	}
	?>
</table>
<table class="content" >
<tr class="<?=$modgroup[$self]?>2"><td><?=$row?> <?=$srclbl?></td></tr>
</table>
	<?
}

if ( in_array("tup",$rep) ){
?>
<h2>Uptime <?=$stslbl?></h2>
<table class="content"><tr class="<?=$modgroup[$self]?>2">
<th colspan=2  width=10%><img src="img/32/dev.png"><br>Device</th>
<th><img src="img/32/umgr.png"><br><?=$conlbl?></th>
<th><img src="img/32/clock.png"><br>Uptime [<?=$tim[d]?>]</th>
</tr>
<?
	if($ord){
		asort($topup);
	}else{
		arsort($topup);
	}
	$row = 0;
	foreach ($topup as $dv => $ticks){
		if ($row % 2){$bg = "txta"; $bi = "imga";}else{$bg = "txtb"; $bi = "imgb";}
		$row++;
		$ud  = rawurlencode($dv);
		$upt = sprintf("%01.2f",$ticks / 8640000);
		$ubar = Bar($upt,365);
		echo "<tr class=\"$bg\"><th class=\"$bi\">\n";
		echo "<a href=Devices-Status.php?dev=$ud><img src=\"img/dev/$dico[$dv].png\" title=\"$dtyp[$dv]\"><p></a>$dv</th>\n";
		echo "<td><a href=telnet://$dip[$dv]>$dip[$dv]</td><td>$dcon[$dv]</td><td>$ubar $upt</td></tr>\n";
		if($row == $lim){break;}
	}
	?>
</table>
<table class="content" >
<tr class="<?=$modgroup[$self]?>2"><td><?=$row?> Devices</td></tr>
</table>
	<?
}

}
include_once ("inc/footer.php");
?>
