Files
librenms-librenms/includes/discovery/discovery-protocols.inc.php

275 lines
14 KiB
PHP
Raw Normal View History

<?php
global $link_exists;
$community = $device['community'];
2015-07-13 20:10:26 +02:00
if ($device['os'] == 'ironware' && $config['autodiscovery']['xdp'] === true) {
echo ' Brocade FDP: ';
$fdp_array = snmpwalk_cache_twopart_oid($device, 'snFdpCacheEntry', array(), 'FOUNDRY-SN-SWITCH-GROUP-MIB');
d_echo($fdp_array);
2015-07-13 20:10:26 +02:00
if ($fdp_array) {
unset($fdp_links);
foreach (array_keys($fdp_array) as $key) {
$interface = dbFetchRow('SELECT * FROM `ports` WHERE `device_id` = ? AND `ifIndex` = ?', array($device['device_id'], $key));
$fdp_if_array = $fdp_array[$key];
d_echo($fdp_if_array);
2015-07-13 20:10:26 +02:00
foreach (array_keys($fdp_if_array) as $entry_key) {
$fdp = $fdp_if_array[$entry_key];
$remote_device_id = dbFetchCell('SELECT `device_id` FROM `devices` WHERE `sysName` = ? OR `hostname` = ?', array($fdp['snFdpCacheDeviceId'], $fdp['snFdpCacheDeviceId']));
if (!$remote_device_id) {
2016-10-18 03:06:03 +00:00
$skip_discovery = false;
if ($skip_discovery === false) {
$skip_discovery = can_skip_discovery($config['autodiscovery']['xdp_exclude']['sysname_regexp'], $fdp['snFdpCacheDeviceId'], $fdp['snFdpCacheDeviceId']);
2016-10-17 00:38:47 -04:00
}
2016-10-18 03:06:03 +00:00
if ($skip_discovery === false) {
2016-10-20 20:24:40 -04:00
$skip_discovery = can_skip_discovery($config['autodiscovery']['xdp_exclude']['sysdesc_regexp'], $fdp['snFdpCacheVersion'], $fdp['snFdpCacheDeviceId']);
2016-10-17 00:38:47 -04:00
}
2016-10-18 03:06:03 +00:00
if ($skip_discovery === false) {
2016-10-17 00:38:47 -04:00
$remote_device_id = discover_new_device($fdp['snFdpCacheDeviceId'], $device, 'FDP', $interface);
}
2015-07-13 20:10:26 +02:00
}
if ($remote_device_id) {
$if = $fdp['snFdpCacheDevicePort'];
$remote_port_id = dbFetchCell('SELECT port_id FROM `ports` WHERE (`ifDescr` = ? OR `ifName` = ?) AND `device_id` = ?', array($if, $if, $remote_device_id));
} else {
2015-07-13 20:10:26 +02:00
$remote_port_id = '0';
}
discover_link($interface['port_id'], $fdp['snFdpCacheVendorId'], $remote_port_id, $fdp['snFdpCacheDeviceId'], $fdp['snFdpCacheDevicePort'], $fdp['snFdpCachePlatform'], $fdp['snFdpCacheVersion'], $device['device_id'], $remote_device_id);
}
}//end foreach
}//end if
}//end if
echo ' CISCO-CDP-MIB: ';
unset($cdp_array);
if ($config['autodiscovery']['xdp'] === true) {
$cdp_array = snmpwalk_cache_twopart_oid($device, 'cdpCache', array(), 'CISCO-CDP-MIB');
d_echo($cdp_array);
if ($cdp_array) {
unset($cdp_links);
foreach (array_keys($cdp_array) as $key) {
$interface = dbFetchRow('SELECT * FROM `ports` WHERE device_id = ? AND `ifIndex` = ?', array($device['device_id'], $key));
$cdp_if_array = $cdp_array[$key];
d_echo($cdp_if_array);
foreach (array_keys($cdp_if_array) as $entry_key) {
$cdp = $cdp_if_array[$entry_key];
if (is_valid_hostname($cdp['cdpCacheDeviceId']) || ($config['discovery_by_ip'] == true)) {
$ip_arr = explode(" ", $cdp['cdpCacheAddress']);
$a = hexdec($ip_arr[0]);
$b = hexdec($ip_arr[1]);
$c = hexdec($ip_arr[2]);
$d = hexdec($ip_arr[3]);
$cdp_ip = "$a.$b.$c.$d";
$remote_device_id = dbFetchCell('SELECT `device_id` FROM `devices` WHERE `sysName` = ? OR `hostname` = ? OR `hostname` = ?', array($cdp['cdpCacheDeviceId'], $cdp['cdpCacheDeviceId'], $cdp_ip));
if (!$remote_device_id) {
2016-10-18 03:06:03 +00:00
$skip_discovery = false;
if ($skip_discovery === false) {
$skip_discovery = can_skip_discovery($config['autodiscovery']['cdp_exclude']['platform_regexp'], $cdp['cdpCachePlatform'], $cdp['cdpCacheDeviceId']);
2016-10-17 00:38:47 -04:00
}
2016-10-18 03:06:03 +00:00
if ($skip_discovery === false) {
$skip_discovery = can_skip_discovery($config['autodiscovery']['xdp_exclude']['sysname_regexp'], $cdp['cdpCacheDeviceId'], $cdp['cdpCacheDeviceId']);
2016-10-17 00:38:47 -04:00
}
2016-10-18 03:06:03 +00:00
if ($skip_discovery === false) {
$skip_discovery = can_skip_discovery($config['autodiscovery']['xdp_exclude']['sysdesc_regexp'], $cdp['cdpCacheVersion'], $cdp['cdpCacheDeviceId']);
2016-10-17 00:38:47 -04:00
}
2016-10-18 03:06:03 +00:00
if ($skip_discovery === false) {
2016-10-17 00:38:47 -04:00
if ($config['discovery_by_ip'] !== true) {
$remote_device_id = discover_new_device($cdp['cdpCacheDeviceId'], $device, 'CDP', $interface);
} else {
$remote_device_id = discover_new_device($cdp_ip, $device, 'CDP', $interface);
}
}
}
if ($remote_device_id) {
$if = $cdp['cdpCacheDevicePort'];
$remote_port_id = dbFetchCell('SELECT port_id FROM `ports` WHERE (`ifDescr` = ? OR `ifName` = ?) AND `device_id` = ?', array($if, $if, $remote_device_id));
} else {
$remote_port_id = '0';
}
if ($interface['port_id'] && $cdp['cdpCacheDeviceId'] && $cdp['cdpCacheDevicePort']) {
discover_link($interface['port_id'], 'cdp', $remote_port_id, $cdp['cdpCacheDeviceId'], $cdp['cdpCacheDevicePort'], $cdp['cdpCachePlatform'], $cdp['cdpCacheVersion'], $device['device_id'], $remote_device_id);
}
} else {
echo 'X';
}//end if
}//end foreach
2015-07-13 20:10:26 +02:00
}//end foreach
}//end if
2015-07-13 20:10:26 +02:00
}//end if
unset(
$lldp_array,
$cdp_array
);
2015-07-13 20:10:26 +02:00
if ($device['os'] == 'pbn' && $config['autodiscovery']['xdp'] === true) {
echo ' NMS-LLDP-MIB: ';
$lldp_array = snmpwalk_cache_oid($device, 'lldpRemoteSystemsData', array(), 'NMS-LLDP-MIB', 'pbn');
d_echo($lldp_array);
if ($lldp_array) {
unset($lldp_links);
foreach (array_keys($lldp_array) as $key) {
$lldp = $lldp_array[$key];
d_echo($lldp);
$interface = dbFetchRow('SELECT * FROM `ports` WHERE `device_id` = ? AND `ifIndex` = ?', array($device['device_id'], $lldp['lldpRemLocalPortNum']));
$remote_device_id = dbFetchCell('SELECT `device_id` FROM `devices` WHERE `sysName` = ? OR `hostname` = ?', array($lldp['lldpRemSysName'], $lldp['lldpRemSysName']));
if (!$remote_device_id && is_valid_hostname($lldp['lldpRemSysName'])) {
2016-10-18 03:06:03 +00:00
$skip_discovery = false;
if ($skip_discovery === false) {
$skip_discovery = can_skip_discovery($config['autodiscovery']['xdp_exclude']['sysname_regexp'], $lldp['lldpRemSysName'], $lldp['lldpRemSysName']);
2016-10-17 00:38:47 -04:00
}
2016-10-18 03:06:03 +00:00
if ($skip_discovery === false) {
$skip_discovery = can_skip_discovery($config['autodiscovery']['xdp_exclude']['sysdesc_regexp'], $lldp['lldpRemSysDesc'], $lldp['lldpRemSysName']);
2016-10-17 00:38:47 -04:00
}
2016-10-18 03:06:03 +00:00
if ($skip_discovery === false) {
2016-10-17 00:38:47 -04:00
$remote_device_id = discover_new_device($lldp['lldpRemSysName'], $device, 'LLDP', $interface);
}
}
if ($remote_device_id) {
$if = $lldp['lldpRemPortDesc'];
$id = $lldp['lldpRemPortId'];
$remote_port_id = dbFetchCell('SELECT `port_id` FROM `ports` WHERE (`ifDescr` = ? OR `ifName` = ? OR `ifDescr` = ? OR `ifName` = ?) AND `device_id` = ?', array($if, $if, $id, $id, $remote_device_id));
} else {
$remote_port_id = '0';
}
if (is_numeric($interface['port_id']) && isset($lldp['lldpRemSysName']) && isset($lldp['lldpRemPortId'])) {
discover_link($interface['port_id'], 'lldp', $remote_port_id, $lldp['lldpRemSysName'], $lldp['lldpRemPortId'], null, $lldp['lldpRemSysDesc'], $device['device_id'], $remote_device_id);
}
}//end foreach
}//end if
} elseif ($config['autodiscovery']['xdp'] === true) {
echo ' LLDP-MIB: ';
$lldp_array = snmpwalk_cache_threepart_oid($device, 'lldpRemoteSystemsData', array(), 'LLDP-MIB');
d_echo($lldp_array);
$dot1d_array = snmpwalk_cache_oid($device, 'dot1dBasePortIfIndex', array(), 'BRIDGE-MIB');
d_echo($dot1d_array);
if ($lldp_array) {
$lldp_links = '';
foreach (array_keys($lldp_array) as $key) {
$lldp_if_array = $lldp_array[$key];
d_echo($lldp_if_array);
foreach (array_keys($lldp_if_array) as $entry_key) {
if (is_numeric($dot1d_array[$entry_key]['dot1dBasePortIfIndex'])) {
$ifIndex = $dot1d_array[$entry_key]['dot1dBasePortIfIndex'];
} else {
$ifIndex = $entry_key;
2015-07-13 20:10:26 +02:00
}
$interface = dbFetchRow('SELECT * FROM `ports` WHERE `device_id` = ? AND `ifIndex` = ?', array($device['device_id'], $ifIndex));
$lldp_instance = $lldp_if_array[$entry_key];
d_echo($lldp_instance);
foreach (array_keys($lldp_instance) as $entry_instance) {
$lldp = $lldp_instance[$entry_instance];
$remote_device_id = dbFetchCell('SELECT `device_id` FROM `devices` WHERE `sysName` = ? OR `hostname` = ?', array($lldp['lldpRemSysName'], $lldp['lldpRemSysName']));
if (!$remote_device_id && is_valid_hostname($lldp['lldpRemSysName'])) {
2016-10-18 03:06:03 +00:00
$skip_discovery = false;
if ($skip_discovery === false) {
$skip_discovery = can_skip_discovery($config['autodiscovery']['xdp_exclude']['sysname_regexp'], $lldp['lldpRemSysName'], $lldp['lldpRemSysName']);
2016-10-17 00:38:47 -04:00
}
2016-10-18 03:06:03 +00:00
if ($skip_discovery === false) {
$skip_discovery = can_skip_discovery($config['autodiscovery']['xdp_exclude']['sysdesc_regexp'], $lldp['lldpRemSysDesc'], $lldp['lldpRemSysName']);
2016-10-17 00:38:47 -04:00
}
2016-10-20 20:36:18 -04:00
if ($skip_discovery === false) {
2016-10-17 00:38:47 -04:00
$remote_device_id = discover_new_device($lldp['lldpRemSysName'], $device, 'LLDP', $interface);
}
}
2015-12-13 20:15:43 +01:00
// normalize MAC address if present
2016-08-15 14:53:44 -06:00
$remote_port_mac_address = '';
if ($lldp['lldpRemPortIdSubtype'] == 'macAddress') {
$remote_port_mac_address = str_replace(array(' ', ':', '-'), '', strtolower($lldp['lldpRemPortId']));
2015-12-13 20:15:43 +01:00
}
// get remote device hostname from db by MAC address and replace lldpRemSysName if absent
if (!$remote_device_id && $remote_port_mac_address) {
$remote_device_id = dbFetchCell('SELECT `device_id` FROM `ports` WHERE ifPhysAddress = ? AND `deleted` = ?', array($remote_port_mac_address, '0'));
2015-12-13 20:15:43 +01:00
if ($remote_device_id) {
2015-12-13 21:00:39 +01:00
$remote_device_hostname = dbFetchRow('SELECT `hostname` FROM `devices` WHERE `device_id` = ?', array($remote_device_id));
}
2015-12-13 20:15:43 +01:00
if ($remote_device_hostname['hostname']) {
$lldp['lldpRemSysName'] = $remote_device_hostname['hostname'];
}
}
if ($remote_device_id) {
$if = $lldp['lldpRemPortDesc'];
$id = $lldp['lldpRemPortId'];
$remote_port_id = dbFetchCell('SELECT `port_id` FROM `ports` WHERE (`ifDescr` = ? OR `ifName` = ? OR `ifDescr` = ? OR `ifName` = ? OR `ifPhysAddress` = ?) AND `device_id` = ?', array($if, $if, $id, $id, $remote_port_mac_address, $remote_device_id));
} else {
$remote_port_id = '0';
}
if (is_numeric($interface['port_id']) && isset($lldp['lldpRemSysName']) && isset($lldp['lldpRemPortId'])) {
discover_link($interface['port_id'], 'lldp', $remote_port_id, $lldp['lldpRemSysName'], $lldp['lldpRemPortId'], null, $lldp['lldpRemSysDesc'], $device['device_id'], $remote_device_id);
}
}//end foreach
2015-07-13 20:10:26 +02:00
}//end foreach
}//end foreach
}//end if
unset(
$dot1d_array
);
}//end elseif
2015-07-13 20:10:26 +02:00
unset(
$lldp_array
);
2015-12-13 20:15:43 +01:00
echo ' OSPF Discovery: ';
2015-07-13 20:10:26 +02:00
if ($config['autodiscovery']['ospf'] === true) {
2015-04-11 15:01:14 +01:00
echo "enabled\n";
2015-07-13 20:10:26 +02:00
foreach (dbFetchRows('SELECT DISTINCT(`ospfNbrIpAddr`),`device_id` FROM `ospf_nbrs` WHERE `device_id`=?', array($device['device_id'])) as $nbr) {
2015-04-11 15:01:14 +01:00
$ip = $nbr['ospfNbrIpAddr'];
if (match_network($config['autodiscovery']['nets-exclude'], $ip)) {
2015-07-13 20:10:26 +02:00
echo 'x';
2015-04-11 15:01:14 +01:00
continue;
}
2015-07-13 20:10:26 +02:00
2015-04-11 15:01:14 +01:00
if (!match_network($config['nets'], $ip)) {
2015-07-13 20:10:26 +02:00
echo 'i';
2015-04-11 15:01:14 +01:00
continue;
}
2015-07-13 20:10:26 +02:00
$name = gethostbyaddr($ip);
$remote_device_id = discover_new_device($name, $device, 'OSPF');
2015-04-11 15:01:14 +01:00
}
} else {
2015-07-13 20:10:26 +02:00
echo "disabled\n";
2015-04-11 15:01:14 +01:00
}
d_echo($link_exists);
$sql = "SELECT * FROM `links` AS L, `ports` AS I WHERE L.local_port_id = I.port_id AND I.device_id = '".$device['device_id']."'";
2015-07-13 20:10:26 +02:00
foreach (dbFetchRows($sql) as $test) {
$local_port_id = $test['local_port_id'];
$remote_hostname = $test['remote_hostname'];
2015-07-13 20:10:26 +02:00
$remote_port = $test['remote_port'];
d_echo("$local_port_id -> $remote_hostname -> $remote_port \n");
2015-07-13 20:10:26 +02:00
if (!$link_exists[$local_port_id][$remote_hostname][$remote_port]) {
echo '-';
$rows = dbDelete('links', '`id` = ?', array($test['id']));
d_echo("$rows deleted ");
2015-07-13 20:10:26 +02:00
}
}
unset(
$link_exists,
$sql
);
2015-07-13 20:10:26 +02:00
echo "\n";