Use display name in alerts (#13650)

* Use display name in alerts

* remove error from baseline
This commit is contained in:
Tony Murray
2022-01-04 17:40:42 -06:00
committed by GitHub
parent 785b0bae53
commit 3c0831c821
4 changed files with 30 additions and 50 deletions

View File

@@ -30,10 +30,11 @@
namespace LibreNMS\Alert;
use App\Models\DevicePerf;
use App\Facades\DeviceCache;
use LibreNMS\Config;
use LibreNMS\Enum\Alert;
use LibreNMS\Enum\AlertState;
use LibreNMS\Polling\ConnectivityHelper;
use LibreNMS\Util\Time;
use Log;
@@ -85,41 +86,41 @@ class RunAlerts
{
$obj = [];
$i = 0;
$device = dbFetchRow('SELECT hostname, sysName, sysDescr, sysContact, os, type, ip, hardware, version, serial, features, purpose, notes, uptime, status, status_reason, locations.location FROM devices LEFT JOIN locations ON locations.id = devices.location_id WHERE device_id = ?', [$alert['device_id']]);
$attribs = get_dev_attribs($alert['device_id']);
$device = DeviceCache::get($alert['device_id']);
$obj['hostname'] = $device['hostname'];
$obj['sysName'] = $device['sysName'];
$obj['sysDescr'] = $device['sysDescr'];
$obj['sysContact'] = $device['sysContact'];
$obj['os'] = $device['os'];
$obj['type'] = $device['type'];
$obj['ip'] = inet6_ntop($device['ip']);
$obj['hardware'] = $device['hardware'];
$obj['version'] = $device['version'];
$obj['serial'] = $device['serial'];
$obj['features'] = $device['features'];
$obj['location'] = $device['location'];
$obj['uptime'] = $device['uptime'];
$obj['uptime_short'] = Time::formatInterval($device['uptime'], 'short');
$obj['uptime_long'] = Time::formatInterval($device['uptime']);
$obj['description'] = $device['purpose'];
$obj['notes'] = $device['notes'];
$obj['hostname'] = $device->hostname;
$obj['sysName'] = $device->sysName;
$obj['display'] = $device->displayName();
$obj['sysDescr'] = $device->sysDescr;
$obj['sysContact'] = $device->sysContact;
$obj['os'] = $device->os;
$obj['type'] = $device->type;
$obj['ip'] = $device->ip;
$obj['hardware'] = $device->hardware;
$obj['version'] = $device->version;
$obj['serial'] = $device->serial;
$obj['features'] = $device->features;
$obj['location'] = (string) $device->location;
$obj['uptime'] = $device->uptime;
$obj['uptime_short'] = Time::formatInterval($device->uptime, 'short');
$obj['uptime_long'] = Time::formatInterval($device->uptime);
$obj['description'] = $device->purpose;
$obj['notes'] = $device->notes;
$obj['alert_notes'] = $alert['note'];
$obj['device_id'] = $alert['device_id'];
$obj['device_id'] = $device->device_id;
$obj['rule_id'] = $alert['rule_id'];
$obj['id'] = $alert['id'];
$obj['proc'] = $alert['proc'];
$obj['status'] = $device['status'];
$obj['status_reason'] = $device['status_reason'];
if (can_ping_device($attribs)) {
$ping_stats = DevicePerf::where('device_id', $alert['device_id'])->latest('timestamp')->first();
$obj['status'] = $device->status;
$obj['status_reason'] = $device->status_reason;
if ((new ConnectivityHelper($device))->canPing()) {
$ping_stats = $device->perf()->latest('timestamp')->first();
$obj['ping_timestamp'] = $ping_stats->timestamp;
$obj['ping_loss'] = $ping_stats->loss;
$obj['ping_min'] = $ping_stats->min;
$obj['ping_max'] = $ping_stats->max;
$obj['ping_avg'] = $ping_stats->avg;
$obj['debug'] = json_decode($ping_stats->debug, true);
$obj['debug'] = $ping_stats->debug;
}
$extra = $alert['details'];
@@ -127,7 +128,7 @@ class RunAlerts
$template = $tpl->getTemplate($obj);
if ($alert['state'] >= AlertState::ACTIVE) {
$obj['title'] = $template->title ?: 'Alert for device ' . $device['hostname'] . ' - ' . ($alert['name'] ? $alert['name'] : $alert['rule']);
$obj['title'] = $template->title ?: 'Alert for device ' . $obj['display'] . ' - ' . ($alert['name'] ?: $alert['rule']);
if ($alert['state'] == AlertState::ACKNOWLEDGED) {
$obj['title'] .= ' got acknowledged';
} elseif ($alert['state'] == AlertState::WORSE) {
@@ -166,7 +167,7 @@ class RunAlerts
$extra['count'] = 0;
dbUpdate(['details' => gzcompress(json_encode($id['details']), 9)], 'alert_log', 'id = ?', [$alert['id']]);
$obj['title'] = $template->title_rec ?: 'Device ' . $device['hostname'] . ' recovered from ' . ($alert['name'] ? $alert['name'] : $alert['rule']);
$obj['title'] = $template->title_rec ?: 'Device ' . $obj['display'] . ' recovered from ' . ($alert['name'] ?: $alert['rule']);
$obj['elapsed'] = $this->timeFormat(strtotime($alert['time_logged']) - strtotime($id['time_logged']));
$obj['id'] = $id['id'];
foreach ($extra['rule'] as $incident) {

View File

@@ -47,6 +47,7 @@ been up for 30344 seconds`.
- Hostname of the Device: `$alert->hostname`
- sysName of the Device: `$alert->sysName`
- sysDescr of the Device: `$alert->sysDescr`
- display name of the Device: `$alert->display`
- sysContact of the Device: `$alert->sysContact`
- OS of the Device: `$alert->os`
- Type of Device: `$alert->type`

View File

@@ -532,23 +532,6 @@ function object_is_cached($section, $obj)
}
} // object_is_cached
/**
* Checks if config allows us to ping this device
* $attribs contains an array of all of this devices
* attributes
*
* @param array $attribs Device attributes
* @return bool
**/
function can_ping_device($attribs)
{
if (Config::get('icmp_check') && ! (isset($attribs['override_icmp_disable']) && $attribs['override_icmp_disable'] == 'true')) {
return true;
} else {
return false;
}
} // end can_ping_device
function search_phrase_column($c)
{
global $searchPhrase;

View File

@@ -55,11 +55,6 @@ parameters:
count: 1
path: LibreNMS/Alert/RunAlerts.php
-
message: "#^Parameter \\#1 \\$json of function json_decode expects string, array\\|null given\\.$#"
count: 1
path: LibreNMS/Alert/RunAlerts.php
-
message: "#^Method LibreNMS\\\\Alert\\\\Template\\:\\:getBody\\(\\) has no return type specified\\.$#"
count: 1