<?php

/*
** CBLM
** $Id: matrix.php,v 1.22 2008/02/11 17:16:26 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('util.php');
	require('menu.php');
	require('db.php');

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

	$periods = $config['periods'];
	$period = isset($_REQUEST['period']) ? $_REQUEST['period'] : 120;
	$host = !isset($_REQUEST['host']) ? 1 : $_REQUEST['host'];
	$tos = !isset($_REQUEST['tos']) ? -1 : $_REQUEST['tos'];
	$field = !isset($_REQUEST['field']) ? 2 : $_REQUEST['field'];

	/* Set defaults */
	$_REQUEST['src_name'] = '';
	$_REQUEST['dst_name'] = '';
	$_REQUEST['src_ip'] = '';
	$_REQUEST['dst_ip'] = '';

	if (!isset($_REQUEST['f'][0])) {
		$_REQUEST['f'][0] = 'r_avg';
	}

	if (!isset($_REQUEST['f'][1])) {
		$_REQUEST['f'][1] = 'loss';
	}

	if (isset($_REQUEST['secs']) && $period <= $_REQUEST['secs']) {
		$period = $_REQUEST['secs'] * 2;
	}

	$mtime = explode(" ", microtime());
	$starttime = $mtime[1] + $mtime[0]; 

	/* Input Selection */
	printf("<HTML>\n<HEAD>\n<TITLE>CBLM Matrix View</TITLE>\n");
	if (isset($_REQUEST['refresh']) && $_REQUEST['refresh'] >= 10) {
		printf("<META HTTP-EQUIV=\"Refresh\" CONTENT=%d>\n", $_REQUEST['refresh']);
	}
	printf("</HEAD>\n<BODY>\n");
	menu();
	$con = connect();

	printf("<TABLE ALIGN=\"CENTER\"><TR><TD ALIGN=\"CENTER\"><B><FORM>\n");


	/* Query database for Precision Values */
	$sql = "SELECT secs FROM data GROUP BY secs ORDER BY secs";
	$result = mysql_query($sql);
	printf("&nbsp;Precision <SELECT NAME=\"secs\">\n");
	printf("<OPTION %s VALUE=\"-1\">any</OPTION>\n", $_REQUEST['secs'] == "-1" ? 'SELECTED' : '');
	while ($line = mysql_fetch_array($result, MYSQL_ASSOC)) {
		if ($line['secs'] >= 86400) {
			$var = "day";
			$val = $line['secs'] / 86400;
		} else if ($line['secs'] >= 3600) {
			$var = "hour";
			$val = $line['secs'] / 3600;
		} else if ($line['secs'] >= 60) {
			$var = "min";
			$val = $line['secs'] / 60;
		} else {
			$var = "sec";
			$val = $line['secs'];
		}
		if (!is_float($val)) {
			printf("<OPTION %s VALUE=\"%d\">%d %s</OPTION>\n", $_REQUEST['secs'] == $line['secs'] ? 'SELECTED' : '', $line['secs'], $val, $var);
		} else {
			printf("<OPTION %s VALUE=\"%d\">%.1f %s</OPTION>\n", $_REQUEST['secs'] == $line['secs'] ? 'SELECTED' : '', $line['secs'], $val, $var);
		}
	}
	printf("</SELECT>");
	
	/* Period */
	printf("Period <SELECT NAME=\"period\">\n");
	printf("<OPTION VALUE=\"%s\">%s sec</OPTION>\n", $period, $period);
	while (list($int, $desc) = each($periods)) {
		printf("<OPTION %s VALUE=\"%d\">%s</OPTION>\n", $period == $int ? "SELECTED" : "", $int, $desc);
	}
	printf("</SELECT>");

	/* Query database for ToS/PRI Values */
	$sql = "SELECT tos, tos << 2 dscp, pri FROM data GROUP BY tos, pri ORDER BY tos, pri";
	$result = mysql_query($sql);
	printf("&nbsp;ToS/PRI <SELECT NAME=\"tos\">\n");
	printf("<OPTION %s VALUE=\"-1\">any</OPTION>\n", $_REQUEST['tos'] == "-1" ? 'SELECTED' : '');
	while ($line = mysql_fetch_array($result, MYSQL_ASSOC)) {
		printf("<OPTION %s VALUE=\"%d:%d\">%d / %d</OPTION>\n", $_REQUEST['tos'] == $line['tos'] . ':' . $line['pri'] ? 'SELECTED' : '', $line['tos'], $line['pri'], $line['tos'], $line['pri']);
	}
	printf("</SELECT>");
	
	/* Probe As */
	printf("&nbsp;Probe As <SELECT NAME=\"host\">\n");
	printf("<OPTION %s VALUE=\"1\">Host</OPTION>\n", $host == 1 ? 'SELECTED' : '');
	printf("<OPTION %s VALUE=\"0\">IP</OPTION>\n", $host == 0 ? 'SELECTED' : '');
	printf("<OPTION %s VALUE=\"2\">Both</OPTION>\n", $host == 2 ? 'SELECTED' : '');
	printf("</SELECT>");
	
	/* Fields */
	printf("&nbsp;Fields <SELECT NAME=\"field\">\n");
	for ($i = 1; $i < 10; $i++) {
		printf("<OPTION %s VALUE=\"%d\">%d</OPTION>\n", $field == $i ? 'SELECTED' : '', $i, $i);
	}
	printf("</SELECT>");
	
	/* Refresh */
	printf("&nbsp;Refresh <SELECT NAME=\"refresh\">\n");
	printf("<OPTION %s VALUE=\"60\">1 min</OPTION>\n", $_REQUEST['refresh'] == 60 ? 'SELECTED' : '');
	printf("<OPTION %s VALUE=\"300\">5 min</OPTION>\n", $_REQUEST['refresh'] == 300 ? 'SELECTED' : '');
	printf("<OPTION %s VALUE=\"900\">15 min</OPTION>\n", $_REQUEST['refresh'] == 900 ? 'SELECTED' : '');
	printf("</SELECT>");
	
	printf("&nbsp;<INPUT VALUE=\"Submit\" TYPE=\"submit\">\n");

	printf("</B></TD></TR><TR><TD ALIGN=\"CENTER\">\n<B>Display</B>");

	for ($i = 0; $i < $field; $i++) {
		printf("&nbsp;<SELECT NAME=\"f[%d]\">\n", $i);
		if ($i > 0) {
			printf("<OPTION VALUE=\"\">None</OPTION>\n");
		}
		printf("<OPTION %s VALUE=\"loss\">Packet Loss</OPTION>\n", $_REQUEST['f'][$i] == 'loss' ? 'SELECTED' : '');
		printf("<OPTION %s VALUE=\"avail\">Availability</OPTION>\n", $_REQUEST['f'][$i] == 'avail' ? 'SELECTED' : '');
		printf("<OPTION %s VALUE=\"r_avg\">RTT Avgerage</OPTION>\n", $_REQUEST['f'][$i] == 'r_avg' ? 'SELECTED' : '');
		printf("<OPTION %s VALUE=\"r_min\">RTT Minimum</OPTION>\n", $_REQUEST['f'][$i] == 'r_min' ? 'SELECTED' : '');
		printf("<OPTION %s VALUE=\"r_max\">RTT Maximum</OPTION>\n", $_REQUEST['f'][$i] == 'r_max' ? 'SELECTED' : '');
		printf("<OPTION %s VALUE=\"r_per\">RTT Percentage</OPTION>\n", $_REQUEST['f'][$i] == 'r_per' ? 'SELECTED' : '');
		printf("<OPTION %s VALUE=\"o_avg\">OTT Average</OPTION>\n", $_REQUEST['f'][$i] == 'o_avg' ? 'SELECTED' : '');
		printf("<OPTION %s VALUE=\"o_min\">OTT Minimum</OPTION>\n", $_REQUEST['f'][$i] == 'o_min' ? 'SELECTED' : '');
		printf("<OPTION %s VALUE=\"o_max\">OTT Maximum</OPTION>\n", $_REQUEST['f'][$i] == 'o_max' ? 'SELECTED' : '');
		printf("<OPTION %s VALUE=\"o_per\">OTT Percentage</OPTION>\n", $_REQUEST['f'][$i] == 'o_per' ? 'SELECTED' : '');
		printf("<OPTION %s VALUE=\"j_avg\">Jitter Average</OPTION>\n", $_REQUEST['f'][$i] == 'j_avg' ? 'SELECTED' : '');
		printf("<OPTION %s VALUE=\"j_min\">Jitter Minimum</OPTION>\n", $_REQUEST['f'][$i] == 'j_min' ? 'SELECTED' : '');
		printf("<OPTION %s VALUE=\"j_max\">Jitter Maximum</OPTION>\n", $_REQUEST['f'][$i] == 'j_max' ? 'SELECTED' : '');
		printf("<OPTION %s VALUE=\"j_per\">Jitter Percentage</OPTION>\n", $_REQUEST['f'][$i] == 'j_per' ? 'SELECTED' : '');
		printf("<OPTION %s VALUE=\"t_faz\">ToS Marking Failure A to Z</OPTION>\n", $_REQUEST['f'][$i] == 't_faz' ? 'SELECTED' : '');
		printf("<OPTION %s VALUE=\"t_fza\">ToS Marking Failure Z to A</OPTION>\n", $_REQUEST['f'][$i] == 't_fza' ? 'SELECTED' : '');
		printf("<OPTION %s VALUE=\"t_paz\">ToS Percentage Failed A to Z</OPTION>\n", $_REQUEST['f'][$i] == 't_paz' ? 'SELECTED' : '');
		printf("<OPTION %s VALUE=\"t_pza\">ToS Percentage Failed Z to A</OPTION>\n", $_REQUEST['f'][$i] == 't_pza' ? 'SELECTED' : '');
		printf("<OPTION %s VALUE=\"p_faz\">Priority Marking Failure A to Z</OPTION>\n", $_REQUEST['f'][$i] == 'p_faz' ? 'SELECTED' : '');
		printf("<OPTION %s VALUE=\"p_fza\">Priority Marking Failure Z to A</OPTION>\n", $_REQUEST['f'][$i] == 'p_fza' ? 'SELECTED' : '');
		printf("<OPTION %s VALUE=\"p_paz\">Priority Percentage Failed A to Z</OPTION>\n", $_REQUEST['f'][$i] == 'p_paz' ? 'SELECTED' : '');
		printf("<OPTION %s VALUE=\"p_pza\">Priority Percentage Failed Z to A</OPTION>\n", $_REQUEST['f'][$i] == 'p_pza' ? 'SELECTED' : '');
		printf("</SELECT>");
		printf("<INPUT NAME=\"t[%d]\" VALUE=\"%s\" SIZE=2>", $i, isset($_REQUEST['t'][$i]) ? $_REQUEST['t'][$i] : '');
		printf("<INPUT NAME=\"w[%d]\" VALUE=\"%s\" SIZE=2>", $i, isset($_REQUEST['w'][$i]) ? $_REQUEST['w'][$i] : '');
	}
	
	printf("</TD></TR></TABLE></FORM>\n<P>\n");

	/* Construct SQL Statement */
	$sql = buildSQL($_REQUEST);
	printf("<!-- SQL: %s -->\n", $sql);
	if ($config['debug']) {
		printf("SQL: %s\n", $sql);
	}

	/* Query database */
	if (!$result = mysql_query($sql)) {
		printf("<TABLE ALIGN=\"CENTER\">\n<TR><TD><B>[Database select failed]</B></TD></TR></TABLE>\n");
		exit(0);
	}

	while ($line = mysql_fetch_array($result, MYSQL_ASSOC)) {
		$src = $line['src_ip'];
		$dst = $line['dst_ip'];
	
		$data[$src][$dst]['f'][0] = $line[$_REQUEST['f'][0]];
		if (isset($_REQUEST['f'][1])) {
			$data[$src][$dst]['f'][1] = $line[$_REQUEST['f'][1]];
		}
		if (isset($_REQUEST['f'][2])) {
			$data[$src][$dst]['f'][2] = $line[$_REQUEST['f'][2]];
		}
		$srcs[] = $src;
		if ($host == 1) {
			$hosts[$src] = $line['src_name'];
		} else if ($host == 2) {
			$hosts[$src] = sprintf("%s<BR>(%s)", $line['src_name'], $line['src_ip']);
		} else {
			$hosts[$src] = $line['src_ip'];
		}
	}
	/* There must be at least 2 rows. */
	if (count($srcs) > 1) {
		$srcs = array_unique($srcs);
	} else {
		printf("<TABLE ALIGN=\"CENTER\">\n<TR><TD><B>[No data for selected setings]</B></TD></TR></TABLE>\n");
		exit(0);
	}

	/* Table header */
	reset($data);
	printf("<TABLE ALIGN=\"CENTER\">\n<TR><TD NOWRAP ALIGN=\"center\" BGCOLOR=\"%s\">&nbsp;SRC \\ DST&nbsp;</TD>", $color_header);
	while (list(,$src) = each($srcs)) {
		printf("<TD NOWRAP BGCOLOR=\"%s\" ALIGN=\"center\">&nbsp;%s&nbsp;</TD>", $color_header, $hosts[$src]);
	}
	printf("</TR>\n");

	/* Table Body */
	$dsts = $srcs;
	reset($srcs);
	while (list(,$src) = each($srcs)) {
		printf("<TR><TD BGCOLOR=\"%s\" ALIGN=\"right\">%s&nbsp;</TD>", $color_header, $hosts[$src]);
		reset($dsts);
		while (list(,$dst) = each($dsts)) {
			if ($src == $dst) {
				printf("<TD NOWRAP BGCOLOR=\"%s\">&nbsp;</TD>", $color_blank);
			} else {
				$buf = "";
				for ($i = 0; $i < count($_REQUEST['f']) && $i < $field; $i++) {
					$val = $data[$src][$dst]['f'][$i];
					if (strlen($_REQUEST['f'][$i]) > 0) {
						switch ($_REQUEST['f'][$i]) {
							case 'loss':
								$fail = isset($_REQUEST['w'][$i]) && strlen($_REQUEST['w'][$i]) > 0 ? $_REQUEST['w'][$i] : 1.0;
								$warn = isset($_REQUEST['t'][$i]) && strlen($_REQUEST['t'][$i]) > 0 ? $_REQUEST['t'][$i] : 0.001;
								if ($val >= $fail) {
									$color = $color_fail;
								} else if ($val >= $warn) {
									$color = $color_warn;
								} else {
									$color = $color_good;
								}
								$buf .= sprintf(" <FONT COLOR=\"%s\">%.3f</FONT>%% /", $color, $val);
								break;

							case 'avail':
							case 'r_per':
							case 'o_per':
							case 'j_per':
								$fail = isset($_REQUEST['w'][$i]) && strlen($_REQUEST['w'][$i]) > 0 ? $_REQUEST['w'][$i] : 99.0;
								$warn = isset($_REQUEST['t'][$i]) && strlen($_REQUEST['t'][$i]) > 0 ? $_REQUEST['t'][$i] : 99.999;
								if ($val <= $fail) {
									$color = $color_fail;
								} else if ($val <= $warn) {
									$color = $color_warn;
								} else {
									$color = $color_good;
								}
								$buf .= sprintf(" <FONT COLOR=\"%s\">%.3f</FONT>%% /", $color, $val);
								break;
	
							case 't_paz':
							case 't_pza':
							case 'p_paz':
							case 'p_pza':
								$fail = isset($_REQUEST['w'][$i]) && strlen($_REQUEST['w'][$i]) > 0 ? $_REQUEST['w'][$i] : 5.0;
								$warn = isset($_REQUEST['t'][$i]) && strlen($_REQUEST['t'][$i]) > 0 ? $_REQUEST['t'][$i] : 1.0;
								if ($val >= $fail) {
									$color = $color_fail;
								} else if ($val >= $warn) {
									$color = $color_warn;
								} else {
									$color = $color_good;
								}
								$buf .= sprintf(" <FONT COLOR=\"%s\">%.2f</FONT>%% /", $color, $val);
								break;

							case 't_faz':
							case 't_fza':
							case 'p_faz':
							case 'p_fza':
								$fail = isset($_REQUEST['w'][$i]) && strlen($_REQUEST['w'][$i]) > 0 ? $_REQUEST['w'][$i] : 5.0;
								$warn = isset($_REQUEST['t'][$i]) && strlen($_REQUEST['t'][$i]) > 0 ? $_REQUEST['t'][$i] : 1.0;
								if ($val >= $fail) {
									$color = $color_fail;
									$val = "Fail";
								} else if ($val >= $warn) {
									$color = $color_warn;
									$val = "Warn";
								} else {
									$color = $color_good;
									$val = "Pass";
								}
								$buf .= sprintf(" <FONT COLOR=\"%s\">%s</FONT> /", $color, $val);
								break;

							default:
								$fail = isset($_REQUEST['w'][$i]) && strlen($_REQUEST['w'][$i]) > 0 ? $_REQUEST['w'][$i] : 100.0;
								$warn = isset($_REQUEST['t'][$i]) && strlen($_REQUEST['t'][$i]) > 0 ? $_REQUEST['t'][$i] : 75.0;
								if ($val >= $fail) {
									$color = $color_fail;
								} else if ($val >= $warn) {
									$color = $color_warn;
								} else {
									$color = $color_good;
								}
								$buf .= sprintf(" <FONT COLOR=\"%s\">%.2f</FONT> ms /", $color, $val);
								break;
						}
					}
				}
				$buf = ereg_replace('(^.*) /$', '\1', $buf);	
				printf("<TD BGCOLOR=\"%s\" ALIGN=\"center\">%s</TD>", $color_cell, $buf);
			}
		}
		printf("</TR>\n");
	}

	$mtime = explode(" ", microtime());
	$mtime = $mtime[1] + $mtime[0]; 
	printf("<TR><TD COLSPAN=%d ALIGN=\"right\"><I><FONT SIZE=-2>Generated in %.1f secs</FONT></I></TD></TR>", 1 + count($dsts), $mtime - $starttime);

	printf("</TABLE>\n");
	printf("<DIV ALIGN=\"right\"><A HREF=\"%s\">%s %s</A></DIV>\n", $config['url'], $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
 */

?>
