Fix syslog widget priority filtering (#13411)

* Fix widget filtering
syslog stores priority as a string
filter strings, not numbers

* Add enums for SyslogSeverity and CheckStatus
This commit is contained in:
Tony Murray
2021-10-29 03:32:36 -05:00
committed by GitHub
parent b979761cef
commit 38773598e0
7 changed files with 123 additions and 46 deletions

View File

@@ -0,0 +1,34 @@
<?php
/**
* Check.php
*
* Nagios/monitoring style check status
*
* 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 2021 Tony Murray
* @author Tony Murray <murraytony@gmail.com>
*/
namespace LibreNMS\Enum;
class CheckStatus
{
const OK = 0;
const WARNING = 1;
const ERROR = 2;
const UNKNOWN = 3;
}

View File

@@ -0,0 +1,60 @@
<?php
/**
* SyslogSeverity.php
*
* Mapping of syslog priorities. For user translated strings trans('syslog.0') = Emergency.
*
* 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 2021 Tony Murray
* @author Tony Murray <murraytony@gmail.com>
*/
namespace LibreNMS\Enum;
class SyslogSeverity
{
const EMERGENCY = 'emerg';
const ALERT = 'alert';
const ERROR = 'err';
const WARNING = 'warning';
const NOTICE = 'notice';
const INFO = 'info';
const DEBUG = 'debug';
const CRITICAL = 'crit';
const LEVELS = [
0 => self::EMERGENCY,
1 => self::ALERT,
2 => self::CRITICAL,
3 => self::ERROR,
4 => self::WARNING,
5 => self::NOTICE,
6 => self::INFO,
7 => self::DEBUG,
];
const STATUS = [
self::EMERGENCY => CheckStatus::ERROR,
self::ALERT => CheckStatus::ERROR,
self::CRITICAL => CheckStatus::ERROR,
self::ERROR => CheckStatus::ERROR,
self::WARNING => CheckStatus::WARNING,
self::NOTICE => CheckStatus::OK,
self::DEBUG => CheckStatus::UNKNOWN,
self::INFO => CheckStatus::OK,
];
}

View File

@@ -26,6 +26,7 @@
namespace App\Http\Controllers\Table;
use App\Models\Syslog;
use LibreNMS\Enum\SyslogSeverity;
class SyslogController extends TableController
{
@@ -71,17 +72,22 @@ class SyslogController extends TableController
{
return Syslog::hasAccess($request->user())
->with('device')
->when($request->device_group, function ($query) use ($request) {
$query->inDeviceGroup($request->device_group);
->when($request->device_group, function ($query, $group) {
$query->inDeviceGroup($group);
})
->when($request->from, function ($query) use ($request) {
$query->where('timestamp', '>=', $request->from);
->when($request->from, function ($query, $from) {
$query->where('timestamp', '>=', $from);
})
->when($request->to, function ($query) use ($request) {
$query->where('timestamp', '<=', $request->to);
->when($request->to, function ($query, $to) {
$query->where('timestamp', '<=', $to);
})
->when($request->level, function ($query) use ($request) {
$query->where('level', '<=', $request->level);
->when($request->level, function ($query, $level) {
if ($level >= 7) {
return; // include everything
}
$levels = array_slice(SyslogSeverity::LEVELS, 0, $level + 1);
$query->whereIn('level', $levels);
});
}

View File

@@ -5,6 +5,7 @@ namespace Database\Factories;
use App\Models\Syslog;
use Carbon\Carbon;
use Illuminate\Database\Eloquent\Factories\Factory;
use LibreNMS\Enum\SyslogSeverity;
/** @extends Factory<Syslog> */
class SyslogFactory extends Factory
@@ -24,7 +25,7 @@ class SyslogFactory extends Factory
public function definition()
{
$facilities = ['kern', 'user', 'mail', 'daemon', 'auth', 'syslog', 'lpr', 'news', 'uucp', 'cron', 'authpriv', 'ftp', 'ntp', 'security', 'console', 'solaris-cron', 'local0', 'local1', 'local2', 'local3', 'local4', 'local5', 'local6', 'local7'];
$levels = ['emerg', 'alert', 'crit', 'err', 'warning', 'notice', 'info', 'debug'];
$levels = SyslogSeverity::LEVELS;
return [
'facility' => $this->faker->randomElement($facilities),

View File

@@ -25,41 +25,6 @@ use LibreNMS\Util\IP;
use LibreNMS\Util\Laravel;
use Symfony\Component\Process\Process;
function generate_priority_status($priority)
{
$map = [
'emerg' => 2,
'alert' => 2,
'crit' => 2,
'err' => 2,
'warning' => 1,
'notice' => 0,
'info' => 0,
'debug' => 3,
'' => 0,
];
return isset($map[$priority]) ? $map[$priority] : 0;
}
function graylog_severity_label($severity)
{
$map = [
'0' => 'label-danger',
'1' => 'label-danger',
'2' => 'label-danger',
'3' => 'label-danger',
'4' => 'label-warning',
'5' => 'label-info',
'6' => 'label-info',
'7' => 'label-default',
'' => 'label-info',
];
$barColor = isset($map[$severity]) ? $map[$severity] : 'label-info';
return '<span class="alert-status ' . $barColor . '" style="margin-right:8px;float:left;"></span>';
}
/**
* Execute and snmp command, filter debug output unless -v is specified
*

View File

@@ -18,6 +18,8 @@
*/
use LibreNMS\Config;
use LibreNMS\Enum\CheckStatus;
use LibreNMS\Enum\SyslogSeverity;
$filter_hostname = $vars['hostname'];
$filter_range = $vars['range'];
@@ -67,6 +69,12 @@ $context = stream_context_create([
]);
$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')) {
@@ -81,8 +89,11 @@ foreach ($messages['messages'] as $message) {
$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' => graylog_severity_label($message['message']['level']) . $displayTime,
'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'],

View File

@@ -29,7 +29,7 @@
<label for="level-{{ $id }}" class="control-label">@lang('Priority')</label>
<select class="form-control" name="level" id="level-{{ $id }}">
@foreach($priorities as $val => $name)
<option value="{{ $val }}" @if($level == $val) selected @endif>{{ $name }}</option>
<option value="{{ $val }}" @if($level ? $level == $val : $val == 7) selected @endif>{{ $name }}</option>
@endforeach
</select>
</div>