Skip to content

Commit 87a9fea

Browse files
murrantlaf
andauthored
Alert UI fixes (librenms#17821)
* Handle non-fatal errors for error tests * Alert webui error fixes Some may be risky, but I did do some testing # Conflicts: # includes/html/table/alerts.inc.php * whitespace * Update edit_alert_transport.inc.php --------- Co-authored-by: Neil Lathwood <[email protected]>
1 parent c776f98 commit 87a9fea

17 files changed

+66
-38
lines changed

LibreNMS/Alert/Transport.php

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@
1111

1212
abstract class Transport implements TransportInterface
1313
{
14-
protected ?array $config;
14+
protected array $config;
1515
protected string $name = '';
1616

1717
public static function make(string $type): TransportInterface
@@ -41,7 +41,7 @@ public static function list(): array
4141

4242
public function __construct(?AlertTransport $transport = null)
4343
{
44-
$this->config = $transport ? $transport->transport_config : [];
44+
$this->config = (array) ($transport ? $transport->transport_config : []);
4545
}
4646

4747
/**
@@ -114,7 +114,7 @@ public function displayDetails(): string
114114
continue;
115115
}
116116

117-
$val = $this->config[$item['name']];
117+
$val = $this->config[$item['name']] ?? '';
118118
if ($item['type'] == 'password') {
119119
$val = '********';
120120
} elseif ($item['type'] == 'select') {

LibreNMS/Util/Exceptions.php

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
<?php
2+
3+
namespace LibreNMS\Util;
4+
5+
use ErrorException;
6+
7+
class Exceptions
8+
{
9+
private const FATAL_ERROR_MASK = E_ERROR | E_PARSE | E_CORE_ERROR | E_COMPILE_ERROR | E_USER_ERROR | E_RECOVERABLE_ERROR;
10+
11+
public static function isFatalError(ErrorException $e): bool
12+
{
13+
return (bool) ($e->getSeverity() & self::FATAL_ERROR_MASK);
14+
}
15+
}

app/Http/Controllers/AlertTransportController.php

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
use Illuminate\Http\Request;
88
use LibreNMS\Alert\AlertData;
99
use LibreNMS\Exceptions\AlertTransportDeliveryException;
10+
use LibreNMS\Util\Exceptions;
1011

1112
class AlertTransportController extends Controller
1213
{
@@ -30,6 +31,14 @@ public function test(Request $request, AlertTransport $transport): \Illuminate\H
3031
} catch (\Exception $e) {
3132
\Log::error($e);
3233
$result = basename($e->getFile(), '.php') . ':' . $e->getLine() . ' ' . $e->getMessage();
34+
35+
// if non-fatal error, return ok
36+
if ($e instanceof \ErrorException && ! Exceptions::isFatalError($e)) {
37+
return response()->json([
38+
'status' => 'ok',
39+
'message' => strip_tags($result),
40+
]);
41+
}
3342
}
3443

3544
return response()->json([

html/ajax_list.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@
2222
exit('Unauthorized');
2323
}
2424

25-
Debug::set($_REQUEST['debug']);
25+
Debug::set($_REQUEST['debug'] ?? false);
2626

2727
$type = basename($_REQUEST['type']);
2828

includes/html/common/alert-log.inc.php

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,8 @@
4343
$admin_verbose_details = '<th data-column-id="verbose_details" data-sortable="false">Details</th>';
4444
}
4545

46+
$device_id = (int) ($vars['device_id'] ?? 0);
47+
4648
$common_output[] = '<div class="panel panel-default panel-condensed">
4749
<div class="panel-heading">
4850
<div class="row">
@@ -56,7 +58,7 @@
5658
</div>
5759
';
5860

59-
$device = DeviceCache::get((int) $vars['device_id']);
61+
$device = DeviceCache::get($device_id);
6062
$device_selected = json_encode($device->exists ? ['id' => $device->device_id, 'text' => $device->displayName()] : '');
6163

6264
if (isset($_POST['state'])) {
@@ -150,7 +152,7 @@
150152
post: function () {
151153
return {
152154
id: "alertlog",
153-
device_id: \'' . htmlspecialchars($_POST['device_id'] ?? $device_id) . '\',
155+
device_id: \'' . htmlspecialchars($device_id) . '\',
154156
state: \'' . htmlspecialchars($_POST['state']) . '\',
155157
min_severity: \'' . htmlspecialchars($_POST['min_severity']) . '\'
156158
};
@@ -164,7 +166,7 @@
164166
max = high - low;
165167
search = $(\'.search-field\').val();
166168
167-
$(".pdf-export").html("<a href=\'pdf.php?report=alert-log&device_id=' . htmlspecialchars($_POST['device_id']) . '&string=" + search + "&results=" + max + "&start=" + low + "\'><i class=\'fa fa-file-pdf-o fa-lg icon-theme\' aria-hidden=\'true\'></i> Export to PDF</a>");
169+
$(".pdf-export").html("<a href=\'pdf.php?report=alert-log&device_id=' . htmlspecialchars($device_id) . '&string=" + search + "&results=" + max + "&start=" + low + "\'><i class=\'fa fa-file-pdf-o fa-lg icon-theme\' aria-hidden=\'true\'></i> Export to PDF</a>");
168170
169171
grid.find(".incident-toggle").each(function () {
170172
$(this).parent().addClass(\'incident-toggle-td\');

includes/html/forms/alert-rules.inc.php

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,7 @@
4040
$message = '';
4141

4242
$builder_json = $vars['builder_json'];
43-
$override_query = $vars['override_query'];
43+
$override_query = $vars['override_query'] ?? null;
4444

4545
$options = [
4646
'override_query' => $override_query,
@@ -93,7 +93,7 @@
9393

9494
$extra_json = json_encode($extra);
9595

96-
if (! is_array($vars['maps']) && $invert_map) {
96+
if (isset($vars['maps']) && ! is_array($vars['maps']) && $invert_map) {
9797
exit(json_encode([
9898
'status' => 'error',
9999
'message' => 'Invert map is on but no selection in devices, groups and locations match list<br />',
@@ -160,7 +160,7 @@
160160
$devices = [];
161161
$groups = [];
162162
$locations = [];
163-
foreach ((array) $vars['maps'] as $item) {
163+
foreach ((array) ($vars['maps'] ?? []) as $item) {
164164
if (Str::startsWith($item, 'l')) {
165165
$locations[] = (int) substr($item, 1);
166166
} elseif (Str::startsWith($item, 'g')) {
@@ -177,7 +177,7 @@
177177
//Update transport groups and transports - can't use dbSyncRelationship
178178
$transports = [];
179179
$groups = [];
180-
foreach ((array) $vars['transports'] as $item) {
180+
foreach ((array) ($vars['transports'] ?? []) as $item) {
181181
if (Str::startsWith($item, 'g')) {
182182
$groups[] = (int) substr($item, 1);
183183
} else {

includes/html/forms/alert-transports.inc.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -84,7 +84,7 @@
8484
$transport_config = (array) json_decode(dbFetchCell('SELECT transport_config FROM alert_transports WHERE transport_id=?', [$transport_id]), true);
8585
foreach ($result['config'] as $tmp_config) {
8686
if (isset($tmp_config['name']) && $tmp_config['type'] !== 'hidden') {
87-
$transport_config[$tmp_config['name']] = $vars[$tmp_config['name']];
87+
$transport_config[$tmp_config['name']] = $vars[$tmp_config['name']] ?? null;
8888
}
8989
}
9090
//Update the json config field

includes/html/forms/parse-alert-rule.inc.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@
1919
exit('ERROR: You need to be admin');
2020
}
2121
$alert_id = $vars['alert_id'];
22-
$template_id = $vars['template_id'];
22+
$template_id = $vars['template_id'] ?? null;
2323

2424
if (is_numeric($alert_id) && $alert_id > 0) {
2525
$rule = dbFetchRow('SELECT * FROM `alert_rules` WHERE `id` = ? LIMIT 1', [$alert_id]);

includes/html/forms/schedule-maintenance.inc.php

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -35,16 +35,15 @@
3535

3636
$title = $_POST['title'];
3737
$notes = $_POST['notes'];
38-
$recurring = $_POST['recurring'] ? 1 : 0;
38+
$recurring = isset($_POST['recurring']) ? 1 : 0;
3939
$start_recurring_dt = $_POST['start_recurring_dt'];
4040
$end_recurring_dt = $_POST['end_recurring_dt'];
4141
$start_recurring_hr = $_POST['start_recurring_hr'];
4242
$end_recurring_hr = $_POST['end_recurring_hr'];
43-
$recurring_day = $_POST['recurring_day'];
4443
$start = $_POST['start'];
45-
[$duration_hour, $duration_min] = explode(':', $_POST['duration']);
44+
[$duration_hour, $duration_min] = isset($_POST['duration']) ? explode(':', $_POST['duration']) : [null, null];
4645
$end = $_POST['end'];
47-
$maps = $_POST['maps'];
46+
$maps = $_POST['maps'] ?? null;
4847

4948
if (isset($duration_hour) && isset($duration_min)) {
5049
$end = date('Y-m-d H:i:00', strtotime('+' . intval($duration_hour) . ' hour ' . intval($duration_min) . ' minute', strtotime($start)));
@@ -111,7 +110,7 @@
111110
$end_recurring_hr = '00:00:00';
112111
}
113112

114-
if (! is_array($_POST['maps'])) {
113+
if (! is_array($maps)) {
115114
$message .= 'Not mapped to any groups or devices<br />';
116115
}
117116

@@ -140,7 +139,7 @@
140139
dbDelete('alert_schedulables', '`schedule_id`=?', [$alert_schedule->schedule_id]);
141140
}
142141

143-
foreach ($_POST['maps'] as $target) {
142+
foreach ($maps as $target) {
144143
$type = 'device';
145144
if (Str::startsWith($target, 'l')) {
146145
$type = 'location';

includes/html/forms/sql-from-alert-collection.inc.php

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -53,10 +53,10 @@
5353
$output = [
5454
'status' => 'ok',
5555
'name' => $rule['name'],
56-
'notes' => $rule['notes'],
57-
'builder' => $rule['builder'] ?: QueryBuilderParser::fromOld($rule['rule'])->toArray(),
58-
'extra' => array_replace($default_extra, (array) $rule['extra']),
59-
'severity' => $rule['severity'] ?: Config::get('alert_rule.severity'),
56+
'notes' => $rule['notes'] ?? null,
57+
'builder' => $rule['builder'] ?? QueryBuilderParser::fromOld($rule['rule'])->toArray(),
58+
'extra' => array_replace($default_extra, (array) ($rule['extra'] ?? [])),
59+
'severity' => $rule['severity'] ?? Config::get('alert_rule.severity'),
6060
'invert_map' => Config::get('alert_rule.invert_map'),
6161
];
6262
} else {

0 commit comments

Comments
 (0)