mirror of
https://github.com/stylersnico/librenms.git
synced 2026-07-12 16:06:25 +02:00
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
This commit is contained in:
@@ -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");
|
||||
|
||||
?>
|
||||
|
||||
@@ -1,5 +1,32 @@
|
||||
<?php
|
||||
|
||||
function discover_link($local_interface_id, $protocol, $remote_interface_id, $remote_hostname, $remote_port, $remote_platform, $remote_version) {
|
||||
|
||||
global $config; global $debug; global $link_exists;
|
||||
if (mysql_result(@mysql_query("SELECT COUNT(*) FROM `links` WHERE `remote_hostname` = '$remote_hostname' AND `local_interface_id` = '$local_interface_id'
|
||||
AND `protocol` = '$protocol' AND `remote_port` = '$remote_port'"),0) == "0")
|
||||
{
|
||||
$sql = "INSERT INTO `links` (`local_interface_id`,`protocol`,`remote_interface_id`,`remote_hostname`,`remote_port`,`remote_platform`,`remote_version`)
|
||||
VALUES ('$local_interface_id','$protocol','$remote_interface_id','$remote_hostname','$remote_port','$remote_platform','$remote_version')";
|
||||
mysql_query($sql);
|
||||
echo("+"); if($debug) {echo("$sql");}
|
||||
} else {
|
||||
$data = mysql_fetch_array(mysql_query("SELECT * FROM `links` WHERE `remote_hostname` = '$remote_hostname' AND `local_interface_id` = '$local_interface_id'
|
||||
AND `protocol` = '$protocol' AND `remote_port` = '$remote_port'"));
|
||||
if($data['remote_interface_id'] == $remote_interface_id && $data['remote_platform'] == $remote_platform && $remote_version == $remote_version)
|
||||
{
|
||||
echo(".");
|
||||
} else {
|
||||
$sql = "UPDATE `links` SET `remote_interface_id` = $remote_interface_id, `remote_platform` = '$remote_platform', `remote_version` = '$remote_version' WHERE `id` = '".$data['id']."'";
|
||||
mysql_query($sql);
|
||||
echo("U"); if($debug) {echo("$sql");}
|
||||
}
|
||||
}
|
||||
$link_exists[$local_interface_id][$remote_hostname][$remote_port] = 1;
|
||||
|
||||
}
|
||||
|
||||
|
||||
function discover_fan($device, $oid, $index, $type, $descr, $precision = 1, $low_limit = NULL, $high_limit = NULL, $current = NULL) {
|
||||
|
||||
global $config; global $debug;
|
||||
|
||||
@@ -136,7 +136,7 @@ function snmpwalk_cache_twopart_oid($oid, $device, $array, $mib = 0) {
|
||||
$device_id = $device['device_id'];
|
||||
foreach(explode("\n", $data) as $entry) {
|
||||
list($oid,$value) = explode("=", $entry);
|
||||
$oid = trim($oid); $value = trim($value);
|
||||
$oid = trim($oid); $value = trim($value); $value = str_replace("\"", "", $value);
|
||||
list($oid, $first, $second) = explode(".", $oid);
|
||||
if (!strstr($value, "at this OID") && isset($oid) && isset($first) && isset($second)) {
|
||||
$array[$device_id][$first][$second][$oid] = $value;
|
||||
@@ -155,7 +155,7 @@ function snmpwalk_cache_threepart_oid($oid, $device, $array, $mib = 0) {
|
||||
$device_id = $device['device_id'];
|
||||
foreach(explode("\n", $data) as $entry) {
|
||||
list($oid,$value) = explode("=", $entry);
|
||||
$oid = trim($oid); $value = trim($value);
|
||||
$oid = trim($oid); $value = trim($value); $value = str_replace("\"", "", $value);
|
||||
list($oid, $first, $second, $third) = explode(".", $oid);
|
||||
if($debug) {echo("$entry || $oid || $first || $second || $third\n");}
|
||||
if (!strstr($value, "at this OID") && isset($oid) && isset($first) && isset($second) && isset($third)) {
|
||||
|
||||
@@ -411,8 +411,8 @@ function delHost($id)
|
||||
$int_if = $int_data['ifDescr'];
|
||||
$int_id = $int_data['interface_id'];
|
||||
mysql_query("DELETE from `adjacencies` WHERE `interface_id` = '$int_id'");
|
||||
mysql_query("DELETE from `links` WHERE `src_if` = '$int_id'");
|
||||
mysql_query("DELETE from `links` WHERE `dst_if` = '$int_id'");
|
||||
mysql_query("DELETE from `links` WHERE `local_interface_id` = '$int_id'");
|
||||
mysql_query("DELETE from `links` WHERE `remote_interface_id` = '$int_id'");
|
||||
mysql_query("DELETE from `ipaddr` WHERE `interface_id` = '$int_id'");
|
||||
mysql_query("DELETE from `ip6adjacencies` WHERE `interface_id` = '$int_id'");
|
||||
mysql_query("DELETE from `ip6addr` WHERE `interface_id` = '$int_id'");
|
||||
@@ -449,8 +449,8 @@ function retireHost($id)
|
||||
$int_if = $int_data['ifDescr'];
|
||||
$int_id = $int_data['interface_id'];
|
||||
mysql_query("DELETE from `adjacencies` WHERE `interface_id` = '$int_id'");
|
||||
mysql_query("DELETE from `links` WHERE `src_if` = '$int_id'");
|
||||
mysql_query("DELETE from `links` WHERE `dst_if` = '$int_id'");
|
||||
mysql_query("DELETE from `links` WHERE `local_interface_id` = '$int_id'");
|
||||
mysql_query("DELETE from `links` WHERE `remote_interface_id` = '$int_id'");
|
||||
mysql_query("DELETE from `ipaddr` WHERE `interface_id` = '$int_id'");
|
||||
mysql_query("DELETE from `ip6adjacencies` WHERE `interface_id` = '$int_id'");
|
||||
mysql_query("DELETE from `ip6addr` WHERE `interface_id` = '$int_id'");
|
||||
|
||||
Reference in New Issue
Block a user