mirror of
				https://github.com/librenms/librenms.git
				synced 2024-10-07 16:52:45 +00:00 
			
		
		
		
	ports discovery updates
git-svn-id: http://www.observium.org/svn/observer/trunk@3171 61d68cd4-352d-0410-923a-c4978735b2b8
This commit is contained in:
		@@ -4,65 +4,63 @@
 | 
				
			|||||||
 | 
					
 | 
				
			||||||
echo("Ports : ");
 | 
					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 = array();
 | 
				
			||||||
$ports = snmpwalk_cache_oid($device, "ifDescr", $ports, "IF-MIB");
 | 
					$ports = snmpwalk_cache_oid($device, "ifDescr", $ports, "IF-MIB");
 | 
				
			||||||
#$ports = snmpwalk_cache_oid($device, "ifName", $ports, "IF-MIB");
 | 
					$ports = snmpwalk_cache_oid($device, "ifName", $ports, "IF-MIB");
 | 
				
			||||||
#$ports = snmpwalk_cache_oid($device, "ifType", $ports, "IF-MIB");
 | 
					$ports = snmpwalk_cache_oid($device, "ifType", $ports, "IF-MIB");
 | 
				
			||||||
 | 
					 | 
				
			||||||
$interface_ignored = 0;
 | 
					 | 
				
			||||||
$interface_added   = 0;
 | 
					 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					### New interface detection
 | 
				
			||||||
