2021-10-01 18:58:12 -05:00
|
|
|
<?php
|
2021-10-13 08:49:19 -05:00
|
|
|
/**
|
2021-10-01 18:58:12 -05:00
|
|
|
* SnmpResponse.php
|
|
|
|
|
*
|
|
|
|
|
* Responsible for parsing net-snmp output into usable PHP data structures.
|
|
|
|
|
*
|
|
|
|
|
* 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-10-13 08:49:19 -05:00
|
|
|
* along with this program. If not, see <https://www.gnu.org/licenses/>.
|
|
|
|
|
*
|
|
|
|
|
* @link https://www.librenms.org
|
2021-10-01 18:58:12 -05:00
|
|
|
*
|
|
|
|
|
* @copyright 2021 Tony Murray
|
|
|
|
|
* @author Tony Murray <murraytony@gmail.com>
|
|
|
|
|
*/
|
|
|
|
|
|
|
|
|
|
namespace LibreNMS\Data\Source;
|
|
|
|
|
|
|
|
|
|
use Illuminate\Support\Arr;
|
2021-11-12 13:49:09 -06:00
|
|
|
use Illuminate\Support\Collection;
|
2021-10-01 18:58:12 -05:00
|
|
|
use Illuminate\Support\Str;
|
2021-11-30 21:33:18 -06:00
|
|
|
use LibreNMS\Config;
|
2022-11-30 19:50:46 -06:00
|
|
|
use LibreNMS\Util\Oid;
|
2021-10-01 18:58:12 -05:00
|
|
|
use Log;
|
|
|
|
|
|
|
|
|
|
class SnmpResponse
|
|
|
|
|
{
|
2022-11-30 19:50:46 -06:00
|
|
|
protected const KEY_VALUE_DELIMITER = ' = ';
|
|
|
|
|
|
|
|
|
|
public readonly string $raw;
|
|
|
|
|
public readonly int $exitCode;
|
|
|
|
|
public readonly string $stderr;
|
|
|
|
|
|
|
|
|
|
private ?string $errorMessage = null;
|
|
|
|
|
private ?array $values = null;
|
2021-10-01 18:58:12 -05:00
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Create a new response object filling with output from the net-snmp command.
|
|
|
|
|
*
|
|
|
|
|
* @param string $output
|
|
|
|
|
* @param string $errorOutput
|
|
|
|
|
* @param int $exitCode
|
|
|
|
|
*/
|
|
|
|
|
public function __construct(string $output, string $errorOutput = '', int $exitCode = 0)
|
|
|
|
|
{
|
2022-11-30 19:50:46 -06:00
|
|
|
$this->raw = (string) preg_replace('/Wrong Type \(should be .*\): /', '', $output);
|
2021-10-01 18:58:12 -05:00
|
|
|
$this->stderr = $errorOutput;
|
2022-11-30 19:50:46 -06:00
|
|
|
$this->exitCode = $exitCode;
|
2021-10-01 18:58:12 -05:00
|
|
|
}
|
|
|
|
|
|
2022-11-30 19:50:46 -06:00
|
|
|
public function isValid(bool $ignore_partial = false): bool
|
2021-10-01 18:58:12 -05:00
|
|
|
{
|
|
|
|
|
$this->errorMessage = '';
|
2022-11-30 19:50:46 -06:00
|
|
|
$raw = $ignore_partial ? $this->getRawWithoutBadLines() : $this->raw;
|
|
|
|
|
|
2021-10-01 18:58:12 -05:00
|
|
|
// not checking exitCode because I think it may lead to false negatives
|
2021-11-24 11:22:15 -06:00
|
|
|
$invalid = preg_match('/(Timeout: No Response from .*|Unknown user name|Authentication failure|Error: OID not increasing: .*)/', $this->stderr, $errors)
|
2022-11-30 19:50:46 -06:00
|
|
|
|| empty($raw)
|
|
|
|
|
|| preg_match('/(No Such Instance|No Such Object|No more variables left).*/', $raw, $errors);
|
2021-10-01 18:58:12 -05:00
|
|
|
|
|
|
|
|
if ($invalid) {
|
|
|
|
|
$this->errorMessage = $errors[0] ?? 'Empty Output';
|
2022-11-30 19:50:46 -06:00
|
|
|
Log::debug(sprintf('SNMP query failed. Exit Code: %s Empty: %s Bad String: %s', $this->exitCode, var_export(empty($raw), true), $errors[0] ?? 'not found'));
|
2021-10-01 18:58:12 -05:00
|
|
|
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Get the error message if any
|
|
|
|
|
*/
|
|
|
|
|
public function getErrorMessage(): string
|
|
|
|
|
{
|
|
|
|
|
if (empty($this->errorMessage)) {
|
|
|
|
|
$this->isValid(); // if no error message, double check.
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return $this->errorMessage;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
2022-11-30 19:50:46 -06:00
|
|
|
* Gets the first value of this response.
|
|
|
|
|
* If an oid or list of oids is given, return the first one found.
|
|
|
|
|
* If forceNumeric is set, force the search to use numeric oids even if textual oids are given
|
|
|
|
|
*
|
|
|
|
|
* @throws \LibreNMS\Exceptions\InvalidOidException
|
2021-10-01 18:58:12 -05:00
|
|
|
*/
|
2022-11-30 19:50:46 -06:00
|
|
|
public function value(array|string $oids = [], bool $forceNumeric = false): string
|
2021-10-01 18:58:12 -05:00
|
|
|
{
|
2022-11-30 19:50:46 -06:00
|
|
|
$values = $this->values();
|
2021-10-01 18:58:12 -05:00
|
|
|
|
2022-11-30 19:50:46 -06:00
|
|
|
if (empty($oids)) {
|
|
|
|
|
return Arr::first($values, default: '');
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
$oids = Arr::wrap($oids);
|
|
|
|
|
foreach ($oids as $oid) {
|
|
|
|
|
if ($forceNumeric) {
|
|
|
|
|
// translate all to numeric to make it easier to match
|
|
|
|
|
$oid = Oid::toNumeric($oid);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (isset($values[$oid]) && $values[$oid] !== '') {
|
|
|
|
|
return $values[$oid];
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// try to match table format
|
|
|
|
|
if (str_contains($this->raw, '[')) {
|
|
|
|
|
foreach ($oids as $oid) {
|
|
|
|
|
$dot_index_oid = preg_replace('/\.([^.]+)/', '[$1]', $oid);
|
|
|
|
|
// if new oid is different and exists and is not an empty string
|
|
|
|
|
if ($dot_index_oid !== $oid && isset($values[$dot_index_oid]) && $values[$dot_index_oid] !== '') {
|
|
|
|
|
return $values[$dot_index_oid];
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return '';
|
2021-10-01 18:58:12 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public function values(): array
|
|
|
|
|
{
|
2022-11-30 19:50:46 -06:00
|
|
|
if (isset($this->values)) {
|
|
|
|
|
return $this->values;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
$this->values = [];
|
2021-10-01 18:58:12 -05:00
|
|
|
$line = strtok($this->raw, PHP_EOL);
|
|
|
|
|
while ($line !== false) {
|
2021-11-17 16:26:50 -06:00
|
|
|
if (Str::contains($line, ['at this OID', 'this MIB View', 'End of MIB'])) {
|
2021-10-01 18:58:12 -05:00
|
|
|
// these occur when we seek past the end of data, usually the end of the response, but grab the next line and continue
|
|
|
|
|
$line = strtok(PHP_EOL);
|
|
|
|
|
continue;
|
|
|
|
|
}
|
|
|
|
|
|
2022-11-30 19:50:46 -06:00
|
|
|
$parts = explode(self::KEY_VALUE_DELIMITER, $line, 2);
|
2021-10-13 08:49:19 -05:00
|
|
|
if (count($parts) == 1) {
|
|
|
|
|
array_unshift($parts, '');
|
|
|
|
|
}
|
|
|
|
|
[$oid, $value] = $parts;
|
2021-10-01 18:58:12 -05:00
|
|
|
|
|
|
|
|
$line = strtok(PHP_EOL); // get the next line and concatenate multi-line values
|
2022-11-30 19:50:46 -06:00
|
|
|
while ($line !== false && ! Str::contains($line, self::KEY_VALUE_DELIMITER)) {
|
2021-10-01 18:58:12 -05:00
|
|
|
$value .= PHP_EOL . $line;
|
|
|
|
|
$line = strtok(PHP_EOL);
|
|
|
|
|
}
|
|
|
|
|
|
2021-11-30 21:33:18 -06:00
|
|
|
// remove extra escapes
|
|
|
|
|
if (Config::get('snmp.unescape')) {
|
|
|
|
|
$value = stripslashes($value);
|
|
|
|
|
}
|
|
|
|
|
|
2021-11-10 20:48:22 -06:00
|
|
|
if (Str::startsWith($value, '"') && Str::endsWith($value, '"')) {
|
|
|
|
|
// unformatted string from net-snmp, remove extra escapes
|
2022-11-30 19:50:46 -06:00
|
|
|
$this->values[$oid] = trim(stripslashes($value), "\" \n\r");
|
2021-11-10 20:48:22 -06:00
|
|
|
} else {
|
2022-11-30 19:50:46 -06:00
|
|
|
$this->values[$oid] = trim($value);
|
2021-11-10 20:48:22 -06:00
|
|
|
}
|
2021-10-01 18:58:12 -05:00
|
|
|
}
|
|
|
|
|
|
2022-11-30 19:50:46 -06:00
|
|
|
return $this->values;
|
2021-10-01 18:58:12 -05:00
|
|
|
}
|
|
|
|
|
|
2024-08-21 01:12:09 -05:00
|
|
|
/**
|
|
|
|
|
* Create a key to value pair for an OID
|
|
|
|
|
* Only works for single indexed tables
|
|
|
|
|
* You may omit $oid if there is only one $oid in the walk
|
|
|
|
|
*/
|
|
|
|
|
public function pluck(?string $oid = null): array
|
|
|
|
|
{
|
|
|
|
|
$output = [];
|
|
|
|
|
$oid = $oid ?? '[a-zA-Z0-9:.-]+';
|
|
|
|
|
$regex = "/^{$oid}[[.](\d+)]?$/";
|
|
|
|
|
|
|
|
|
|
foreach ($this->values() as $key => $value) {
|
|
|
|
|
if (preg_match($regex, $key, $matches)) {
|
|
|
|
|
$output[$matches[1]] = $value;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return $output;
|
|
|
|
|
}
|
|
|
|
|
|
2021-11-12 13:49:09 -06:00
|
|
|
public function valuesByIndex(array &$array = []): array
|
|
|
|
|
{
|
|
|
|
|
foreach ($this->values() as $oid => $value) {
|
2022-11-30 19:50:46 -06:00
|
|
|
$parts = $this->getOidParts($oid);
|
|
|
|
|
$name = array_shift($parts);
|
|
|
|
|
$index = implode('.', $parts);
|
|
|
|
|
|
2021-11-12 13:49:09 -06:00
|
|
|
$array[$index][$name] = $value;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return $array;
|
|
|
|
|
}
|
|
|
|
|
|
2021-10-01 18:58:12 -05:00
|
|
|
public function table(int $group = 0, array &$array = []): array
|
|
|
|
|
{
|
|
|
|
|
foreach ($this->values() as $key => $value) {
|
2022-11-30 19:50:46 -06:00
|
|
|
$parts = $this->getOidParts($key);
|
2022-01-30 16:28:18 -06:00
|
|
|
|
|
|
|
|
// move the oid name to the correct depth
|
|
|
|
|
array_splice($parts, $group, 0, array_shift($parts));
|
2021-10-01 18:58:12 -05:00
|
|
|
|
|
|
|
|
// merge the parts into an array, creating keys if they don't exist
|
|
|
|
|
$tmp = &$array;
|
|
|
|
|
foreach ($parts as $part) {
|
|
|
|
|
$key = trim($part, '"');
|
|
|
|
|
$tmp = &$tmp[$key];
|
|
|
|
|
}
|
|
|
|
|
$tmp = $value; // assign the value as the leaf
|
|
|
|
|
}
|
|
|
|
|
|
2021-10-13 08:49:19 -05:00
|
|
|
return Arr::wrap($array); // if no parts, wrap the value
|
2021-10-01 18:58:12 -05:00
|
|
|
}
|
|
|
|
|
|
2021-11-12 13:49:09 -06:00
|
|
|
/**
|
2021-11-17 16:26:50 -06:00
|
|
|
* Map an snmp table with callback. If invalid data is encountered, an empty collection is returned.
|
2021-11-12 13:49:09 -06:00
|
|
|
* Variables passed to the callback will be an array of row values followed by each individual index.
|
|
|
|
|
*/
|
|
|
|
|
public function mapTable(callable $callback): Collection
|
|
|
|
|
{
|
2022-11-30 19:50:46 -06:00
|
|
|
if (! $this->isValid(true)) {
|
2021-11-17 16:26:50 -06:00
|
|
|
return new Collection;
|
|
|
|
|
}
|
|
|
|
|
|
2024-07-22 21:23:10 -05:00
|
|
|
$data = [];
|
|
|
|
|
foreach ($this->values() as $key => $value) {
|
|
|
|
|
$parts = $this->getOidParts($key);
|
|
|
|
|
$oid = array_shift($parts);
|
|
|
|
|
$data[implode('][', $parts)][$oid] = $value;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
$return = new Collection;
|
|
|
|
|
foreach ($data as $index => $values) {
|
|
|
|
|
$return->push(call_user_func($callback, $values, ...explode('][', (string) $index)));
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return $return;
|
2021-11-12 13:49:09 -06:00
|
|
|
}
|
|
|
|
|
|
2021-10-01 18:58:12 -05:00
|
|
|
/**
|
|
|
|
|
* @return int
|
|
|
|
|
*/
|
|
|
|
|
public function getExitCode(): int
|
|
|
|
|
{
|
|
|
|
|
return $this->exitCode;
|
|
|
|
|
}
|
2022-01-30 16:28:18 -06:00
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Filter bad lines from the raw output, examples:
|
|
|
|
|
* "No Such Instance currently exists at this OID"
|
|
|
|
|
* "No more variables left in this MIB View (It is past the end of the MIB tree)"
|
|
|
|
|
*/
|
2022-11-30 19:50:46 -06:00
|
|
|
public function getRawWithoutBadLines(): string
|
2022-01-30 16:28:18 -06:00
|
|
|
{
|
2022-11-30 19:50:46 -06:00
|
|
|
return (string) preg_replace([
|
|
|
|
|
'/^.*No Such Instance currently exists.*$/m',
|
2023-10-05 01:29:22 -05:00
|
|
|
'/(\n[^\r\n]+No more variables left[^\r\n]+)+$/',
|
2022-11-30 19:50:46 -06:00
|
|
|
], '', $this->raw);
|
2022-01-30 16:28:18 -06:00
|
|
|
}
|
2022-06-07 02:04:32 -05:00
|
|
|
|
|
|
|
|
public function append(SnmpResponse $response): SnmpResponse
|
|
|
|
|
{
|
2022-11-30 19:50:46 -06:00
|
|
|
$newResponse = new static(
|
|
|
|
|
$this->raw . $response->raw,
|
|
|
|
|
$this->stderr . $response->stderr,
|
|
|
|
|
$this->exitCode ?: $response->exitCode,
|
|
|
|
|
);
|
2022-06-07 02:04:32 -05:00
|
|
|
|
2022-11-30 19:50:46 -06:00
|
|
|
$newResponse->errorMessage = $this->errorMessage ?: $response->errorMessage;
|
|
|
|
|
|
|
|
|
|
return $newResponse;
|
2022-06-07 02:04:32 -05:00
|
|
|
}
|
2022-11-07 12:00:47 -06:00
|
|
|
|
|
|
|
|
public function __toString(): string
|
|
|
|
|
{
|
|
|
|
|
return $this->raw;
|
|
|
|
|
}
|
2022-11-30 19:50:46 -06:00
|
|
|
|
|
|
|
|
private function getOidParts(string $key): array
|
|
|
|
|
{
|
|
|
|
|
// table
|
|
|
|
|
if (Str::contains($key, '[')) {
|
|
|
|
|
preg_match_all('/([^[\]]+)/', $key, $parts);
|
|
|
|
|
|
|
|
|
|
return $parts[1]; // get all group 1 matches
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// regular oid
|
|
|
|
|
return explode('.', $key);
|
|
|
|
|
}
|
2023-10-29 22:46:04 -05:00
|
|
|
|
|
|
|
|
public function __sleep()
|
|
|
|
|
{
|
|
|
|
|
return ['raw', 'exitCode', 'stderr'];
|
|
|
|
|
}
|
2021-10-01 18:58:12 -05:00
|
|
|
}
|