From 9272cb3b923ea56decacddc68b7cb932f7d8b6b4 Mon Sep 17 00:00:00 2001 From: SourceDoctor Date: Sat, 23 May 2020 03:08:05 +0200 Subject: [PATCH] Enumerate Alert Level (#11652) --- LibreNMS/Enum/Alert.php | 36 +++++++++++++++++++ app/Checks.php | 2 +- app/Facades/LogManager.php | 3 +- .../Controllers/Table/EventlogController.php | 11 +++--- app/Models/Eventlog.php | 3 +- includes/polling/functions.inc.php | 12 +++---- 6 files changed, 53 insertions(+), 14 deletions(-) create mode 100644 LibreNMS/Enum/Alert.php diff --git a/LibreNMS/Enum/Alert.php b/LibreNMS/Enum/Alert.php new file mode 100644 index 0000000000..ee99552dd5 --- /dev/null +++ b/LibreNMS/Enum/Alert.php @@ -0,0 +1,36 @@ +. + * + * @package LibreNMS + * @link http://librenms.org + * @copyright 2020 Thomas Berberich + * @author Thomas Berberich + */ + +namespace LibreNMS\Enum; + +abstract class Alert +{ + const UNKNOWN = 0; + const OK = 1; + const INFO = 2; + const NOTICE = 3; + const WARNING = 4; + const ERROR = 5; +} diff --git a/app/Checks.php b/app/Checks.php index 63014c6ec6..6dcdce4375 100644 --- a/app/Checks.php +++ b/app/Checks.php @@ -90,7 +90,7 @@ class Checks $user = Auth::user(); if ($user->isAdmin()) { - $notifications = Notification::isUnread($user)->where('severity', '>', 1)->get(); + $notifications = Notification::isUnread($user)->where('severity', '>', \LibreNMS\Enum\Alert::OK)->get(); foreach ($notifications as $notification) { Toastr::error("$notification->body", $notification->title); } diff --git a/app/Facades/LogManager.php b/app/Facades/LogManager.php index 7e6bd92054..7865748e00 100644 --- a/app/Facades/LogManager.php +++ b/app/Facades/LogManager.php @@ -26,6 +26,7 @@ namespace App\Facades; use Auth; +use LibreNMS\Enum\Alert; class LogManager extends \Illuminate\Log\LogManager { @@ -38,7 +39,7 @@ class LogManager extends \Illuminate\Log\LogManager * @param int $severity 1: ok, 2: info, 3: notice, 4: warning, 5: critical, 0: unknown * @param int $reference the id of the referenced entity. Supported types: interface */ - public function event($text, $device = null, $type = null, $severity = 2, $reference = null) + public function event($text, $device = null, $type = null, $severity = Alert::INFO, $reference = null) { (new \App\Models\Eventlog([ 'device_id' => $device instanceof \App\Models\Device ? $device->device_id : $device, diff --git a/app/Http/Controllers/Table/EventlogController.php b/app/Http/Controllers/Table/EventlogController.php index 8f4e744a17..e491ce33bb 100644 --- a/app/Http/Controllers/Table/EventlogController.php +++ b/app/Http/Controllers/Table/EventlogController.php @@ -29,6 +29,7 @@ use App\Models\Eventlog; use Carbon\Carbon; use LibreNMS\Config; use LibreNMS\Util\Url; +use LibreNMS\Enum\Alert; class EventlogController extends TableController { @@ -119,15 +120,15 @@ class EventlogController extends TableController private function severityLabel($eventlog_severity) { switch ($eventlog_severity) { - case 1: + case Alert::OK: return "label-success"; //OK - case 2: + case Alert::INFO: return "label-info"; //Informational - case 3: + case Alert::NOTICE: return "label-primary"; //Notice - case 4: + case Alert::WARNING: return "label-warning"; //Warning - case 5: + case Alert::ERROR: return "label-danger"; //Critical default: return "label-default"; //Unknown diff --git a/app/Models/Eventlog.php b/app/Models/Eventlog.php index e57d5e4d8d..6a3989be01 100644 --- a/app/Models/Eventlog.php +++ b/app/Models/Eventlog.php @@ -26,6 +26,7 @@ namespace App\Models; use Carbon\Carbon; +use LibreNMS\Enum\Alert; class Eventlog extends DeviceRelatedModel { @@ -45,7 +46,7 @@ class Eventlog extends DeviceRelatedModel * @param int $severity 1: ok, 2: info, 3: notice, 4: warning, 5: critical, 0: unknown * @param int $reference the id of the referenced entity. Supported types: interface */ - public static function log($text, $device = null, $type = null, $severity = 2, $reference = null) + public static function log($text, $device = null, $type = null, $severity = Alert::INFO, $reference = null) { $log = new static([ 'reference' => $reference, diff --git a/includes/polling/functions.inc.php b/includes/polling/functions.inc.php index 821b3d0f2a..6d73fba6be 100644 --- a/includes/polling/functions.inc.php +++ b/includes/polling/functions.inc.php @@ -3,6 +3,7 @@ use Illuminate\Support\Str; use LibreNMS\Config; use LibreNMS\RRD\RrdDefinition; +use LibreNMS\Enum\Alert; use LibreNMS\Exceptions\JsonAppException; use LibreNMS\Exceptions\JsonAppPollingFailedException; use LibreNMS\Exceptions\JsonAppParsingFailedException; @@ -592,26 +593,25 @@ function update_application($app, $response, $metrics = array(), $status = '') $app_name = \LibreNMS\Util\StringHelpers::nicecase($app['app_type']); - # $severity 1: ok, 2: info, 3: notice, 4: warning, 5: critical, 0: unknown switch ($data['app_state']) { case 'OK': - $severity = 1; + $severity = Alert::OK; $event_msg = "changed to OK"; break; case 'ERROR': - $severity = 5; + $severity = Alert::ERROR; $event_msg = "ends with ERROR"; break; case 'LEGACY': - $severity = 4; + $severity = Alert::WARNING; $event_msg = "Client Agent is deprecated"; break; case 'UNSUPPORTED': - $severity = 5; + $severity = Alert::ERROR; $event_msg = "Client Agent Version is not supported"; break; default: - $severity = 0; + $severity = Alert::UNKNOWN; $event_msg = "has UNKNOWN state"; break; }