<?php

/*
** SellaNMS
** $Id: interfaceviewer_template.php,v 1.10 2006/01/24 07:08:09 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');
require_once('../libs/util.php');

function print_event($stamp, $description) {
	printf("\t\t<TR CLASS=\"tblentry\">\n");
	printf("\t\t<TD NOWRAP>&nbsp;%s&nbsp;</TD>\n", $stamp);
	printf("\t\t<TD NOWRAP>&nbsp;%s&nbsp;</TD>\n", $description);
	printf("\t\t</TR>\n");
}

function print_item($label, $oid) {
	if ($oid) {
		printf("\t<TR CLASS=\"tblhdr\">\n");
		printf("\t\t<TD NOWRAP>&nbsp;%s&nbsp;</TD>\n", $label);
		printf("\t\t<TD CLASS=\"tblentry\" WIDTH=\"100%%\" NOWRAP>&nbsp;%s&nbsp;</TD>\n", $oid);
		printf("\t</TR>\n");
	}
}

function print_ipaddress($address, $netmask) {
	printf("\t\t<TR CLASS=\"tblentry\">\n");
	printf("\t\t<TD NOWRAP>&nbsp;%s&nbsp;</TD>\n", $address);
	printf("\t\t<TD WIDTH=\"100%%\" NOWRAP>&nbsp;%s&nbsp;</TD>\n", $netmask);
	printf("\t\t</TR>\n");
}

$data = getFormData();

$db = mysql_connect($config['databaseServer'], $config['databaseUsername'], $config['databasePassword']);
mysql_select_db($config['databaseName'], $db);

if ($data['interface']) {
	$sql = sprintf('SELECT variable, value FROM interface_data WHERE interface = %d', $data['interface']);

	$result = mysql_query($sql, $db);
	while ($row = mysql_fetch_array($result)) {
		$info[$row['variable']] = $row['value'];
	}

	$sql = sprintf("SELECT neiid_1.value AS ip, neiid_2.value AS netmask FROM ip_data AS neiid_1, ip_data AS neiid_2, ip WHERE neiid_1.variable = '.1.3.6.1.2.1.4.20.1.1' AND neiid_2.variable = '.1.3.6.1.2.1.4.20.1.3' AND neiid_1.ip = neiid_2.ip AND neiid_1.ip = ip.id AND ip.interface = %d AND neiid_1.value NOT LIKE '0.0.0.0 %%' ORDER BY ip, netmask", $data['interface']);

	$result = mysql_query($sql, $db);
	for ($i = 0; $row = mysql_fetch_array($result); $i++) {
		$ipaddress[$i]['ip'] = $row['ip'];
		$ipaddress[$i]['netmask'] = $row['netmask'];
	}
	if ($info['.1.3.6.1.2.1.2.2.1.5']) {
		if ($info['.1.3.6.1.2.1.2.2.1.5'] >= 1000000000) {
			$info['.1.3.6.1.2.1.2.2.1.5'] = sprintf('%3.2f Gb/s', $info['.1.3.6.1.2.1.2.2.1.5'] / 1000000000);
		} else if ($info['.1.3.6.1.2.1.2.2.1.5'] >= 1000000) {
			$info['.1.3.6.1.2.1.2.2.1.5'] = sprintf('%3.2f Mb/s', $info['.1.3.6.1.2.1.2.2.1.5'] / 1000000);
		} else if ($info['.1.3.6.1.2.1.2.2.1.5'] >= 1000) {
			$info['.1.3.6.1.2.1.2.2.1.5'] = sprintf('%3.2f Kb/s', $info['.1.3.6.1.2.1.2.2.1.5'] / 1000);
		} else {
			$info['.1.3.6.1.2.1.2.2.1.5'] = sprintf('%3.2f b/s', $info['.1.3.6.1.2.1.2.2.1.5']);
		}
	}
	$items[0]['label'] = 'Name';
	$items[0]['value'] = $info['.1.3.6.1.2.1.2.2.1.2'];
	$items[1]['label'] = 'Description';
	$items[1]['value'] = $info['.1.3.6.1.2.1.31.1.1.1.18'];
	$items[2]['label'] = 'Administrative State';
	$items[2]['value'] = sprintf('%s (%s)', ($info['.1.3.6.1.2.1.2.2.1.7'] == 1) ? 'up' : 'down', $info['.1.3.6.1.2.1.2.2.1.7']);
	$items[3]['label'] = 'Operational State';
	$items[3]['value'] = sprintf('%s (%s)', ($info['.1.3.6.1.2.1.2.2.1.8'] == 1) ? 'up' : 'down', $info['.1.3.6.1.2.1.2.2.1.8']);
	$items[4]['label'] = 'Type';
	$items[4]['value'] = $info['.1.3.6.1.2.1.2.2.1.3'];
	$items[5]['label'] = 'Speed';
	$items[5]['value'] = $info['.1.3.6.1.2.1.2.2.1.5'];
	$items[6]['label'] = 'MTU';
	$items[6]['value'] = $info['.1.3.6.1.2.1.2.2.1.4'];
	$items[7]['label'] = 'Physical Address';
	$items[7]['value'] = $info['.1.3.6.1.2.1.2.2.1.6'];
	$items[8]['label'] = 'Connected To';
	$items[8]['value'] = false;

	$sql = sprintf("SELECT UNIX_TIMESTAMP(gui_event.timestamp) AS stamp, element_data.value AS element, interface_data.value AS interface, ip_data.value AS ip, gui_event.brief AS description FROM gui_event, element_data, interface_data, ip_data, element, interface, ip WHERE (gui_event.element = element.id AND gui_event.interface = interface.id AND gui_event.ip = ip.id) AND (element.id = element_data.element AND interface.id = interface_data.interface AND ip_data.ip = ip.id) AND ip_data.variable = '.1.3.6.1.2.1.4.20.1.1' AND interface_data.variable = '.1.3.6.1.2.1.2.2.1.2' AND element_data.variable = '.1.3.6.1.2.1.1.5.0' AND interface.id = '%d' ORDER BY stamp desc, element, interface, ip", $data['interface']);

	$events = array();
	$result = mysql_query($sql, $db);
	for ($i = 0; $row = mysql_fetch_array($result); $i++) {
		$events[$i]['stamp'] = date($config['dateFormat'], $row['stamp']);
		$events[$i]['descr'] = $row['description'];
	}
} else {
	$info['.1.3.6.1.2.1.2.2.1.2'] = 'No Interface Selected';
}

?>
	<TABLE WIDTH="100%" CELLSPACING="1" CELLPADDING="0" BORDER="0">
		<TR CLASS="tbltitle">
			<TD COLSPAN="2">&nbsp;Interface Information: <?php printf('%s', $info['.1.3.6.1.2.1.2.2.1.2'] ? $info['.1.3.6.1.2.1.2.2.1.2'] : 'No Interface Name'); ?>&nbsp;</TD>
		</TR>
<?php

for ($i = 0; $i < count($items); $i++) {
	print_item($items[$i]['label'], $items[$i]['value']);
}
printf("\t\t\t</TABLE>\n");

if (count($ipaddress) > 0) {

?>
	<BR>
	<TABLE WIDTH="100%" CELLSPACING="1" CELLPADDING="0" BORDER="0">
		<TR CLASS="tbltitle">
			<TD COLSPAN="2">IP Address Information: <?php printf('%d', count($ipaddress)); ?></TD>
		</TR>
		<TR CLASS="tblhdr">
			<TD>&nbsp;Address&nbsp;</TD>
			<TD WIDTH="100%">&nbsp;Netmask&nbsp;</TD>
		</TR>
<?php

	for ($i = 0; $i < count($ipaddress); $i++) {
		print_ipaddress($ipaddress[$i]['ip'], $ipaddress[$i]['netmask']);
	}
	printf("\t\t\t</TABLE>\n");
}

if (count($events) > 0) {

?>
	<BR>
	<TABLE WIDTH="100%" CELLSPACING="1" CELLPADDING="0" BORDER="0">
		<TR CLASS="tbltitle">
			<TD COLSPAN="2">&nbsp;Interface Events: <?php printf('%d', count($events)); ?>&nbsp;</TD>
		</TR>
		<TR CLASS="tblhdr">
			<TD>&nbsp;Timestamp&nbsp;</TD>
			<TD WIDTH="100%">&nbsp;Description&nbsp;</TD>
		</TR>
<?php

	for ($i = 0; $i < count($events); $i++) {
		print_event($events[$i]['stamp'], $events[$i]['descr']);
	}
	printf("\t\t\t</TABLE>\n");
}

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

?>
