Rewrite Poller Management to Blade/Eloquent (#11277)

* Rewrite Poller Management to Blade/Eloquent

* remove further no more needed file

* Code Climate

* Code Climate

* hardcode tabs

* change URL style

* change prefix from poller-groups to poller

* .

* parameter fix

* Refactor to use more direct routing.
remove switch statement.

Co-authored-by: Tony Murray <murraytony@gmail.com>
This commit is contained in:
SourceDoctor
2020-03-13 20:58:37 +01:00
committed by GitHub
parent c9e90cc827
commit 4e349efd11
21 changed files with 617 additions and 610 deletions

View File

@@ -1,159 +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.
*/
if (!Auth::user()->hasGlobalAdmin()) {
echo ('ERROR: You need to be admin');
} else {
?>
<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 Poller Group then please click Delete.</p>
</div>
<div class="modal-footer">
<form role="form" class="remove_group_form">
<?php echo csrf_field() ?>
<button type="button" class="btn btn-default" data-dismiss="modal">Cancel</button>
<button type="submit" class="btn btn-danger danger" id="group-removal" data-target="group-removal">Delete</button>
<input type="hidden" name="group_id" id="group_id" value="">
<input type="hidden" name="type" id="type" value="poller-group-remove">
<input type="hidden" name="confirm" id="confirm" value="yes">
</form>
</div>
</div>
</div>
</div>
<div class="modal fade bs-example-modal-lg" id="poller-groups" tabindex="-1" role="dialog" aria-labelledby="Create" 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>
<h4 class="modal-title" id="Create">Poller Groups</h4>
</div>
<div class="modal-body">
<form method="post" role="form" id="poller_groups" class="form-horizontal poller-groups-form">
<?php echo csrf_field() ?>
<input type="hidden" name="group_id" id="group_id" value="">
<div class="row">
<div class="col-md-12">
<span id="response"></span>
</div>
</div>
<div class="row">
<div class="col-md-12">
<div class="form-group">
<label for="group_name" class="col-sm-3 control-label">Group Name:</label>
<div class="col-sm-9">
<input type="input" class="form-control" id="group_name" name="group_name" placeholder="Group Name">
</div>
</div>
<div class="form-group">
<label for="descr" class="col-sm-3 control-label">Description:</label>
<div class="col-sm-9">
<input type="input" class="form-control" id="descr" name="descr" placeholder="Description">
</div>
</div>
<div class="form-group">
<div class="col-sm-offset-3 col-sm-9">
<button type="submit" class="btn btn-primary btn-sm" id="create-group" name="create-group">Add Poller Group</button>
</div>
</div>
</div>
</div>
</div>
</div>
</div>
</div>
</form>
<script>
$('#confirm-delete').on('show.bs.modal', function(e) {
group_id = $(e.relatedTarget).data('group_id');
$("#group_id").val(group_id);
});
$('#group-removal').click('', function(e) {
e.preventDefault();
group_id = $("#group_id").val();
$.ajax({
type: "POST",
url: "ajax_form.php",
data: $('form.remove_group_form').serialize() ,
success: function(msg) {
$("#thanks").html('<div class="alert alert-info">'+msg+'</div>');
$("#confirm-delete").modal('hide');
$("#"+group_id).remove();
},
error: function() {
$("#thanks").html('<div class="alert alert-info">An error occurred removing the token.</div>');
$("#confirm-delete").modal('hide');
}
});
});
$('#poller-groups').on('show.bs.modal', function (event) {
var button = $(event.relatedTarget);
var group_id = button.data('group_id');
$('#group_id').val(group_id);
if(group_id != '') {
$('#group_id').val(group_id);
$.ajax({
type: "POST",
url: "ajax_form.php",
data: { type: "parse-poller-groups", group_id: group_id },
dataType: "json",
success: function(output) {
$('#group_name').val(output['group_name']);
$('#descr').val(output['descr']);
}
});
}
});
$('#create-group').click('', function(e) {
e.preventDefault();
var group_name = $("#group_name").val();
var descr = $("#descr").val();
var group_id = $('#group_id').val();
$.ajax({
type: "POST",
url: "ajax_form.php",
data: { type: "poller-groups", group_name: group_name, descr: descr, group_id: group_id },
dataType: "html",
success: function(msg){
if(msg.indexOf("ERROR:") <= -1) {
$("#message").html('<div class="alert alert-info">'+msg+'</div>');
$("#poller-groups").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.</div>');
}
});
});
</script>
<?php
}

View File

@@ -1,55 +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.
*/
$no_refresh = true;
echo '<ul class="nav nav-tabs">';
$poll_tabs = [
[
'name' => 'Pollers',
'icon' => 'fa-th-large',
],
];
if (\LibreNMS\Config::get('distributed_poller')) {
$poll_tabs[] = array(
'name' => 'Groups',
'icon' => 'fa-th',
);
}
$poll_tabs[] = [
'name' => 'Performance',
'icon' => 'fa-line-chart',
];
$poll_tabs[] = [
'name' => 'Log',
'icon' => 'fa-file-text',
];
$current_tab = basename($vars['tab'] ?? 'pollers');
foreach ($poll_tabs as $tab) {
$taburl = strtolower($tab['name']);
echo '<li role="presentation" ' . ($current_tab == $taburl ? ' class="active"' : '') . '><a href="';
echo generate_url(['page' => 'pollers', 'tab' => $taburl]);
echo '"><i class="fa ' . $tab['icon'] . ' fa-lg icon-theme" aria-hidden="true"></i> ' . $tab['name'];
echo '</a></li>';
}
echo '</ul>';
include_once "includes/html/pages/pollers/$current_tab.inc.php";

View File

@@ -1,72 +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\Config;
$pagetitle[] = 'Poller Groups';
require_once 'includes/html/modal/poller_groups.inc.php';
?>
<br />
<button type="button" class="btn btn-primary btn-sm" data-toggle="modal" data-target="#poller-groups">Create new poller group</button>
<br /><br />
<div class="table-responsive">
<table class="table table-striped table-bordered table-hover table-condensed">
<tr>
<th>ID</th>
<th>Group Name</th>
<th>Devices</th>
<th>Description</th>
<th>Action</th>
</tr>
<?php
$default_group = ['id' => 0,
'group_name' => 'General',
'descr' => ''];
$group_list = dbFetchRows('SELECT * FROM `poller_groups`');
$default_poller = Config::get('distributed_poller_group');
array_unshift($group_list, $default_group);
foreach ($group_list as $group) {
$group_device_count = dbFetchCell('SELECT COUNT(*) FROM devices WHERE `poller_group`=?', $group['id']);
$group_name = $group['group_name'];
if ($group['id'] == $default_poller) {
$group_name .= ' (default Poller)';
}
echo '
<tr id="'.$group['id'].'">
<td>'.$group['id'].'</td>
<td>'.$group_name.'</td>
<td><a href="/devices/poller_group='.$group['id'].'")">'.$group_device_count.'</a></td>
<td>'.$group['descr'].'</td>';
echo '<td>';
if ($group['id']) {
echo '<button type="button" class="btn btn-success btn-xs" id="'.$group['id'].'" data-group_id="'.$group['id'].'" data-toggle="modal" data-target="#poller-groups">Edit</button> <button type="button" class="btn btn-danger btn-xs" id="'.$group['id'].'" data-group_id="'.$group['id'].'" data-toggle="modal" data-target="#confirm-delete">Delete</button>';
}
echo '</td>
</tr>
';
}
?>
</table>
</div>

View File

@@ -1,68 +0,0 @@
<?php
/**
* log.inc.php
*
* -Description-
*
* 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 Tony Murray
* @author Tony Murray <murraytony@gmail.com>
*/
$no_refresh = true;
$pagetitle[] = 'Poll Log';
if (isset($vars['filter'])) {
$type = $vars['filter'];
}
?>
<table id="poll-log" class="table table-condensed table-hover table-striped">
<thead>
<tr>
<th data-column-id="hostname">Hostname</th>
<th data-column-id="last_polled">Last Polled</th>
<th data-column-id="poller_group">Poller Group</th>
<th data-column-id="last_polled_timetaken" data-order="desc">Polling Duration (Seconds)</th>
</tr>
</thead>
</table>
<script>
searchbar = "<div id=\"{{ctx.id}}\" class=\"{{css.header}}\"><div class=\"row\">"+
"<div class=\"col-sm-8 actionBar\"><span class=\"pull-left\">"+
"<a href='<?php echo generate_url(['page' => 'pollers', 'tab' => 'log']); ?>' class='btn btn-primary btn-sm <?php echo $vars['filter'] == 'unpolled' ? '' : 'active' ?>'>All devices</a> "+
"<a href='<?php echo generate_url(['page' => 'pollers', 'tab' => 'log', 'filter' => 'unpolled']); ?>' class='btn btn-danger btn-sm <?php echo $vars['filter'] == 'unpolled' ? 'active' : '' ?>'>Unpolled devices</a>"+
"</div><div class=\"col-sm-4 actionBar\"><p class=\"{{css.search}}\"></p><p class=\"{{css.actions}}\"></p></div>";
var grid = $("#poll-log").bootgrid({
ajax: true,
rowCount: [50, 100, 250, -1],
columnSelection: false,
templates: {
header: searchbar
},
post: function ()
{
return {
id: "poll-log",
type: "<?php echo $type;?>"
};
},
url: "ajax_table.php"
});
</script>

View File

@@ -1,37 +0,0 @@
<?php
$pagetitle[] = 'Poll Performance';
?>
<br />
<div class="panel panel-default">
<div class="panel-heading">
<h3 class="panel-title">Total Poller Time</h3>
</div>
<div class="panel-body">
<?php
$graph_array = [
'type' => 'global_poller_perf',
'legend' => 'yes',
'height' => 100,
];
require 'includes/html/print-graphrow.inc.php';
?>
</div>
</div>
<div class="panel panel-default">
<div class="panel-heading">
<h3 class="panel-title">Total Poller Time Per Module</h3>
</div>
<div class="panel-body">
<?php
$graph_array = [
'type' => 'global_poller_modules_perf',
'legend' => 'yes',
'height' => 100,
];
require 'includes/html/print-graphrow.inc.php';
?>
</div>
</div>

View File

@@ -1,175 +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\Config;
$pagetitle[] = 'Pollers';
require_once 'includes/html/modal/delete_poller.inc.php';
?>
<br />
<?php
$query = 'SELECT *,UNIX_TIMESTAMP(NOW()) AS `now`, UNIX_TIMESTAMP(`last_polled`) AS `then` FROM `pollers` ORDER BY poller_name';
$rows = dbFetchRows($query);
if (count($rows) !== 0) {
echo '
<div class="panel panel-default">
<div class="panel-heading">
<h3 class="panel-title">Standard Pollers</h3>
</div>
<div class="panel-body">
<div class="table-responsive">
<table class="table table-striped table-bordered table-hover table-condensed">
<tr>
<th>Poller Name</th>
<th>Devices Polled</th>
<th>Total Poll Time</th>
<th>Last Ran</th>
<th>Actions</th>
</tr>';
foreach ($rows as $poller) {
$old = ($poller['now'] - $poller['then']);
$step = Config::get('rrd.step', 300);
if ($old >= $step) {
$row_class = 'danger';
} elseif ($old >= ($step * 0.95)) {
$row_class = 'warning';
} else {
$row_class = 'success';
}
$actions = "";
if (\Auth::user()->hasGlobalAdmin() && $old > ($step * 2)) {
// missed 2 polls show delete button
$actions .= "<button type='button' class='btn btn-danger btn-sm' aria-label='Delete' data-toggle='modal' data-target='#confirm-delete' data-id='{$poller['id']}' data-pollertype='delete-poller' name='delete-poller'><i class='fa fa-trash' aria-hidden='true'></i></button>";
}
echo '
<tr class="'.$row_class.'" id="row_' . $poller['id'] . '">
<td>'.$poller['poller_name'].'</td>
<td>'.$poller['devices'].'</td>
<td>'.$poller['time_taken'].' Seconds</td>
<td>'.$poller['last_polled'].'</td>
<td>'.$actions.'</td>
</tr>
';
}
echo '
</table>
</div>
</div>
</div>';
}
$query = 'SELECT *,UNIX_TIMESTAMP(NOW()) AS `now`, UNIX_TIMESTAMP(`last_report`) AS `then` FROM `poller_cluster` ORDER BY poller_name';
$rows = dbFetchRows($query);
if (count($rows) !== 0) {
echo '
<div class="panel panel-default">
<div class="panel-heading">
<h3 class="panel-title">Poller Cluster Health</h3>
</div>
<div class="panel-body">
<div class="table-responsive">
<table class="table table-striped table-bordered table-condensed">
<tr>
<th>Name</th>
<th>Node ID</th>
<th>Version</th>
<th>Groups Served</th>
<th>Last Checkin</th>
<th>Cluster Master</th>
<th>Job</th>
<th>Workers</th>
<th>Devices Actioned<br><small>Last Interval</small></th>
<th>Devices Pending</th>
<th>Worker Seconds<br><small>Consumed/Maximum</small></th>
<th>Actions</th>
</tr>';
foreach ($rows as $poller) {
$old = ($poller['now'] - $poller['then']);
$step = Config::get('rrd.step', 300);
if ($old >= $step) {
$row_class = 'danger';
} elseif ($old >= ($step * 0.95)) {
$row_class = 'warning';
} else {
$row_class = 'success';
}
$actions = "";
if (\Auth::user()->hasGlobalAdmin() && $old > ($step * 2)) {
// missed 2 polls show delete button
$actions .= "<button type='button' class='btn btn-danger btn-sm' aria-label='Delete' data-toggle='modal' data-target='#confirm-delete' data-id='{$poller['id']}' data-pollertype='delete-cluster-poller' name='delete-cluster-poller'><i class='fa fa-trash' aria-hidden='true'></i></button>";
}
$stat_query = 'SELECT * FROM `poller_cluster_stats` WHERE `parent_poller`=' . $poller['id'] . ';';
$stat_row = dbFetchRows($stat_query);
$stat_count = count($stat_row);
$first_row = true;
foreach ($stat_row as $stats) {
// Emit the row container
echo '<tr class="'.$row_class.'" id="row_' . $poller['id'] . '">';
if ($first_row) {
// On the first iteration, print some rowspanned columns
echo '
<td rowspan="'.$stat_count.'">'.$poller['poller_name'].'</td>
<td rowspan="'.$stat_count.'"' . (empty($poller['node_id']) ? ' class="danger"' : '') . '>'.$poller['node_id'].'</td>
<td rowspan="'.$stat_count.'">'.$poller['poller_version'].'</td>
<td rowspan="'.$stat_count.'">'.$poller['poller_groups'].'</td>
<td rowspan="'.$stat_count.'">'.$poller['last_report'].'</td>
<td rowspan="'.$stat_count.'">'. ($poller['master'] ? "Yes" : "No") .'</td>';
}
// Emit the job stats
echo '
<td>'.$stats['poller_type'].'</td>
<td>'.$stats['workers'].'</td>
<td>'.$stats['devices'].'</td>
<td>'.$stats['depth'].'</td>
<td>'.$stats['worker_seconds'].' / '.$stats['frequency']*$stats['workers'].'</td>';
if ($first_row) {
// On the first iteration, print some rowspanned columns
echo '<td rowspan="'.$stat_count.'">'.$actions.'</td>';
}
// End the row
echo '</tr>';
$first_row = false;
}
}
echo '
</table>
<small>
Worker seconds indicates the maximum polling throughput a node can achieve in perfect conditions. If the consumed is close to the maximum, consider adding more threads, or better tuning your groups.<br>
If there are devices pending but consumed worker seconds is low, your hardware is not sufficient for the number of devices and the poller cannot reach maximum throughput.
</small>
</div>
</div>
</div>';
}
?>