Device groups rewrite (#10346)

* Device Groups rewrite
Updated web ui
Static or dynamic groups allowed
Alert rule query builder
Translation support
Permissions support

* cleanup, make relationship save, and validate it

* builder WIP

* rules builder and rules saving/loading

* Parse query builder to Laravel Fluent query

* Upgrade existing groups when editing.
Properly update only dynamic groups when polling.

* remove unused old code
Update API and other places to use Eloquent

* debug output in poller restored

* Fix up some things
creating static
improved validation
fix js error on creation
Fix static groups in polling

* hide pattern for static group

* Implement authorization
Use in the menu too

* update schema

* fix rollback

* Don't abort on invalid queries

* fixes to query builder

* add test data, looks like macros aren't handled (omitted them because groups don't use them generally)

* Add macro support for QueryBuilderFluentParser

* add test for macro that accepts value

* More space in forms
Retain rules when converted to static
no duplicate names allowed

* Better error feedback
Update related devices on save

* Add button icon

* format

* update docs

* fix tests

* Fix some QueryBuilderFluentParser issues with OR
updated/more test data

* Show device groups runtime
fix querybuilder.json format

* Store table joins in the rules to minimize polling time
Update group joins in daily.sh (and when they are saved)

* Update daily.php

* Add units to time
This commit is contained in:
Tony Murray
2019-06-19 16:01:53 -05:00
committed by GitHub
parent d8931e1946
commit 1a60c44eb0
38 changed files with 1065 additions and 1360 deletions

View File

