Severity Enum (#14620)

* Severity Enum
Replace old Alert constants

* Fix whitespace

* Additional uses

* style fixes

* Fix test too

* More missed locations

* More
This commit is contained in:
Tony Murray
2023-08-05 12:12:36 -05:00
committed by GitHub
parent 9382959c8c
commit d1b7004fef
172 changed files with 591 additions and 473 deletions

View File

@@ -32,6 +32,7 @@ use Cache;
use Carbon\Carbon;
use Illuminate\Support\Facades\Auth;
use LibreNMS\Config;
use LibreNMS\Enum\Severity;
class Checks
{
@@ -51,7 +52,7 @@ class Checks
$user = Auth::user();
if ($user->isAdmin()) {
$notifications = Notification::isUnread($user)->where('severity', '>', \LibreNMS\Enum\Alert::OK)->get();
$notifications = Notification::isUnread($user)->where('severity', '>', Severity::Ok->value)->get();
foreach ($notifications as $notification) {
flash()
->using('template.librenms')

View File

@@ -6,6 +6,7 @@ use App\Models\Alert;
use App\Models\Eventlog;
use Illuminate\Http\Request;
use LibreNMS\Config;
use LibreNMS\Enum\Severity;
class AlertController extends Controller
{
@@ -41,7 +42,7 @@ class AlertController extends Controller
if ($alert->save()) {
if (in_array($state, [2, 22])) {
$rule_name = $alert->rule->name;
Eventlog::log("$username acknowledged alert $rule_name note: $ack_msg", $alert->device_id, 'alert', 2, $alert->id);
Eventlog::log("$username acknowledged alert $rule_name note: $ack_msg", $alert->device_id, 'alert', Severity::Info, $alert->id);
}
return response()->json([

View File

@@ -28,7 +28,7 @@ namespace App\Http\Controllers\Table;
use App\Models\Eventlog;
use Carbon\Carbon;
use LibreNMS\Config;
use LibreNMS\Enum\Alert;
use LibreNMS\Enum\Severity;
use LibreNMS\Util\Url;
class EventlogController extends TableController
@@ -124,26 +124,18 @@ class EventlogController extends TableController
}
/**
* @param int $eventlog_severity
* @param Severity $eventlog_severity
* @return string $eventlog_severity_icon
*/
private function severityLabel($eventlog_severity)
{
switch ($eventlog_severity) {
case Alert::OK:
return 'label-success'; //OK
case Alert::INFO:
return 'label-info'; //Informational
case Alert::NOTICE:
return 'label-primary'; //Notice
case Alert::WARNING:
return 'label-warning'; //Warning
case Alert::ERROR:
return 'label-danger'; //Critical
default:
return 'label-default'; //Unknown
}
return match ($eventlog_severity) {
Severity::Ok => 'label-success',
Severity::Info => 'label-info',
Severity::Notice => 'label-primary',
Severity::Warning => 'label-warning',
Severity::Error => 'label-danger',
default => 'label-default', // Unknown
};
}
// end eventlog_severity
}

View File

@@ -28,7 +28,7 @@ namespace App\Models;
use Carbon\Carbon;
use Illuminate\Database\Eloquent\Relations\MorphTo;
use Illuminate\Support\Facades\Auth;
use LibreNMS\Enum\Alert;
use LibreNMS\Enum\Severity;
class Eventlog extends DeviceRelatedModel
{
@@ -36,6 +36,9 @@ class Eventlog extends DeviceRelatedModel
protected $primaryKey = 'event_id';
public $timestamps = false;
protected $fillable = ['datetime', 'device_id', 'message', 'type', 'reference', 'username', 'severity'];
protected $casts = [
'severity' => Severity::class,
];
// ---- Helper Functions ----
/**
@@ -46,10 +49,10 @@ class Eventlog extends DeviceRelatedModel
* @param string $text message describing the event
* @param Device|int|null $device related device
* @param string $type brief category for this event. Examples: sensor, state, stp, system, temperature, interface
* @param int $severity 1: ok, 2: info, 3: notice, 4: warning, 5: critical, 0: unknown
* @param Severity $severity 1: ok, 2: info, 3: notice, 4: warning, 5: critical, 0: unknown
* @param int|string|null $reference the id of the referenced entity. Supported types: interface
*/
public static function log($text, $device = null, $type = null, $severity = Alert::INFO, $reference = null): void
public static function log($text, $device = null, $type = null, Severity $severity = Severity::Info, $reference = null): void
{
$model = app()->make(Eventlog::class);
$model->_log($text, $device, $type, $severity, $reference);
@@ -61,10 +64,10 @@ class Eventlog extends DeviceRelatedModel
* @param string $text
* @param Device|int|null $device
* @param string $type
* @param int $severity
* @param Severity $severity
* @param int|string|null $reference
*/
public function _log($text, $device = null, $type = null, $severity = Alert::INFO, $reference = null): void
public function _log($text, $device = null, $type = null, Severity $severity = Severity::Info, $reference = null): void
{
$log = new static([
'reference' => $reference,

View File

@@ -7,6 +7,7 @@ use App\ApiClients\Oxidized;
use App\Models\Device;
use App\Models\Eventlog;
use File;
use LibreNMS\Enum\Severity;
use Log;
class DeviceObserver
@@ -19,7 +20,7 @@ class DeviceObserver
*/
public function created(Device $device): void
{
Eventlog::log("Device $device->hostname has been created", $device, 'system', 3);
Eventlog::log("Device $device->hostname has been created", $device, 'system', Severity::Notice);
(new Oxidized)->reloadNodes();
}
@@ -46,11 +47,11 @@ class DeviceObserver
// key attribute changes
foreach (['os', 'sysName', 'version', 'hardware', 'features', 'serial', 'icon', 'type', 'ip'] as $attribute) {
if ($device->isDirty($attribute)) {
Eventlog::log(self::attributeChangedMessage($attribute, $device->$attribute, $device->getOriginal($attribute)), $device, 'system', 3);
Eventlog::log(self::attributeChangedMessage($attribute, $device->$attribute, $device->getOriginal($attribute)), $device, 'system', Severity::Notice);
}
}
if ($device->isDirty('location_id')) {
Eventlog::log(self::attributeChangedMessage('location', (string) $device->location, null), $device, 'system', 3);
Eventlog::log(self::attributeChangedMessage('location', (string) $device->location, null), $device, 'system', Severity::Notice);
}
}
@@ -71,7 +72,7 @@ class DeviceObserver
Log::error("Could not delete RRD files for: $device->hostname", [$e]);
}
Eventlog::log("Device $device->hostname has been removed", 0, 'system', 3);
Eventlog::log("Device $device->hostname has been removed", 0, 'system', Severity::Notice);
(new Oxidized)->reloadNodes();
}

View File

@@ -4,6 +4,7 @@ namespace App\Observers;
use App\Models\Eventlog;
use App\Models\Package;
use LibreNMS\Enum\Severity;
use Log;
class PackageObserver
@@ -16,7 +17,7 @@ class PackageObserver
*/
public function created(Package $package): void
{
Eventlog::log('Package installed: ' . $package, $package->device_id, 'package', 3);
Eventlog::log('Package installed: ' . $package, $package->device_id, 'package', Severity::Notice);
Log::info("+ $package");
}
@@ -30,7 +31,7 @@ class PackageObserver
{
if ($package->getOriginal('version') !== $package->version || $package->getOriginal('build') !== $package->build) {
$message = $package . ' from ' . $package->getOriginal('version') . ($package->getOriginal('build') ? '-' . $package->getOriginal('build') : '');
Eventlog::log('Package updated: ' . $message, $package->device_id, 'package', 3);
Eventlog::log('Package updated: ' . $message, $package->device_id, 'package', Severity::Notice);
Log::info("u $message");
}
}
@@ -43,7 +44,7 @@ class PackageObserver
*/
public function deleted(Package $package): void
{
Eventlog::log('Package removed: ' . $package, $package->device_id, 'package', 3);
Eventlog::log('Package removed: ' . $package, $package->device_id, 'package', Severity::Notice);
Log::info("- $package");
}

View File

@@ -4,6 +4,7 @@ namespace App\Observers;
use App\Models\Eventlog;
use App\Models\Stp;
use LibreNMS\Enum\Severity;
use LibreNMS\Util\Time;
class StpObserver
@@ -17,16 +18,16 @@ class StpObserver
public function updating(Stp $stp)
{
if ($stp->isDirty('designatedRoot')) {
Eventlog::log('STP designated root changed: ' . $stp->getOriginal('designatedRoot') . ' > ' . $stp->designatedRoot, $stp->device_id, 'stp', 4);
Eventlog::log('STP designated root changed: ' . $stp->getOriginal('designatedRoot') . ' > ' . $stp->designatedRoot, $stp->device_id, 'stp', Severity::Warning);
}
if ($stp->isDirty('rootPort')) {
Eventlog::log('STP root port changed: ' . $stp->getOriginal('rootPort') . ' > ' . $stp->rootPort, $stp->device_id, 'stp', 4);
Eventlog::log('STP root port changed: ' . $stp->getOriginal('rootPort') . ' > ' . $stp->rootPort, $stp->device_id, 'stp', Severity::Warning);
}
if ($stp->isDirty('rootPort')) {
$time = Time::formatInterval((int) $stp->timeSinceTopologyChange);
Eventlog::log('STP topology changed after: ' . $time, $stp->device_id, 'stp', 4);
Eventlog::log('STP topology changed after: ' . $time, $stp->device_id, 'stp', Severity::Warning);
}
}
}