Skip to content

Commit

Permalink
alert: Added ability to make notes for acking alerts + record who did…
Browse files Browse the repository at this point in the history
… so (librenms#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
  • Loading branch information
laf authored and TheMysteriousX committed May 20, 2018
1 parent c26a13d commit b7c92ee
Show file tree
Hide file tree
Showing 12 changed files with 229 additions and 49 deletions.
60 changes: 39 additions & 21 deletions html/includes/common/alerts.inc.php
Expand Up @@ -193,7 +193,8 @@
<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>';
Expand Down Expand Up @@ -252,7 +253,7 @@
$(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\');
Expand All @@ -262,26 +263,43 @@
});
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>';
Expand Down
75 changes: 54 additions & 21 deletions html/includes/forms/ack-alert.inc.php
@@ -1,26 +1,42 @@
<?php

/*
* LibreNMS
/**
* ack-alert.inc.php
*
* 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.
*
* Copyright (c) 2014 Neil Lathwood <https://github.com/laf/ http://www.lathwood.co.uk/fa>
* 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.
*
* 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.
* 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;
Expand All @@ -30,11 +46,28 @@
$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,
]));
53 changes: 53 additions & 0 deletions html/includes/forms/alert-notes.inc.php
@@ -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,
]));
3 changes: 1 addition & 2 deletions html/includes/forms/alert-rules.inc.php
Expand Up @@ -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
Expand Down Expand Up @@ -150,7 +150,6 @@
dbSyncRelationship('alert_group_map', 'rule_id', $rule_id, 'group_id', $groups);
}


die(json_encode([
'status' => $status,
'message' => $message,
Expand Down
66 changes: 66 additions & 0 deletions html/includes/modal/alert_notes.inc.php
@@ -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>
1 change: 0 additions & 1 deletion html/includes/modal/alert_template.inc.php
Expand Up @@ -210,7 +210,6 @@
});

$('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}';
Expand Down
13 changes: 10 additions & 3 deletions html/includes/table/alerts.inc.php
Expand Up @@ -119,8 +119,8 @@
$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;

Expand Down Expand Up @@ -164,7 +164,7 @@
}

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']));
Expand All @@ -178,6 +178,12 @@
}
}

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>',
Expand All @@ -189,6 +195,7 @@
'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>",
);
}

Expand Down
1 change: 1 addition & 0 deletions html/pages/alerts.inc.php
Expand Up @@ -23,6 +23,7 @@

<?php
$device['device_id'] = '-1';
require_once 'includes/modal/alert_notes.inc.php';
require_once 'includes/common/alerts.inc.php';
echo implode('', $common_output);
unset($device['device_id']);
Expand Down
2 changes: 2 additions & 0 deletions html/pages/front/tiles.php
Expand Up @@ -21,6 +21,8 @@
$no_refresh = true;
$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
$dashboards = get_dashboards();
list($user_dashboards, $shared_dashboards) = array_reduce($dashboards, function ($ret, $dash) {
Expand Down
2 changes: 1 addition & 1 deletion includes/alerts.inc.php
Expand Up @@ -229,7 +229,7 @@ function RunRules($device_id)
c_echo('Status: %bNOCHG');
} else {
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');
}
c_echo(PHP_EOL . 'Status: %gOK');
Expand Down
1 change: 1 addition & 0 deletions misc/db_schema.yaml
Expand Up @@ -27,6 +27,7 @@ alerts:
- { Field: state, Type: int(11), 'Null': false, Extra: '' }
- { Field: alerted, 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 }
Indexes:
PRIMARY: { Name: PRIMARY, Columns: [id], Unique: true, Type: BTREE }
Expand Down
1 change: 1 addition & 0 deletions sql-schema/248.sql
@@ -0,0 +1 @@
ALTER TABLE `alerts` ADD `note` TEXT NOT NULL AFTER `open`;

0 comments on commit b7c92ee

Please sign in to comment.