@@ -12,6 +12,8 @@
* the source code distribution for details.
*/
use App\Models\Device;
use App\Models\DeviceGroup;
use LibreNMS\Alerting\QueryBuilderParser;
use LibreNMS\Authentication\LegacyAuth;
use LibreNMS\Config;
@@ -826,7 +828,7 @@ function list_available_health_graphs()
'name' => 'device_'.$graph['sensor_class'],
);
}
$device = \App\Models\Device::find($device_id);
$device = Device::find($device_id);
if ($device) {
if ($device->processors()->count() > 0) {
@@ -1820,21 +1822,24 @@ function get_device_groups()
{
$app = \Slim\Slim::getInstance();
$router = $app->router()->getCurrentRoute()->getParams();
$status = 'error';
$code = 404;
$hostname = $router['hostname'];
// use hostname as device_id if it's all digits
$device_id = ctype_digit($hostname) ? $hostname : getidbyname($hostname);
if (is_numeric($device_id)) {
$groups = GetGroupsFromDevice($device_id, 1);
if (!empty($router['hostname'])) {
$device = ctype_digit($router['hostname']) ? Device::find($router['hostname']) : Device::findByHostname($router['hostname']);
if (is_null($device)) {
api_error(404, 'Device not found');
}
$query = $device->groups();
} else {
$groups = GetDeviceGroups();
$query = DeviceGroup::query();
}
if (empty($groups)) {
$groups = $query->orderBy('name')->get();
if ($groups->isEmpty()) {
api_error(404, 'No device groups found');
}
api_success($groups, 'groups', 'Found ' . count($groups) . ' device groups');
api_success($groups->makeHidden('pivot')->toArray(), 'groups', 'Found ' . $groups->count() . ' device groups');
}
function get_devices_by_group()
@@ -1842,19 +1847,25 @@ function get_devices_by_group()
check_is_read();
$app = \Slim\Slim::getInstance();
$router = $app->router()->getCurrentRoute()->getParams();
$name = urldecode($router['name']);
$devices = array();
$full = $_GET['full'];
if (empty($name)) {
if (empty($router['name'])) {
api_error(400, 'No device group name provided');
}
$group_id = dbFetchCell("SELECT `id` FROM `device_groups` WHERE `name`=?", array($name));
$devices = GetDevicesFromGroup($group_id, true, $full);
if (empty($devices)) {
$name = urldecode($router['name']);
$device_group = ctype_digit($name) ? DeviceGroup::find($name) : DeviceGroup::where('name', $name)->first();
if (empty($device_group)) {
api_error(404, 'Device group not found');
}
$devices = $device_group->devices()->get(empty($_GET['full']) ? ['devices.device_id'] : ['*']);
if ($devices->isEmpty()) {
api_error(404, 'No devices found in group ' . $name);
}
api_success($devices, 'devices');
api_success($devices->makeHidden('pivot')->toArray(), 'devices');
}
@@ -2050,7 +2061,7 @@ function get_fdb()
}
check_device_permission($device_id);
$device = \App\Models\Device::find($device_id);
$device = Device::find($device_id);
if ($device) {
$fdb = $device->portsFdb;
api_success($fdb, 'ports_fdb');

View File

@@ -115,11 +115,8 @@ if (defined('SHOW_SETTINGS')) {
<select class="form-control" name="group">';
$common_output[] = '<option value=""' . ($current_group == '' ? ' selected' : '') . '>any group</option>';
$device_groups = GetDeviceGroups();
$common_output[] = "<!-- " . print_r($device_groups, true) . " -->";
foreach ($device_groups as $group) {
$group_id = $group['id'];
$common_output[] = "<option value=\"$group_id\"" . (is_numeric($current_group) && $current_group == $group_id ? ' selected' : '') . ">" . $group['name'] . " - " . $group['description'] . "</option>";
foreach (\App\Models\DeviceGroup::orderBy('name')->get(['id', 'name', 'desc']) as $group) {
$common_output[] = "<option value=\"$group->id\"" . (is_numeric($current_group) && $current_group == $group->id ? ' selected' : '') . ">" . $group->name . " - " . $group->desc . "</option>";
}
$common_output[] = '
</select>

View File

@@ -1,53 +0,0 @@
<?php
/*
* LibreNMS
*
* Copyright (c) 2014 Neil Lathwood <https://github.com/laf/ http://www.lathwood.co.uk/fa>
*
* 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.
*/
use LibreNMS\Authentication\LegacyAuth;
if (!LegacyAuth::user()->hasGlobalAdmin()) {
die('ERROR: You need to be admin');
}
$pattern = $_POST['patterns'];
$group_id = $_POST['group_id'];
$name = mres($_POST['name']);
$desc = mres($_POST['desc']);
if (is_array($pattern)) {
$pattern = implode(' ', $pattern);
} elseif (!empty($_POST['pattern']) && !empty($_POST['condition']) && !empty($_POST['value'])) {
$pattern = '%'.$_POST['pattern'].' '.$_POST['condition'].' ';
if (is_numeric($_POST['value'])) {
$pattern .= $_POST['value'];
} else {
$pattern .= '"'.$_POST['value'].'"';
}
}
if (empty($pattern)) {
$update_message = 'ERROR: No group was generated';
} elseif (is_numeric($group_id) && $group_id > 0) {
if (EditDeviceGroup($group_id, $name, $desc, $pattern)) {
$update_message = "Edited Group: <i>$name: $pattern</i>";
} else {
$update_message = 'ERROR: Failed to edit Group: <i>'.$pattern.'</i>';
}
} else {
if (AddDeviceGroup($name, $desc, $pattern)) {
$update_message = "Added Group: <i>$name: $pattern</i>";
} else {
$update_message = 'ERROR: Failed to add Group: <i>'.$pattern.'</i>';
}
}
echo $update_message;

View File

@@ -1,36 +0,0 @@
<?php
/*
* LibreNMS
*
* Copyright (c) 2014 Neil Lathwood <https://github.com/laf/ http://www.lathwood.co.uk/fa>
*
* 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.
*/
use LibreNMS\Authentication\LegacyAuth;
header('Content-type: text/plain');
if (!LegacyAuth::user()->hasGlobalAdmin()) {
die('ERROR: You need to be admin');
}
if (!is_numeric($_POST['group_id'])) {
echo 'ERROR: No group selected';
exit;
} else {
if (dbDelete('device_groups', '`id` = ?', array($_POST['group_id']))) {
dbDelete('alert_group_map', 'group_id=?', [$_POST['group_id']]);
echo 'Group has been deleted.';
exit;
} else {
echo 'ERROR: Group has not been deleted.';
exit;
}
}

View File

@@ -1,41 +0,0 @@
<?php
/*
* LibreNMS
*
* Copyright (c) 2014 Neil Lathwood <https://github.com/laf/ http://www.lathwood.co.uk/fa>
*
* 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.
*/
use LibreNMS\Authentication\LegacyAuth;
if (!LegacyAuth::user()->hasGlobalAdmin()) {
header('Content-type: text/plain');
die('ERROR: You need to be admin');
}
$group_id = $_POST['group_id'];
if (is_numeric($group_id) && $group_id > 0) {
$group = dbFetchRow('SELECT * FROM `device_groups` WHERE `id` = ? LIMIT 1', array($group_id));
$group_split = preg_split('/([a-zA-Z0-9_\-\.\=\%\<\>\ \"\'\!\~\(\)\*\/\@\[\]\^\$]+[&&\|\|]+)/', $group['pattern'], -1, (PREG_SPLIT_DELIM_CAPTURE | PREG_SPLIT_NO_EMPTY));
$count = (count($group_split) - 1);
if (preg_match('/\&\&$/', $group_split[$count]) == 1 || preg_match('/\|\|$/', $group_split[$count]) == 1) {
$group_split[$count] = $group_split[$count];
} else {
$group_split[$count] = $group_split[$count].' &&';
}
$output = array(
'name' => $group['name'],
'desc' => $group['desc'],
'pattern' => $group_split,
);
header('Content-type: application/json');
echo _json_encode($output);
}

View File

@@ -1,71 +0,0 @@
<?php
/*
* LibreNMS
*
* Copyright (c) 2014 Neil Lathwood <https://github.com/laf/ http://www.lathwood.co.uk/fa>
*
* 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.
*/
use LibreNMS\Authentication\LegacyAuth;
if (!LegacyAuth::user()->hasGlobalAdmin()) {
die('ERROR: You need to be admin');
}
?>
<div class="modal fade" id="confirm-delete" tabindex="-1" role="dialog" aria-labelledby="Delete" aria-hidden="true">
<div class="modal-dialog modal-sm">
<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="Delete">Confirm Delete</h5>
</div>
<div class="modal-body">
<p>If you would like to remove the device group then please click Delete.</p>
</div>
<div class="modal-footer">
<form role="form" class="remove_token_form">
<button type="button" class="btn btn-default" data-dismiss="modal">Cancel</button>
<button type="submit" class="btn btn-danger danger" id="device-group-removal" data-target="device-group-removal">Delete</button>
<input type="hidden" name="group_id" id="group_id" value="">
<input type="hidden" name="confirm" id="confirm" value="yes">
</form>
</div>
</div>
</div>
</div>
<script>
$('#confirm-delete').on('show.bs.modal', function(e) {
group_id = $(e.relatedTarget).data('group_id');
$("#group_id").val(group_id);
});
$('#device-group-removal').click('', function(event) {
event.preventDefault();
var group_id = $("#group_id").val();
$.ajax({
type: 'POST',
url: 'ajax_form.php',
data: { type: "delete-device-group", group_id: group_id },
dataType: "html",
success: function(msg) {
if(msg.indexOf("ERROR:") <= -1) {
$("#row_"+group_id).remove();
}
$("#message").html('<div class="alert alert-info">'+msg+'</div>');
$("#confirm-delete").modal('hide');
},
error: function() {
$("#message").html('<div class="alert alert-info">The device group could not be deleted.</div>');
$("#confirm-delete").modal('hide');
}
});
});
</script>

View File

@@ -1,252 +0,0 @@
<?php
/*
* LibreNMS
*
* Copyright (c) 2014 Neil Lathwood <https://github.com/laf/ http://www.lathwood.co.uk/fa>
*
* 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.
*/
use LibreNMS\Authentication\LegacyAuth;
if (LegacyAuth::user()->hasGlobalAdmin()) {
?>
<div class="modal fade bs-example-modal-sm" id="create-group" tabindex="-1" role="dialog" aria-labelledby="Create" aria-hidden="true">
<div class="modal-dialog">
<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="Create">Device Groups</h5>
</div>
<div class="modal-body">
<form method="post" role="form" id="devices-group" class="form-horizontal group-form">
<div class="form-group">
<div class="col-sm-12">
<span id="ajax_response"></span>
</div>
</div>
<div class='form-group'>
<label for='name' class='col-sm-3 control-label'>Name: </label>
<div class='col-sm-9'>
<input type='text' id='name' name='name' class='form-control has-feedback' maxlength='200'>
</div>
</div>
<div class='form-group'>
<label for='desc' class='col-sm-3 control-label'>Description: </label>
<div class='col-sm-9'>
<input type='text' id='desc' name='desc' class='form-control has-feedback' maxlength='200'>
</div>
</div>
<input type="hidden" name="group_id" id="group_id" value="">
<input type="hidden" name="type" id="type" value="create-device-group">
<div class="form-group">
<label for='pattern' class='col-sm-3 control-label'>Pattern: </label>
<div class="col-sm-5">
<input type='text' id='suggest' name='pattern' class='form-control has-feedback' placeholder='I.e: devices.status'/>
</div>
</div>
<div class="form-group">
<div class="col-sm-offset-3 col-sm-9">
<p>Start typing for suggestions, use '.' for indepth selection</p>
</div>
</div>
<div class="form-group">
<label for='condition' class='col-sm-3 control-label'>Condition: </label>
<div class="col-sm-5">
<select id='condition' name='condition' placeholder='Condition' class='form-control has-feedback'>
<option value='='>Equals</option>
<option value='!='>Not Equals</option>
<option value='~'>Like</option>
<option value='!~'>Not Like</option>
<option value='>'>Larger than</option>
<option value='>='>Larger than or Equals</option>
<option value='<'>Smaller than</option>
<option value='<='>Smaller than or Equals</option>
</select>
</div>
</div>
<div class="form-group">
<label for='value' class='col-sm-3 control-label'>Value: </label>
<div class="col-sm-5">
<input type='text' id='value' name='value' class='form-control has-feedback'/>
</div>
</div>
<div class="form-group">
<label for='group-glue' class='col-sm-3 control-label'>Connection: </label>
<div class="col-sm-5">
<button class="btn btn-warning btn-sm" type="submit" name="group-glue" value="&&" id="and" name="and">And</button>
<button class="btn btn-warning btn-sm" type="submit" name="group-glue" value="||" id="or" name="or">Or</button>
<span id="next-step-and"></span>
</div>
</div>
<div class="row">
<div class="col-md-12">
<span id="response"></span>
</div>
</div>
<div class="form-group">
<div class="col-sm-offset-3 col-sm-3">
<button class="btn btn-success btn-sm" type="submit" name="group-submit" id="group-submit" value="save">Save Group</button>
</div>
</div>
</form>
</div>
</div>
</div>
</div>
<script>
$('#create-group').on('hide.bs.modal', function (event) {
$('#response').data('tagmanager').empty();
$('#name').val('');
$('#desc').val('');
});
$('#create-group').on('show.bs.modal', function (event) {
var button = $(event.relatedTarget);
var group_id = button.data('group_id');
var modal = $(this)
$('#group_id').val(group_id);
$('#tagmanager').tagmanager();
$('#response').tagmanager({
strategy: 'array',
tagFieldName: 'patterns[]'
});
if (group_id > 0) {
$.ajax({
type: "POST",
url: "ajax_form.php",
data: {type: "parse-device-group", group_id: group_id},
dataType: "json",
success: function (output) {
var arr = [];
$.each(output['pattern'], function (key, value) {
arr.push(value);
});
$('#response').data('tagmanager').populate(arr);
$('#name').val(output['name']);
$('#desc').val(output['desc']);
}
});
}
});
var cache = {};
var suggestions = new Bloodhound({
datumTokenizer: Bloodhound.tokenizers.obj.whitespace('name'),
queryTokenizer: Bloodhound.tokenizers.whitespace,
remote: {
url: "ajax_rulesuggest.php?device_id=-1&term=%QUERY",
filter: function (output) {
return $.map(output, function (item) {
return {
name: item.name,
};
});
},
wildcard: "%QUERY"
}
});
suggestions.initialize();
$('#suggest').typeahead({
hint: true,
highlight: true,
minLength: 1,
classNames: {
menu: 'typeahead-left'
}
},
{
source: suggestions.ttAdapter(),
async: true,
displayKey: 'name',
valueKey: name,
templates: {
suggestion: Handlebars.compile('<p>&nbsp;{{name}}</p>')
}
});
$('#and, #or').click('', function(e) {
e.preventDefault();
$("#next-step-and").html("");
var entity = $('#suggest').val();
var condition = $('#condition').val();
var value = $('#value').val();
var glue = $(this).val();
if(entity != '' && condition != '') {
$('#response').tagmanager({
strategy: 'array',
tagFieldName: 'patterns[]'
});
if(value.indexOf("%") < 0 && isNaN(value)) {
value = '"'+value+'"';
}
if(entity.indexOf("%") < 0) {
entity = '%'+entity;
}
$('#response').data('tagmanager').populate([ entity+' '+condition+' '+value+' '+glue ]);
}
});
$('#group-submit').click('', function(e) {
e.preventDefault();
$.ajax({
type: "POST",
url: "ajax_form.php",
data: $('form.group-form').serialize(),
success: function(msg){
if(msg.indexOf("ERROR:") <= -1) {
$("#message").html('<div class="alert alert-info">'+msg+'</div>');
$("#create-group").modal('hide');
$('#response').data('tagmanager').empty();
setTimeout(function() {
location.reload(1);
}, 1000);
} else {
$('#ajax_response').html('<div class="alert alert-danger alert-dismissible"><button type="button" class="close" data-dismiss="alert" aria-label="Close"><span aria-hidden="true">&times;</span></button>'+msg+'</div>');
}
},
error: function(){
$("#message").html('<div class="alert alert-info">An error occurred creating this group.</div>');
$("#create-group").modal('hide');
}
});
});
$( "#name, #suggest, #value" ).blur(function() {
var $this = $(this);
var name = $('#name').val();
var suggest = $('#suggest').val();
var value = $('#value').val();
if (name == "") {
$("#next-step-and").html("");
$("#suggest").closest('.form-group').removeClass('has-error');
$("#value").closest('.form-group').removeClass('has-error');
$("#name").closest('.form-group').addClass('has-error');
} else if (suggest == "") {
$("#next-step-and").html("");
$("#name").closest('.form-group').removeClass('has-error');
$("#value").closest('.form-group').removeClass('has-error');
$("#suggest").closest('.form-group').addClass('has-error');
} else if (value == "") {
$("#next-step-and").html("");
$("#name").closest('.form-group').removeClass('has-error');
$("#suggest").closest('.form-group').removeClass('has-error');
$("#value").closest('.form-group').addClass('has-error');
} else {
$("#name").closest('.form-group').removeClass('has-error');
$("#suggest").closest('.form-group').removeClass('has-error');
$("#value").closest('.form-group').removeClass('has-error');
$("#next-step-and").html('<i class="fa fa-long-arrow-left fa-col-danger"></i> Click AND / OR');
}
});
</script>
<?php
}

View File

@@ -1,37 +0,0 @@
<?php
require_once 'includes/html/modal/new_device_group.inc.php';
require_once 'includes/html/modal/delete_device_group.inc.php';
$no_refresh = true;
$group_count_check = array_filter(GetDeviceGroups());
if (!empty($group_count_check)) {
echo '<div class="row"><div class="col-sm-12"><span id="message"></span></div></div>';
echo '<div class="table-responsive">';
echo '<table class="table table-condensed table-hover"><thead><tr>';
echo '<th>Name</th><th>Description</th><th>Pattern</th><th>Actions</th>';
echo '</tr></thead><tbody>';
foreach (GetDeviceGroups() as $group) {
echo '<tr id="row_'.$group['id'].'">';
echo '<td>'.$group['name'].'</td>';
echo '<td>'.$group['desc'].'</td>';
echo '<td>'.formatDeviceGroupPattern($group['pattern'], json_decode($group['params'])).'</td>';
echo '<td>';
echo "<button type='button' class='btn btn-primary btn-sm' aria-label='Edit' data-toggle='modal' data-target='#create-group' data-group_id='".$group['id']."' name='edit-device-group'";
if (!is_null($group['params'])) {
echo " disabled title='LibreNMS V2 device groups cannot be edited in LibreNMS V1'";
}
echo "><i class='fa fa-pencil' aria-hidden='true'></i></button> ";
echo "<button type='button' class='btn btn-danger btn-sm' aria-label='Delete' data-toggle='modal' data-target='#confirm-delete' data-group_id='".$group['id']."' name='delete-device-group'><i class='fa fa-trash' aria-hidden='true'></i></button>";
echo '</td>';
echo '</tr>';
}
echo '</tbody></table></div>';
} else { //if $group_count_check is empty, aka no group found, then display a message to the user.
echo "<center>Looks like no groups have been created, let's create one now. Click on <b>Create New Group</b> to create one.</center><br>";
echo "<center><button type='button' class='btn btn-primary btn-sm' aria-label='Add' data-toggle='modal' data-target='#create-group' data-group_id='' name='create-device-group'>Create new Group</button></center>";
}
if (!empty($group_count_check)) { //display create new node group when $group_count_check has a value so that the user can define more groups in the future.
echo "<hr>";
echo "<center><button type='button' class='btn btn-primary btn-sm' aria-label='Add' data-toggle='modal' data-target='#create-group' data-group_id='' name='create-device-group'>Create new Group</button></center>";
}

View File

@@ -180,7 +180,7 @@ if ($format == "graph") {
}
if (!empty($vars['group'])) {
$where .= " AND ( ";
foreach (GetDevicesFromGroup($vars['group']) as $dev) {
foreach (DB::table('device_group_device')->where('device_group_id', $vars['group'])->pluck('device_id') as $dev) {
$where .= "device_id = ? OR ";
$sql_param[] = $dev;
}