<?php

/*
** SellaNMS
** $Id: repairs.php,v 1.16 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
*/

class Repairs extends Module {
	function getContent($tabs) {
		global $lang;

		for ($i = 0; $i < $tabs; $i++) {
			$indent .= "\t";
		}
		$elements = $this->getRepairedElements();

		$content = "";

		$content .= sprintf("%s<TABLE WIDTH=\"100%%\" CELLSPACING=\"1\" CELLPADDING\"0\" BORDER=\"0\">\n", $indent);
		$content .= sprintf("%s\t<TR BGCOLOR=\"28397c\">\n", $indent);
		$content .= sprintf("%s\t\t<TD COLSPAN=\"3\">\n", $indent);
		$content .= sprintf("%s\t\t\t<FONT COLOR=\"ffffff\" SIZE=\"+0\">%s: %s</FONT>\n", $indent, $lang['Recently Recovered Elements'], sizeof($elements));
		$content .= sprintf("%s\t\t</TD>\n", $indent);
		$content .= sprintf("%s\t</TR>\n", $indent);
		$content .= sprintf("%s\t<TR BGCOLOR=\"000000\">\n", $indent);
		$content .= sprintf("%s\t\t<TD>\n", $indent);
		$content .= sprintf("%s\t\t\t<FONT COLOR=\"ffffff\">%s</FONT>\n", $indent, $lang['Timestamp']);
		$content .= sprintf("%s\t\t</TD>\n", $indent);
		$content .= sprintf("%s\t\t<TD WIDTH=\"100%%\" COLSPAN=\"2\">\n", $indent);
		$content .= sprintf("%s\t\t\t<FONT COLOR=\"ffffff\">%s</FONT>\n", $indent, $lang['Element']);
		$content .= sprintf("%s\t\t</TD>\n", $indent);
		$content .= sprintf("%s\t</TR>\n", $indent);

		$cutoff_time = time() - 600;
		while(list($i, ) = each($elements)) {
			if ($elements[$i]['timestamp_raw'] > $cutoff_time) { /* Within 10 minutes */
				$color = '44aa44'; //Green
			} else {
				$color = '337733'; //Dark Green
			}
			$content .= sprintf("%s\t<TR BGCOLOR=\"%s\">\n", $indent, $color);
			$content .= sprintf("%s\t\t<TD NOWRAP>\n", $indent);
			$content .= sprintf("%s\t\t\t<FONT COLOR=\"000000\">%s</FONT>\n", $indent, $elements[$i]['timestamp']);
			$content .= sprintf("%s\t\t</TD>\n", $indent);

			if ($elements[$i]['element']) {
				$content .= sprintf("%s\t\t<TD NOWRAP>\n", $indent);
				$content .= sprintf("%s\t\t\t<FONT COLOR=\"000000\">%s</FONT>\n", $indent, $elements[$i]['element']);
				$content .= sprintf("%s\t\t</TD>\n", $indent);
				$content .= sprintf("%s\t\t<TD WIDTH=\"100%%\" NOWRAP>\n", $indent);
				$content .= sprintf("%s\t\t\t<FONT COLOR=\"000000\">%s (<A HREF=\"telnet://%s\">%s</A>)</FONT>\n", $indent, $elements[$i]['interface'], $elements[$i]['ip'], $elements[$i]['ip']);
			} else {
				$content .= sprintf("%s\t\t<TD NOWRAP COLSPAN=\"2\">\n", $indent);
				$content .= sprintf("%s\t\t\t<FONT COLOR=\"000000\">%s</FONT>\n", $indent, $elements[$i]['address']);
			}
			$content .= sprintf("%s\t\t</TD>\n", $indent);
			$content .= sprintf("%s\t</TR>\n", $indent);
		}

		unset($elements);	

		$content .= sprintf("%s</TABLE>\n", $indent);

		return $content;
	}

	function shouldUpdate() {
		global $config;

		$module_name = get_class($this);

		global ${$module_name};

		$current_recoveries = array();
		$old_recoveries = ${$module_name}['old_recoveries'];
		if (!isset($old_recoveries)) {
			$old_recoveries = array();
		}

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

		$sql = 'SELECT DISTINCT ed1.event_target_id AS t1 FROM event_data AS ed1 WHERE ed1.class = 2 ORDER BY ed1.event_target_id';

		$result = mysql_query($sql, $db);

		$changed = false;
		while ($row = mysql_fetch_array($result)) {
			$current_recoveries[$row['t1']] = true;

			if ($old_recoveries[$row['t1']] == true) {
				$old_recoveries[$row['t1']] = false;
			} else {
				$changed = true;
			}
		}
		mysql_close($db);

		while (list(,$i) = each($old_recoveries)) {
			if ($i == true) {
				$changed = true;
			}
		}
		${$module_name}['old_recoveries'] = $current_recoveries;

		if ($changed == true) {
			return true;
		} else {
			return false;
		}
	}

	function getRepairedElements() {
		global $config;

		$ret_val = array();

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

		$sql = "CREATE TEMPORARY TABLE recovery (id int unsigned)";
		$result = mysql_query($sql, $db);

		$sql = "INSERT INTO recovery SELECT event_data.id FROM event_data WHERE event_data.class <= '2' ORDER BY event_data.event_target_id,event_data.priority DESC,event_data.stamp DESC";
		$result = mysql_query($sql, $db);

		$sql = "SELECT UNIX_TIMESTAMP(event_data.stamp) AS stamp, event_data.class AS class, event_data.priority as priority, event_data.protocol as protocol, event_data.brief AS description, element_data.value AS element, interface_data.value AS interface, ip_data.value AS ip FROM recovery, event_data, event_target, element, element_data, interface, ip, interface_data, ip_data WHERE recovery.id = event_data.id AND event_data.event_target_id = event_target.id AND (event_target.element = element.id AND event_target.interface = interface.id AND event_target.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 LIKE '.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' GROUP BY event_data.event_target_id HAVING event_data.class = 2";

		$result = mysql_query($sql, $db);	
		for ($id = 0; $row = mysql_fetch_array($result); $id++) {
			$ret_val[$id]['timestamp'] = date('M j H:i', $row['stamp']);
			$ret_val[$id]['timestamp_raw'] = $row['stamp'];
			$ret_val[$id]['element'] = $row['element'];
			$ret_val[$id]['interface'] = $row['interface'];
			$ret_val[$id]['ip'] = $row['ip'];
			$ret_val[$id]['protocol'] = $row['protocol'];
			$ret_val[$id]['description'] = $row['description'];
		}

		$sql = "DROP TABLE recovery";
		$result = mysql_query($sql, $db);

		mysql_close($db);

		return $ret_val;
	}
}

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

?>
