refactor: Ability to edit default alert template (#7121)

* newfeature: ability to edit default alert template

* Fix style errors

* Added missing db query

* Removed unnecessary code, and added faq section

* Minor documentation about default alert template. And put back wrongly removed lines

* Fixing the silly old mistakes... again
This commit is contained in:
Aldemir Akpinar
2017-08-12 22:21:13 +03:00
committed by Neil Lathwood
parent 0ebf436d47
commit 34d0fc3199
7 changed files with 90 additions and 29 deletions

View File

@@ -2,7 +2,7 @@ source: Alerting/Templates.md
# <a name="templates">Templates</a>
Templates can be assigned to a single or a group of rules and can contain any kind of text.
Templates can be assigned to a single or a group of rules and can contain any kind of text. There is also a default template which is used for any rule that isn't associated with a template. This template can be found under `Alert Templates` page and can be edited. It also has an option revert it back to its default content.
The template-parser understands `if` and `foreach` controls and replaces certain placeholders with information gathered about the alert.
@@ -100,7 +100,7 @@ Note the use of double-quotes. Single quotes (`'`) in templates will be escaped
We include a few templates for you to use, these are specific to the type of alert rules you are creating. For example if you create a rule that would alert on BGP sessions then you can
assign the BGP template to this rule to provide more information.
The included templates are:
The included templates apart from the default template are:
- BGP Sessions
- Ports

View File

@@ -28,6 +28,7 @@ source: Support/FAQ.md
- [What is the Difference between Disable Device and Ignore a Device?](#faq28)
- [Why can't Normal and Global View users see Oxidized?](#faq29)
- [What is the Demo User for?](#faq30)
- [Why does modifying 'Default Alert Template' fail?](#faq31)
### Developing
- [How do I add support for a new OS?](#faq8)
@@ -339,3 +340,10 @@ Configs can often contain sensitive data. Because of that only global admins can
### <a name="faq30"> What is the Demo User for?</a>
Demo users allow full access except adding/editing users and deleting devices and can't change passwords.
### <a name="faq31"> Why does modifying 'Default Alert Template' fail?</a>
This template's entry could be missing in the database. Please run:
```bash
mysql -u librenms -p < sql-schema/202.sql
```

View File

@@ -59,10 +59,14 @@ if (!empty($name)) {
} elseif ($_REQUEST['template']) {
//Create new template
if (dbInsert(array('template' => $_REQUEST['template'], 'name' => $name, 'title' => $_REQUEST['title'], 'title_rec' => $_REQUEST['title_rec']), "alert_templates")) {
$ok = "Alert template has been created.";
if ($name != 'Default Alert Template') {
if (dbInsert(array('template' => $_REQUEST['template'], 'name' => $name, 'title' => $_REQUEST['title'], 'title_rec' => $_REQUEST['title_rec']), "alert_templates")) {
$ok = "Alert template has been created.";
} else {
$error = "Could not create alert template";
}
} else {
$error = "Could not create alert template";
$error = "This template name is reserved!";
}
} else {
$error = "We could not work out what you wanted to do!";

View File

@@ -25,7 +25,6 @@ if (is_admin() === false) {
<h4 class="modal-title" id="Create">Alert Rules</h4>
</div>
<div class="modal-body">
<form method="post" role="form" id="rules" class="form alert-template-form">
<div class="row">
<div class="col-md-12">
<span id="response"></span>
@@ -87,6 +86,7 @@ foreach ($operators as $operator) {
<input type="text" class="form-control" id="value" name="value" autocomplete="off"><br /><br />
<input type="text" class="form-control" id="line" name="line"><br /><br />
<input type="hidden" name="template_id" id="template_id">
<input type="hidden" name="default_template" id="default_template" value="0">
<button type="button" class="btn btn-primary" id="add_line" name="add_line">Add line</button>
</div>
</div>
@@ -95,14 +95,18 @@ foreach ($operators as $operator) {
</div>
</div>
</div>
</form>
<script>
$('#alert-template').on('show.bs.modal', function (event) {
var button = $(event.relatedTarget);
var template_id = $('#template_id').val();
var default_template = $('#default_template').val();
if(template_id != null && template_id != '') {
if(default_template == "1") {
$('#create-template').after('<span class="pull-right"><button class="btn btn-primary btn-sm" id="reset-default">Reset to Default</button></span>');
$('#name').prop("disabled",true);
}
$('#create-template').text('Update template');
$.ajax({
type: "POST",
@@ -118,6 +122,7 @@ $('#alert-template').on('show.bs.modal', function (event) {
});
}
});
$('#alert-template').on('hide.bs.modal', function(event) {
$('#template_id').val('');
$('#template').val('');
@@ -125,6 +130,10 @@ $('#alert-template').on('hide.bs.modal', function(event) {
$('#value').val('');
$('#name').val('');
$('#create-template').text('Create template');
$('#default-template').val('0');
$('#reset-default').remove();
$('#name').prop("disabled",false);
$('#error').val('');
});
$('#create-template').click('', function(e) {
@@ -134,26 +143,7 @@ $('#create-template').click('', function(e) {
var name = $("#name").val();
var title = $("#title").val();
var title_rec = $("#title_rec").val();
$.ajax({
type: "POST",
url: "ajax_form.php",
data: { type: "alert-templates", template: template , name: name, template_id: template_id, title: title, title_rec: title_rec},
dataType: "html",
success: function(msg){
if(msg.indexOf("ERROR:") <= -1) {
$("#message").html('<div class="alert alert-info">'+msg+'</div>');
$("#alert-template").modal('hide');
setTimeout(function() {
location.reload(1);
}, 1000);
} else {
$("#error").html('<div class="alert alert-info">'+msg+'</div>');
}
},
error: function(){
$("#error").html('<div class="alert alert-info">An error occurred updating this alert template.</div>');
}
});
alertTemplateAjaxOps(template, name, template_id, title, title_rec);
});
$('#add_line').click('', function(e) {
@@ -217,6 +207,40 @@ $('#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}';
var name = 'Default Alert Template';
alertTemplateAjaxOps(template, name, template_id, '', '');
});
function alertTemplateAjaxOps(template, name, template_id, title, title_rec)
{
$.ajax({
type: "POST",
url: "ajax_form.php",
data: { type: "alert-templates", template: template, name: name, template_id: template_id, title: title, title_rec: title_rec},
dataType: "html",
success: function(msg){
if(msg.indexOf("ERROR:") <= -1) {
$("#message").html('<div class="alert alert-info">'+msg+'</div>');
$("#alert-template").modal('hide');
setTimeout(function() {
location.reload(1);
}, 1000);
} else {
$("#error").html('<div class="alert alert-danger">'+msg+'</div>');
}
},
error: function(){
$("#error").html('<div class="alert alert-danger">An error occurred updating this alert template.</div>');
}
});
}
function updateLine(value) {
var line = $('#line').val();
//$('#value').prop('disabled',true);

View File

@@ -24,9 +24,18 @@ require_once 'includes/modal/attach_alert_template.inc.php';
</tr>
</thead>
<tbody>
<tr data-row-id="0">
<td>0</td>
<td>Default Alert Template</td>
<td></td>
</tr>
<?php
$full_query = "SELECT id, name from alert_templates";
foreach (dbFetchRows($full_query, $param) as $template) {
if ($template['name'] == 'Default Alert Template') {
$default_tplid = $template['id'];
continue;
}
echo '<tr data-row-id="'.$template['id'].'">
<td>'.$template['id'].'</td>
<td>'.$template['name'].'</td>
@@ -59,14 +68,26 @@ $(document).ready(function() {
},
formatters: {
"commands": function(column, row) {
var response = "<button type=\"button\" class=\"btn btn-xs btn-primary command-edit\" data-toggle='modal' data-target='#alert-template' data-template_id=\"" + row.id + "\" data-template_action='edit' name='edit-alert-template'><i class=\"fa fa-pencil\" aria-hidden=\"true\"></i></button> " + "<button type=\"button\" class=\"btn btn-xs btn-danger command-delete\" data-toggle=\"modal\" data-target='#confirm-delete-alert-template' data-template_id=\"" + row.id + "\" name='delete-alert-template'><i class=\"fa fa-trash-o\" aria-hidden=\"true\"></i></button> " + "<button type='button' class='btn btn-warning btn-xs command-attach' data-toggle='modal' data-target='#attach-alert-template' data-template_id='" + row.id + "' name='attach-alert-template'><i class='fa fa-th-list' aria-hidden='true'></i></button>";
var response = '';
if(row.id == 0) {
response = "<button type=\"button\" class=\"btn btn-xs btn-primary command-edit\" data-toggle='modal' data-target='#alert-template' data-template_id=\"" + row.id + "\" data-template_action='edit' name='edit-alert-template'><i class=\"fa fa-pencil\" aria-hidden=\"true\"></i></button> " + "<button type=\"button\" class=\"btn btn-xs btn-danger command-delete\" disabled=\"disabled\"><i class=\"fa fa-trash-o\" aria-hidden=\"true\"></i></button> " + "<button type='button' class='btn btn-warning btn-xs command-attach' disabled=\"disabled\"><i class='fa fa-th-list' aria-hidden='true'></i></button>";
} else {
response = "<button type=\"button\" class=\"btn btn-xs btn-primary command-edit\" data-toggle='modal' data-target='#alert-template' data-template_id=\"" + row.id + "\" data-template_action='edit' name='edit-alert-template'><i class=\"fa fa-pencil\" aria-hidden=\"true\"></i></button> " + "<button type=\"button\" class=\"btn btn-xs btn-danger command-delete\" data-toggle=\"modal\" data-target='#confirm-delete-alert-template' data-template_id=\"" + row.id + "\" name='delete-alert-template'><i class=\"fa fa-trash-o\" aria-hidden=\"true\"></i></button> " + "<button type='button' class='btn btn-warning btn-xs command-attach' data-toggle='modal' data-target='#attach-alert-template' data-template_id='" + row.id + "' name='attach-alert-template'><i class='fa fa-th-list' aria-hidden='true'></i></button>";
}
return response;
}
},
}).on("loaded.rs.jquery.bootgrid", function() {
/* Executes after data is loaded and rendered */
grid.find(".command-edit").on("click", function(e) {
$('#template_id').val($(this).data("template_id"));
var localtmpl_id = $(this).data("template_id");
if(localtmpl_id == 0) {
$('#default_template').val("1");
$('#template_id').val(<?=$default_tplid?>);
} else {
$('#default_template').val("0");
$('#template_id').val(localtmpl_id);
}
$("#alert-template").modal('show');
}).end().find(".command-delete").on("click", function(e) {
$('#template_id').val($(this).data("template_id"));

View File

@@ -446,6 +446,9 @@ function DescribeAlert($alert)
$i = 0;
$device = dbFetchRow('SELECT hostname, sysName, sysDescr, hardware, version, location, purpose, notes, uptime FROM devices WHERE device_id = ?', array($alert['device_id']));
$tpl = dbFetchRow('SELECT `template`,`title`,`title_rec` FROM `alert_templates` JOIN `alert_template_map` ON `alert_template_map`.`alert_templates_id`=`alert_templates`.`id` WHERE `alert_template_map`.`alert_rule_id`=?', array($alert['rule_id']));
if (!$tpl) {
$tpl = dbFetchRow("SELECT `template`,`title`,`title_rec` FROM `alert_templates` WHERE `name`='Default Alert Template'");
}
$default_tpl = "%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}";
$obj['hostname'] = $device['hostname'];
$obj['sysName'] = $device['sysName'];

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

@@ -0,0 +1 @@
INSERT INTO `alert_templates` (name, template) values('Default Alert 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}');