diff --git a/includes/common.php b/includes/common.php index cfdd0c6863..d434ab67cc 100644 --- a/includes/common.php +++ b/includes/common.php @@ -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. diff --git a/includes/html/pages/alert-log.inc.php b/includes/html/pages/alert-log.inc.php index d585fb2a41..664a37fb24 100644 --- a/includes/html/pages/alert-log.inc.php +++ b/includes/html/pages/alert-log.inc.php @@ -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 '
@@ -31,17 +51,25 @@ echo '
'; if (isset($_POST['device_id'])) { - $default_option = ''; + $selected_device = ''; } else { - $default_option = ''; + $selected_device = ''; } if (isset($_POST['state'])) { - $state = htmlspecialchars($_POST['state']); + $selected_state = ''; } else { - $state = '-1'; + $selected_state = ''; + $_POST['state'] = -1; +} +if (isset($_POST['min_severity'])) { + $selected_min_severity = ''; +} else { + $selected_min_severity = ''; + $_POST['min_severity'] = ''; } - ?> @@ -76,7 +104,7 @@ if (isset($_POST['state'])) { Device  \ \ \
\
\ @@ -84,9 +112,26 @@ if (isset($_POST['state'])) {  State  \ \ \ +
\ +
\ + \ + \
\ \ @@ -97,7 +142,8 @@ if (isset($_POST['state'])) { return { id: "alertlog", device_id: '', - state: '' + state: '', + min_severity: '' }; }, url: "ajax_table.php" diff --git a/includes/html/table/alertlog.inc.php b/includes/html/table/alertlog.inc.php index d86e2f20df..0d796878e5 100644 --- a/includes/html/table/alertlog.inc.php +++ b/includes/html/table/alertlog.inc.php @@ -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 { diff --git a/includes/html/table/alerts.inc.php b/includes/html/table/alerts.inc.php index 074a7920db..fa45959a11 100644 --- a/includes/html/table/alerts.inc.php +++ b/includes/html/table/alerts.inc.php @@ -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'])) {