|
| 1 | +<?php |
| 2 | + |
| 3 | +/** |
| 4 | + * ModuleList.php |
| 5 | + * |
| 6 | + * -Description- |
| 7 | + * |
| 8 | + * This program is free software: you can redistribute it and/or modify |
| 9 | + * it under the terms of the GNU General Public License as published by |
| 10 | + * the Free Software Foundation, either version 3 of the License, or |
| 11 | + * (at your option) any later version. |
| 12 | + * |
| 13 | + * This program is distributed in the hope that it will be useful, |
| 14 | + * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 15 | + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.See the |
| 16 | + * GNU General Public License for more details. |
| 17 | + * |
| 18 | + * You should have received a copy of the GNU General Public License |
| 19 | + * along with this program. If not, see <https://www.gnu.org/licenses/>. |
| 20 | + * |
| 21 | + * @link https://www.librenms.org |
| 22 | + * |
| 23 | + * @copyright 2025 Tony Murray |
| 24 | + * @author Tony Murray <[email protected]> |
| 25 | + */ |
| 26 | + |
| 27 | +namespace LibreNMS\Util; |
| 28 | + |
| 29 | +use App\Facades\LibrenmsConfig; |
| 30 | +use App\Models\Device; |
| 31 | +use Illuminate\Support\Facades\Log; |
| 32 | +use LibreNMS\Enum\ProcessType; |
| 33 | +use LibreNMS\Polling\ModuleStatus; |
| 34 | + |
| 35 | +class ModuleList |
| 36 | +{ |
| 37 | + /** |
| 38 | + * @param array<string, bool|array<string>> $overrides |
| 39 | + */ |
| 40 | + public function __construct( |
| 41 | + public readonly array $overrides = [], |
| 42 | + ) { |
| 43 | + } |
| 44 | + |
| 45 | + /** |
| 46 | + * @param array<string> $overrides |
| 47 | + */ |
| 48 | + public static function fromUserOverrides(array $overrides): self |
| 49 | + { |
| 50 | + $modules = []; |
| 51 | + $flattened = array_merge(...array_map(fn ($item) => explode(',', $item), $overrides)); |
| 52 | + |
| 53 | + foreach ($flattened as $module) { |
| 54 | + $enabled = true; |
| 55 | + |
| 56 | + if (str_contains($module, '/')) { |
| 57 | + [$module, $submodule] = explode('/', $module, 2); |
| 58 | + $enabled = $modules[$module] ?? []; // load existing submodules |
| 59 | + $enabled[] = $submodule; |
| 60 | + } |
| 61 | + |
| 62 | + if (Module::exists($module)) { |
| 63 | + $modules[$module] = $enabled; |
| 64 | + } |
| 65 | + } |
| 66 | + |
| 67 | + return new self($modules); |
| 68 | + } |
| 69 | + |
| 70 | + public function hasOverride(): bool |
| 71 | + { |
| 72 | + return ! empty($this->overrides); |
| 73 | + } |
| 74 | + |
| 75 | + public function moduleHasOverride(string $module_name): bool |
| 76 | + { |
| 77 | + return isset($this->overrides[$module_name]); |
| 78 | + } |
| 79 | + |
| 80 | + public function printOverrides(ProcessType $type): void |
| 81 | + { |
| 82 | + if ($this->hasOverride()) { |
| 83 | + $modules = array_map(fn ($module, $status) => $module . (is_array($status) ? '(' . implode(',', $status) . ')' : ''), array_keys($this->overrides), array_values($this->overrides)); |
| 84 | + |
| 85 | + Log::debug(sprintf('Override %s modules: %s', $type->name, implode(', ', $modules))); |
| 86 | + } |
| 87 | + } |
| 88 | + |
| 89 | + /** |
| 90 | + * @return array<string, ModuleStatus> |
| 91 | + */ |
| 92 | + public function modulesWithStatus(ProcessType $type, Device $device): array |
| 93 | + { |
| 94 | + $default_modules = match ($type) { |
| 95 | + ProcessType::poller => LibrenmsConfig::get('poller_modules', []), |
| 96 | + ProcessType::discovery => LibrenmsConfig::get('discovery_modules', []), |
| 97 | + }; |
| 98 | + |
| 99 | + $modules_with_overrides = empty($this->overrides) |
| 100 | + ? array_keys($default_modules) |
| 101 | + : array_keys(array_intersect_key($default_modules, $this->overrides)); // ensure order |
| 102 | + |
| 103 | + $module_status = []; |
| 104 | + foreach ($modules_with_overrides as $module_name) { |
| 105 | + $module_status[$module_name] = $this->moduleStatus($type, $module_name, $device); |
| 106 | + } |
| 107 | + |
| 108 | + // core is always enabled |
| 109 | + return ['core' => new ModuleStatus(true)] + $module_status; |
| 110 | + } |
| 111 | + |
| 112 | + private function moduleStatus(ProcessType $type, string $module_name, Device $device): ModuleStatus |
| 113 | + { |
| 114 | + $override = $this->overrides[$module_name] ?? null; |
| 115 | + |
| 116 | + return match ($type) { |
| 117 | + ProcessType::discovery => new ModuleStatus( |
| 118 | + LibrenmsConfig::get("discovery_modules.$module_name"), |
| 119 | + LibrenmsConfig::get("os.{$device->os}.discovery_modules.$module_name"), |
| 120 | + $device->getAttrib("discover_$module_name"), |
| 121 | + $override == null ? null : true, |
| 122 | + is_array($override) ? $override : null, |
| 123 | + ), |
| 124 | + ProcessType::poller => new ModuleStatus( |
| 125 | + LibrenmsConfig::get("poller_modules.$module_name"), |
| 126 | + LibrenmsConfig::get("os.{$device->os}.poller_modules.$module_name"), |
| 127 | + $device->getAttrib("poll_$module_name"), |
| 128 | + $override == null ? null : true, |
| 129 | + is_array($override) ? $override : null, |
| 130 | + ), |
| 131 | + }; |
| 132 | + } |
| 133 | +} |
0 commit comments