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="details" data-sortable="false"></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') {
$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);
}).on("mouseleave", function() {
$(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");
if( $(this).parent().find(".incident-toggle").hasClass(\'fa-plus\') ) {
$(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) {
e.preventDefault();
var alert_id = $(this).data("alert_id");
var state = $(this).data("state");
$.ajax({
type: "POST",
url: "ajax_form.php",
data: { type: "ack-alert", alert_id: alert_id, state: state },
success: function(msg){
toastr.success(msg);
if(msg.indexOf("ERROR:") <= -1) {
$(".alerts").each(function(index) {
var $sortDictionary = $(this).bootgrid("getSortDictionary");
$(this).reload;
$(this).bootgrid("sort", $sortDictionary);
});
var alert_state = $(this).data("alert_state");
if (alert_state != 2) {
var ack_msg = window.prompt("Enter the reason you are acknowledging this alert:");
} else {
var ack_msg = "";
}
if (typeof ack_msg == "string") {
var alert_id = $(this).data("alert_id");
var state = $(this).data("state");
$.ajax({
type: "POST",
url: "ajax_form.php",
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>';

View File

@@ -1,26 +1,42 @@
<?php
/*
* LibreNMS
/**
* ack-alert.inc.php
*
* 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
* 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. Please see LICENSE.txt at the top level of
* the source code distribution for details.
* 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: text/plain');
$alert_id = mres($_POST['alert_id']);
$state = mres($_POST['state']);
use LibreNMS\Config;
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)) {
echo 'ERROR: No alert selected';
exit;
$message = 'No alert selected';
} elseif (!is_numeric($state)) {
echo 'ERROR: No state passed';
exit;
$message = 'No state passed';
} else {
if ($state == 2) {
$state = 1;
@@ -30,11 +46,28 @@ if (!is_numeric($alert_id)) {
$open = 1;
}
if (dbUpdate(array('state' => $state, 'open' => $open), 'alerts', 'id=?', array($alert_id)) >= 0) {
echo 'Alert acknowledged status changed.';
exit;
$data = ['state' => $state, 'open' => $open];
if (!empty($ack_msg)) {
$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 {
echo 'ERROR: Alert has not been acknowledged.';
exit;
$message = 'Alert has not been acknowledged.';
}
}//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
*
* LibreNMS alert-rules.inc.php for processor
* LibreNMS alert-rules.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
@@ -150,7 +150,6 @@ if (is_numeric($rule_id) && $rule_id > 0) {
dbSyncRelationship('alert_group_map', 'rule_id', $rule_id, 'group_id', $groups);
}
die(json_encode([
'status' => $status,
'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) {
console.log('zart');
e.preventDefault();
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}';

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']));
$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_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_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-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;
@@ -164,7 +164,7 @@ foreach (dbFetchRows($sql, $param) as $alert) {
}
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']));
@@ -178,6 +178,12 @@ foreach (dbFetchRows($sql, $param) as $alert) {
}
}
if (empty($alert['note'])) {
$note_class = 'default';
} else {
$note_class = 'warning';
}
$response[] = array(
'id' => $rulei++,
'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'],
'ack_ico' => $ack_ico,
'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>",
);
}