From 9af156eb95dfc90c51aa2cfebd2471f7191f8f7a Mon Sep 17 00:00:00 2001 From: Adam Amstrong Date: Fri, 11 May 2012 17:43:30 +0000 Subject: [PATCH] ports discovery updates git-svn-id: http://www.observium.org/svn/observer/trunk@3171 61d68cd4-352d-0410-923a-c4978735b2b8 --- includes/discovery/ports.inc.php | 74 ++++++++++++++++---------------- includes/functions.php | 2 +- includes/polling/ports.inc.php | 1 + 3 files changed, 38 insertions(+), 39 deletions(-) diff --git a/includes/discovery/ports.inc.php b/includes/discovery/ports.inc.php index f9a8f9fc88..07a843ff88 100755 --- a/includes/discovery/ports.inc.php +++ b/includes/discovery/ports.inc.php @@ -4,65 +4,63 @@ echo("Ports : "); +/// Loop database and build a little cache to reduce db hits. +foreach(dbFetchRows("SELECT * FROM `ports` WHERE `device_id` = ?", array($device['device_id'])) as $port) +{ + $ports_db[$port['ifIndex']] = $port; + $ports_l[$port['ifIndex']] = $port['interface_id']; +} + +#print_r($ports_db); + $ports = array(); $ports = snmpwalk_cache_oid($device, "ifDescr", $ports, "IF-MIB"); -#$ports = snmpwalk_cache_oid($device, "ifName", $ports, "IF-MIB"); -#$ports = snmpwalk_cache_oid($device, "ifType", $ports, "IF-MIB"); - -$interface_ignored = 0; -$interface_added = 0; +$ports = snmpwalk_cache_oid($device, "ifName", $ports, "IF-MIB"); +$ports = snmpwalk_cache_oid($device, "ifType", $ports, "IF-MIB"); +### New interface detection foreach ($ports as $ifIndex => $port) { + /// Check the port against our filters. if (is_port_valid($port, $device)) { - if ($device['os'] == "vmware" && preg_match("/Device ([a-z0-9]+) at .*/", $port['ifDescr'], $matches)) { $port['ifDescr'] = $matches[1]; } - $port['ifDescr'] = fixifName($port['ifDescr']); - if ($debug) echo("\n $if "); - if (mysql_result(mysql_query("SELECT COUNT(*) FROM `ports` WHERE `device_id` = '".$device['device_id']."' AND `ifIndex` = '$ifIndex'"), 0) == '0') + if (!is_array($ports_db[$ifIndex])) { - mysql_query("INSERT INTO `ports` (`device_id`,`ifIndex`,`ifDescr`) VALUES ('".$device['device_id']."','$ifIndex','".mres($port['ifDescr'])."')"); - # Add Interface - echo("+"); + $interface_id = dbInsert(array('device_id' => $device['device_id'], 'ifIndex' => $ifIndex), 'ports'); + echo("Adding: ".$port['ifName']."(".$ifIndex.")(".$ports_db[$port['ifIndex']]['interface_id'].")"); + } elseif ($ports_db[$ifIndex]['deleted'] == "1") { + dbUpdate(array('deleted' => '0'), 'ports', '`interface_id` = ?', array($ports_db[$ifIndex]['interface_id'])); + $ports_db[$ifIndex]['deleted'] = "0"; + echo("U"); } else { - mysql_query("UPDATE `ports` SET `deleted` = '0' WHERE `device_id` = '".$device['device_id']."' AND `ifIndex` = '$ifIndex'"); - echo("."); + echo ("."); } - $int_exists[] = "$ifIndex"; + /// We've seen it. Remove it from the cache. + unset($ports_l[$ifIndex]); } else { - # Ignored Interface - if (mysql_result(mysql_query("SELECT COUNT(*) FROM `ports` WHERE `device_id` = '".$device['device_id']."' AND `ifIndex` = '$ifIndex'"), 0) != '0') - { - mysql_query("UPDATE `ports` SET `deleted` = '1' WHERE `device_id` = '".$device['device_id']."' AND `ifIndex` = '$ifIndex'"); - # Delete Interface - echo("-"); ## Deleted Interface - } else { - echo("X"); ## Ignored Interface + if (is_array($ports_db[$port['ifIndex']])) { + if ($ports_db[$port['ifIndex']]['deleted'] != "1") + { + dbUpdate(array('deleted' => '1'), 'ports', '`interface_id` = ?', array($ports_db[$ifIndex]['interface_id'])); + $ports_db[$ifIndex]['deleted'] = "1"; + } } + echo("X"); } } +### End New interface detection -$sql = "SELECT * FROM `ports` WHERE `device_id` = '".$device['device_id']."' AND `deleted` = '0'"; -$query = mysql_query($sql); +### If it's in our $ports_l list, that means it's not been seen. Mark it deleted. -while ($test_if = mysql_fetch_assoc($query)) +foreach($ports_l as $ifIndex => $port_id) { - unset($exists); - $i = 0; - while ($i < count($int_exists) && !isset($exists)) + if($ports_db[$ifIndex]['deleted'] == "0") { - $this_if = $test_if['ifIndex']; - if ($int_exists[$i] == $this_if) { $exists = 1; } - $i++; - } - if (!$exists) - { - echo("-"); - mysql_query("UPDATE `ports` SET `deleted` = '1' WHERE interface_id = '" . $test_if['interface_id'] . "'"); + dbUpdate(array('deleted' => '1'), 'ports', '`interface_id` = ?', array($port_id)); + echo("-".$ifIndex); } } -unset($temp_exists); echo("\n"); ?> diff --git a/includes/functions.php b/includes/functions.php index ed91d62107..e1e5c0acc9 100755 --- a/includes/functions.php +++ b/includes/functions.php @@ -766,7 +766,7 @@ function include_dir($dir, $regex = "") function is_port_valid($port, $device) { - global $config; + global $config; $debug; if (strstr($port['ifDescr'], "irtual")) { diff --git a/includes/polling/ports.inc.php b/includes/polling/ports.inc.php index e89d390c5c..071642f11e 100755 --- a/includes/polling/ports.inc.php +++ b/includes/polling/ports.inc.php @@ -132,6 +132,7 @@ foreach ($port_stats as $ifIndex => $port) { if (is_port_valid($port, $device)) { + echo("valid"); if (!is_array($ports[$port['ifIndex']])) { $interface_id = dbInsert(array('device_id' => $device['device_id'], 'ifIndex' => $ifIndex), 'ports');