Copy existing Alert Rule (#11195)

This commit is contained in:
SourceDoctor 2020-02-26 15:07:48 +01:00 committed by GitHub
parent baf2f9a4ce
commit e7f587e9f0
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
4 changed files with 196 additions and 0 deletions

View File

@ -0,0 +1,66 @@
<?php
/**
* alert-rules.inc.php
*
* LibreNMS alert-rules.inc.php for processor
*
* 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 Thomas Berberich
* @author Thomas Berberich <sourcehhdoctor@gmail.com>
*/
use LibreNMS\Alerting\QueryBuilderParser;
use LibreNMS\Config;
header('Content-type: application/json');
if (!Auth::user()->hasGlobalAdmin()) {
die(json_encode([
'status' => 'error',
'message' => 'ERROR: You need to be admin',
]));
}
$rule_id = $vars['rule_id'];
if (is_numeric($rule_id)) {
$rule = dbFetchRow('SELECT * FROM alert_rules where id=?', array($rule_id));
$default_extra = [
'mute' => Config::get('alert_rule.mute_alerts'),
'count' => Config::get('alert_rule.max_alerts'),
'delay' => 60 * Config::get('alert_rule.delay'),
'invert' => Config::get('alert_rule.invert_rule_match'),
'interval' => 60 * Config::get('alert_rule.interval'),
'recovery' => Config::get('alert_rule.recovery_alerts'),
];
$output = [
'status' => 'ok',
'name' => $rule['name'] . ' - Copy',
'builder' => QueryBuilderParser::fromJson($rule['builder']),
'extra' => array_replace($default_extra, (array)json_decode($rule['extra'])),
'severity' => $rule['severity'] ?: Config::get('alert_rule.severity'),
'invert_map' => $rule['invert_map'],
];
} else {
$output = [
'status' => 'error',
'message' => 'Invalid template'
];
}
die(json_encode($output));

View File

@ -0,0 +1,123 @@
<?php
/**
* alert_rule_list.inc.php
*
* LibreNMS alert_rule_list modal
*
* 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 Thomas Berberich
* @author Thomas Berberich <sourcehhdoctor@gmail.com>
*/
use LibreNMS\Alerting\QueryBuilderParser;
if (!Auth::user()->hasGlobalAdmin()) {
die('ERROR: You need to be admin');
}
?>
<div class="modal fade" id="search_alert_rule_modal" tabindex="-1" role="dialog" aria-labelledby="search_alert_rule_list" 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="search_alert_rule_list">Running Alert rules</h5>
</div>
<div class="modal-body">
<div class="table-responsive">
<table id="alert_rule_list" class="table table-condensed table-hover">
<thead>
<tr>
<th data-column-id="alert_name" data-width="200px">Name</th>
<th data-column-id="alert_rule">Rule</th>
<th data-column-id="alert_severity">Severity</th>
<td data-column-id="alert_action" data-formatter="alert_action"></td>
</tr>
</thead>
<?php
$alert_rules = dbFetchRows('SELECT * FROM alert_rules order by name');
foreach ($alert_rules as $rule) {
if (empty($rule['builder'])) {
$rule_display = $rule['rule'];
} elseif ($rule_extra['options']['override_query'] === 'on') {
$rule_display = 'Custom SQL Query';
} else {
$rule_display = QueryBuilderParser::fromJson($rule['builder'])->toSql(false);
}
echo "
<tr>
<td>{$rule['name']}</td>
<td><i>".htmlentities($rule_display)."</i></td>
<td>{$rule['severity']}</td>
<td>{$rule['id']}</td>
</tr>
";
}
?>
</table>
<script>
var alert_grid = $("#alert_rule_list").bootgrid({
caseSensitive: false,
formatters: {
"alert_action": function (column, row) {
return "<button type=\"button\" id=\"alert_rule_from_list\" name=\"alert_rule_from_list\" data-rule_id=\"" + row.alert_action + "\" class=\"btn btn-sm btn-primary alert_rule_from_list\">Select</button";
}
},
templates: {
footer: "<div id=\"{{ctx.id}}\" class=\"{{css.footer}}\"><div class=\"row\"><div class=\"col-sm-12\"><p class=\"{{css.pagination}}\"></p></div></div></div>"
}
}).on("loaded.rs.jquery.bootgrid", function()
{
alert_grid.find(".alert_rule_from_list").on("click", function(e) {
var alert_rule_id = $(this).data("rule_id");
$.ajax({
type: "POST",
url: "ajax_form.php",
data: {type: 'sql-from-alert-rules', rule_id: alert_rule_id},
dataType: "json",
success: function (data) {
if (data.status == 'ok') {
$("#search_alert_rule_modal").one('hidden.bs.modal', function(event) {
loadRule(data);
$('#create-alert').modal('show');
});
$("#search_alert_rule_modal").modal('hide');
} else {
toastr.error(data.message);
}
},
error: function () {
toastr.error('Failed to process template');
}
});
}).end();
});
</script>
</div>
</div>
</div>
</div>
</div>
<script>
$("#search_alert_rule_modal").on('hidden.bs.modal', function(e) {
$("#alert_rule_id").val('');
$("#rule_suggest").val('');
$("#rule_display").html('');
});
</script>

View File

@ -72,6 +72,7 @@ if (Auth::user()->hasGlobalAdmin()) {
<li><a href="#" name="import-query" id="import-query">SQL Query</a></li>
<li><a href="#" name="import-old-format" id="import-old-format">Old Format</a></li>
<li><a href="#" name="import-collection" id="import-collection">Collection</a></li>
<li><a href="#" name="import-alert_rule" id="import-alert_rule">Alert Rule</a></li>
</ul>
</div>
</div>
@ -282,6 +283,11 @@ if (Auth::user()->hasGlobalAdmin()) {
$("#search_rule_modal").modal('show');
});
$('#import-alert_rule').on('click', function (e) {
e.preventDefault();
$("#search_alert_rule_modal").modal('show');
});
$('#create-alert').on('show.bs.modal', function(e) {
//get data-id attribute of the clicked element
var rule_id = $(e.relatedTarget).data('rule_id');

View File

@ -77,6 +77,7 @@ 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'; // 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/alert_rule_list.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';