<?php

/*
** CBLM
** $Id: index.php,v 1.16 2008/02/01 04:36:02 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');

	/* Connect to database */
	$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'];

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

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

	printf("<HTML>\n<HEAD>\n<TITLE>%s %s</TITLE>\n</HEAD>\n<BODY>\n", $config['program'], $config['version']);
	menu();

?>
	<TABLE ALIGN="center">
		<TR>
			<TD COLSPAN="6" BGCOLOR="#aeaeae" ALIGN="center"><B><? printf("%s %s", $config['program'], $config['version']); ?></B></TD>
		</TR>
		<TR BGCOLOR="#aeaeae">
			<TD><B>Probe</B></TD>
			<TD><B>Updated (min)</B></TD>
			<TD><B>Location</B></TD>
			<TD ALIGN="center"><B>Avg RTT</B></TD>
			<TD ALIGN="center"><B>Avg JTR</B></TD>
			<TD ALIGN="center"><B>Loss</B></TD>
		</TR>
<?
	$sql = "SELECT INET_NTOA(ip) ip, name, DATE_FORMAT(updated, '%c/%e/%y %k:%i') updated, (NOW() - updated)/60 min_ago, location, detail, AVG(rtt_avg)/1000 rtt_avg, AVG(jtr_avg)/1000 jtr_avg, 100-SUM(rx)/SUM(tx)*100 loss FROM probes LEFT JOIN data ON (probes.ip = data.src) WHERE secs = 60 AND UNIX_TIMESTAMP() - UNIX_TIMESTAMP(data.timestamp) < 3600 GROUP BY ip ORDER BY name";
	$result = mysql_query($sql);
	while ($line = mysql_fetch_array($result, MYSQL_ASSOC)) {
		if (strcmp($line['name'], $line['ip']) != 0) {
			printf("<TR BGCOLOR=\"#c9c9c9\"><TD>%s (%s)</TD><TD>%s (%d)</TD><TD>%s</TD><TD ALIGN=\"center\">%.2f ms</TD><TD ALIGN=\"center\">%.2f ms</TD><TD ALIGN=\"center\">%.2f%%</TD></TR>\n", $line['name'], $line['ip'], $line['updated'], $line['min_ago'], $line['location'], $line['rtt_avg'], $line['jtr_avg'], $line['loss']);
		} else {
			printf("<TR BGCOLOR=\"#c9c9c9\"><TD>%s</TD><TD>%s (%d)</TD><TD>%s</TD><TD ALIGN=\"center\">%.2f ms</TD><TD ALIGN=\"center\">%.2f ms</TD><TD ALIGN=\"center\">%.2f%%</TD></TR>\n", $line['name'], $line['updated'], $line['min_ago'], $line['location'], $line['rtt_avg'], $line['jtr_avg'], $line['loss']);
		}
		if (strlen($line['detail']) > 0) {
			printf("<TR BGCOLOR=\"#d3d3d3\"><TD COLSPAN=\"6\" ALIGN=\"left\">%s</TD></TR>\n", $line['detail']);
		}
	}
?>
		<TR BGCOLOR="#aeaeae">
			<TD COLSPAN=3 ALIGN="right"><B>Total</B>&nbsp;&nbsp;</TD>
<?
	$sql = "SELECT AVG(rtt_avg)/1000 rtt_avg, AVG(jtr_avg)/1000 jtr_avg, 100-(SUM(rx)/SUM(tx)*100) loss FROM data WHERE timestamp > NOW() - 3600";

	$result = mysql_query($sql);
	$line = mysql_fetch_array($result, MYSQL_ASSOC);
	printf("<TD ALIGN=\"center\">%.2f ms</TD><TD ALIGN=\"center\">%.2f ms</TD><TD ALIGN=\"center\">%.2f%%</TD></TR>\n", $line['rtt_avg'], $line['jtr_avg'], $line['loss']);

	$mtime = explode(" ", microtime());
	$mtime = $mtime[1] + $mtime[0]; 
	printf("<TR><TD COLSPAN=6 ALIGN=\"right\"><I><FONT SIZE=-2>Generated in %.1f secs</FONT></I></TD></TR>", $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
 */

?>
