From 76a29f41e3c0a839fa8c7a7de97a8bfa2ea4421a Mon Sep 17 00:00:00 2001 From: Maximilian Wilhelm Date: Wed, 3 Feb 2016 22:48:18 +0100 Subject: [PATCH] Fix detection of deleted interfaces for ifName/ifDescr port association mode. The previous ifIndex based port mapping schema did detect deleted ports by checking ports in the DB against the ports currently polled via SNMP which breaks when using another port mapping schema. This commit fixes problem by checking known and found ports by their port_id. Signed-off-by: Maximilian Wilhelm --- includes/polling/ports.inc.php | 40 +++++++++++++++++++++++----------- 1 file changed, 27 insertions(+), 13 deletions(-) diff --git a/includes/polling/ports.inc.php b/includes/polling/ports.inc.php index 6a46068614..3bbcf5ff39 100644 --- a/includes/polling/ports.inc.php +++ b/includes/polling/ports.inc.php @@ -224,6 +224,7 @@ foreach ($ports_mapped['maps']['ifIndex'] as $ifIndex => $port_id) { } +$ports_found = array (); // New interface detection foreach ($port_stats as $ifIndex => $port) { // Store ifIndex in port entry and prefetch ifName as we'll need it multiple times @@ -289,6 +290,9 @@ foreach ($port_stats as $ifIndex => $port) { */ $ports[$port_id]['ifIndex'] = $ifIndex; $port_stats[$ifIndex]['port_id'] = $port_id; + + /* Build a list of all ports, identified by their port_id, found within this poller run. */ + $ports_found[] = $port_id; } // Port vanished (mark as deleted) @@ -307,8 +311,28 @@ foreach ($ports as $port) { $port_id = $port['port_id']; $ifIndex = $port['ifIndex']; - echo 'Port ' . $port['ifName'] . ': ' . $port['ifDescr'] . '(' . $ifIndex . ') '; - if ($port_stats[$ifIndex] && $port['disabled'] != '1') { + $port_info_string = 'Port ' . $port['ifName'] . ': ' . $port['ifDescr'] . " ($ifIndex / #$port_id) "; + + /* We don't care for disabled ports, go on */ + if ($port['disabled'] == 1) { + echo "$port_info_string disabled.\n"; + continue; + } + + /** + * If this port did not show up in $port_stats before it has been deleted + * since the last poller run. Mark it deleted in the database and go on. + */ + if (! in_array ($port_id, $ports_found)) { + if ($port['deleted'] != '1') { + dbUpdate(array('deleted' => '1'), 'ports', '`device_id` = ? AND `port_id` = ?', array($device['device_id'], $port_id)); + echo "$port_info_string deleted.\n"; + } + continue; + } + + echo $port_info_string; + if ($port_stats[$ifIndex]) { // Check to make sure Port data is cached. $this_port = &$port_stats[$ifIndex]; @@ -654,19 +678,8 @@ foreach ($ports as $port) { $updated += dbUpdate($port['update_extended'], 'ports_statistics', '`port_id` = ?', array($port_id)); d_echo("$updated updated"); } - // End Update Database } - else if ($port['disabled'] != '1') { - echo 'Port Deleted'; - // Port missing from SNMP cache. - if ($port['deleted'] != '1') { - dbUpdate(array('deleted' => '1'), 'ports', '`device_id` = ? AND `port_id` = ?', array($device['device_id'], $port_id)); - } - } - else { - echo 'Port Disabled.'; - }//end if echo "\n"; @@ -676,3 +689,4 @@ foreach ($ports as $port) { // Clear Variables Here unset($port_stats); +unset($ports_found);