<?php

/*
** SellaNMS
** $Id: traceroute.php,v 1.8 2005/03/31 18:57:12 sella Exp $
** Copyright (c) 2001 Digital Genesis Software, LLC. All Rights Reserved.
** Released under the GPL Version 2 License.
** http://www.digitalgenesis.com
*/

require_once("config/config.php");
require_once("libs/dgsutil/cgi.php");

$data = getFormData();

?>

<HTML>
	<HEAD>
<?php printf("\t\t<LINK REL=\"stylesheet\" HREF=\"common/%s.css\" TYPE=\"text/css\">\n", isset($config['css']) ? $config['css'] : 'default'); ?>
	</HEAD>
	<BODY TOPMARGIN="0" LEFTMARGIN="0" MARGINWIDTH="0" MARGINHEIGHT="0">
		<TABLE CELLSPACING="1" CELLPADDING="0" BORDER="0" WIDTH="100%">
			<TR CLASS="tbltitle">
				<TD COLSPAN="4" ALIGN="left">Traceroute: <?php printf('%s', escapeshellcmd($data['ip'])); ?></TD>
			</TR>

			<TR CLASS="tblhdr">
				<TD>Hop</TD>
				<TD>Reverse</TD>
				<TD>IP Address</TD>
				<TD>Latency</TD>
			</TR>

		<?php
			flush();

			$command = sprintf("/usr/sbin/traceroute -q1 -w2 %s", escapeshellcmd($data['ip']));
			$output = shell_exec($command);

			$lines = split("\n", $output);

			$trace = '';
			while (list($i, $v) = each($lines)) {
				$fields = split(":", ereg_replace("\(([0-9]+\.[0-9]+\.[0-9]+\.[0-9]+)\)", "\\1", ereg_replace(" +", ":", ereg_replace("^", " ", $v))));

				if ($fields[2] == '*') {
					$trace .= sprintf("\t\t\t<TR CLASS=\"eventfailure1\">\n");
	
					$trace .= sprintf("\t\t\t\t<TD>%s</TD>\n", $fields[1]);
					$trace .= sprintf("\t\t\t\t<TD COLSPAN=\"3\" ALIGN=\"center\">-request timed out-</TD>\n");

					$trace .= sprintf("\t\t\t</TR>\n");
				} else if ($fields[1]) {
					$trace .= sprintf("\t\t\t<TR CLASS=\"eventrecover1\">\n");
	
					$trace .= sprintf("\t\t\t\t<TD>%s</TD>\n", $fields[1]);
					if ($fields[2] != $fields[3]) {
						$trace .= sprintf("\t\t\t\t<TD>%s</TD>\n", $fields[2]);
					} else {
						$trace .= sprintf("\t\t\t\t<TD>&nbsp;</TD>\n");
					}
					$trace .= sprintf("\t\t\t\t<TD>%s</TD>\n", $fields[3]);
					$trace .= sprintf("\t\t\t\t<TD>%s ms</TD>\n", $fields[4]);

					$trace .= sprintf("\t\t\t</TR>\n");
				}
			}
			printf("%s", $trace);
		?>
		</TABLE>

		<CENTER>
			[<A HREF="<?php printf('%s', $data['detail']); ?>">detail screen</A>]
		</CENTER>
	</BODY>
</HTML>

<?

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

?>
