<?php

/*
** CBLM
** $Id: list.php,v 1.4 2006/05/26 05:59:20 sella Exp $
** Copyright (c) 2005-2006 Digital Genesis Software, LLC. All Rights Reserved.
** Released under the GPL Version 2 License.
** http://www.digitalgenesis.com
*/

	require('config/config.php');
	require('menu.php');

	# Variables
	$color_blank = "#9a9a9a";
	$color_header = "#aeaeae";
	$color_cell = "#c9c9c9";
	$color_good = "#005500";
	$color_warn = "#ff3300";
	$color_fail = "#ff0000";

	$intervals = $config['intervals'];
	$interval = isset($_REQUEST['interval']) ? $_REQUEST['interval'] : 120;
	$host = !isset($_REQUEST['host']) ? 1 : $_REQUEST['host'];
	$tos = !isset($_REQUEST['tos']) ? -1 : $_REQUEST['tos'];
	$secs = !isset($_REQUEST['secs']) ? 60 : $_REQUEST['secs'];

	$server = $config['databaseServer'];
	if (isset($config['databasePort']) && strlen($config['databasePort']) > 1) {
		$server .= ':' . $config['databasePort'];
	}
	$dbase = $config['databaseName'];
	$username = $config['databaseUsername'];
	$password = $config['databasePassword'];
	$persistent = $config['databasePersistent'];


	if ($persistent) {
		$con = mysql_pconnect($server, $username, $password);
	} else {
		$con = mysql_connect($server, $username, $password);
	}
	mysql_select_db($dbase);

	$sql = sprintf("SELECT p_src.name src_name, p_dst.name dst_name, INET_NTOA(data.src) src_ip, INET_NTOA(data.dst) dst_ip, SUM(rx)/SUM(tx)*100 avail, 100-(SUM(rx)/SUM(tx)*100) loss, AVG(rtt_min)/1000.0 r_min, AVG(rtt_max)/1000 r_max, AVG(rtt_avg)/1000 r_avg, AVG(ott_min)/1000 o_min, AVG(ott_max)/1000 o_max, AVG(ott_avg)/1000 o_avg, AVG(jtr_min)/1000 j_min, AVG(jtr_max)/1000 j_max, AVG(jtr_avg)/1000 j_avg, SUM(tos_a2z) tos_a2z, SUM(tos_z2a) tos_z2a, SUM(pri_a2z) pri_a2z, SUM(pri_z2a) pri_z2a,SUM(pkt_ooo) pkt_ooo, SUM(pkt_dup) pkt_dup FROM data LEFT JOIN probes AS p_src ON (p_src.ip = src) LEFT JOIN probes AS p_dst ON (p_dst.ip = dst) WHERE secs = %d GROUP BY src, dst ORDER BY src, dst", $secs);

	$result = mysql_query($sql);

	printf("<HTML>\n<HEAD>\n<TITLE>CBLM List View</TITLE>\n");
	if ($_REQUEST['refresh'] >= 5) {
		printf("<META HTTP-EQUIV=\"Refresh\" CONTENT=%d>\n", $_REQUEST['refresh']);
	} else {
		printf("<META HTTP-EQUIV=\"Refresh\" CONTENT=%d>\n", 60);
	}
	printf("</HEAD>\n<BODY>\n");
	menu();
	echo "<table>\n";
	printf("<tr><td>Source</td><td>Destination</td><td>Source IP</td><td>Destination IP</td><td>Avail</td><td>Loss</td><td>RTT Min</td><td>RTT Max</td><td>RTT Avg</td><td>OTT Min</td><td>OTT Max</td><td>OTT Avg</td><td>JTR Min</td><td>JTR Max</td><td>JTR Avg</td><td>ToS A2Z</td><td>ToS A2A</td><td>Pri A2Z</td><td>Pri A2A</td><td>OOO</td><td>Dup</td></tr>\n");
	while ($line = mysql_fetch_array($result, MYSQL_ASSOC)) {
		//print_r($line);
		echo "\t<tr bgcolor=grey>\n";
		while (list($id, $col_value) = each($line)) {
			if (is_numeric($col_value)) {
				if (ereg("^tos_|^pri_|^pkt_", $id)) {
					printf("\t\t<td><center>%d</center></td>\n", $col_value);
				} else {
					printf("\t\t<td><center>%0.3f</center></td>\n", $col_value);
				}
			} else {
				printf("\t\t<td>%s</td>\n", $col_value);
			}
		}
		echo "\t</tr>\n";
	}
	echo "</table>\n";

	// Free resultset
	mysql_free_result($result);

	printf("<P>Source summary (all samples)<BR>\n");

	$sql = sprintf("SELECT p_src.name src, SUM(rx)/SUM(tx)*100 avail, 100-(SUM(rx)/SUM(tx)*100) loss, AVG(rtt_min)/1000.0 r_min, AVG(rtt_max)/1000 r_max, AVG(rtt_avg)/1000 r_avg, AVG(ott_min)/1000 o_min, AVG(ott_max)/1000 o_max, AVG(ott_avg)/1000 o_avg, AVG(jtr_min)/1000 j_min, AVG(jtr_max)/1000 j_max, AVG(jtr_avg)/1000 j_avg, SUM(tos_a2z) tos_a2z, SUM(tos_z2a) tos_z2a, SUM(pri_a2z) pri_a2z, SUM(pri_z2a) pri_z2a, SUM(pkt_ooo) pkt_ooo, SUM(pkt_dup) pkt_dup FROM data, probes p_src WHERE secs = %d AND p_src.ip = src GROUP BY src ORDER BY src", $secs);

	$result = mysql_query($sql);

	echo "<table>\n";
	printf("<tr><td>Source</td><td>Avail</td><td>Loss</td><td>RTT Min</td><td>RTT Max</td><td>RTT Avg</td><td>OTT Min</td><td>OTT Max</td><td>OTT Avg</td><td>JTR Min</td><td>JTR Max</td><td>JTR Avg</td><td>ToS A2Z</td><td>ToS A2A</td><td>Pri A2Z</td><td>Pri A2A</td><td>OOO</td><td>Dup</td></tr>\n");
	while ($line = mysql_fetch_array($result, MYSQL_ASSOC)) {
		echo "\t<tr bgcolor=grey>\n";
		while (list($id, $col_value) = each($line)) {
			if (is_numeric($col_value)) {
				if (ereg("^tos_|^pri_|^pkt_", $id)) {
					printf("\t\t<td><center>%d</center></td>\n", $col_value);
				} else {
					printf("\t\t<td><center>%0.3f</center></td>\n", $col_value);
				}
			} else {
				printf("\t\t<td>%s</td>\n", $col_value);
			}
		}
		echo "\t</tr>\n";
	}
	echo "</table>\n";

	printf("<P>Source summary minus maint window<BR>\n");

	$sql = sprintf("SELECT p_src.name src, SUM(rx)/SUM(tx)*100 avail, 100-(SUM(rx)/SUM(tx)*100) loss, AVG(rtt_min)/1000.0 r_min, AVG(rtt_max)/1000 r_max, AVG(rtt_avg)/1000 r_avg, AVG(ott_min)/1000 o_min, AVG(ott_max)/1000 o_max, AVG(ott_avg)/1000 o_avg, AVG(jtr_min)/1000 j_min, AVG(jtr_max)/1000 j_max, AVG(jtr_avg)/1000 j_avg, SUM(tos_a2z) tos_a2z, SUM(tos_z2a) tos_z2a, SUM(pri_a2z) pri_a2z, SUM(pri_z2a) pri_z2a, SUM(pkt_ooo) pkt_ooo, SUM(pkt_dup) pkt_dup FROM data, probes p_src WHERE secs = %d AND (HOUR(data.timestamp) > 7 && HOUR(data.timestamp) < 20) AND p_src.ip = src GROUP BY src ORDER BY src", $secs);
	$result = mysql_query($sql);

	echo "<table>\n";
	printf("<tr><td>Source</td><td>Avail</td><td>Loss</td><td>RTT Min</td><td>RTT Max</td><td>RTT Avg</td><td>OTT Min</td><td>OTT Max</td><td>OTT Avg</td><td>JTR Min</td><td>JTR Max</td><td>JTR Avg</td><td>ToS A2Z</td><td>ToS A2A</td><td>Pri A2Z</td><td>Pri A2A</td><td>OOO</td><td>Dup</td></tr>\n");
	while ($line = mysql_fetch_array($result, MYSQL_ASSOC)) {
		echo "\t<tr bgcolor=grey>\n";
		while (list($id, $col_value) = each($line)) {
			if (is_numeric($col_value)) {
				if (ereg("^tos_|^pri_|^pkt_", $id)) {
					printf("\t\t<td><center>%d</center></td>\n", $col_value);
				} else {
					printf("\t\t<td><center>%0.3f</center></td>\n", $col_value);
				}
			} else {
				printf("\t\t<td>%s</td>\n", $col_value);
			}
		}
		echo "\t</tr>\n";
	}
	echo "</table>\n";

	printf("<P>Source summary of maint window<BR>\n");

	$sql = sprintf("SELECT p_src.name src, SUM(rx)/SUM(tx)*100 avail, 100-(SUM(rx)/SUM(tx)*100) loss, AVG(rtt_min)/1000.0 r_min, AVG(rtt_max)/1000 r_max, AVG(rtt_avg)/1000 r_avg, AVG(ott_min)/1000 o_min, AVG(ott_max)/1000 o_max, AVG(ott_avg)/1000 o_avg, AVG(jtr_min)/1000 j_min, AVG(jtr_max)/1000 j_max, AVG(jtr_avg)/1000 j_avg, SUM(tos_a2z) tos_a2z, SUM(tos_z2a) tos_z2a, SUM(pri_a2z) pri_a2z, SUM(pri_z2a) pri_z2a, SUM(pkt_ooo) pkt_ooo, SUM(pkt_dup) pkt_dup FROM data, probes p_src WHERE secs = %d AND (HOUR(data.timestamp) <= 7 || HOUR(data.timestamp) >= 20) AND p_src.ip = src GROUP BY src ORDER BY src", $secs);
	$result = mysql_query($sql);

	echo "<table>\n";
	printf("<tr><td>Source</td><td>Avail</td><td>Loss</td><td>RTT Min</td><td>RTT Max</td><td>RTT Avg</td><td>OTT Min</td><td>OTT Max</td><td>OTT Avg</td><td>JTR Min</td><td>JTR Max</td><td>JTR Avg</td><td>ToS A2Z</td><td>ToS A2A</td><td>Pri A2Z</td><td>Pri A2A</td><td>OOO</td><td>Dup</td></tr>\n");
	while ($line = mysql_fetch_array($result, MYSQL_ASSOC)) {
		echo "\t<tr bgcolor=grey>\n";
		while (list($id, $col_value) = each($line)) {
			if (is_numeric($col_value)) {
				if (ereg("^tos_|^pri_|^pkt_", $id)) {
					printf("\t\t<td><center>%d</center></td>\n", $col_value);
				} else {
					printf("\t\t<td><center>%0.3f</center></td>\n", $col_value);
				}
			} else {
				printf("\t\t<td>%s</td>\n", $col_value);
			}
		}
		echo "\t</tr>\n";
	}
	echo "</table>\n";

	printf("</TABLE>\n");
	printf("<DIV ALIGN=\"right\">%s %s</DIV>\n", $config['program'], $config['version']);
	printf("</BODY>\n</HTML>\n");

	// Free resultset
	mysql_free_result($result);

	// Closing connection
	mysql_close($con);

/*
 * Local Variables:
 * c-basic-offset: 3
 * tab-width: 3
 * End:
 * vim: noet ts=3 sw=3 sts=3
 */

?>
