Files

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

138 lines
3.7 KiB
PHP
Raw Permalink Normal View History

2018-08-11 23:37:45 +02:00
<?php
/**
* Trap.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/>.
2018-08-11 23:37:45 +02:00
*
2021-02-09 00:29:04 +01:00
* @link https://www.librenms.org
2021-09-10 20:09:53 +02:00
*
2018-08-11 23:37:45 +02:00
* @copyright 2018 Tony Murray
* @author Tony Murray <[email protected]>
*/
namespace LibreNMS\Snmptrap;
use App\Models\Device;
2022-11-09 09:47:19 +01:00
use App\Models\Eventlog;
2018-08-11 23:37:45 +02:00
use Illuminate\Support\Collection;
2020-04-17 17:37:56 -05:00
use Illuminate\Support\Str;
2023-08-05 12:12:36 -05:00
use LibreNMS\Enum\Severity;
2018-08-11 23:37:45 +02:00
use LibreNMS\Util\IP;
class Trap
{
2022-11-05 14:43:54 -05:00
public readonly string $raw;
public readonly string $hostname;
public readonly ?string $ip;
protected Collection $oid_data;
protected ?Device $device = null;
2018-08-11 23:37:45 +02:00
/**
* Construct a trap from raw trap text
*/
2022-11-05 14:43:54 -05:00
public function __construct(string $trap)
2018-08-11 23:37:45 +02:00
{
$this->raw = $trap;
2022-11-05 14:43:54 -05:00
$lines = explode(PHP_EOL, trim($trap));
2018-08-11 23:37:45 +02:00
$this->hostname = array_shift($lines);
$line = array_shift($lines);
2023-07-25 18:08:10 -05:00
if ($line) {
preg_match('/\[([0-9.:a-fA-F]+)]/', $line, $matches);
}
2022-11-05 14:43:54 -05:00
$this->ip = $matches[1] ?? '';
2018-08-11 23:37:45 +02:00
// parse the oid data
2022-11-05 14:43:54 -05:00
$this->oid_data = (new Collection($lines))->mapWithKeys(function ($line) {
2018-08-11 23:37:45 +02:00
[$oid, $data] = explode(' ', $line, 2);
2020-09-21 14:54:51 +02:00
2018-08-11 23:37:45 +02:00
return [$oid => trim($data, '"')];
});
}
/**
* Find the first in this trap by substring
*
2021-09-08 23:35:56 +02:00
* @param string|string[] $search
2018-08-11 23:37:45 +02:00
* @return string
*/
2022-11-05 14:43:54 -05:00
public function findOid(array|string $search): string
2018-08-11 23:37:45 +02:00
{
return $this->oid_data->keys()->first(function ($oid) use ($search) {
2020-04-17 17:37:56 -05:00
return Str::contains($oid, $search);
2022-11-05 14:43:54 -05:00
}, '');
2018-08-11 23:37:45 +02:00
}
/**
* Find all oids that match the given string
2021-09-10 20:09:53 +02:00
*
2021-09-08 23:35:56 +02:00
* @param string|string[] $search
2018-08-11 23:37:45 +02:00
* @return array
*/
2022-11-05 14:43:54 -05:00
public function findOids(array|string $search): array
2018-08-11 23:37:45 +02:00
{
return $this->oid_data->keys()->filter(function ($oid) use ($search) {
2020-04-17 17:37:56 -05:00
return Str::contains($oid, $search);
2018-08-11 23:37:45 +02:00
})->all();
}
2022-11-05 14:43:54 -05:00
public function getOidData(string $oid): string
2018-08-11 23:37:45 +02:00
{
2022-11-05 14:43:54 -05:00
return $this->oid_data->get($oid, '');
2018-08-11 23:37:45 +02:00
}
2022-11-05 14:43:54 -05:00
public function getDevice(): ?Device
2018-08-11 23:37:45 +02:00
{
if (is_null($this->device) && IP::isValid($this->ip)) {
$this->device = Device::findByHostname($this->hostname) ?: Device::findByIp($this->ip);
}
return $this->device;
}
2022-11-05 14:43:54 -05:00
public function getTrapOid(): string
2018-08-11 23:37:45 +02:00
{
return $this->getOidData('SNMPv2-MIB::snmpTrapOID.0');
}
2020-09-23 17:00:02 +02:00
/**
* Render the Trap for debugging purpose
*
2021-09-08 23:35:56 +02:00
* @param bool $detailed
2020-09-23 17:00:02 +02:00
* @return string
*/
2022-11-05 14:43:54 -05:00
public function toString(bool $detailed = false): string
2020-09-23 17:00:02 +02:00
{
if ($detailed) {
return $this->getTrapOid() . "\n" . json_encode($this->oid_data->reject(function ($value, $key) {
return Str::contains($key, 'SNMPv2-MIB::snmpTrapOID.0');
})->all());
}
2022-11-05 14:43:54 -05:00
return $this->getTrapOid();
}
/**
* Log this trap in the eventlog with the given message
*/
2023-08-05 12:12:36 -05:00
public function log(string $message, Severity $severity = Severity::Info, string $type = 'trap', int|null|string $reference = null): void
2022-11-05 14:43:54 -05:00
{
2022-11-09 09:47:19 +01:00
Eventlog::log($message, $this->getDevice(), $type, $severity, $reference);
2020-09-23 17:00:02 +02:00
}
2018-08-11 23:37:45 +02:00
}