Module status cleanups (#15461)

* Module status helper

* Correct reference

* Rename function to make it more explicit

* Same for exists checks, normalize argument names

* Use new functions
This commit is contained in:
Tony Murray 2023-10-16 05:03:32 -07:00 committed by GitHub
parent abf4fa0004
commit fb59cfec1c
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
16 changed files with 60 additions and 52 deletions

View File

@ -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());
}
/**

View File

@ -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;

View File

@ -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

View File

@ -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;
}
/**

View File

@ -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;
}
/**

View File

@ -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());
}
/**

View File

@ -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

View File

@ -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());
}
/**

View File

@ -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());
}
/**

View File

@ -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;
}
/**

View File

@ -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

View File

@ -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;
}
/**

View File

@ -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());
}
/**

View File

@ -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;
}

View File

@ -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',

View File

@ -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"));
}
}