2022-09-07 16:53:16 -05:00
|
|
|
<?php
|
|
|
|
/**
|
|
|
|
* Module.php
|
|
|
|
*
|
|
|
|
* -Description-
|
|
|
|
*
|
|
|
|
* This program is free software: you can redistribute it and/or modify
|
|
|
|
* it under the terms of the GNU General Public License as published by
|
|
|
|
* the Free Software Foundation, either version 3 of the License, or
|
|
|
|
* (at your option) any later version.
|
|
|
|
*
|
|
|
|
* This program is distributed in the hope that it will be useful,
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.See the
|
|
|
|
* GNU General Public License for more details.
|
|
|
|
*
|
|
|
|
* You should have received a copy of the GNU General Public License
|
|
|
|
* along with this program. If not, see <https://www.gnu.org/licenses/>.
|
|
|
|
*
|
|
|
|
* @link https://www.librenms.org
|
|
|
|
*
|
|
|
|
* @copyright 2022 Tony Murray
|
|
|
|
* @author Tony Murray <murraytony@gmail.com>
|
|
|
|
*/
|
|
|
|
|
|
|
|
namespace LibreNMS\Util;
|
|
|
|
|
2023-10-04 10:32:59 -05:00
|
|
|
use App\Models\Device;
|
|
|
|
use LibreNMS\Config;
|
2022-09-07 16:53:16 -05:00
|
|
|
use LibreNMS\Modules\LegacyModule;
|
2023-10-04 10:32:59 -05:00
|
|
|
use LibreNMS\Polling\ModuleStatus;
|
2022-09-07 16:53:16 -05:00
|
|
|
|
|
|
|
class Module
|
|
|
|
{
|
2023-10-16 05:03:32 -07:00
|
|
|
public static function exists(string $module_name): bool
|
2022-09-07 16:53:16 -05:00
|
|
|
{
|
2024-08-25 19:56:50 -05:00
|
|
|
if (class_exists(StringHelpers::toClass($module_name, '\\LibreNMS\\Modules\\'))) {
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
return Config::has('discovery_modules.' . $module_name) || Config::has('poller_modules.' . $module_name);
|
2023-10-16 05:03:32 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
public static function fromName(string $module_name): \LibreNMS\Interfaces\Module
|
|
|
|
{
|
|
|
|
$module_class = StringHelpers::toClass($module_name, '\\LibreNMS\\Modules\\');
|
2022-09-07 16:53:16 -05:00
|
|
|
|
2023-10-16 05:03:32 -07:00
|
|
|
return class_exists($module_class) ? new $module_class : new LegacyModule($module_name);
|
2022-09-07 16:53:16 -05:00
|
|
|
}
|
2023-10-04 10:32:59 -05:00
|
|
|
|
2023-10-16 05:03:32 -07:00
|
|
|
public static function legacyDiscoveryExists(string $module_name): bool
|
2023-10-04 10:32:59 -05:00
|
|
|
{
|
2023-10-16 05:03:32 -07:00
|
|
|
return is_file(base_path("includes/discovery/$module_name.inc.php"));
|
2023-10-04 10:32:59 -05:00
|
|
|
}
|
|
|
|
|
2023-10-16 05:03:32 -07:00
|
|
|
public static function legacyPollingExists(string $module_name): bool
|
2023-10-04 10:32:59 -05:00
|
|
|
{
|
2023-10-16 05:03:32 -07:00
|
|
|
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(
|
2024-09-23 10:11:05 -05:00
|
|
|
Config::get("poller_modules.$module_name"),
|
2023-10-16 05:03:32 -07:00
|
|
|
Config::get("os.{$device->os}.poller_modules.$module_name"),
|
|
|
|
$device->getAttrib("poll_$module_name"),
|
|
|
|
$manual,
|
|
|
|
);
|
2023-10-04 10:32:59 -05:00
|
|
|
}
|
2024-08-25 19:56:50 -05:00
|
|
|
|
|
|
|
public static function parseUserOverrides(array $overrides): array
|
|
|
|
{
|
|
|
|
$modules = [];
|
|
|
|
|
|
|
|
foreach ($overrides as $index => $module) {
|
|
|
|
// parse submodules (only supported by some modules)
|
|
|
|
if (str_contains($module, '/')) {
|
|
|
|
[$module, $submodule] = explode('/', $module, 2);
|
|
|
|
$modules[$module][] = $submodule;
|
|
|
|
} elseif (self::exists($module)) {
|
|
|
|
$modules[$module] = true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return $modules;
|
|
|
|
}
|
2022-09-07 16:53:16 -05:00
|
|
|
}
|