Files

Ignoring revisions in .git-blame-ignore-revs. Click here to bypass and see the normal blame view.

322 lines
11 KiB
PHP
Raw Permalink Normal View History

2020-10-05 07:26:37 -05:00
<?php
/**
2020-10-05 07:26:37 -05:00
* Core.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
2021-02-09 00:29:04 +01:00
* along with this program. If not, see <https://www.gnu.org/licenses/>.
2020-10-05 07:26:37 -05:00
*
2021-02-09 00:29:04 +01:00
* @link https://www.librenms.org
*
* @copyright 2021 Tony Murray
2020-10-05 07:26:37 -05:00
* @author Tony Murray <murraytony@gmail.com>
*/
namespace LibreNMS\Modules;
use App\Models\Device;
2022-11-09 09:47:19 +01:00
use App\Models\Eventlog;
2020-10-05 07:26:37 -05:00
use Illuminate\Support\Str;
use LibreNMS\Config;
2023-08-05 12:12:36 -05:00
use LibreNMS\Enum\Severity;
2023-10-04 10:32:59 -05:00
use LibreNMS\Interfaces\Data\DataStorageInterface;
use LibreNMS\Interfaces\Module;
use LibreNMS\OS;
2023-10-04 10:32:59 -05:00
use LibreNMS\Polling\ModuleStatus;
use LibreNMS\RRD\RrdDefinition;
use LibreNMS\Util\Compare;
2022-04-15 10:19:54 -05:00
use LibreNMS\Util\Number;
use LibreNMS\Util\Time;
use Log;
use SnmpQuery;
2020-10-05 07:26:37 -05:00
class Core implements Module
2020-10-05 07:26:37 -05:00
{
2022-09-07 16:53:16 -05:00
/**
* @inheritDoc
*/
public function dependencies(): array
{
return [];
}
2023-10-04 10:32:59 -05:00
public function shouldDiscover(OS $os, ModuleStatus $status): bool
{
return ! $os->getDevice()->snmp_disable && $os->getDevice()->status;
}
2022-09-07 16:53:16 -05:00
public function discover(OS $os): void
{
$snmpdata = SnmpQuery::numeric()->get(['SNMPv2-MIB::sysObjectID.0', 'SNMPv2-MIB::sysDescr.0', 'SNMPv2-MIB::sysName.0'])
->values();
$device = $os->getDevice();
$device->fill([
'sysObjectID' => $snmpdata['.1.3.6.1.2.1.1.2.0'] ?? null,
2021-11-10 20:48:22 -06:00
'sysName' => $snmpdata['.1.3.6.1.2.1.1.5.0'] ?? null,
'sysDescr' => $snmpdata['.1.3.6.1.2.1.1.1.0'] ?? null,
]);
foreach ($device->getDirty() as $attribute => $value) {
2023-08-05 12:12:36 -05:00
Eventlog::log($value . ' -> ' . $device->$attribute, $device, 'system', Severity::Notice);
$os->getDeviceArray()[$attribute] = $value; // update device array
}
// detect OS
$device->os = self::detectOS($device, false);
if ($device->isDirty('os')) {
2023-08-05 12:12:36 -05:00
Eventlog::log('Device OS changed: ' . $device->getOriginal('os') . ' -> ' . $device->os, $device, 'system', Severity::Notice);
$os->getDeviceArray()['os'] = $device->os;
2024-08-21 05:20:45 -05:00
Log::info('OS Changed ');
}
// Set type to a predefined type for the OS if it's not already set
$loaded_os_type = Config::get("os.$device->os.type");
if (! $device->getAttrib('override_device_type') && $loaded_os_type != $device->type) {
$device->type = $loaded_os_type;
Log::debug("Device type changed to $loaded_os_type!");
}
$device->save();
2024-08-21 05:20:45 -05:00
Log::notice('OS: ' . Config::getOsSetting($device->os, 'text') . " ($device->os)\n");
}
2023-10-04 10:32:59 -05:00
public function shouldPoll(OS $os, ModuleStatus $status): bool
{
return ! $os->getDevice()->snmp_disable && $os->getDevice()->status;
}
public function poll(OS $os, DataStorageInterface $datastore): void
{
$device = $os->getDevice();
$oids = [];
// fill required fields if they are empty
if (! isset($device->sysDescr)) {
$oids[] = 'SNMPv2-MIB::sysDescr.0';
}
if (! isset($device->sysObjectID)) {
$oids[] = 'SNMPv2-MIB::sysObjectID.0';
}
$oids[] = 'SNMPv2-MIB::sysUpTime.0'; // always poll uptime
$snmpdata = SnmpQuery::numeric()->get($oids)->values();
$device->fill([
'sysDescr' => $snmpdata['.1.3.6.1.2.1.1.1.0'] ?? $device->sysDescr,
'sysObjectID' => $snmpdata['.1.3.6.1.2.1.1.2.0'] ?? $device->sysObjectID,
]);
2023-10-04 10:32:59 -05:00
$this->calculateUptime($os, $snmpdata['.1.3.6.1.2.1.1.3.0'] ?? null, $datastore);
2021-10-20 02:22:29 -05:00
$device->save();
}
2024-09-09 02:09:19 -05:00
public function dataExists(Device $device): bool
{
2024-09-09 02:09:19 -05:00
return false; // no module specific data
}
public function cleanup(Device $device): int
{
return 0; // nothing to cleanup
}
2022-09-07 16:53:16 -05:00
/**
* @inheritDoc
*/
public function dump(Device $device, string $type): ?array
2022-09-07 16:53:16 -05:00
{
return null; // all data here is stored in the devices table and covered by the os module
2022-09-07 16:53:16 -05:00
}
2020-10-05 07:26:37 -05:00
/**
* Detect the os of the given device.
*
* @param Device $device device to check
2021-09-08 23:35:56 +02:00
* @param bool $fetch fetch sysDescr and sysObjectID fresh from the device
2020-10-05 07:26:37 -05:00
* @return string the name of the os
2021-09-10 20:09:53 +02:00
*
2020-10-05 07:26:37 -05:00
* @throws \Exception
*/
public static function detectOS(Device $device, bool $fetch = true): string
2020-10-05 07:26:37 -05:00
{
if ($fetch) {
// some devices act oddly when getting both OIDs at once
$device->sysDescr = SnmpQuery::device($device)->get('SNMPv2-MIB::sysDescr.0')->value();
$device->sysObjectID = SnmpQuery::device($device)->numeric()->get('SNMPv2-MIB::sysObjectID.0')->value();
2020-10-05 07:26:37 -05:00
}
Log::debug("| $device->sysDescr | $device->sysObjectID | \n");
2020-10-05 07:26:37 -05:00
$deferred_os = [];
$generic_os = [
'airos',
'freebsd',
'linux',
];
// check yaml files
2022-01-29 21:09:05 -06:00
\LibreNMS\Util\OS::loadAllDefinitions();
2020-10-05 07:26:37 -05:00
$os_defs = Config::get('os');
2022-01-29 21:09:05 -06:00
2020-10-05 07:26:37 -05:00
foreach ($os_defs as $os => $def) {
if (isset($def['discovery']) && ! in_array($os, $generic_os)) {
if (self::discoveryIsSlow($def)) {
// defer all os that use snmpget or snmpwalk
$deferred_os[] = $os;
continue;
}
foreach ($def['discovery'] as $item) {
if (self::checkDiscovery($device, $item, $def['mib_dir'] ?? null)) {
return $os;
}
}
}
}
// check deferred os
$deferred_os = array_merge($deferred_os, $generic_os);
foreach ($deferred_os as $os) {
foreach ($os_defs[$os]['discovery'] as $item) {
if (self::checkDiscovery($device, $item, $os_defs[$os]['mib_dir'] ?? null)) {
return $os;
}
}
}
return 'generic';
}
/**
* Check an array of conditions if all match, return true
* sysObjectID if sysObjectID starts with any of the values under this item
* sysDescr if sysDescr contains any of the values under this item
* sysDescr_regex if sysDescr matches any of the regexes under this item
* snmpget perform an snmpget on `oid` and check if the result contains `value`. Other subkeys: options, mib, mibdir
*
* Appending _except to any condition will invert the match.
*
* @param Device $device
2021-09-08 23:35:56 +02:00
* @param array $array Array of items, keys should be sysObjectID, sysDescr, or sysDescr_regex
* @param string|array $mibdir MIB directory for evaluated OS
2020-10-05 07:26:37 -05:00
* @return bool the result (all items passed return true)
*/
protected static function checkDiscovery(Device $device, array $array, $mibdir): bool
2020-10-05 07:26:37 -05:00
{
// all items must be true
foreach ($array as $key => $value) {
if ($check = Str::endsWith($key, '_except')) {
$key = substr($key, 0, -7);
}
if ($key == 'sysObjectID') {
if (Str::startsWith($device['sysObjectID'] ?? '', $value) == $check) {
2020-10-05 07:26:37 -05:00
return false;
}
} elseif ($key == 'sysDescr') {
if (Str::contains($device['sysDescr'] ?? '', $value) == $check) {
2020-10-05 07:26:37 -05:00
return false;
}
} elseif ($key == 'sysDescr_regex') {
if (preg_match_any($device['sysDescr'] ?? '', $value) == $check) {
2020-10-05 07:26:37 -05:00
return false;
}
} elseif ($key == 'sysObjectID_regex') {
if (preg_match_any($device['sysObjectID'] ?? '', $value) == $check) {
2020-10-05 07:26:37 -05:00
return false;
}
} elseif ($key == 'snmpget') {
$get_value = SnmpQuery::device($device)
->options($value['options'] ?? null)
->mibDir($value['mib_dir'] ?? $mibdir)
->get(isset($value['mib']) ? "{$value['mib']}::{$value['oid']}" : $value['oid'])
->value();
if (Compare::values($get_value, $value['value'], $value['op'] ?? 'contains') == $check) {
2020-10-05 07:26:37 -05:00
return false;
}
} elseif ($key == 'snmpwalk') {
$walk_value = SnmpQuery::device($device)
->options($value['options'] ?? null)
->mibDir($value['mib_dir'] ?? $mibdir)
->walk(isset($value['mib']) ? "{$value['mib']}::{$value['oid']}" : $value['oid'])
2022-11-30 19:50:46 -06:00
->raw;
if (Compare::values($walk_value, $value['value'], $value['op'] ?? 'contains') == $check) {
2020-10-05 07:26:37 -05:00
return false;
}
}
}
return true;
}
2023-10-04 10:32:59 -05:00
private function calculateUptime(OS $os, ?string $sysUpTime, DataStorageInterface $datastore): void
2021-10-20 02:22:29 -05:00
{
global $agent_data;
$device = $os->getDevice();
if (Config::get("os.$device->os.bad_uptime")) {
return;
}
if (! empty($agent_data['uptime'])) {
$uptime = round((float) substr($agent_data['uptime'], 0, strpos($agent_data['uptime'], ' ')));
2024-08-21 05:20:45 -05:00
Log::info("Using UNIX Agent Uptime ($uptime)");
2021-10-20 02:22:29 -05:00
} else {
$uptime_data = SnmpQuery::make()->get(['SNMP-FRAMEWORK-MIB::snmpEngineTime.0', 'HOST-RESOURCES-MIB::hrSystemUptime.0'])->values();
$uptime = max(
2022-04-15 10:19:54 -05:00
round(Number::cast($sysUpTime) / 100),
Config::get("os.$device->os.bad_snmpEngineTime") ? 0 : Number::cast($uptime_data['SNMP-FRAMEWORK-MIB::snmpEngineTime.0'] ?? 0),
Config::get("os.$device->os.bad_hrSystemUptime") ? 0 : round(Number::cast($uptime_data['HOST-RESOURCES-MIB::hrSystemUptime.0'] ?? 0) / 100)
2021-10-20 02:22:29 -05:00
);
Log::debug("Uptime seconds: $uptime\n");
}
// set it if unless it is wrong
if ($uptime > 0) {
if ($uptime < $device->uptime) {
2023-08-05 12:12:36 -05:00
Eventlog::log('Device rebooted after ' . Time::formatInterval($device->uptime) . " -> {$uptime}s", $device, 'reboot', Severity::Warning, $device->uptime);
2021-10-25 13:22:23 +02:00
if (Config::get('discovery_on_reboot')) {
$device->last_discovered = null;
$device->save();
}
2021-10-20 02:22:29 -05:00
}
2023-10-04 10:32:59 -05:00
$datastore->put($os->getDeviceArray(), 'uptime', [
2021-10-20 02:22:29 -05:00
'rrd_def' => RrdDefinition::make()->addDataset('uptime', 'GAUGE', 0),
], $uptime);
$os->enableGraph('uptime');
2023-10-04 10:32:59 -05:00
Log::info('Uptime: ' . Time::formatInterval($uptime));
2021-10-20 02:22:29 -05:00
$device->uptime = $uptime;
}
}
2022-09-07 16:53:16 -05:00
protected static function discoveryIsSlow(array $def): bool
2020-10-05 07:26:37 -05:00
{
foreach ($def['discovery'] as $item) {
if (array_key_exists('snmpget', $item) || array_key_exists('snmpwalk', $item)) {
return true;
}
}
return false;
}
}