Skip to content

Commit

Permalink
Merge pull request #2834 from ekoyle/alerts_widget_groups
Browse files Browse the repository at this point in the history
Add group filtering to alerts widget
  • Loading branch information
f0o committed Jan 23, 2016
2 parents 09acef9 + 5b074cb commit a9bb52d
Show file tree
Hide file tree
Showing 2 changed files with 88 additions and 4 deletions.
66 changes: 64 additions & 2 deletions html/includes/common/alerts.inc.php
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
<?php

require_once $config['install_dir'].'/includes/device-groups.inc.php';

/* FIXME: is there a central place we can put this? */

$alert_states = array(
Expand All @@ -23,6 +25,7 @@
$current_acknowledged = isset($widget_settings['acknowledged']) ? $widget_settings['acknowledged'] : '';
$current_severity = isset($widget_settings['severity']) ? $widget_settings['severity'] : '';
$current_state = isset($widget_settings['state']) ? $widget_settings['state'] : '';
$current_group = isset($widget_settings['group']) ? $widget_settings['group'] : '';

$common_output[] = '
<form class="form" onsubmit="widget_settings(this); return false;">
Expand Down Expand Up @@ -73,6 +76,25 @@
</select>
</div>
</div>
<div class="form-group row">
<div class="col-sm-4">
<label for="group" class="control-label">Device Group:</label>
</div>
<div class="col-sm-8">
<select class="form-control" name="group">';
$common_output[] = '<option value=""'.($current_group == '' ? ' selected' : '').'>any group</option>';

$device_groups = GetDeviceGroups();
$common_output[] = "<!-- ".print_r($device_groups, true)." -->";
foreach ($device_groups as $group) {
$group_id = $group['id'];
$common_output[] = "<option value=\"$group_id\"".(is_numeric($current_group) && $current_group == $group_id ? ' selected' : '').">".$group['name']." - ".$group['description']."</option>";
}
$common_output[] = '
</select>
</div>
</div>
<div class="form-group">
<div class="col-sm-12">
<button type="submit" class="btn btn-default">Set</button>
Expand All @@ -86,10 +108,46 @@
$acknowledged = $widget_settings['acknowledged'];
$state = $widget_settings['state'];
$min_severity = $widget_settings['min_severity'];
$group = $widget_settings['group'];

$title = "Alerts";

// state can be 0 or '', be sure they are treated differently
if (is_numeric($state)) {
$state_name = array_search($state, $alert_states);
$title = "$title ($state_name)";
}
elseif ($state) {
$title = "$title ($state)";
}

if (is_numeric($acknowledged)) {
if ($acknowledged == '0') {
$title = "Unacknowledged $title";
}
elseif ($acknowledged == '1') {
$title = "Acknowledged $title";
}
}

if (is_numeric($group)) {
$group_row = dbFetchRow("SELECT * FROM device_groups WHERE id = ?", array($group));
if ($group_row) {
$title = "$title for ".$group_row['name'];
}
}

$common_output[] = "<!-- ".print_r($widget_settings, TRUE)." -->";
$common_output[] = "<!-- ".print_r($acknowledged, TRUE)." -->";
if ($min_severity) {
$sev_name = $min_severity;
if (is_numeric($min_severity)) {
$sev_name = array_search($min_severity, $alert_severities);
$title = "$title >=$sev_name";
}
}

$widget_settings['title'] = $title;

$group = $widget_settings['group'];

$common_output[] = '
<div class="row">
Expand Down Expand Up @@ -131,6 +189,10 @@
$common_output[]="min_severity: '$min_severity',\n";
}

if (is_numeric($group)) {
$common_output[]="group: '$group',\n";
}

$common_output[]='
device_id: \'' . $device['device_id'] .'\'
}
Expand Down
26 changes: 24 additions & 2 deletions html/includes/table/alerts.inc.php
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
<?php

require_once $config['install_dir'].'/includes/device-groups.inc.php';

$where = 1;

$alert_states = array(
Expand All @@ -18,6 +20,8 @@
'critical' => 3
);

$show_recovered = FALSE;

if (is_numeric($_POST['device_id']) && $_POST['device_id'] > 0) {
$where .= ' AND `alerts`.`device_id`='.$_POST['device_id'];
}
Expand All @@ -29,6 +33,9 @@

if (is_numeric($_POST['state'])) {
$where .= " AND `alerts`.`state`=".$_POST['state'];
if ($_POST['state'] == $alert_states['recovered']) {
$show_recovered = TRUE;
}
}

if (isset($_POST['min_severity'])) {
Expand All @@ -43,8 +50,23 @@
}
}

if (is_numeric($_POST['group'])) {
$group_pattern = dbFetchCell('SELECT `pattern` FROM `device_groups` WHERE id = '.$_POST['group']);
$group_pattern = rtrim($group_pattern, '&&');
$group_pattern = rtrim($group_pattern, '||');

$device_id_sql = GenGroupSQL($group_pattern);
if ($device_id_sql) {
$where .= " AND devices.device_id IN ($device_id_sql)";
}
}

if (!$show_recovered) {
$where .= " AND `alerts`.`state`!=".$alert_states['recovered'];
}

if (isset($searchPhrase) && !empty($searchPhrase)) {
$sql_search .= " AND (`timestamp` LIKE '%$searchPhrase%' OR `rule` LIKE '%$searchPhrase%' OR `name` LIKE '%$searchPhrase%' OR `hostname` LIKE '%$searchPhrase%')";
$where .= " AND (`timestamp` LIKE '%$searchPhrase%' OR `rule` LIKE '%$searchPhrase%' OR `name` LIKE '%$searchPhrase%' OR `hostname` LIKE '%$searchPhrase%')";
}

$sql = ' FROM `alerts` LEFT JOIN `devices` ON `alerts`.`device_id`=`devices`.`device_id`';
Expand All @@ -55,7 +77,7 @@
$param[] = $_SESSION['user_id'];
}

$sql .= " RIGHT JOIN `alert_rules` ON `alerts`.`rule_id`=`alert_rules`.`id` WHERE $where AND `alerts`.`state`!=".$alert_states['recovered']." $sql_search";
$sql .= " RIGHT JOIN `alert_rules` ON `alerts`.`rule_id`=`alert_rules`.`id` WHERE $where";

$count_sql = "SELECT COUNT(`alerts`.`id`) $sql";
$total = dbFetchCell($count_sql, $param);
Expand Down

0 comments on commit a9bb52d

Please sign in to comment.