Renovated Alert Rules (#11115)

* Change select order from id to name

* added example icmp/snmp down rules given by @kkrumm1

* Renovated Alert Rules

* Defaulted sort by name
* Moved top buttons and results selector outside of table and aligned them with pull-left and pull-right
* Collapsed '#' (ID) into 'Type' and added titles for the icons
* Added Devices and Transports columns for each rule
* Moved Extra column next to transports
* Added icons for Enabled when a user does not have global admin
* Changed row_# variable to rule_id_#
* Some 'else' cleanup
* Added various title tags for more information when hovering
* Moved pagination outside of table and align it with pull-left and added a bootgrid style summary pulled-right
* Added table & th tags for bootgrid (but didn't turn it on)

* code climate, round 1

* code climate, round 2

* add hrefs for device & device group edit

* added trailing slash for device-groups/../edit/

* prevent #name conflict with transport modal

* add hrefs for transport & transport group edit

* use popover consistently

* code climate, round 3

* removed unused variables

* code climate, round 4

* popover variables

* reload after successful delete

* more informative feedback

* use toastr, not #message, & don't reload

* added license header & fail faster if not admin

* use (more informative) ajax error message

* delete confirmation with alert name in the modal

* print each device per line

* add href for all devices

* refresh status & enabled data-content when/if a rule is turned on/off

* use DRY style per @SourceDoctor (& my) preference); codeclimate meh

* point devices popover to the right
This commit is contained in:
Joseph Tingiris
2020-02-09 20:06:12 -05:00
committed by GitHub
parent 06b9ace0fb
commit c2dfe7b21c
5 changed files with 294 additions and 89 deletions

View File

@@ -1,11 +1,38 @@
<?php
/**
* print-alert-rules.inc.php
*
* LibreNMS print alert rules table
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*
* @package LibreNMS
* @link http://librenms.org
* @copyright 2020 The LibreNMS Community
* @author Original Author <unknown>
* @author Joseph Tingiris <joseph.tingiris@gmail.com>
*/
if (!Auth::user()->hasGlobalAdmin()) {
die('ERROR: You need to be admin');
}
use LibreNMS\Alerting\QueryBuilderParser;
$no_refresh = true;
?>
<div class="row">
<div class="col-sm-12">
<span id="message"></span>
@@ -48,11 +75,13 @@ if (isset($_POST['create-default'])) {
}
require_once 'includes/html/modal/new_alert_rule.inc.php';
require_once 'includes/html/modal/delete_alert_rule.inc.php';
require_once 'includes/html/modal/alert_rule_collection.inc.php';
?>
<form method="post" action="" id="result_form">
<?php
require_once 'includes/html/modal/delete_alert_rule.inc.php'; // Also dies if !Auth::user()->hasGlobalAdmin()
require_once 'includes/html/modal/alert_rule_collection.inc.php'; // Also dies if !Auth::user()->hasGlobalAdmin()
require_once 'includes/html/modal/edit_transport_group.inc.php';
require_once 'includes/html/modal/edit_alert_transport.inc.php';
echo '<form method="post" action="" id="result_form">';
echo csrf_field();
if (isset($_POST['results_amount']) && $_POST['results_amount'] > 0) {
$results = $_POST['results'];
@@ -60,27 +89,15 @@ if (isset($_POST['results_amount']) && $_POST['results_amount'] > 0) {
$results = 50;
}
echo '<div class="table-responsive">
<table class="table table-hover table-condensed" width="100%">
<tr>
<th>#</th>
<th>Name</th>
<th>Rule</th>
<th>Severity</th>
<th>Status</th>
<th>Extra</th>
<th>Enabled</th>
<th style="width:86px;">Action</th>
</tr>';
echo '<div class="table-responsive">';
echo '<div class="col pull-left">';
echo '<button type="button" class="btn btn-primary btn-sm" data-toggle="modal" data-target="#create-alert" data-device_id="'.$device['device_id'].'"><i class="fa fa-plus"></i> Create new alert rule</button>';
echo '<i> - OR - </i>';
echo '<button type="button" class="btn btn-primary btn-sm" data-toggle="modal" data-target="#search_rule_modal" data-device_id="'.$device['device_id'].'"><i class="fa fa-plus"></i> Create rule from collection</button>';
echo '</div>';
echo '<td colspan="7">';
if (Auth::user()->hasGlobalAdmin()) {
echo '<button type="button" class="btn btn-primary btn-sm" data-toggle="modal" data-target="#create-alert" data-device_id="'.$device['device_id'].'"><i class="fa fa-plus"></i> Create new alert rule</button>';
echo '<i> - OR - </i>';
echo '<button type="button" class="btn btn-primary btn-sm" data-toggle="modal" data-target="#search_rule_modal" data-device_id="'.$device['device_id'].'"><i class="fa fa-plus"></i> Create rule from collection</button>';
}
echo '</td>
<td><select name="results" id="results" class="form-control input-sm" onChange="updateResults(this);">';
echo '<div class="col pull-right">';
echo '<select data-toggle="popover" data-placement="left" data-content="results per page" name="results" id="results" class="form-control input-sm" onChange="updateResults(this);">';
$result_options = array(
'10',
'50',
@@ -95,11 +112,14 @@ foreach ($result_options as $option) {
if ($results == $option) {
echo ' selected';
}
echo ">$option</option>";
}
echo '</select>';
echo '</div>';
echo '</select></td>';
echo '</div>';
echo '<br>';
$param = [];
if (isset($device['device_id']) && $device['device_id'] > 0) {
@@ -119,7 +139,7 @@ if (isset($device['device_id']) && $device['device_id'] > 0) {
$full_query = 'SELECT alert_rules.* FROM alert_rules';
}
$full_query .= ' ORDER BY id ASC';
$full_query .= ' ORDER BY name ASC';
$rule_list = dbFetchRows($full_query, $param);
$count = count($rule_list);
@@ -132,6 +152,26 @@ if (isset($_POST['page_number']) && $_POST['page_number'] > 0 && $_POST['page_nu
$start = (($page_number - 1) * $results);
?>
<div class="table-responsive">
<table id="alert-rules-table" class="table table-condensed table-hover table-striped">
<thead>
<tr>
<th data-column-id="Type">Type<th>
<th data-column-id="Name">Name</th>
<th data-column-id="Devices">Devices<th>
<th data-column-id="Transports">Transports<th>
<th data-column-id="Extra">Extra</th>
<th data-column-id="Rule">Rule</th>
<th data-column-id="Severity">Severity</th>
<th data-column-id="Status">Status</th>
<th data-column-id="Enabled">Enabled</th>
<th data-column-id="Action" style="width:86px;">Action</th>
</tr>
</thead>
<tbody>
<?php
$index = 0;
foreach ($rule_list as $rule) {
$index++;
@@ -147,15 +187,19 @@ foreach ($rule_list as $rule) {
$ico = 'check';
$col = 'success';
$extra = '';
$status_msg = '';
if (sizeof($sub) == 1) {
$sub = $sub[0];
if ((int) $sub['state'] === 0) {
$ico = 'check';
$col = 'success';
} elseif ((int) $sub['state'] === 1 || (int) $sub['state'] === 2) {
$status_msg = "All devices matching " . $rule['name'] . " are OK";
}
if ((int) $sub['state'] === 1 || (int) $sub['state'] === 2) {
$ico = 'exclamation';
$col = 'danger';
$extra = 'danger';
$status_msg = "Some devices matching " . $rule['name'] . " are currently alerting";
}
}
@@ -167,6 +211,7 @@ foreach ($rule_list as $rule) {
$ico = 'pause';
$col = '';
$extra = 'active';
$status_msg = $rule['name'] . " is OFF";
} else {
$alert_checked = 'checked';
}
@@ -175,23 +220,125 @@ foreach ($rule_list as $rule) {
$device_count = dbFetchCell('SELECT COUNT(*) FROM alert_device_map WHERE rule_id=?', [$rule['id']]);
$group_count = dbFetchCell('SELECT COUNT(*) FROM alert_group_map WHERE rule_id=?', [$rule['id']]);
if ($device_count && $group_count) {
$popover_msg = 'Restricted rule';
$icon_indicator = 'fa fa-connectdevelop fa-fw text-primary';
} elseif ($device_count) {
$popover_msg = 'Device restricted rule';
if ($device_count) {
$popover_msg = 'Device restricted rule #' . $rule['id'];
$icon_indicator = 'fa fa-server fa-fw text-primary';
} elseif ($group_count) {
$popover_msg = 'Group restricted rule';
}
if ($group_count) {
$popover_msg = 'Group restricted rule #' . $rule['id'];
$icon_indicator = 'fa fa-th fa-fw text-primary';
} else {
$popover_msg = 'Global alert rule';
}
if ($device_count && $group_count) {
$popover_msg = 'Device and Group restricted rule #' . $rule['id'];
$icon_indicator = 'fa fa-connectdevelop fa-fw text-primary';
}
if (!$device_count && !$group_count) {
$popover_msg = 'Global alert rule #' . $rule['id'];
$icon_indicator = 'fa fa-globe fa-fw text-success';
}
echo "<tr class='".$extra."' id='row_".$rule['id']."'>";
echo "<td><i>#".((int) $rule['id'])."</i><br /><i class=\"$icon_indicator\"></i></td>";
echo "<tr class='".$extra."' id='rule_id_".$rule['id']."'>";
// Type
echo "<td colspan=\"2\"><div data-toggle='popover' data-placement='top' data-content=\"$popover_msg\" class=\"$icon_indicator\"></div></td>";
// Name
echo '<td>'.$rule['name'].'</td>';
// Devices (and Groups)
if ($rule['invert_map'] == 0) {
$groups_msg = 'Only devices in this group.';
$devices_msg = 'Only this device.';
$except_device_or_group = null;
}
if ($rule['invert_map'] == 1) {
$devices_msg = 'All devices EXCEPT this device. ';
$groups_msg = 'All devices EXCEPT this group.';
$except_device_or_group = '<strong><em>EXCEPT</em></strong> ';
}
$devices_and_groups_popover='right';
$groups=null;
if ($group_count) {
$group_query = 'SELECT device_groups.name, device_groups.id FROM alert_group_map, device_groups WHERE alert_group_map.rule_id=? and alert_group_map.group_id = device_groups.id ORDER BY name';
$group_maps = dbFetchRows($group_query, [$rule['id']]);
foreach ($group_maps as $group_map) {
$groups .= "$except_device_or_group<a href=\"/device-groups/" . $group_map['id'] . "/edit\" data-container='body' data-toggle='popover' data-placement='$devices_and_groups_popover' data-content='Edit device group " . $group_map['name'] . "' title='$groups_msg' target=\"_blank\">" . $group_map['name'] . "</a><br>";
}
}
$devices=null;
if ($device_count) {
$device_query = 'SELECT devices.device_id,devices.hostname FROM alert_device_map, devices WHERE alert_device_map.rule_id=? and alert_device_map.device_id = devices.device_id ORDER BY hostname';
$device_maps = dbFetchRows($device_query, [$rule['id']]);
foreach ($device_maps as $device_map) {
$devices .= "$except_device_or_group<a href=\"/device/device=" . $device_map['device_id'] . "/tab=edit/\" data-container='body' data-toggle='popover' data-placement='$devices_and_groups_popover' data-content='Edit device " . $device_map['hostname'] . "' title='$devices_msg' target=\"_blank\">" . $device_map['hostname'] . "</a><br>";
}
}
echo "<td colspan='2'>";
if ($groups) {
// Individual Groups first
echo $groups;
}
if ($devices) {
// Individual Devices last
echo $devices;
}
if (!$devices && !$groups) {
// All Devices
echo "<a href=\"/devices\" data-container='body' data-toggle='popover' data-placement='$devices_and_groups_popover' data-content='View All Devices' target=\"_blank\">All Devices</a><br>";
}
echo "</td>";
// Transports
$transport_count=dbFetchCell('SELECT COUNT(*) FROM alert_transport_map WHERE rule_id=?', [$rule['id']]);
$transports_popover='right';
$transports=null;
if ($transport_count) {
$transport_maps = dbFetchRows('SELECT transport_or_group_id,target_type FROM alert_transport_map WHERE alert_transport_map.rule_id=? ORDER BY target_type', [$rule['id']]);
foreach ($transport_maps as $transport_map) {
$transport_name=null;
if ($transport_map['target_type'] == "group") {
$transport_name = dbFetchCell('SELECT transport_group_name FROM alert_transport_groups WHERE transport_group_id=?', [$transport_map['transport_or_group_id']]);
$transport_edit = "<a href='' data-toggle='modal' data-target='#edit-transport-group' data-group_id='" . $transport_map['transport_or_group_id'] . "' data-container='body' data-toggle='popover' data-placement='$transports_popover' data-content='Edit transport group $transport_name'>" . $transport_name . "</a>";
}
if ($transport_map['target_type'] == "single") {
$transport_name = dbFetchCell('SELECT transport_name FROM alert_transports WHERE transport_id=?', [$transport_map['transport_or_group_id']]);
$transport_edit = "<a href='' data-toggle='modal' data-target='#edit-alert-transport' data-transport_id='" . $transport_map['transport_or_group_id'] . "' data-container='body' data-toggle='popover' data-placement='$transports_popover' data-content='Edit transport $transport_name'>" . $transport_name . "</a>";
}
$transports .= $transport_edit . "<br>";
}
}
if (!$transport_count || !$transports) {
$default_transports = dbFetchRows('SELECT transport_id, transport_name FROM alert_transports WHERE is_default=1 ORDER BY transport_name', []);
foreach ($default_transports as $default_transport) {
$transport_edit = "<a href='' data-toggle='modal' data-target='#edit-alert-transport' data-transport_id='" . $default_transport['transport_id'] . "' data-container='body' data-toggle='popover' data-placement='$transports_popover' data-content='Edit default transport " . $default_transport['transport_name'] . "'>" . $default_transport['transport_name'] . "</a>";
$transports .= $transport_edit . "<br>";
}
}
if (!$transports) {
$transports = "none";
}
echo "<td colspan='2'>$transports</td>";
// Extra
echo '<td><small>Max: '.$rule_extra['count'].'<br />Delay: '.$rule_extra['delay'].'<br />Interval: '.$rule_extra['interval'].'</small></td>';
// Rule
echo "<td class='col-sm-4'>";
if ($rule_extra['invert'] === true) {
echo '<strong><em>Inverted</em></strong> ';
@@ -206,63 +353,91 @@ foreach ($rule_list as $rule) {
}
echo '<i>'.htmlentities($rule_display).'</i></td>';
echo '<td>'.$rule['severity'].'</td>';
echo "<td><span id='alert-rule-".$rule['id']."' class='fa fa-fw fa-2x fa-".$ico.' text-'.$col."'></span> ";
// Severity
echo '<td>'.($rule['severity'] == "ok" ? strtoupper($rule['severity']) : ucwords($rule['severity'])).'</td>';
// Status
$status_popover='top';
echo "<td><span data-toggle='popover' data-placement='$status_popover' data-content='$status_msg' id='alert-rule-".$rule['id']."' class='fa fa-fw fa-2x fa-".$ico.' text-'.$col."'></span> ";
if ($rule_extra['mute'] === true) {
echo "<i class='fa fa-fw fa-2x fa-volume-off text-primary' aria-hidden='true'></i></td>";
echo "<div data-toggle='popover' data-content='Alerts for " . $rule['name'] . " are muted' class='fa fa-fw fa-2x fa-volume-off text-primary' aria-hidden='true'></div></td>";
}
echo '<td><small>Max: '.$rule_extra['count'].'<br />Delay: '.$rule_extra['delay'].'<br />Interval: '.$rule_extra['interval'].'</small></td>';
// Enabled
$enabled_popover='top';
echo '<td>';
if (Auth::user()->hasGlobalAdmin()) {
echo "<input id='".$rule['id']."' type='checkbox' name='alert-rule' data-orig_class='".$orig_class."' data-orig_colour='".$orig_col."' data-orig_state='".$orig_ico."' data-alert_id='".$rule['id']."' ".$alert_checked." data-size='small' data-content='".$popover_msg."' data-toggle='modal'>";
if ($rule['disabled']) {
$enabled_msg = $rule['name'] . " is OFF";
}
if (!$rule['disabled']) {
$enabled_msg = $rule['name'] . " is ON";
}
echo "<div id='on-off-checkbox-" .$rule['id']. "' data-toggle='popover' data-placement='$enabled_popover' data-content='" . $enabled_msg . "' class='btn-group btn-group-sm' role='group'>";
echo "<input id='".$rule['id']."' type='checkbox' name='alert-rule' data-orig_class='".$orig_class."' data-orig_colour='".$orig_col."' data-orig_state='".$orig_ico."' data-alert_id='".$rule['id']."' data-alert_name='".$rule['name']."' data-alert_status='". $status_msg. "' ".$alert_checked." data-size='small' data-toggle='modal'>";
echo "</div>";
echo '</td>';
// Action
$action_popover='left';
echo '<td>';
if (Auth::user()->hasGlobalAdmin()) {
echo "<div class='btn-group btn-group-sm' role='group'>";
echo "<button type='button' class='btn btn-primary' data-toggle='modal' data-target='#create-alert' data-rule_id='".$rule['id']."' name='edit-alert-rule' data-content='".$popover_msg."' data-container='body'><i class='fa fa-lg fa-pencil' aria-hidden='true'></i></button> ";
echo "<button type='button' class='btn btn-danger' aria-label='Delete' data-toggle='modal' data-target='#confirm-delete' data-alert_id='".$rule['id']."' name='delete-alert-rule' data-content='".$popover_msg."' data-container='body'><i class='fa fa-lg fa-trash' aria-hidden='true'></i></button>";
}
echo "<div class='btn-group btn-group-sm' role='group'>";
echo "<button type='button' class='btn btn-primary' data-toggle='modal' data-placement='$action_popover' data-target='#create-alert' data-rule_id='".$rule['id']."' name='edit-alert-rule' data-content='Edit alert rule " . $rule['name'] . "' data-container='body'><i class='fa fa-lg fa-pencil' aria-hidden='true'></i></button> ";
echo "<button type='button' class='btn btn-danger' aria-label='Delete' data-placement='$action_popover' data-toggle='modal' data-target='#confirm-delete' data-alert_id='".$rule['id']."' data-alert_name='".$rule['name']."' name='delete-alert-rule' data-content='Delete alert rule " . $rule['name'] . "' data-container='body'><i class='fa fa-lg fa-trash' aria-hidden='true'></i></button>";
echo '</td>';
echo "</tr>\r\n";
}//end foreach
?>
</tbody>
</table>
</div>
<?php
// Pagination
if (($count % $results) > 0) {
echo ' <tr>
<td colspan="8" align="center">'.generate_pagination($count, $results, $page_number).'</td>
</tr>';
echo '<div class="table-responsive">';
echo '<div class="col pull-left">';
echo generate_pagination($count, $results, $page_number);
echo '</div>';
echo '<div class="col pull-right">';
$showing_start = ($page_number*$results)-$results+1;
$showing_end = $page_number*$results;
if ($showing_end > $count) {
$showing_end = $count;
}
echo "<p class=\"pagination\">Showing $showing_start to $showing_end of $count alert rules</p>";
echo '</div>';
echo '</div>';
}
echo '</table>
<input type="hidden" name="page_number" id="page_number" value="'.$page_number.'">
echo '<input type="hidden" name="page_number" id="page_number" value="'.$page_number.'">
<input type="hidden" name="results_amount" id="results_amount" value="'.$results.'">
</form>
</div>';
</form>';
if ($count < 1) {
if (Auth::user()->hasGlobalAdmin()) {
echo '<div class="row">
<div class="col-sm-12">
<form role="form" method="post">
' . csrf_field() . '
<p class="text-center">
<button type="submit" class="btn btn-success btn-lg" id="create-default" name="create-default"><i class="fa fa-plus"></i> Click here to create the default alert rules!</button>
</p>
</form>
</div>
</div>';
}
echo '<div class="row">
<div class="col-sm-12">
<form role="form" method="post">
' . csrf_field() . '
<p class="text-center">
<button type="submit" class="btn btn-success btn-lg" id="create-default" name="create-default"><i class="fa fa-plus"></i> Click here to create the default alert rules!</button>
</p>
</form>
</div>
</div>';
}
?>
<script>
$("[data-toggle='modal'], [data-toggle='popover']").popover({
trigger: 'hover',
'placement': 'top'
trigger: 'hover'
});
$('#ack-alert').click('', function(e) {
event.preventDefault();
@@ -290,6 +465,8 @@ $('input[name="alert-rule"]').on('switchChange.bootstrapSwitch', function(event
event.preventDefault();
var $this = $(this);
var alert_id = $(this).data("alert_id");
var alert_name = $(this).data("alert_name");
var alert_status = $(this).data("alert_status");
var orig_state = $(this).data("orig_state");
var orig_colour = $(this).data("orig_colour");
var orig_class = $(this).data("orig_class");
@@ -305,15 +482,19 @@ $('input[name="alert-rule"]').on('switchChange.bootstrapSwitch', function(event
$('#alert-rule-'+alert_id).addClass('fa-'+orig_state);
$('#alert-rule-'+alert_id).removeClass('text-default');
$('#alert-rule-'+alert_id).addClass('text-'+orig_colour);
$('#row_'+alert_id).removeClass('active');
$('#row_'+alert_id).addClass(orig_class);
$('#alert-rule-'+alert_id).attr('data-content', alert_status);
$('#on-off-checkbox-'+alert_id).attr('data-content', alert_name+' is ON');
$('#rule_id_'+alert_id).removeClass('active');
$('#rule_id_'+alert_id).addClass(orig_class);
} else {
$('#alert-rule-'+alert_id).removeClass('fa-'+orig_state);
$('#alert-rule-'+alert_id).addClass('fa-pause');
$('#alert-rule-'+alert_id).removeClass('text-'+orig_colour);
$('#alert-rule-'+alert_id).addClass('text-default');
$('#row_'+alert_id).removeClass('warning');
$('#row_'+alert_id).addClass('active');
$('#alert-rule-'+alert_id).attr('data-content', alert_name+' is OFF');
$('#on-off-checkbox-'+alert_id).attr('data-content', alert_name+' is OFF');
$('#rule_id_'+alert_id).removeClass('warning');
$('#rule_id_'+alert_id).addClass('active');
}
} else {
$("#message").html('<div class="alert alert-info">'+msg+'</div>');
@@ -338,5 +519,4 @@ function changePage(page,e) {
$('#page_number').val(page);
$('#result_form').submit();
}
</script>