mirror of
https://github.com/librenms/librenms.git
synced 2024-10-07 16:52:45 +00:00
Graylog API properly throw errors (#15188)
* Graylog API properly throw errors remove unused legacy graylog code * Add type casts
This commit is contained in:
@@ -34,26 +34,21 @@ class GraylogApi
|
||||
private \Illuminate\Http\Client\PendingRequest $client;
|
||||
private string $api_prefix = '';
|
||||
|
||||
public function __construct(array $config = [])
|
||||
public function __construct()
|
||||
{
|
||||
if (version_compare(Config::get('graylog.version', '2.4'), '2.1', '>=')) {
|
||||
$this->api_prefix = '/api';
|
||||
}
|
||||
|
||||
if (empty($config)) {
|
||||
$base_uri = Config::get('graylog.server');
|
||||
if ($port = Config::get('graylog.port')) {
|
||||
$base_uri .= ':' . $port;
|
||||
}
|
||||
|
||||
$config = [
|
||||
'base_uri' => $base_uri,
|
||||
'auth' => [Config::get('graylog.username'), Config::get('graylog.password')],
|
||||
'headers' => ['Accept' => 'application/json'],
|
||||
];
|
||||
$base_uri = Config::get('graylog.server');
|
||||
if ($port = Config::get('graylog.port')) {
|
||||
$base_uri .= ':' . $port;
|
||||
}
|
||||
|
||||
$this->client = Http::client()->withOptions($config);
|
||||
$this->client = Http::client()
|
||||
->baseUrl($base_uri)
|
||||
->withBasicAuth(Config::get('graylog.username'), Config::get('graylog.password'))
|
||||
->acceptJson();
|
||||
}
|
||||
|
||||
public function getStreams(): array
|
||||
@@ -92,7 +87,7 @@ class GraylogApi
|
||||
'filter' => $filter,
|
||||
];
|
||||
|
||||
$response = $this->client->get($uri, $data);
|
||||
$response = $this->client->get($uri, $data)->throw();
|
||||
|
||||
return $response->json() ?: [];
|
||||
}
|
||||
|
@@ -61,12 +61,12 @@ class GraylogController extends SimpleTableController
|
||||
]);
|
||||
|
||||
$search = $request->get('searchPhrase');
|
||||
$device_id = $request->get('device');
|
||||
$device_id = (int) $request->get('device');
|
||||
$device = $device_id ? Device::find($device_id) : null;
|
||||
$range = $request->get('range', 0);
|
||||
$limit = $request->get('rowCount', 10);
|
||||
$page = $request->get('current', 1);
|
||||
$offset = ($page - 1) * $limit;
|
||||
$range = (int) $request->get('range', 0);
|
||||
$limit = (int) $request->get('rowCount', 10);
|
||||
$page = (int) $request->get('current', 1);
|
||||
$offset = (int) (($page - 1) * $limit);
|
||||
$loglevel = $request->get('loglevel') ?? Config::get('graylog.loglevel');
|
||||
|
||||
$query = $api->buildSimpleQuery($search, $device) .
|
||||
|
@@ -1,111 +0,0 @@
|
||||
<?php
|
||||
/*
|
||||
* LibreNMS
|
||||
*
|
||||
* Copyright (c) 2014 Neil Lathwood <https://github.com/laf/ http://www.lathwood.co.uk/fa>
|
||||
*
|
||||
* 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. Please see LICENSE.txt at the top level of
|
||||
* the source code distribution for details.
|
||||
*
|
||||
* @package LibreNMS
|
||||
* @subpackage webui
|
||||
* @link https://www.librenms.org
|
||||
* @copyright 2018 LibreNMS
|
||||
* @author LibreNMS Contributors
|
||||
*/
|
||||
|
||||
use LibreNMS\Config;
|
||||
use LibreNMS\Enum\CheckStatus;
|
||||
use LibreNMS\Enum\SyslogSeverity;
|
||||
|
||||
$filter_hostname = $vars['hostname'];
|
||||
$filter_range = $vars['range'];
|
||||
|
||||
if (isset($searchPhrase) && ! empty($searchPhrase)) {
|
||||
$query = 'message:"' . $searchPhrase . '"';
|
||||
} else {
|
||||
$query = '*';
|
||||
}
|
||||
|
||||
if (isset($current)) {
|
||||
$offset = ($current * $rowCount) - $rowCount;
|
||||
$limit = $rowCount;
|
||||
}
|
||||
|
||||
if ($rowCount != -1) {
|
||||
$extra_query = "&limit=$limit&offset=$offset";
|
||||
}
|
||||
|
||||
if (! empty($filter_hostname)) {
|
||||
if (! empty($query)) {
|
||||
$query .= ' && ';
|
||||
}
|
||||
$ip = gethostbyname($filter_hostname);
|
||||
$device = device_by_name($filter_hostname);
|
||||
$query .= 'source:"' . $filter_hostname . '" || source:"' . $ip . '"';
|
||||
if (isset($device['ip']) && $ip != $device['ip']) {
|
||||
$query .= ' || source:"' . $device['ip'] . '"';
|
||||
}
|
||||
}
|
||||
|
||||
if (Config::has('graylog.base_uri')) {
|
||||
$graylog_base = Config::get('graylog.base_uri');
|
||||
} elseif (version_compare(Config::get('graylog.version'), '2.1', '>=')) {
|
||||
$graylog_base = '/api/search/universal/relative';
|
||||
} else {
|
||||
$graylog_base = '/search/universal/relative';
|
||||
}
|
||||
|
||||
$graylog_url = Config::get('graylog.server') . ':' . Config::get('graylog.port') . $graylog_base . '?query=' . urlencode($query) . '&range=' . $filter_range . $extra_query;
|
||||
|
||||
$context = stream_context_create([
|
||||
'http' => [
|
||||
'header' => 'Authorization: Basic ' . base64_encode(Config::get('graylog.username') . ':' . Config::get('graylog.password')) . "\r\n" .
|
||||
'Accept: application/json',
|
||||
],
|
||||
]);
|
||||
|
||||
$messages = json_decode(file_get_contents($graylog_url, false, $context), true);
|
||||
$labels = [
|
||||
CheckStatus::OK => 'label-info',
|
||||
CheckStatus::UNKNOWN => 'label-default',
|
||||
CheckStatus::WARNING => 'label-warning',
|
||||
CheckStatus::ERROR => 'label-danger',
|
||||
];
|
||||
|
||||
foreach ($messages['messages'] as $message) {
|
||||
if (Config::has('graylog.timezone')) {
|
||||
$userTimezone = new DateTimeZone(Config::get('graylog.timezone'));
|
||||
$graylogTime = new DateTime($message['message']['timestamp']);
|
||||
$offset = $userTimezone->getOffset($graylogTime);
|
||||
|
||||
$timeInterval = DateInterval::createFromDateString((string) $offset . 'seconds');
|
||||
$graylogTime->add($timeInterval);
|
||||
$displayTime = $graylogTime->format('Y-m-d H:i:s');
|
||||
} else {
|
||||
$displayTime = $message['message']['timestamp'];
|
||||
}
|
||||
|
||||
$color = $labels[SyslogSeverity::STATUS[$message['message']['level']] ?? CheckStatus::UNKNOWN];
|
||||
$label = "<span class=\"alert-status $color\" style=\"margin-right:8px;float:left;\"></span>";
|
||||
|
||||
$response[] = [
|
||||
'timestamp' => $label . $displayTime,
|
||||
'source' => '<a href="' . \LibreNMS\Util\Url::generate(['page' => 'device', 'device' => $message['message']['source']]) . '">' . $message['message']['source'] . '</a>',
|
||||
'message' => $message['message']['message'],
|
||||
'facility' => $message['message']['facility'],
|
||||
'level' => $message['message']['level'],
|
||||
];
|
||||
}
|
||||
|
||||
if (empty($messages['total_results'])) {
|
||||
$total = 0;
|
||||
} else {
|
||||
$total = $messages['total_results'];
|
||||
}
|
||||
|
||||
$output = ['current'=>$current, 'rowCount'=>$rowCount, 'rows'=>$response, 'total'=>$total];
|
||||
echo json_encode($output, JSON_PRETTY_PRINT | JSON_UNESCAPED_SLASHES | JSON_UNESCAPED_UNICODE);
|
Reference in New Issue
Block a user