mirror of
https://github.com/librenms/librenms.git
synced 2024-10-07 16:52:45 +00:00
refactor: rewrite is_valid_port() (#7360)
* refactor: speed up is_valid_port() Convert is_valid_port to Config Several Config optimizations Update documentation * fix getCombined() when os key is not set, but global is. Add more tests
This commit is contained in:
committed by
Neil Lathwood
parent
777eb1f823
commit
6ea6218f47
@@ -32,3 +32,5 @@ discovery_modules:
|
||||
discovery:
|
||||
- sysDescr:
|
||||
- Cisco Catalyst Operating System Software
|
||||
bad_if:
|
||||
- vlan
|
||||
|
@@ -10,3 +10,6 @@ discovery:
|
||||
- sysDescr_regex:
|
||||
- '/^DES-/'
|
||||
- '/^DGS-/'
|
||||
good_if:
|
||||
- po
|
||||
- vlan
|
||||
|
@@ -11,6 +11,7 @@
|
||||
*
|
||||
*/
|
||||
|
||||
use LibreNMS\Config;
|
||||
use LibreNMS\Exceptions\HostExistsException;
|
||||
use LibreNMS\Exceptions\HostIpExistsException;
|
||||
use LibreNMS\Exceptions\HostUnreachableException;
|
||||
@@ -979,95 +980,75 @@ function include_dir($dir, $regex = "")
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Check if port is valid to poll.
|
||||
* Settings: empty_ifdescr, good_if, bad_if, bad_if_regexp, bad_ifname_regexp, bad_ifalias_regexp, bad_iftype
|
||||
*
|
||||
* @param array $port
|
||||
* @param array $device
|
||||
* @return bool
|
||||
*/
|
||||
function is_port_valid($port, $device)
|
||||
{
|
||||
|
||||
global $config;
|
||||
|
||||
if (empty($port['ifDescr']) && empty($port['ifAlias']) && empty($port['ifName'])) {
|
||||
// check empty values first
|
||||
if (empty($port['ifDescr'])) {
|
||||
// If these are all empty, we are just going to show blank names in the ui
|
||||
$valid = 0;
|
||||
} else {
|
||||
$valid = 1;
|
||||
$if = strtolower($port['ifDescr']);
|
||||
$ifname = strtolower($port['ifName']);
|
||||
$ifalias = strtolower($port['ifAlias']);
|
||||
$fringe = $config['bad_if'];
|
||||
if (is_array($config['os'][$device['os']]['bad_if'])) {
|
||||
$fringe = array_merge($config['bad_if'], $config['os'][$device['os']]['bad_if']);
|
||||
if (empty($port['ifAlias']) && empty($port['ifName'])) {
|
||||
return false;
|
||||
}
|
||||
$config['good_if'] = $config['good_if'] ?: array();
|
||||
if (is_array($config['os'][$device['os']]['good_if'])) {
|
||||
$good_if = array_merge($config['good_if'], $config['os'][$device['os']]['good_if']);
|
||||
}
|
||||
foreach ($fringe as $bi) {
|
||||
if (stristr($if, $bi)) {
|
||||
if (!str_contains($good_if, $if)) {
|
||||
$valid = 0;
|
||||
d_echo("ignored : $bi : $if");
|
||||
}
|
||||
}
|
||||
}
|
||||
if (is_array($config['bad_if_regexp'])) {
|
||||
$fringe = $config['bad_if_regexp'];
|
||||
if (is_array($config['os'][$device['os']]['bad_if_regexp'])) {
|
||||
$fringe = array_merge($config['bad_if_regexp'], $config['os'][$device['os']]['bad_if_regexp']);
|
||||
}
|
||||
foreach ($fringe as $bi) {
|
||||
if (preg_match($bi ."i", $if)) {
|
||||
$valid = 0;
|
||||
d_echo("ignored : $bi : " . $if);
|
||||
}
|
||||
}
|
||||
}
|
||||
if (is_array($config['bad_ifname_regexp'])) {
|
||||
$fringe = $config['bad_ifname_regexp'];
|
||||
if (is_array($config['os'][$device['os']]['bad_ifname_regexp'])) {
|
||||
$fringe = array_merge($config['bad_ifname_regexp'], $config['os'][$device['os']]['bad_ifname_regexp']);
|
||||
}
|
||||
foreach ($fringe as $bi) {
|
||||
if (preg_match($bi ."i", $ifname)) {
|
||||
$valid = 0;
|
||||
d_echo("ignored : $bi : ".$ifname);
|
||||
}
|
||||
}
|
||||
}
|
||||
if (is_array($config['bad_ifalias_regexp'])) {
|
||||
$fringe = $config['bad_ifalias_regexp'];
|
||||
if (is_array($config['os'][$device['os']]['bad_ifalias_regexp'])) {
|
||||
$fringe = array_merge($config['bad_ifalias_regexp'], $config['os'][$device['os']]['bad_ifalias_regexp']);
|
||||
}
|
||||
foreach ($fringe as $bi) {
|
||||
if (preg_match($bi ."i", $ifalias)) {
|
||||
$valid = 0;
|
||||
d_echo("ignored : $bi : ".$ifalias);
|
||||
}
|
||||
}
|
||||
}
|
||||
if (is_array($config['bad_iftype'])) {
|
||||
$fringe = $config['bad_iftype'];
|
||||
if (is_array($config['os'][$device['os']]['bad_iftype'])) {
|
||||
$fringe = array_merge($config['bad_iftype'], $config['os'][$device['os']]['bad_iftype']);
|
||||
}
|
||||
foreach ($fringe as $bi) {
|
||||
if (stristr($port['ifType'], $bi)) {
|
||||
$valid = 0;
|
||||
d_echo("ignored ifType : ".$port['ifType']." (matched: ".$bi." )");
|
||||
}
|
||||
}
|
||||
}
|
||||
if (empty($port['ifDescr']) && !$config['os'][$device['os']]['empty_ifdescr']) {
|
||||
$valid = 0;
|
||||
}
|
||||
if ($device['os'] == "catos" && strstr($if, "vlan")) {
|
||||
$valid = 0;
|
||||
}
|
||||
if ($device['os'] == "dlink") {
|
||||
$valid = 1;
|
||||
|
||||
// ifDescr should not be empty unless it is explicitly allowed
|
||||
if (!Config::getOsSetting($device['os'], 'empty_ifdescr', false)) {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
return $valid;
|
||||
$ifDescr = $port['ifDescr'];
|
||||
$ifName = $port['ifName'];
|
||||
$ifAlias = $port['ifAlias'];
|
||||
$ifType = $port['ifType'];
|
||||
|
||||
if (str_contains($ifDescr, Config::getOsSetting($device['os'], 'good_if'), true)) {
|
||||
return true;
|
||||
}
|
||||
|
||||
foreach (Config::getCombined($device['os'], 'bad_if') as $bi) {
|
||||
if (str_contains($ifDescr, $bi, true)) {
|
||||
d_echo("ignored by ifDescr: $ifDescr (matched: $bi)\n");
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
foreach (Config::getCombined($device['os'], 'bad_if_regexp') as $bir) {
|
||||
if (preg_match($bir ."i", $ifDescr)) {
|
||||
d_echo("ignored by ifDescr: $ifDescr (matched: $bir)\n");
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
foreach (Config::getCombined($device['os'], 'bad_ifname_regexp') as $bnr) {
|
||||
if (preg_match($bnr ."i", $ifName)) {
|
||||
d_echo("ignored by ifName: $ifName (matched: $bnr)\n");
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
foreach (Config::getCombined($device['os'], 'bad_ifalias_regexp') as $bar) {
|
||||
if (preg_match($bar ."i", $ifAlias)) {
|
||||
d_echo("ignored by ifName: $ifAlias (matched: $bar)\n");
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
foreach (Config::getCombined($device['os'], 'bad_iftype') as $bt) {
|
||||
if (str_contains($ifType, $bt)) {
|
||||
d_echo("ignored by ifType: $ifType (matched: $bt )\n");
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
function scan_new_plugins()
|
||||
|
Reference in New Issue
Block a user