diff --git a/LibreNMS/Modules/Isis.php b/LibreNMS/Modules/Isis.php index 11f03aba23..bad454c84b 100644 --- a/LibreNMS/Modules/Isis.php +++ b/LibreNMS/Modules/Isis.php @@ -60,7 +60,7 @@ class Isis implements Module public function shouldDiscover(OS $os, ModuleStatus $status): bool { - return $status->isEnabled() && ! $os->getDevice()->snmp_disable && $os->getDevice()->status; + return $status->isEnabledAndDeviceUp($os->getDevice()); } /** @@ -81,7 +81,7 @@ class Isis implements Module public function shouldPoll(OS $os, ModuleStatus $status): bool { - return $status->isEnabled() && ! $os->getDevice()->snmp_disable && $os->getDevice()->status; + return $status->isEnabledAndDeviceUp($os->getDevice()); } /** diff --git a/LibreNMS/Modules/LegacyModule.php b/LibreNMS/Modules/LegacyModule.php index f07dd38836..2c3495de01 100644 --- a/LibreNMS/Modules/LegacyModule.php +++ b/LibreNMS/Modules/LegacyModule.php @@ -73,7 +73,7 @@ class LegacyModule implements Module public function discover(OS $os): void { - if (! is_file(base_path("includes/discovery/$this->name.inc.php"))) { + if (! \LibreNMS\Util\Module::legacyDiscoveryExists($this->name)) { echo "Module $this->name does not exist, please remove it from your configuration"; return; @@ -92,21 +92,13 @@ class LegacyModule implements Module public function shouldPoll(OS $os, ModuleStatus $status): bool { - if (! $status->isEnabled()) { - return false; - } - - if (! $os->getDevice()->status) { - return false; - } - // all legacy modules require snmp except ipmi and unix-agent - return ! $os->getDevice()->snmp_disable || in_array($this->name, ['ipmi', 'unix-agent']); + return $status->isEnabledAndDeviceUp($os->getDevice(), check_snmp: ! in_array($this->name, ['ipmi', 'unix-agent'])); } public function poll(OS $os, DataStorageInterface $datastore): void { - if (! is_file(base_path("includes/polling/$this->name.inc.php"))) { + if (! \LibreNMS\Util\Module::legacyPollingExists($this->name)) { echo "Module $this->name does not exist, please remove it from your configuration"; return; diff --git a/LibreNMS/Modules/Mempools.php b/LibreNMS/Modules/Mempools.php index 653aceb118..7676316070 100644 --- a/LibreNMS/Modules/Mempools.php +++ b/LibreNMS/Modules/Mempools.php @@ -53,7 +53,7 @@ class Mempools implements Module public function shouldDiscover(OS $os, ModuleStatus $status): bool { - return $status->isEnabled() && ! $os->getDevice()->snmp_disable && $os->getDevice()->status; + return $status->isEnabledAndDeviceUp($os->getDevice()); } public function discover(OS $os): void @@ -79,7 +79,7 @@ class Mempools implements Module public function shouldPoll(OS $os, ModuleStatus $status): bool { - return $status->isEnabled() && ! $os->getDevice()->snmp_disable && $os->getDevice()->status; + return $status->isEnabledAndDeviceUp($os->getDevice()); } public function poll(OS $os, DataStorageInterface $datastore): void diff --git a/LibreNMS/Modules/Mpls.php b/LibreNMS/Modules/Mpls.php index 337b8b58ad..0c247dc9d6 100644 --- a/LibreNMS/Modules/Mpls.php +++ b/LibreNMS/Modules/Mpls.php @@ -51,7 +51,7 @@ class Mpls implements Module public function shouldDiscover(OS $os, ModuleStatus $status): bool { - return $status->isEnabled() && ! $os->getDevice()->snmp_disable && $os->getDevice()->status && $os instanceof MplsDiscovery; + return $status->isEnabledAndDeviceUp($os->getDevice()) && $os instanceof MplsDiscovery; } /** @@ -101,7 +101,7 @@ class Mpls implements Module public function shouldPoll(OS $os, ModuleStatus $status): bool { - return $status->isEnabled() && ! $os->getDevice()->snmp_disable && $os->getDevice()->status && $os instanceof MplsPolling; + return $status->isEnabledAndDeviceUp($os->getDevice()) && $os instanceof MplsPolling; } /** diff --git a/LibreNMS/Modules/Nac.php b/LibreNMS/Modules/Nac.php index 75a9e14c50..95564f9f6e 100644 --- a/LibreNMS/Modules/Nac.php +++ b/LibreNMS/Modules/Nac.php @@ -62,7 +62,7 @@ class Nac implements Module public function shouldPoll(OS $os, ModuleStatus $status): bool { - return $status->isEnabled() && ! $os->getDevice()->snmp_disable && $os->getDevice()->status && $os instanceof NacPolling; + return $status->isEnabledAndDeviceUp($os->getDevice()) && $os instanceof NacPolling; } /** diff --git a/LibreNMS/Modules/Netstats.php b/LibreNMS/Modules/Netstats.php index 0f4efaba4d..3aecd2fec3 100644 --- a/LibreNMS/Modules/Netstats.php +++ b/LibreNMS/Modules/Netstats.php @@ -191,7 +191,7 @@ class Netstats implements Module public function shouldPoll(OS $os, ModuleStatus $status): bool { - return $status->isEnabled() && ! $os->getDevice()->snmp_disable && $os->getDevice()->status; + return $status->isEnabledAndDeviceUp($os->getDevice()); } /** diff --git a/LibreNMS/Modules/Os.php b/LibreNMS/Modules/Os.php index cb46e4ce59..b87dfdb54a 100644 --- a/LibreNMS/Modules/Os.php +++ b/LibreNMS/Modules/Os.php @@ -47,7 +47,7 @@ class Os implements Module public function shouldDiscover(\LibreNMS\OS $os, ModuleStatus $status): bool { - return $status->isEnabled() && ! $os->getDevice()->snmp_disable && $os->getDevice()->status; + return $status->isEnabledAndDeviceUp($os->getDevice()); } public function discover(\LibreNMS\OS $os): void @@ -70,7 +70,7 @@ class Os implements Module public function shouldPoll(\LibreNMS\OS $os, ModuleStatus $status): bool { - return $status->isEnabled() && ! $os->getDevice()->snmp_disable && $os->getDevice()->status; + return $status->isEnabledAndDeviceUp($os->getDevice()); } public function poll(\LibreNMS\OS $os, DataStorageInterface $datastore): void diff --git a/LibreNMS/Modules/Ospf.php b/LibreNMS/Modules/Ospf.php index 62f6897c20..b7852a2bf5 100644 --- a/LibreNMS/Modules/Ospf.php +++ b/LibreNMS/Modules/Ospf.php @@ -65,7 +65,7 @@ class Ospf implements Module public function shouldPoll(OS $os, ModuleStatus $status): bool { - return $status->isEnabled() && ! $os->getDevice()->snmp_disable && $os->getDevice()->status; + return $status->isEnabledAndDeviceUp($os->getDevice()); } /** diff --git a/LibreNMS/Modules/PrinterSupplies.php b/LibreNMS/Modules/PrinterSupplies.php index 05a4c27e36..556cbbbe1b 100644 --- a/LibreNMS/Modules/PrinterSupplies.php +++ b/LibreNMS/Modules/PrinterSupplies.php @@ -49,7 +49,7 @@ class PrinterSupplies implements Module public function shouldDiscover(OS $os, ModuleStatus $status): bool { - return $status->isEnabled() && ! $os->getDevice()->snmp_disable && $os->getDevice()->status; + return $status->isEnabledAndDeviceUp($os->getDevice()); } /** @@ -72,7 +72,7 @@ class PrinterSupplies implements Module public function shouldPoll(OS $os, ModuleStatus $status): bool { - return $status->isEnabled() && ! $os->getDevice()->snmp_disable && $os->getDevice()->status; + return $status->isEnabledAndDeviceUp($os->getDevice()); } /** diff --git a/LibreNMS/Modules/Slas.php b/LibreNMS/Modules/Slas.php index 50e869e58f..123da19de2 100644 --- a/LibreNMS/Modules/Slas.php +++ b/LibreNMS/Modules/Slas.php @@ -46,7 +46,7 @@ class Slas implements Module public function shouldDiscover(OS $os, ModuleStatus $status): bool { - return $status->isEnabled() && ! $os->getDevice()->snmp_disable && $os->getDevice()->status && $os instanceof SlaDiscovery; + return $status->isEnabledAndDeviceUp($os->getDevice()) && $os instanceof SlaDiscovery; } /** @@ -66,7 +66,7 @@ class Slas implements Module public function shouldPoll(OS $os, ModuleStatus $status): bool { - return $status->isEnabled() && ! $os->getDevice()->snmp_disable && $os->getDevice()->status && $os instanceof SlaPolling; + return $status->isEnabledAndDeviceUp($os->getDevice()) && $os instanceof SlaPolling; } /** diff --git a/LibreNMS/Modules/Stp.php b/LibreNMS/Modules/Stp.php index a46c995d35..2f4b35c36b 100644 --- a/LibreNMS/Modules/Stp.php +++ b/LibreNMS/Modules/Stp.php @@ -48,7 +48,7 @@ class Stp implements Module public function shouldDiscover(OS $os, ModuleStatus $status): bool { - return $status->isEnabled() && ! $os->getDevice()->snmp_disable && $os->getDevice()->status; + return $status->isEnabledAndDeviceUp($os->getDevice()); } public function discover(OS $os): void @@ -70,7 +70,7 @@ class Stp implements Module public function shouldPoll(OS $os, ModuleStatus $status): bool { - return $status->isEnabled() && ! $os->getDevice()->snmp_disable && $os->getDevice()->status; + return $status->isEnabledAndDeviceUp($os->getDevice()); } public function poll(OS $os, DataStorageInterface $datastore): void diff --git a/LibreNMS/Modules/Vminfo.php b/LibreNMS/Modules/Vminfo.php index f6c6fee83f..c86cacce22 100644 --- a/LibreNMS/Modules/Vminfo.php +++ b/LibreNMS/Modules/Vminfo.php @@ -50,11 +50,7 @@ class Vminfo implements \LibreNMS\Interfaces\Module public function shouldDiscover(OS $os, ModuleStatus $status): bool { // libvirt does not use snmp, only ssh tunnels - if (! Config::get('enable_libvirt') && $os->getDevice()->snmp_disable) { - return false; - } - - return $status->isEnabled() && $os->getDevice()->status && $os instanceof VminfoDiscovery; + return $status->isEnabledAndDeviceUp($os->getDevice(), check_snmp: ! Config::get('enable_libvirt')) && $os instanceof VminfoDiscovery; } /** @@ -73,7 +69,7 @@ class Vminfo implements \LibreNMS\Interfaces\Module public function shouldPoll(OS $os, ModuleStatus $status): bool { - return $status->isEnabled() && ! $os->getDevice()->snmp_disable && $os->getDevice()->status && $os instanceof VminfoPolling; + return $status->isEnabledAndDeviceUp($os->getDevice()) && $os instanceof VminfoPolling; } /** diff --git a/LibreNMS/Modules/Xdsl.php b/LibreNMS/Modules/Xdsl.php index a5e9a372b7..1e09361ed1 100644 --- a/LibreNMS/Modules/Xdsl.php +++ b/LibreNMS/Modules/Xdsl.php @@ -62,7 +62,7 @@ class Xdsl implements Module public function shouldDiscover(OS $os, ModuleStatus $status): bool { - return $status->isEnabled() && ! $os->getDevice()->snmp_disable && $os->getDevice()->status; + return $status->isEnabledAndDeviceUp($os->getDevice()); } /** @@ -77,7 +77,7 @@ class Xdsl implements Module public function shouldPoll(OS $os, ModuleStatus $status): bool { - return $status->isEnabled() && ! $os->getDevice()->snmp_disable && $os->getDevice()->status; + return $status->isEnabledAndDeviceUp($os->getDevice()); } /** diff --git a/LibreNMS/Poller.php b/LibreNMS/Poller.php index 8f433a71fd..ae91373da3 100644 --- a/LibreNMS/Poller.php +++ b/LibreNMS/Poller.php @@ -175,7 +175,7 @@ class Poller $datastore = app('Datastore'); foreach (array_keys(Config::get('poller_modules')) as $module) { - $module_status = Module::status($module, $this->device, $this->isModuleManuallyEnabled($module)); + $module_status = Module::pollingStatus($module, $this->device, $this->isModuleManuallyEnabled($module)); $should_poll = false; $start_memory = memory_get_usage(); $module_start = microtime(true); @@ -300,7 +300,7 @@ class Poller Config::set("poller_submodules.$module", $existing_submodules); } - if (! Module::exists($module)) { + if (! Module::exists($module) && ! Module::legacyPollingExists($module)) { unset($this->module_override[$index]); continue; } diff --git a/LibreNMS/Polling/ModuleStatus.php b/LibreNMS/Polling/ModuleStatus.php index fbf6f9afa3..87a0a75d07 100644 --- a/LibreNMS/Polling/ModuleStatus.php +++ b/LibreNMS/Polling/ModuleStatus.php @@ -25,6 +25,8 @@ namespace LibreNMS\Polling; +use App\Models\Device; + class ModuleStatus { public function __construct( @@ -69,6 +71,15 @@ class ModuleStatus return 'globally'; } + public function isEnabledAndDeviceUp(Device $device, bool $check_snmp = true): bool + { + if ($check_snmp && $device->snmp_disable) { + return false; + } + + return $this->isEnabled() && $device->status; + } + public function __toString(): string { return sprintf('Module %s: Global %s | OS %s | Device %s | Manual %s', diff --git a/LibreNMS/Util/Module.php b/LibreNMS/Util/Module.php index 4280694e22..0aabf6497d 100644 --- a/LibreNMS/Util/Module.php +++ b/LibreNMS/Util/Module.php @@ -32,26 +32,35 @@ use LibreNMS\Polling\ModuleStatus; class Module { - public static function fromName(string $name): \LibreNMS\Interfaces\Module + public static function exists(string $module_name): bool { - $module_class = StringHelpers::toClass($name, '\\LibreNMS\\Modules\\'); - - return class_exists($module_class) ? new $module_class : new LegacyModule($name); + return class_exists(StringHelpers::toClass($module_name, '\\LibreNMS\\Modules\\')); } - public static function status(string $name, Device $device, ?bool $manual = null): ModuleStatus + public static function fromName(string $module_name): \LibreNMS\Interfaces\Module + { + $module_class = StringHelpers::toClass($module_name, '\\LibreNMS\\Modules\\'); + + return class_exists($module_class) ? new $module_class : new LegacyModule($module_name); + } + + public static function legacyDiscoveryExists(string $module_name): bool + { + return is_file(base_path("includes/discovery/$module_name.inc.php")); + } + + public static function legacyPollingExists(string $module_name): bool + { + return is_file(base_path("includes/polling/$module_name.inc.php")); + } + + public static function pollingStatus(string $module_name, Device $device, ?bool $manual = null): ModuleStatus { return new ModuleStatus( - Config::get('poller_modules.' . $name), - Config::get("os.{$device->os}.poller_modules.$name"), - $device->getAttrib('poll_' . $name), + Config::get("poller_modules.$module_name"), + Config::get("os.{$device->os}.poller_modules.$module_name"), + $device->getAttrib("poll_$module_name"), $manual, ); } - - public static function exists(string $module): bool - { - return class_exists(StringHelpers::toClass($module, '\\LibreNMS\\Modules\\')) - || is_file(base_path("includes/polling/$module.inc.php")); - } }