alert: Added ability to make notes for acking alerts + record who did so (#8433)

* alert: Added ability to make notes for acking alerts + record who did so

* Updated schema

* moved sql file

* Updated from comments in PR

* warning changed to blue

* reset notes + keep notes on ack
This commit is contained in:
Neil Lathwood
2018-04-26 06:00:56 +01:00
committed by Tony Murray
parent b3d190ec52
commit 1eee6e8195
12 changed files with 229 additions and 49 deletions

View File

@@ -193,7 +193,8 @@ if (defined('SHOW_SETTINGS')) {
<th data-column-id="rule">Rule</th> <th data-column-id="rule">Rule</th>
<th data-column-id="details" data-sortable="false"></th> <th data-column-id="details" data-sortable="false"></th>
<th data-column-id="hostname">Hostname</th> <th data-column-id="hostname">Hostname</th>
<th data-column-id="ack_ico" data-sortable="false">ACK</th>'; <th data-column-id="ack_ico" data-sortable="false">ACK</th>
<th data-column-id="notes" data-sortable="false">Notes</th>';
if ($proc == '1') { if ($proc == '1') {
$common_output[] = '<th data-column-id="proc" data-sortable="false">URL</th>'; $common_output[] = '<th data-column-id="proc" data-sortable="false">URL</th>';
@@ -252,7 +253,7 @@ var alerts_grid = $("#alerts_' . $unique_id . '").bootgrid({
$(this).find(".incident-toggle").fadeIn(200); $(this).find(".incident-toggle").fadeIn(200);
}).on("mouseleave", function() { }).on("mouseleave", function() {
$(this).find(".incident-toggle").fadeOut(200); $(this).find(".incident-toggle").fadeOut(200);
}).on("click", "td:not(.incident-toggle-td)", function() { }).on("click", "td(.incident-toggle-td)", function() {
var target = $(this).parent().find(".incident-toggle").data("target"); var target = $(this).parent().find(".incident-toggle").data("target");
if( $(this).parent().find(".incident-toggle").hasClass(\'fa-plus\') ) { if( $(this).parent().find(".incident-toggle").hasClass(\'fa-plus\') ) {
$(this).parent().find(".incident-toggle").toggleClass(\'fa-plus fa-minus\'); $(this).parent().find(".incident-toggle").toggleClass(\'fa-plus fa-minus\');
@@ -262,26 +263,43 @@ var alerts_grid = $("#alerts_' . $unique_id . '").bootgrid({
}); });
alerts_grid.find(".command-ack-alert").on("click", function(e) { alerts_grid.find(".command-ack-alert").on("click", function(e) {
e.preventDefault(); e.preventDefault();
var alert_id = $(this).data("alert_id"); var alert_state = $(this).data("alert_state");
var state = $(this).data("state"); if (alert_state != 2) {
$.ajax({ var ack_msg = window.prompt("Enter the reason you are acknowledging this alert:");
type: "POST", } else {
url: "ajax_form.php", var ack_msg = "";
data: { type: "ack-alert", alert_id: alert_id, state: state }, }
success: function(msg){ if (typeof ack_msg == "string") {
toastr.success(msg); var alert_id = $(this).data("alert_id");
if(msg.indexOf("ERROR:") <= -1) { var state = $(this).data("state");
$(".alerts").each(function(index) { $.ajax({
var $sortDictionary = $(this).bootgrid("getSortDictionary"); type: "POST",
$(this).reload; url: "ajax_form.php",
$(this).bootgrid("sort", $sortDictionary); dataType: "json",
}); data: { type: "ack-alert", alert_id: alert_id, state: state, ack_msg: ack_msg },
success: function (data) {
if (data.status == "ok") {
toastr.success(data.message);
$(".alerts").each(function(index) {
var $sortDictionary = $(this).bootgrid("getSortDictionary");
$(this).reload;
$(this).bootgrid("sort", $sortDictionary);
});
} else {
toastr.error(data.message);
}
},
error: function(){
toastr.error(data.message);
} }
}, });
error: function(){ }
toastr.error("An error occurred acking this alert"); });
} alerts_grid.find(".command-alert-note").on("click", function(e) {
}); e.preventDefault();
var alert_id = $(this).data(\'alert_id\');
$(\'#alert_id\').val(alert_id);
$("#alert_notes_modal").modal(\'show\');
}); });
}); });
</script>'; </script>';

View File

@@ -1,26 +1,42 @@
<?php <?php
/**
/* * ack-alert.inc.php
* LibreNMS
* *
* Copyright (c) 2014 Neil Lathwood <https://github.com/laf/ http://www.lathwood.co.uk/fa> * LibreNMS ack-alert.inc.php
* *
* This program is free software: you can redistribute it and/or modify it * This program is free software: you can redistribute it and/or modify
* under the terms of the GNU General Public License as published by the * it under the terms of the GNU General Public License as published by
* Free Software Foundation, either version 3 of the License, or (at your * the Free Software Foundation, either version 3 of the License, or
* option) any later version. Please see LICENSE.txt at the top level of * (at your option) any later version.
* the source code distribution for details. *
* 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 2018 Neil Lathwood
* @author Neil Lathwood <gh+n@laf.io>
*/ */
header('Content-type: text/plain');
$alert_id = mres($_POST['alert_id']); use LibreNMS\Config;
$state = mres($_POST['state']);
header('Content-type: application/json');
$alert_id = $vars['alert_id'];
$state = $vars['state'];
$ack_msg = $vars['ack_msg'];
$status = 'error';
if (!is_numeric($alert_id)) { if (!is_numeric($alert_id)) {
echo 'ERROR: No alert selected'; $message = 'No alert selected';
exit;
} elseif (!is_numeric($state)) { } elseif (!is_numeric($state)) {
echo 'ERROR: No state passed'; $message = 'No state passed';
exit;
} else { } else {
if ($state == 2) { if ($state == 2) {
$state = 1; $state = 1;
@@ -30,11 +46,28 @@ if (!is_numeric($alert_id)) {
$open = 1; $open = 1;
} }
if (dbUpdate(array('state' => $state, 'open' => $open), 'alerts', 'id=?', array($alert_id)) >= 0) { $data = ['state' => $state, 'open' => $open];
echo 'Alert acknowledged status changed.'; if (!empty($ack_msg)) {
exit; $note = dbFetchCell('SELECT note FROM alerts WHERE id=?', [$alert_id]);
if (!empty($note)) {
$note .= PHP_EOL;
}
$data['note'] = $note . date(Config::get('dateformat.long')) . " - Ack ({$_SESSION['username']}) $ack_msg";
}
if (dbUpdate($data, 'alerts', 'id=?', array($alert_id)) >= 0) {
if ($state === 2) {
$alert_info = dbFetchRow("SELECT `alert_rules`.`name`,`alerts`.`device_id` FROM `alert_rules` LEFT JOIN `alerts` ON `alerts`.`rule_id` = `alert_rules`.`id` WHERE `alerts`.`id` = ?", [$alert_id]);
log_event("{$_SESSION['username']} acknowledged alert {$alert_info['name']}", $alert_info['device_id'], 'alert', 2, $alert_id);
}
$message = 'Alert acknowledged status changed.';
$status = 'ok';
} else { } else {
echo 'ERROR: Alert has not been acknowledged.'; $message = 'Alert has not been acknowledged.';
exit;
} }
}//end if }//end if
die(json_encode([
'status' => $status,
'message' => $message,
]));

View File

@@ -0,0 +1,53 @@
<?php
/**
* alert-notes.inc.php
*
* LibreNMS alert-notes.inc.php
*
* 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 2018 Neil Lathwood
* @author Neil Lathwood <gh+n@laf.io>
*/
header('Content-type: application/json');
$alert_id = $vars['alert_id'];
$sub_type = $vars['sub_type'];
$note = $vars['note'] ?: '';
$status = 'error';
if (is_numeric($alert_id)) {
if ($sub_type === 'get_note') {
$note = dbFetchCell("SELECT `note` FROM `alerts` WHERE `id` = ?", [$alert_id]);
$message = 'Alert note retrieved';
$status = 'ok';
} else {
if (dbUpdate(['note' => $note], 'alerts', '`id` = ?', [$alert_id])) {
$status = 'ok';
$message = 'Note updated';
} else {
$message = 'Could not update note';
}
}
} else {
$message = 'Invalid alert id';
}
die(json_encode([
'status' => $status,
'message' => $message,
'note' => $note,
]));

View File

@@ -2,7 +2,7 @@
/** /**
* alert-rules.inc.php * alert-rules.inc.php
* *
* LibreNMS alert-rules.inc.php for processor * LibreNMS alert-rules.inc.php
* *
* This program is free software: you can redistribute it and/or modify * 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 * it under the terms of the GNU General Public License as published by
@@ -150,7 +150,6 @@ if (is_numeric($rule_id) && $rule_id > 0) {
dbSyncRelationship('alert_group_map', 'rule_id', $rule_id, 'group_id', $groups); dbSyncRelationship('alert_group_map', 'rule_id', $rule_id, 'group_id', $groups);
} }
die(json_encode([ die(json_encode([
'status' => $status, 'status' => $status,
'message' => $message, 'message' => $message,

View File

@@ -0,0 +1,66 @@
<form>
<div class="modal fade" id="alert_notes_modal" tabindex="-1" role="dialog" aria-labelledby="alert_notes" aria-hidden="true">
<div class="modal-dialog modal-lg">
<div class="modal-content">
<div class="modal-header">
<button type="button" class="close" data-dismiss="modal" aria-hidden="true">&times;</button>
<h5 class="modal-title" id="alert_notes">Alert notes</h5>
</div>
<div class="modal-body">
<div class="row">
<div class='col-sm-12'>
<div class="form-group">
<textarea class="form-control" id="note" name="note" rows="15"></textarea>
</div>
</div>
</div>
<div class="row">
<div class='col-sm-12'>
<div class="form-group">
<input type="hidden" id="alert_id" name="alert_id" value="">
<button class="btn btn-success" id="save-alert-notes" name="save-alert-notes">Save notes</button>
</div>
</div>
</div>
</div>
</div>
</div>
</div>
</form>
<script>
$('#alert_notes_modal').on('show.bs.modal', function (event) {
var alert_id = $("#alert_id").val();
$.ajax({
type: "POST",
url: "ajax_form.php",
data: { type: "alert-notes", alert_id: alert_id, sub_type: 'get_note'},
dataType: "json",
success: function (data) {
$("#note").val(data.note);
}
});
});
$("#save-alert-notes").click('', function(event) {
event.preventDefault();
var alert_id = $("#alert_id").val();
var note = $("#note").val();
$.ajax({
type: "POST",
url: "ajax_form.php",
data: { type: "alert-notes", alert_id: alert_id, sub_type: 'set_note', note: note},
dataType: "json",
success: function (data) {
if (data.status == 'ok') {
toastr.success(data.message);
$("#alert_notes_modal").modal('hide');
} else {
toastr.error(data.message);
}
},
error: function() {
toastr.error(data.message);
}
});
});
</script>

View File

@@ -210,7 +210,6 @@ $('#value').keypress(function (e) {
}); });
$('div').on('click', 'button#reset-default', function(e) { $('div').on('click', 'button#reset-default', function(e) {
console.log('zart');
e.preventDefault(); e.preventDefault();
var template_id = $("#template_id").val(); var template_id = $("#template_id").val();
var template = '%title\r\nSeverity: %severity\r\n{if %state == 0}Time elapsed: %elapsed\r\n{/if}Timestamp: %timestamp\r\nUnique-ID: %uid\r\nRule: {if %name}%name{else}%rule{/if}\r\n{if %faults}Faults:\r\n{foreach %faults} #%key: %value.string\r\n{/foreach}{/if}Alert sent to: {foreach %contacts}%value <%key> {/foreach}'; var template = '%title\r\nSeverity: %severity\r\n{if %state == 0}Time elapsed: %elapsed\r\n{/if}Timestamp: %timestamp\r\nUnique-ID: %uid\r\nRule: {if %name}%name{else}%rule{/if}\r\n{if %faults}Faults:\r\n{foreach %faults} #%key: %value.string\r\n{/foreach}{/if}Alert sent to: {foreach %contacts}%value <%key> {/foreach}';

View File

@@ -119,8 +119,8 @@ foreach (dbFetchRows($sql, $param) as $alert) {
$log = dbFetchCell('SELECT details FROM alert_log WHERE rule_id = ? AND device_id = ? ORDER BY id DESC LIMIT 1', array($alert['rule_id'], $alert['device_id'])); $log = dbFetchCell('SELECT details FROM alert_log WHERE rule_id = ? AND device_id = ? ORDER BY id DESC LIMIT 1', array($alert['rule_id'], $alert['device_id']));
$fault_detail = alert_details($log); $fault_detail = alert_details($log);
$alert_to_ack = '<button type="button" class="btn btn-danger command-ack-alert fa fa-eye" aria-hidden="true" title="Mark as acknowledged" data-target="ack-alert" data-state="' . $alert['state'] . '" data-alert_id="' . $alert['id'] . '" name="ack-alert"></button>'; $alert_to_ack = '<button type="button" class="btn btn-danger command-ack-alert fa fa-eye" aria-hidden="true" title="Mark as acknowledged" data-target="ack-alert" data-state="' . $alert['state'] . '" data-alert_id="' . $alert['id'] . '" data-alert_state="' . $alert['state'] . '" name="ack-alert"></button>';
$alert_to_nack = '<button type="button" class="btn btn-warning command-ack-alert fa fa-eye-slash" aria-hidden="true" title="Mark as not acknowledged" data-target="ack-alert" data-state="' . $alert['state'] . '" data-alert_id="' . $alert['id'] . '" name="ack-alert"></button>'; $alert_to_nack = '<button type="button" class="btn btn-primary command-ack-alert fa fa-eye-slash" aria-hidden="true" title="Mark as not acknowledged" data-target="ack-alert" data-state="' . $alert['state'] . '" data-alert_id="' . $alert['id'] . '" data-alert_state="' . $alert['state'] . '" name="ack-alert"></button>';
$ack_ico = $alert_to_ack; $ack_ico = $alert_to_ack;
@@ -164,7 +164,7 @@ foreach (dbFetchRows($sql, $param) as $alert) {
} }
if ((int)$alert['state'] === 2) { if ((int)$alert['state'] === 2) {
$severity_ico = '<span class="alert-status label-warning">&nbsp;</span>'; $severity_ico = '<span class="alert-status label-primary">&nbsp;</span>';
} }
$proc = dbFetchCell('SELECT proc FROM alerts,alert_rules WHERE alert_rules.id = alerts.rule_id AND alerts.id = ?', array($alert['id'])); $proc = dbFetchCell('SELECT proc FROM alerts,alert_rules WHERE alert_rules.id = alerts.rule_id AND alerts.id = ?', array($alert['id']));
@@ -178,6 +178,12 @@ foreach (dbFetchRows($sql, $param) as $alert) {
} }
} }
if (empty($alert['note'])) {
$note_class = 'default';
} else {
$note_class = 'warning';
}
$response[] = array( $response[] = array(
'id' => $rulei++, 'id' => $rulei++,
'rule' => '<i title="' . htmlentities($alert['rule']) . '"><a href="' . generate_url(array('page' => 'alert-rules')) . '">' . htmlentities($alert['name']) . '</a></i>', 'rule' => '<i title="' . htmlentities($alert['rule']) . '"><a href="' . generate_url(array('page' => 'alert-rules')) . '">' . htmlentities($alert['name']) . '</a></i>',
@@ -189,6 +195,7 @@ foreach (dbFetchRows($sql, $param) as $alert) {
'alert_id' => $alert['id'], 'alert_id' => $alert['id'],
'ack_ico' => $ack_ico, 'ack_ico' => $ack_ico,
'proc' => $has_proc, 'proc' => $has_proc,
'notes' => "<button type='button' class='btn btn-$note_class fa fa-sticky-note-o command-alert-note' aria-label='Notes' id='alert-notes' data-alert_id='{$alert['id']}'></button>",
); );
} }

View File

@@ -23,6 +23,7 @@ $page_title = 'Alerts';
<?php <?php
$device['device_id'] = '-1'; $device['device_id'] = '-1';
require_once 'includes/modal/alert_notes.inc.php';
require_once 'includes/common/alerts.inc.php'; require_once 'includes/common/alerts.inc.php';
echo implode('', $common_output); echo implode('', $common_output);
unset($device['device_id']); unset($device['device_id']);

View File

@@ -21,6 +21,8 @@ use LibreNMS\Authentication\Auth;
$no_refresh = true; $no_refresh = true;
$default_dash = get_user_pref('dashboard', 0); $default_dash = get_user_pref('dashboard', 0);
require_once 'includes/modal/alert_notes.inc.php';
// get all dashboards this user can access and put them into two lists user_dashboards and shared_dashboards // get all dashboards this user can access and put them into two lists user_dashboards and shared_dashboards
$dashboards = get_dashboards(); $dashboards = get_dashboards();
list($user_dashboards, $shared_dashboards) = array_reduce($dashboards, function ($ret, $dash) { list($user_dashboards, $shared_dashboards) = array_reduce($dashboards, function ($ret, $dash) {

View File

@@ -229,7 +229,7 @@ function RunRules($device_id)
c_echo('Status: %bNOCHG'); c_echo('Status: %bNOCHG');
} else { } else {
if (dbInsert(array('state' => 0, 'device_id' => $device_id, 'rule_id' => $rule['id']), 'alert_log')) { if (dbInsert(array('state' => 0, 'device_id' => $device_id, 'rule_id' => $rule['id']), 'alert_log')) {
if (!dbUpdate(array('state' => 0, 'open' => 1), 'alerts', 'device_id = ? && rule_id = ?', array($device_id,$rule['id']))) { if (!dbUpdate(array('state' => 0, 'open' => 1, 'note' => ''), 'alerts', 'device_id = ? && rule_id = ?', array($device_id,$rule['id']))) {
dbInsert(array('state' => 0, 'device_id' => $device_id, 'rule_id' => $rule['id'], 'open' => 1, 'alerted' => 0), 'alerts'); dbInsert(array('state' => 0, 'device_id' => $device_id, 'rule_id' => $rule['id'], 'open' => 1, 'alerted' => 0), 'alerts');
} }
c_echo(PHP_EOL . 'Status: %gOK'); c_echo(PHP_EOL . 'Status: %gOK');

View File

@@ -27,6 +27,7 @@ alerts:
- { Field: state, Type: int(11), 'Null': false, Extra: '' } - { Field: state, Type: int(11), 'Null': false, Extra: '' }
- { Field: alerted, Type: int(11), 'Null': false, Extra: '' } - { Field: alerted, Type: int(11), 'Null': false, Extra: '' }
- { Field: open, Type: int(11), 'Null': false, Extra: '' } - { Field: open, Type: int(11), 'Null': false, Extra: '' }
- { Field: note, Type: text, 'Null': false, Extra: '' }
- { Field: timestamp, Type: timestamp, 'Null': false, Extra: 'on update CURRENT_TIMESTAMP', Default: CURRENT_TIMESTAMP } - { Field: timestamp, Type: timestamp, 'Null': false, Extra: 'on update CURRENT_TIMESTAMP', Default: CURRENT_TIMESTAMP }
Indexes: Indexes:
PRIMARY: { Name: PRIMARY, Columns: [id], Unique: true, Type: BTREE } PRIMARY: { Name: PRIMARY, Columns: [id], Unique: true, Type: BTREE }

1
sql-schema/248.sql Normal file
View File

@@ -0,0 +1 @@
ALTER TABLE `alerts` ADD `note` TEXT NOT NULL AFTER `open`;