foreach ($ports as $ifIndex => $port)
 | 
					foreach ($ports as $ifIndex => $port)
 | 
				
			||||||
{
 | 
					{
 | 
				
			||||||
 | 
					  /// Check the port against our filters.
 | 
				
			||||||
  if (is_port_valid($port, $device))
 | 
					  if (is_port_valid($port, $device))
 | 
				
			||||||
  {
 | 
					  {
 | 
				
			||||||
    if ($device['os'] == "vmware" && preg_match("/Device ([a-z0-9]+) at .*/", $port['ifDescr'], $matches)) { $port['ifDescr'] = $matches[1]; }
 | 
					    if (!is_array($ports_db[$ifIndex]))
 | 
				
			||||||
    $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')
 | 
					 | 
				
			||||||
    {
 | 
					    {
 | 
				
			||||||
      mysql_query("INSERT INTO `ports` (`device_id`,`ifIndex`,`ifDescr`) VALUES ('".$device['device_id']."','$ifIndex','".mres($port['ifDescr'])."')");
 | 
					      $interface_id = dbInsert(array('device_id' => $device['device_id'], 'ifIndex' => $ifIndex), 'ports');
 | 
				
			||||||
      # Add Interface
 | 
					      echo("Adding: ".$port['ifName']."(".$ifIndex.")(".$ports_db[$port['ifIndex']]['interface_id'].")");
 | 
				
			||||||
      echo("+");
 | 
					    } 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 {
 | 
					    } 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 {
 | 
					  } else {
 | 
				
			||||||
    # Ignored Interface
 | 
					    if (is_array($ports_db[$port['ifIndex']])) {
 | 
				
			||||||
    if (mysql_result(mysql_query("SELECT COUNT(*) FROM `ports` WHERE `device_id` = '".$device['device_id']."' AND `ifIndex` = '$ifIndex'"), 0) != '0')
 | 
					      if ($ports_db[$port['ifIndex']]['deleted'] != "1")
 | 
				
			||||||
    {
 | 
					      {
 | 
				
			||||||
      mysql_query("UPDATE `ports` SET `deleted` = '1' WHERE `device_id` = '".$device['device_id']."' AND `ifIndex` = '$ifIndex'");
 | 
					        dbUpdate(array('deleted' => '1'), 'ports', '`interface_id` = ?', array($ports_db[$ifIndex]['interface_id']));
 | 
				
			||||||
      # Delete Interface
 | 
					        $ports_db[$ifIndex]['deleted'] = "1";
 | 
				
			||||||
      echo("-"); ## Deleted Interface
 | 
					      }
 | 
				
			||||||
    } else {
 | 
					 | 
				
			||||||
      echo("X"); ## Ignored Interface
 | 
					 | 
				
			||||||
    }
 | 
					    }
 | 
				
			||||||
 | 
					    echo("X");
 | 
				
			||||||
  }
 | 
					  }
 | 
				
			||||||
}
 | 
					}
 | 
				
			||||||
 | 
					### End New interface detection
 | 
				
			||||||
 | 
					
 | 
				
			||||||
$sql = "SELECT * FROM `ports` WHERE `device_id`  = '".$device['device_id']."' AND `deleted` = '0'";
 | 
					### If it's in our $ports_l list, that means it's not been seen. Mark it deleted.
 | 
				
			||||||
$query = mysql_query($sql);
 | 
					 | 
				
			||||||
 | 
					
 | 
				
			||||||
while ($test_if = mysql_fetch_assoc($query))
 | 
					foreach($ports_l as $ifIndex => $port_id)
 | 
				
			||||||
{
 | 
					{
 | 
				
			||||||
  unset($exists);
 | 
					  if($ports_db[$ifIndex]['deleted'] == "0")
 | 
				
			||||||
  $i = 0;
 | 
					 | 
				
			||||||
  while ($i < count($int_exists) && !isset($exists))
 | 
					 | 
				
			||||||
  {
 | 
					  {
 | 
				
			||||||
    $this_if = $test_if['ifIndex'];
 | 
					    dbUpdate(array('deleted' => '1'), 'ports', '`interface_id` = ?', array($port_id));
 | 
				
			||||||
    if ($int_exists[$i] == $this_if) { $exists = 1; }
 | 
					    echo("-".$ifIndex);
 | 
				
			||||||
    $i++;
 | 
					 | 
				
			||||||
  }
 | 
					 | 
				
			||||||
  if (!$exists)
 | 
					 | 
				
			||||||
  {
 | 
					 | 
				
			||||||
    echo("-");
 | 
					 | 
				
			||||||
    mysql_query("UPDATE `ports` SET `deleted` = '1' WHERE interface_id = '" . $test_if['interface_id'] . "'");
 | 
					 | 
				
			||||||
  }
 | 
					  }
 | 
				
			||||||
}
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
unset($temp_exists);
 | 
					 | 
				
			||||||
echo("\n");
 | 
					echo("\n");
 | 
				
			||||||
 | 
					
 | 
				
			||||||
?>
 | 
					?>
 | 
				
			||||||
 
 | 
				
			|||||||
@@ -766,7 +766,7 @@ function include_dir($dir, $regex = "")
 | 
				
			|||||||
function is_port_valid($port, $device)
 | 
					function is_port_valid($port, $device)
 | 
				
			||||||
{
 | 
					{
 | 
				
			||||||
 | 
					
 | 
				
			||||||
  global $config;
 | 
					  global $config; $debug;
 | 
				
			||||||
 | 
					
 | 
				
			||||||
  if (strstr($port['ifDescr'], "irtual"))
 | 
					  if (strstr($port['ifDescr'], "irtual"))
 | 
				
			||||||
  {
 | 
					  {
 | 
				
			||||||
 
 | 
				
			|||||||
@@ -132,6 +132,7 @@ foreach ($port_stats as $ifIndex => $port)
 | 
				
			|||||||
{
 | 
					{
 | 
				
			||||||
  if (is_port_valid($port, $device))
 | 
					  if (is_port_valid($port, $device))
 | 
				
			||||||
  {
 | 
					  {
 | 
				
			||||||
 | 
					    echo("valid");
 | 
				
			||||||
    if (!is_array($ports[$port['ifIndex']]))
 | 
					    if (!is_array($ports[$port['ifIndex']]))
 | 
				
			||||||
    {
 | 
					    {
 | 
				
			||||||
      $interface_id = dbInsert(array('device_id' => $device['device_id'], 'ifIndex' => $ifIndex), 'ports');
 | 
					      $interface_id = dbInsert(array('device_id' => $device['device_id'], 'ifIndex' => $ifIndex), 'ports');
 | 
				
			||||||
 
 | 
				
			|||||||
		Reference in New Issue
	
	Block a user