From 46d2f578e7974686ac1106711bf8da04fc45cc6d Mon Sep 17 00:00:00 2001 From: laf Date: Tue, 18 Oct 2016 03:06:03 +0000 Subject: [PATCH] added central function and updated --- doc/Extensions/Auto-Discovery.md | 8 -- includes/defaults.inc.php | 3 - .../discovery/discovery-protocols.inc.php | 79 ++++++------------- includes/functions.php | 18 +++++ 4 files changed, 44 insertions(+), 64 deletions(-) diff --git a/doc/Extensions/Auto-Discovery.md b/doc/Extensions/Auto-Discovery.md index 7fb73ac59d..44ff640bd8 100644 --- a/doc/Extensions/Auto-Discovery.md +++ b/doc/Extensions/Auto-Discovery.md @@ -86,14 +86,6 @@ Devices may be excluded from cdp discovery by platform. $config['autodiscovery']['cdp_exclude']['platform_regexp'][] = '/WS-C3750G/'; ``` -These devices are excluded by default: - -```php -$config['autodiscovery']['xdp_exclude']['sysdesc_regexp'][] = '/-K9W8-/'; // Cisco Lightweight Access Point -$config['autodiscovery']['cdp_exclude']['platform_regexp'][] = '/^Cisco IP Phone/'; //Cisco IP Phone -$config['autodiscovery']['cdp_exclude']['platform_regexp'][] = '/^CIVS-IPC-/'; //Cisco IP Camera -``` - #### OSPF Enabled by default. diff --git a/includes/defaults.inc.php b/includes/defaults.inc.php index be7e87928e..943fcb390a 100644 --- a/includes/defaults.inc.php +++ b/includes/defaults.inc.php @@ -199,9 +199,6 @@ $config['icmp_check'] = true; // Autodiscovery Settings $config['autodiscovery']['xdp'] = true; -$config['autodiscovery']['xdp_exclude']['sysdesc_regexp'][] = '/-K9W8-/'; // Cisco Lightweight Access Point -$config['autodiscovery']['cdp_exclude']['platform_regexp'][] = '/^Cisco IP Phone/'; //Cisco IP Phone -$config['autodiscovery']['cdp_exclude']['platform_regexp'][] = '/^CIVS-IPC-/'; //Cisco IP Camera // Autodiscover hosts via discovery protocols $config['autodiscovery']['ospf'] = true; // Autodiscover hosts via OSPF diff --git a/includes/discovery/discovery-protocols.inc.php b/includes/discovery/discovery-protocols.inc.php index 63a4587f4d..674728a764 100644 --- a/includes/discovery/discovery-protocols.inc.php +++ b/includes/discovery/discovery-protocols.inc.php @@ -19,21 +19,15 @@ if ($device['os'] == 'ironware' && $config['autodiscovery']['xdp'] === true) { $remote_device_id = dbFetchCell('SELECT `device_id` FROM `devices` WHERE `sysName` = ? OR `hostname` = ?', array($fdp['snFdpCacheDeviceId'], $fdp['snFdpCacheDeviceId'])); if (!$remote_device_id) { - unset($skip_discovery); - foreach ($config['autodiscovery']['xdp_exclude']['sysname_regexp'] as $filter) { - if (preg_match($filter ."i", $fdp['snFdpCacheDeviceId'])) { - $skip_discovery = 1; - d_echo("{$fdp['snFdpCacheDeviceId']} - regexp '{$filter}' matches '{$fdp['snFdpCacheDeviceId']}' - skipping device discovery \n"); - } + $skip_discovery = false; + if ($skip_discovery === false) { + $skip_discovery = can_skip_discovery($config['autodiscovery']['xdp_exclude']['sysname_regexp'], $fdp['snFdpCacheDeviceId'], $fdp['snFdpCacheDeviceId']); } - foreach ($config['autodiscovery']['xdp_exclude']['sysdesc_regexp'] as $filter) { - if (preg_match($filter ."i", $fdp['cdpCacheVersion'])) { - $skip_discovery = 1; - d_echo("{$fdp['snFdpCacheDeviceId']} - regexp '{$filter}' matches '{$fdp['cdpCacheVersion']}' - skipping device discovery \n"); - } + if ($skip_discovery === false) { + $skip_discovery = can_skip_discovery($config['autodiscovery']['xdp_exclude']['sysdesc_regexp'], $fdp['cdpCacheVersion'], $fdp['snFdpCacheDeviceId']); } - if (!$skip_discovery) { + if ($skip_discovery === false) { $remote_device_id = discover_new_device($fdp['snFdpCacheDeviceId'], $device, 'FDP', $interface); } } @@ -68,27 +62,18 @@ if ($config['autodiscovery']['xdp'] === true) { $remote_device_id = dbFetchCell('SELECT `device_id` FROM `devices` WHERE `sysName` = ? OR `hostname` = ?', array($cdp['cdpCacheDeviceId'], $cdp['cdpCacheDeviceId'])); if (!$remote_device_id) { - unset($skip_discovery); - foreach ($config['autodiscovery']['cdp_exclude']['platform_regexp'] as $filter) { - if (preg_match($filter ."i", $cdp['cdpCachePlatform'])) { - $skip_discovery = 1; - d_echo("{$cdp['cdpCacheDeviceId']} - regexp '{$filter}' matches '{$cdp['cdpCachePlatform']}' - skipping device discovery \n"); - } + $skip_discovery = false; + if ($skip_discovery === false) { + $skip_discovery = can_skip_discovery($config['autodiscovery']['cdp_exclude']['platform_regexp'], $cdp['cdpCachePlatform'], $cdp['cdpCacheDeviceId']); } - foreach ($config['autodiscovery']['xdp_exclude']['sysname_regexp'] as $filter) { - if (preg_match($filter ."i", $cdp['cdpCacheDeviceId'])) { - $skip_discovery = 1; - d_echo("{$cdp['cdpCacheDeviceId']} - regexp '{$filter}' matches '{$cdp['cdpCacheDeviceId']}' - skipping device discovery \n"); - } + if ($skip_discovery === false) { + $skip_discovery = can_skip_discovery($config['autodiscovery']['xdp_exclude']['sysname_regexp'], $cdp['cdpCacheDeviceId'], $cdp['cdpCacheDeviceId']); } - foreach ($config['autodiscovery']['xdp_exclude']['sysdesc_regexp'] as $filter) { - if (preg_match($filter ."i", $cdp['cdpCacheVersion'])) { - $skip_discovery = 1; - d_echo("{$cdp['cdpCacheDeviceId']} - regexp '{$filter}' matches '{$cdp['cdpCacheVersion']}' - skipping device discovery \n"); - } + if ($skip_discovery === false) { + $skip_discovery = can_skip_discovery($config['autodiscovery']['xdp_exclude']['sysdesc_regexp'], $cdp['cdpCacheVersion'], $cdp['cdpCacheDeviceId']); } - if (!$skip_discovery) { + if ($skip_discovery === false) { if ($config['discovery_by_ip'] !== true) { $remote_device_id = discover_new_device($cdp['cdpCacheDeviceId'], $device, 'CDP', $interface); } else { @@ -140,21 +125,15 @@ if ($device['os'] == 'pbn' && $config['autodiscovery']['xdp'] === true) { $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'])) { - unset($skip_discovery); - foreach ($config['autodiscovery']['xdp_exclude']['sysname_regexp'] as $filter) { - if (preg_match($filter ."i", $lldp['lldpRemSysName'])) { - $skip_discovery = 1; - d_echo("{$lldp['lldpRemSysName']} - regexp '{$filter}' matches '{$lldp['lldpRemSysName']}' - skipping device discovery \n"); - } + $skip_discovery = false; + if ($skip_discovery === false) { + $skip_discovery = can_skip_discovery($config['autodiscovery']['xdp_exclude']['sysname_regexp'], $lldp['lldpRemSysName'], $lldp['lldpRemSysName']); } - foreach ($config['autodiscovery']['xdp_exclude']['sysdesc_regexp'] as $filter) { - if (preg_match($filter ."i", $lldp['lldpRemSysDesc'])) { - $skip_discovery = 1; - d_echo("{$lldp['lldpRemSysName']} - regexp '{$filter}' matches '{$lldp['lldpRemSysDesc']}' - skipping device discovery \n"); - } + if ($skip_discovery === false) { + $skip_discovery = can_skip_discovery($config['autodiscovery']['xdp_exclude']['sysdesc_regexp'], $lldp['lldpRemSysDesc'], $lldp['lldpRemSysName']); } - if (!$skip_discovery) { + if ($skip_discovery === false) { $remote_device_id = discover_new_device($lldp['lldpRemSysName'], $device, 'LLDP', $interface); } } @@ -198,21 +177,15 @@ if ($device['os'] == 'pbn' && $config['autodiscovery']['xdp'] === true) { $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'])) { - unset($skip_discovery); - foreach ($config['autodiscovery']['xdp_exclude']['sysname_regexp'] as $filter) { - if (preg_match($filter ."i", $lldp['lldpRemSysName'])) { - $skip_discovery = 1; - d_echo("{$lldp['lldpRemSysName']} - regexp '{$filter}' matches '{$lldp['lldpRemSysName']}' - skipping device discovery \n"); - } + $skip_discovery = false; + if ($skip_discovery === false) { + $skip_discovery = can_skip_discovery($config['autodiscovery']['xdp_exclude']['sysname_regexp'], $lldp['lldpRemSysName'], $lldp['lldpRemSysName']); } - foreach ($config['autodiscovery']['xdp_exclude']['sysdesc_regexp'] as $filter) { - if (preg_match($filter ."i", $lldp['lldpRemSysDesc'])) { - $skip_discovery = 1; - d_echo("{$lldp['lldpRemSysName']} - regexp '{$filter}' matches '{$lldp['lldpRemSysDesc']}' - skipping device discovery \n"); - } + if ($skip_discovery === false) { + $skip_discovery = can_skip_discovery($config['autodiscovery']['xdp_exclude']['sysdesc_regexp'], $lldp['lldpRemSysDesc'], $lldp['lldpRemSysName']); } - if (!$skip_discovery) { + if ($skip_discovery === true) { $remote_device_id = discover_new_device($lldp['lldpRemSysName'], $device, 'LLDP', $interface); } } diff --git a/includes/functions.php b/includes/functions.php index 73858ebc49..c2e85714dd 100644 --- a/includes/functions.php +++ b/includes/functions.php @@ -1736,3 +1736,21 @@ function get_toner_levels($device, $raw_value, $capacity) return round($raw_value / $capacity * 100); } + +/** + * check if we should skip this device from discovery + * @param $needles + * @param $haystack + * @param $name + * @return bool + */ +function can_skip_discovery($needles, $haystack, $name) +{ + foreach ((array)$needles as $needle) { + if (preg_match($needle ."i", $haystack)) { + d_echo("{$name} - regexp '{$needle}' matches '{$haystack}' - skipping device discovery \n"); + return true; + } + } + return false; +}