Add severity filter to webui alert history (#10918)

* Add severity filter to webui alert history
* Selected options displayed by default
* Fix selected critical
* fix upper
* Fix CI
* fix code climate
This commit is contained in:
louis-oui
2019-12-16 23:58:54 +01:00
committed by PipoCanaja
parent 9ca6b4c0b8
commit 7d9770c9ff
4 changed files with 91 additions and 28 deletions

View File

@@ -1324,6 +1324,36 @@ function str_i_contains($haystack, $needles)
return false;
}
/**
* Get alert_rules sql filter by minimal severity
*
* @param string|int $min_severity
* @param string $alert_rules_name
* @return string
*/
function get_sql_filter_min_severity($min_severity, $alert_rules_name)
{
$alert_severities = array(
// alert_rules.status is enum('ok','warning','critical')
'ok' => 1,
'warning' => 2,
'critical' => 3,
'ok only' => 4,
'warning only' => 5,
'critical only' => 6,
);
if (is_numeric($min_severity)) {
$min_severity_id = $min_severity;
} elseif (!empty($min_severity)) {
$min_severity_id = $alert_severities[$min_severity];
}
if (isset($min_severity_id)) {
return " AND `$alert_rules_name`.`severity` " . ($min_severity_id > 3 ? "" : ">") . "= " . ($min_severity_id > 3 ? $min_severity_id - 3 : $min_severity_id);
}
return "";
}
if (!function_exists('ends_with')) {
/**
* Determine if a given string ends with a given substring.

View File

@@ -17,6 +17,26 @@ $param = array();
$pagetitle[] = 'Alert Log';
$alert_states = array(
// divined from librenms/alerts.php
'Any' => -1,
'Ok (recovered)' => 0,
'Alert' => 1,
// 'Acknowledged' => 2,
'Worse' => 3,
'Better' => 4,
);
$alert_severities = array(
// alert_rules.status is enum('ok','warning','critical')
'Any' => '',
'Ok, warning and critical' => 1,
'Warning and critical' => 2,
'Critical' => 3,
'OK' => 4,
'Warning' => 5
);
echo '<div class="panel panel-default panel-condensed">
<div class="panel-heading">
<div class="row">
@@ -31,17 +51,25 @@ echo '<div class="panel panel-default panel-condensed">
';
if (isset($_POST['device_id'])) {
$default_option = '<option value="' . (int)$_POST['device_id'] . '" selected="selected">';
$default_option .= htmlentities($_POST['hostname']) . '</option>';
$selected_device = '<option value="' . (int)$_POST['device_id'] . '" selected="selected">';
$selected_device .= htmlentities($_POST['hostname']) . '</option>';
} else {
$default_option = '';
$selected_device = '';
}
if (isset($_POST['state'])) {
$state = htmlspecialchars($_POST['state']);
$selected_state = '<option value="' . $_POST['state'] . '" selected="selected">';
$selected_state .= array_search((int)$_POST['state'], $alert_states) . '</option>';
} else {
$state = '-1';
$selected_state = '';
$_POST['state'] = -1;
}
if (isset($_POST['min_severity'])) {
$selected_min_severity = '<option value="' . $_POST['min_severity'] . '" selected="selected">';
$selected_min_severity .= array_search((int)$_POST['min_severity'], $alert_severities) . '</option>';
} else {
$selected_min_severity = '';
$_POST['min_severity'] = '';
}
?>
@@ -76,7 +104,7 @@ if (isset($_POST['state'])) {
<strong>Device&nbsp;</strong> \
</label> \
<select name="device_id" id="device_id" class="form-control input-sm" style="min-width: 175px;"> \
<?php echo $default_option; ?> \
<?php echo $selected_device; ?> \
</select> \
</div> \
<div class="form-group"> \
@@ -84,9 +112,26 @@ if (isset($_POST['state'])) {
<strong>&nbsp;State&nbsp;</strong> \
</label> \
<select name="state" id="state" class="form-control input-sm"> \
<option value="-1"></option> \
<option value="0">Ok</option> \
<?php echo $selected_state; ?> \
<option value="-1">Any</option> \
<option value="0">Ok (recovered)</option> \
<option value="1">Alert</option> \
<option value="3">Worse</option> \
<option value="4">Better</option> \
</select> \
</div> \
<div class="form-group"> \
<label> \
<strong>&nbsp;Severity&nbsp;</strong> \
</label> \
<select name="min_severity" id="min_severity" class="form-control input-sm"> \
<?php echo $selected_min_severity; ?> \
<option value>Any</option> \
<option value="3">Critical</option> \
<option value="5">Warning</option> \
<option value="4">Ok</option> \
<option value="2">Warning and critical</option> \
<option value="1">Ok, warning and critical</option> \
</select> \
</div> \
<button type="submit" class="btn btn-default input-sm">Filter</button> \
@@ -97,7 +142,8 @@ if (isset($_POST['state'])) {
return {
id: "alertlog",
device_id: '<?php echo htmlspecialchars($_POST['device_id']); ?>',
state: '<?php echo $state; ?>'
state: '<?php echo htmlspecialchars($_POST['state']); ?>',
min_severity: '<?php echo htmlspecialchars($_POST['min_severity']); ?>'
};
},
url: "ajax_table.php"

View File

@@ -25,6 +25,10 @@ if ($vars['state'] >= 0) {
$param[] = mres($vars['state']);
}
if (isset($vars['min_severity'])) {
$where .= get_sql_filter_min_severity($vars['min_severity'], "R");
}
if (Auth::user()->hasGlobalRead()) {
$sql = " FROM `alert_log` AS E LEFT JOIN devices AS D ON E.device_id=D.device_id RIGHT JOIN alert_rules AS R ON E.rule_id=R.id WHERE $where";
} else {

View File

@@ -24,16 +24,6 @@ $alert_states = array(
'better' => 4,
);
$alert_severities = array(
// alert_rules.status is enum('ok','warning','critical')
'ok' => 1,
'warning' => 2,
'critical' => 3,
'ok only' => 4,
'warning only' => 5,
'critical only' => 6,
);
$show_recovered = false;
if (is_numeric($vars['device_id']) && $vars['device_id'] > 0) {
@@ -57,14 +47,7 @@ if (is_numeric($vars['state'])) {
}
if (isset($vars['min_severity'])) {
if (is_numeric($vars['min_severity'])) {
$min_severity_id = $vars['min_severity'];
} elseif (!empty($vars['min_severity'])) {
$min_severity_id = $alert_severities[$vars['min_severity']];
}
if (isset($min_severity_id)) {
$where .= " AND `alert_rules`.`severity` " . ($min_severity_id > 3 ? "" : ">") . "= " . ($min_severity_id > 3 ? $min_severity_id - 3 : $min_severity_id);
}
$where .= get_sql_filter_min_severity($vars['min_severity'], "alert_rules");
}
if (is_numeric($vars['group'])) {