<?php

/*
** CBLM
** $Id: index.php,v 1.21 2008/02/10 06:18:35 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');
	require('db.php');

	/* Variables */
	$unresponsive = 3;

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

	$con = connect();

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

	$sql = "SELECT INET_NTOA(ip) ip, name, DATE_FORMAT(updated, '%c/%e/%y %k:%i') updated, IF (updated > 0, (NOW() - updated)/60, -1) 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 data.timestamp > NOW() - 1800 GROUP BY probes.ip WITH ROLLUP";
	$result = mysql_query($sql);
?>
	<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 (<? printf("%d", (mysql_num_rows($result) > 0) ? mysql_num_rows($result) - 1 : 0); ?>)</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>
<?
	if (mysql_num_rows($result) < 1) {
		printf("<TR BGCOLOR=\"#c9c9c9\"><TD COLSPAN=6 ALIGN=center>No data available</TD></TR>\n");
	}
	while ($line = mysql_fetch_array($result, MYSQL_ASSOC)) {
		if ($line['ip'] == NULL) {
			printf("<TR BGCOLOR=\"#aeaeae\"><TD COLSPAN=3 ALIGN=\"right\"><B>Total</B>&nbsp;&nbsp;</TD>");
			printf("<TD ALIGN=\"center\">%.2f ms</TD><TD ALIGN=\"center\">%.2f ms</TD><TD ALIGN=\"center\">%.3f%%</TD></TR>\n", $line['rtt_avg'], $line['jtr_avg'], $line['loss']);
		} else {
			if ($line['min_ago'] < 0) {
				$updated = 'shutdown';
			} else if ($line['min_ago'] >= $unresponsive) {
				$updated = sprintf("unresponsive (%d)", $line['min_ago']);
			} else {
				$updated = sprintf("%s (%d)", $line['updated'], $line['min_ago']);
			}

			if (strcmp($line['name'], $line['ip']) != 0) {
				printf("<TR BGCOLOR=\"#c9c9c9\"><TD>%s (%s)</TD><TD>%s</TD><TD>%s</TD><TD ALIGN=\"center\">%.2f ms</TD><TD ALIGN=\"center\">%.2f ms</TD><TD ALIGN=\"center\">%.3f%%</TD></TR>\n", $line['name'], $line['ip'], $updated, $line['location'], $line['rtt_avg'], $line['jtr_avg'], $line['loss']);
			} else {
				printf("<TR BGCOLOR=\"#c9c9c9\"><TD>%s</TD><TD>%s</TD><TD>%s</TD><TD ALIGN=\"center\">%.2f ms</TD><TD ALIGN=\"center\">%.2f ms</TD><TD ALIGN=\"center\">%.3f%%</TD></TR>\n", $line['name'], $updated, $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']);
			}
		}
	}

	$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
 */

?>
