From 38f0170b0b490ae2522f7ae837729a2e426db597 Mon Sep 17 00:00:00 2001 From: Adam Amstrong Date: Mon, 15 Feb 2010 23:56:30 +0000 Subject: [PATCH] cdp/lldp rewrite and addition of brocade FDP discovery. now records neighbours even if it doesn't monitor that neighbour git-svn-id: http://www.observium.org/svn/observer/trunk@862 61d68cd4-352d-0410-923a-c4978735b2b8 --- cleanup.php | 10 +- database-update.sql | 4 + html/includes/print-interface.inc.php | 2 +- html/map.php | 24 ++--- html/pages/device.php | 2 +- html/pages/purgeports.php | 4 +- includes/discovery/cdp-lldp.inc.php | 142 ++++++++++++-------------- includes/discovery/functions.inc.php | 27 +++++ includes/functions-poller.inc.php | 4 +- includes/functions.php | 8 +- map.php | 12 +-- test-discovery.php | 2 + 12 files changed, 129 insertions(+), 112 deletions(-) diff --git a/cleanup.php b/cleanup.php index 664b054fb7..729876d8e0 100755 --- a/cleanup.php +++ b/cleanup.php @@ -58,8 +58,8 @@ while($row = mysql_fetch_array($data)) { if($response != $index) { mysql_query("DELETE from interfaces where interface_id = '" . $row['interface_id'] . "'"); mysql_query("DELETE from `adjacencies` WHERE `interface_id` = '" . $row['interface_id'] . "'"); - mysql_query("DELETE from `links` WHERE `src_if` = '" . $row['interface_id'] . "'"); - mysql_query("DELETE from `links` WHERE `dst_if` = '" . $row['interface_id'] . "'"); + mysql_query("DELETE from `links` WHERE `local_interface_id` = '" . $row['interface_id'] . "'"); + mysql_query("DELETE from `links` WHERE `remote_interface_id` = '" . $row['interface_id'] . "'"); mysql_query("DELETE from `ipaddr` WHERE `interface_id` = '" . $row['interface_id'] . "'"); echo("Removed interface " . $row['ifDescr'] . " from " . $row['hostname'] . "
"); } @@ -78,11 +78,11 @@ while ($interface = mysql_fetch_array($interface_query)) { echo(mysql_result(mysql_query("SELECT COUNT(*) FROM `interfaces`"), 0) . " interfaces at end\n"); echo(mysql_result(mysql_query("SELECT COUNT(id) FROM `links`"), 0) . " links at start\n"); -$link_query = mysql_query("SELECT id,src_if,dst_if FROM `links`"); +$link_query = mysql_query("SELECT id,local_interface_id,remote_interface_id FROM `links`"); while ($link = mysql_fetch_array($link_query)) { $id = $link['id']; - $src = $link['src_if']; - $dst = $link['dst_if']; + $src = $link['local_interface_id']; + $dst = $link['remote_interface_id']; if(mysql_result(mysql_query("SELECT COUNT(interface_id) FROM `interfaces` WHERE `interface_id` = '$src'"), 0) == '0' || mysql_result(mysql_query("SELECT COUNT(*) FROM `interfaces` WHERE `interface_id` = '$dst'"), 0) == '0') { mysql_query("delete from links where `id` = '$id'"); echo("Deleting link $id \n"); diff --git a/database-update.sql b/database-update.sql index 4de4504f34..23a06be5cd 100644 --- a/database-update.sql +++ b/database-update.sql @@ -95,3 +95,7 @@ ALTER TABLE `processors` CHANGE `processor_description` `processor_descr` varch ALTER TABLE `fanspeed` CHANGE `fan_mibtype` `fan_type` varchar(64) NOT NULL ; ALTER TABLE `voltage` ADD `volt_index` VARCHAR( 8 ) NOT NULL AFTER `volt_oid`,ADD `volt_type` VARCHAR( 32 ) NOT NULL AFTER `volt_index` ; ALTER TABLE `processors` ADD `processor_precision` INT( 11 ) NOT NULL DEFAULT '1'; +ALTER TABLE `links` CHANGE `cdp` `vendor` VARCHAR( 11 ) NULL DEFAULT NULL; +ALTER TABLE `links` ADD `remote_hostname` VARCHAR( 128 ) NOT NULL ,ADD `remote_port` VARCHAR( 128 ) NOT NULL ,ADD `remote_platform` VARCHAR( 128 ) NOT NULL ,ADD `remote_version` VARCHAR( 256 ) NOT NULL ; +ALTER TABLE `links` CHANGE `src_if` `local_interface_id` INT( 11 ) NULL DEFAULT NULL ,CHANGE `dst_if` `remote_interface_id` INT( 11 ) NULL DEFAULT NULL ; +ALTER TABLE `links` CHANGE `vendor` `protocol` VARCHAR( 11 ) CHARACTER SET latin1 COLLATE latin1_swedish_ci NULL DEFAULT NULL; diff --git a/html/includes/print-interface.inc.php b/html/includes/print-interface.inc.php index a5ff2f7e24..121eefc4ba 100644 --- a/html/includes/print-interface.inc.php +++ b/html/includes/print-interface.inc.php @@ -103,7 +103,7 @@ echo(""); echo(""); if ( strpos($interface['label'], "oopback") === false && !$graph_type) { - $link_query = mysql_query("select * from links AS L, interfaces AS I, devices AS D WHERE L.src_if = '$if_id' AND L.dst_if = I.interface_id AND I.device_id = D.device_id"); + $link_query = mysql_query("select * from links AS L, interfaces AS I, devices AS D WHERE L.local_interface_id = '$if_id' AND L.remote_interface_id = I.interface_id AND I.device_id = D.device_id"); while($link = mysql_fetch_array($link_query)) { # echo("Directly Connected " . generateiflink($link, makeshortif($link['label'])) . " on " . generatedevicelink($link, shorthost($link['hostname'])) . "
"); # $br = "
"; diff --git a/html/map.php b/html/map.php index cadd9e64c2..8266a78ff7 100755 --- a/html/map.php +++ b/html/map.php @@ -25,7 +25,7 @@ if (isset($_GET['format']) && preg_match("/^[a-z]*$/", $_GET['format'])) { if($device) { - $sql = "SELECT * from interfaces AS I, links AS L WHERE I.device_id = ".$device['device_id']." AND L.src_if = I.interface_id"; + $sql = "SELECT * from interfaces AS I, links AS L WHERE I.device_id = ".$device['device_id']." AND L.local_interface_id = I.interface_id"; $links = mysql_query($sql); if (mysql_num_rows($links)) @@ -35,20 +35,20 @@ if (isset($_GET['format']) && preg_match("/^[a-z]*$/", $_GET['format'])) while($link = mysql_fetch_array($links)) { - $src_if = $link['src_if']; - $dst_if = $link['dst_if']; + $local_interface_id = $link['local_interface_id']; + $remote_interface_id = $link['remote_interface_id']; $i = 0; $done = 0; while (isset($linkdone) && $i < count($linkdone)) { - $thislink = "$dst_if $src_if"; + $thislink = "$remote_interface_id $local_interface_id"; if ($linkdone[$i] == $thislink) { $done = 1; } $i++; } if(!$done) { - $linkdone[] = "$src_if $dst_if"; + $linkdone[] = "$local_interface_id $remote_interface_id"; if($link['ifSpeed'] >= "10000000000") { @@ -64,13 +64,13 @@ if (isset($_GET['format']) && preg_match("/^[a-z]*$/", $_GET['format'])) } $src = $device['hostname']; - $dst = mysql_result(mysql_query("SELECT `hostname` FROM `devices` AS D, `interfaces` AS I WHERE I.interface_id = '$dst_if' AND D.device_id = I.device_id"),0); - $dst_host = mysql_result(mysql_query("SELECT D.device_id FROM `devices` AS D, `interfaces` AS I WHERE I.interface_id = '$dst_if' AND D.device_id = I.device_id"),0); + $dst = mysql_result(mysql_query("SELECT `hostname` FROM `devices` AS D, `interfaces` AS I WHERE I.interface_id = '$remote_interface_id' AND D.device_id = I.device_id"),0); + $dst_host = mysql_result(mysql_query("SELECT D.device_id FROM `devices` AS D, `interfaces` AS I WHERE I.interface_id = '$remote_interface_id' AND D.device_id = I.device_id"),0); - $sif = ifNameDescr(mysql_fetch_array(mysql_query("SELECT * FROM interfaces WHERE `interface_id`=" . $link['src_if'])),$device); - $dif = ifNameDescr(mysql_fetch_array(mysql_query("SELECT * FROM interfaces WHERE `interface_id`=" . $link['dst_if']))); + $sif = ifNameDescr(mysql_fetch_array(mysql_query("SELECT * FROM interfaces WHERE `interface_id`=" . $link['local_interface_id'])),$device); + $dif = ifNameDescr(mysql_fetch_array(mysql_query("SELECT * FROM interfaces WHERE `interface_id`=" . $link['remote_interface_id']))); - $map .= "\"" . $sif['interface_id'] . "\" [label=\"" . $sif['label'] . "\", fontsize=12, fillcolor=lightblue URL=\"/device/".$device['device_id']."/interface/$src_if/\"]\n"; + $map .= "\"" . $sif['interface_id'] . "\" [label=\"" . $sif['label'] . "\", fontsize=12, fillcolor=lightblue URL=\"/device/".$device['device_id']."/interface/$local_interface_id/\"]\n"; if (!$ifdone[$src][$sif['interface_id']]) { $map .= "\"$src\" -> \"" . $sif['interface_id'] . "\" [weight=500000, arrowsize=0, len=0];\n"; @@ -80,9 +80,9 @@ if (isset($_GET['format']) && preg_match("/^[a-z]*$/", $_GET['format'])) $map .= "\"$dst\" [URL=\"/device/$dst_host/map/\" fontsize=20 shape=box3d]\n"; if($dst_host == $device['device_id']) { - $map .= "\"" . $dif['interface_id'] . "\" [label=\"" . $dif['label'] . "\", fontsize=12, fillcolor=lightblue, URL=\"/device/$dst_host/interface/$dst_if/\"]\n"; + $map .= "\"" . $dif['interface_id'] . "\" [label=\"" . $dif['label'] . "\", fontsize=12, fillcolor=lightblue, URL=\"/device/$dst_host/interface/$remote_interface_id/\"]\n"; } else { - $map .= "\"" . $dif['interface_id'] . "\" [label=\"" . $dif['label'] . " \", fontsize=12, fillcolor=lightgray, URL=\"/device/$dst_host/interface/$dst_if/\"]\n"; + $map .= "\"" . $dif['interface_id'] . "\" [label=\"" . $dif['label'] . " \", fontsize=12, fillcolor=lightgray, URL=\"/device/$dst_host/interface/$remote_interface_id/\"]\n"; } if (!$ifdone[$dst][$dif['interface_id']]) diff --git a/html/pages/device.php b/html/pages/device.php index 0e21060400..5c1c4c9be6 100644 --- a/html/pages/device.php +++ b/html/pages/device.php @@ -113,7 +113,7 @@ if($config['enable_bgp'] && $device['bgpLocalAs']) { #} -if($_SESSION['userlevel'] >= "5" && mysql_result(mysql_query("SELECT count(*) FROM links AS L, interfaces AS I WHERE I.device_id = '".$device['device_id']."' AND I.interface_id = L.src_if"),0)) { +if($_SESSION['userlevel'] >= "5" && mysql_result(mysql_query("SELECT count(*) FROM links AS L, interfaces AS I WHERE I.device_id = '".$device['device_id']."' AND I.interface_id = L.local_interface_id"),0)) { echo('
  • Map diff --git a/html/pages/purgeports.php b/html/pages/purgeports.php index 3258918f99..d6b15196d5 100644 --- a/html/pages/purgeports.php +++ b/html/pages/purgeports.php @@ -24,8 +24,8 @@ mysql_query("DELETE FROM `pseudowires` WHERE `interface_id` = '$interface_id'"); mysql_query("DELETE FROM `mac_accounting` WHERE `interface_id` = '$interface_id'"); - mysql_query("DELETE FROM `links` WHERE `src_if` = '$interface_id'"); - mysql_query("DELETE FROM `links` WHERE `dst_if` = '$interface_id'"); + mysql_query("DELETE FROM `links` WHERE `local_interface_id` = '$interface_id'"); + mysql_query("DELETE FROM `links` WHERE `remote_interface_id` = '$interface_id'"); mysql_query("DELETE FROM `interfaces_perms` WHERE `interface_id` = '$interface_id'"); mysql_query("DELETE FROM `interfaces` WHERE `interface_id` = '$interface_id'"); } diff --git a/includes/discovery/cdp-lldp.inc.php b/includes/discovery/cdp-lldp.inc.php index 15e06ea76e..76859f9230 100755 --- a/includes/discovery/cdp-lldp.inc.php +++ b/includes/discovery/cdp-lldp.inc.php @@ -2,6 +2,35 @@ $community = $device['community']; +echo("Brocade FDP: "); + +$fdp_array = snmpwalk_cache_twopart_oid("snFdpCacheEntry", $device, array(), "FOUNDRY-SN-SWITCH-GROUP-MIB"); +$fdp_array = $fdp_array[$device['device_id']]; +if($fdp_array) { + unset($fdp_links); + foreach( array_keys($fdp_array) as $key) + { + $interface = mysql_fetch_array(mysql_query("SELECT * FROM `interfaces` WHERE device_id = '".$device['device_id']."' AND `ifIndex` = '".$key."'")); + $fdp_if_array = $fdp_array[$key]; + foreach( array_keys($fdp_if_array) as $entry_key) + { + $fdp = $fdp_if_array[$entry_key]; + $remote_device_id = @mysql_result(mysql_query("SELECT `device_id` FROM `devices` WHERE `sysName` = '".$fdp['snFdpCacheDeviceId']."' OR `hostname`='".$fdp['snFdpCacheDeviceId']."'"), 0); + if($remote_device_id) { + $if = $fdp['snFdpCacheDevicePort']; + $remote_interface_id = @mysql_result(mysql_query("SELECT interface_id FROM `interfaces` WHERE (`ifDescr` = '$if' OR `ifName`='$if') AND `device_id` = '".$remote_device_id."'"),0); + } else { $remote_interface_id = "0"; } + + discover_link($interface['interface_id'], $fdp['snFdpCacheVendorId'], $remote_interface_id, $fdp['snFdpCacheDeviceId'], $fdp['snFdpCacheDevicePort'], $fdp['snFdpCachePlatform'], $fdp['snFdpCacheVersion']); + + + } + } +} + +echo("\n"); + + echo("CISCO-CDP-MIB: "); unset($cdp_array); @@ -13,13 +42,14 @@ if($cdp_array) { $interface = mysql_fetch_array(mysql_query("SELECT * FROM `interfaces` WHERE device_id = '".$device['device_id']."' AND `ifIndex` = '".$key."'")); $cdp_if_array = $cdp_array[$key]; foreach( array_keys($cdp_if_array) as $entry_key) { - $cdp_entry_array = $cdp_if_array[$entry_key]; - if($device['hostname'] && $interface['ifIndex'] && $cdp_entry_array['cdpCacheDeviceId'] && $cdp_entry_array['cdpCacheDevicePort']){ - if(strpos($cdp_entry_array['cdpCacheDeviceId'], ")")) { list(,$cdp_entry_array['cdpCacheDeviceId']) = explode("(", $cdp_entry_array['cdpCacheDeviceId']); echo($cdp_entry_array['cdpCacheDeviceId']); - list($cdp_entry_array['cdpCacheDeviceId'],) = explode(")", $cdp_entry_array['cdpCacheDeviceId']); echo($cdp_entry_array['cdpCacheDeviceId']); } - - $cdp_links .= $device['hostname'] . "," . $interface['ifIndex'] . "," . $cdp_entry_array['cdpCacheDeviceId'] . "," . $cdp_entry_array['cdpCacheDevicePort'] . "\n"; - echo "."; + $cdp = $cdp_if_array[$entry_key]; + $remote_device_id = @mysql_result(mysql_query("SELECT `device_id` FROM `devices` WHERE `sysName` = '".$cdp['cdpCacheDeviceId']."' OR `hostname`='".$cdp['cdpCacheDeviceId']."'"), 0); + if($remote_device_id) { + $if = $cdp['cdpCacheDevicePort']; + $remote_interface_id = @mysql_result(mysql_query("SELECT interface_id FROM `interfaces` WHERE (`ifDescr` = '$if' OR `ifName`='$if') AND `device_id` = '".$remote_device_id."'"),0); + } else { $remote_interface_id = "0"; } + if($interface['interface_id'] && $cdp['cdpCacheDeviceId'] && $cdp['cdpCacheDevicePort']) { + discover_link($interface['interface_id'], 'cdp', $remote_interface_id, $cdp['cdpCacheDeviceId'], $cdp['cdpCacheDevicePort'], $cdp['cdpCachePlatform'], $cdp['cdpCacheVersion']); } } } @@ -37,90 +67,44 @@ if($lldp_array) { $lldp_if_array = $lldp_array[$key]; foreach( array_keys($lldp_if_array) as $entry_key) { $interface = mysql_fetch_array(mysql_query("SELECT * FROM `interfaces` WHERE device_id = '".$device['device_id']."' AND `ifIndex` = '".$entry_key."'")); - $lldp_entry_array = $lldp_if_array[$entry_key]; - foreach ( array_keys($lldp_entry_array) as $entry_instance) { - $lldp_entry_instance_array = $lldp_entry_array[$entry_instance]; - if($device['hostname'] && $interface['ifIndex'] && $lldp_entry_instance_array['lldpRemSysName'] && $lldp_entry_instance_array['lldpRemPortId']){ - if(strpos($lldp_entry_instance_array['lldpRemSysName'], ")")) { list(,$lldp_entry_instance_array['lldpRemSysName']) = explode("(", $lldp_entry_instance_array['lldpRemSysName']); echo($lldp_entry_instance_array['lldpRemSysName']); - list($lldp_entry_instance_array['lldpRemSysName'],) = explode(")", $lldp_entry_instance_array['lldpRemSysName']); echo($lldp_entry_instance_array['lldpRemSysName']); } + $lldp_instance = $lldp_if_array[$entry_key]; + foreach ( array_keys($lldp_instance) as $entry_instance) { + $lldp = $lldp_instance[$entry_instance]; + $remote_device_id = @mysql_result(mysql_query("SELECT `device_id` FROM `devices` WHERE `sysName` = '".$lldp['lldpRemSysName']."' OR `hostname`='".$lldp['lldpRemSysName']."'"), 0); + if($remote_device_id) { + $if = $lldp['lldpRemPortDesc']; + $remote_interface_id = @mysql_result(mysql_query("SELECT interface_id FROM `interfaces` WHERE (`ifDescr` = '$if' OR `ifName`='$if') AND `device_id` = '".$remote_device_id."'"),0); + } else { $remote_interface_id = "0"; } - $lldp_links .= $device['hostname'] . "," . $interface['ifIndex'] . "," . $lldp_entry_instance_array['lldpRemSysName'] . "," . str_replace('"','',$lldp_entry_instance_array['lldpRemPortId']) . "\n"; - echo "."; + if($interface['interface_id'] && $lldp['lldpRemSysDesc'] && $lldp['lldpRemPortDesc']) { + discover_link($interface['interface_id'], 'lldp', $remote_interface_id, $lldp['lldpRemSysName'], $lldp['lldpRemPortDesc'], NULL, $lldp['lldpRemSysDesc']); } } } } } -if($debug) {echo("$lldp_links");} -$discovered_links = (isset($cdp_links) ? $cdp_links : '') . (isset($lldp_links) ? $lldp_links : ''); -if($discovered_links != "\n") { - foreach ( explode("\n" ,$discovered_links) as $link ) { - if ($link == "") { break; } - list($src_host,$src_if, $dst_host, $dst_if) = explode(",", $link); - $dst_host = strtolower($dst_host); - $dst_if = strtolower($dst_if); - $src_host = strtolower($src_host); - $src_if = strtolower($src_if); - $ip = gethostbyname($dst_host); - if ( match_network($config['nets'], $ip) ) { - if ( mysql_result(mysql_query("SELECT COUNT(*) FROM `devices` WHERE `sysName` = '$dst_host' OR `hostname`='$dst_host'"), 0) == '0' ) { - if($config['lldp_autocreate']) { - echo("++ Creating: $dst_host \n"); - createHost ($dst_host, $community, "v2c"); - } - } else { - echo("."); - } - } else { - echo("!($dst_host)"); +if($debug) { print_r($link_exists); } + +$sql = "SELECT * FROM `links` AS L, `interfaces` AS I WHERE L.local_interface_id = I.interface_id AND I.device_id = '".$device['device_id']."'"; +if ($query = mysql_query($sql)) +{ + while ($test = mysql_fetch_array($query)) + { + $local_interface_id = $test['local_interface_id']; + $remote_hostname = $test['remote_hostname']; + $remote_port = $test['remote_port']; + if($debug) { echo("$local_interface_id -> $remote_hostname -> $remote_port \n"); } + if(!$link_exists[$local_interface_id][$remote_hostname][$remote_port]) { + echo("-"); + mysql_query("DELETE FROM `links` WHERE id = '" . $test['id'] . "'"); + if($debug) {echo(mysql_affected_rows()." deleted ");} } - - $dst_if_id = @mysql_result(mysql_query("SELECT I.interface_id FROM `interfaces` AS I, `devices` AS D WHERE (`ifDescr` = '$dst_if' OR `ifName`='$dst_if') AND (sysName = '$dst_host' OR hostname='$dst_host') AND D.device_id = I.device_id"), 0); - - if ( mysql_result(mysql_query("SELECT COUNT(*) FROM `devices` WHERE `sysName` = '$dst_host' OR `hostname`='$dst_host'"), 0) == '1' && - mysql_result(mysql_query("SELECT COUNT(*) FROM `devices` WHERE `hostname` = '$src_host'"), 0) == '1' && - $dst_if_id && - mysql_result(mysql_query("SELECT COUNT(*) FROM `interfaces` AS I, `devices` AS D WHERE `ifIndex` = '$src_if' AND hostname = '$src_host' AND D.device_id = I.device_id"), 0) == '1') - { - $src_if_id = mysql_result(mysql_query("SELECT I.interface_id FROM `interfaces` AS I, `devices` AS D WHERE `ifIndex` = '$src_if' AND hostname = '$src_host' AND D.device_id = I.device_id"), 0); - $link_exists[] = $src_if_id . "," . $dst_if_id; - if ( mysql_result(mysql_query("SELECT COUNT(*) FROM `links` WHERE `dst_if` = '$dst_if_id' AND `src_if` = '$src_if_id'"),0) == '0') - { - $sql = "INSERT INTO `links` (`src_if`, `dst_if`) VALUES ('$src_if_id', '$dst_if_id')"; - mysql_query($sql); - echo("\n++($src_host $src_if -> $dst_host $dst_if)"); - } else { - echo(".."); - } - } else { - - } } } +unset($link_exists); echo("\n"); -$sql = "SELECT * FROM `links` AS L, `interfaces` AS I, `devices` AS D WHERE L.src_if = I.interface_id AND I.device_id = D.device_id AND D.device_id = '".$device['device_id']."'"; -$query = mysql_query($sql); - -while ($test_link = mysql_fetch_array($query)) { - unset($exists); - $i = 0; - while ($i < count($link_exists) && !isset($exists)) { - $this_link = $test_link['src_if'] . ",". $test_link['dst_if']; - if ($link_exists[$i] == $this_link) { $exists = 1; } - $i++; - } - if(!isset($exists)) { - echo("-"); - mysql_query("DELETE FROM `links` WHERE `src_if` = '".$test_link['src_if']."' AND `dst_if` = '".$test_link['dst_if']."'"); - if($debug) { echo($link_exists[$i] . " REMOVED \n"); } - } else { - if($debug) { echo($link_exists[$i] . " VALID \n"); } - } -} - -echo("\n"); ?> diff --git a/includes/discovery/functions.inc.php b/includes/discovery/functions.inc.php index 7123d09916..d860c4326c 100644 --- a/includes/discovery/functions.inc.php +++ b/includes/discovery/functions.inc.php @@ -1,5 +1,32 @@