Merge pull request #610 from laf/issue-608

Web UI components for dist poller
This commit is contained in:
Neil Lathwood
2015-03-19 16:34:12 +00:00
13 changed files with 533 additions and 7 deletions
+63
View File
@@ -0,0 +1,63 @@
#!/usr/bin/env php
<?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.
*/
chdir(dirname($argv[0]));
include("includes/defaults.inc.php");
include("config.php");
include("includes/definitions.inc.php");
include("includes/functions.php");
include("includes/polling/functions.inc.php");
include("includes/alerts.inc.php");
include('includes/console_table.php');
$options = getopt("l:u:r::");
if (isset($options['l'])) {
if ($options['l'] == 'pollers') {
$tbl = new Console_Table();
$tbl->setHeaders(array('ID','Poller Name','Last Polled','# Devices','Poll Time'));
foreach (dbFetchRows("SELECT * FROM `pollers`") as $poller) {
$tbl->addRow(array($poller['id'],$poller['poller_name'],$poller['last_polled'],$poller['devices'],$poller['time_taken']));
}
echo $tbl->getTable();
} elseif ($options['l'] == 'groups') {
$tbl = new Console_Table();
$tbl->setHeaders(array('ID','Group Name','Description'));
foreach (dbFetchRows("SELECT * FROM `poller_groups`") as $groups) {
$tbl->addRow(array($groups['id'],$groups['group_name'],$groups['descr']));
}
echo $tbl->getTable();
}
} elseif (isset($options['u']) && !empty($options['u'])) {
if (is_numeric($options['u'])) {
$db_column = 'id';
} else {
$db_column = 'poller_name';
}
if (dbDelete('pollers',"`$db_column` = ?", array($options['u'])) >= 0) {
echo "Poller " . $options['u'] . " has been removed\n";
}
} elseif (isset($options['r'])) {
if(dbInsert(array('poller_name' => $config['distributed_poller_name'], 'last_polled' => '0000-00-00 00:00:00', 'devices' => 0, 'time_taken' => 0), 'pollers') >= 0) {
echo "Poller " . $config['distributed_poller_name'] . " has been registered\n";
}
} else {
echo "-l pollers | groups List registered pollers or poller groups\n";
echo "-u <id> | <poller name> Unregister a poller\n";
echo "-r Register this install as a poller\n";
}
?>
+25
View File
@@ -0,0 +1,25 @@
<?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(is_admin() === false) {
die('ERROR: You need to be admin');
}
$group_id = ($_POST['group_id']);
if(is_numeric($group_id) && $group_id > 0) {
$group = dbFetchRow("SELECT * FROM `poller_groups` WHERE `id` = ? LIMIT 1",array($group_id));
$output = array('group_name'=>$group['group_name'],'descr'=>$group['descr']);
echo _json_encode($output);
}
+30
View File
@@ -0,0 +1,30 @@
<?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 (!is_numeric($_POST['group_id'])) {
echo('error with data');
exit;
} else {
if($_POST['confirm'] == 'yes')
{
$delete = dbDelete('poller_groups', '`id` = ?', array($_POST['group_id']));
if ($delete > '0') {
echo('Poller group has been removed');
exit;
} else {
echo('An error occurred removing the Poller group');
exit;
}
}
}
+47
View File
@@ -0,0 +1,47 @@
<?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(is_admin() === false) {
die('ERROR: You need to be admin');
}
$ok = '';
$error = '';
$group_id = $_POST['group_id'];
$group_name = mres($_POST['group_name']);
$descr = mres($_POST['descr']);
if(!empty($group_name)) {
if( is_numeric($group_id)) {
if (dbUpdate(array('group_name' => $group_name, 'descr' => $descr), "poller_groups", "id = ?", array($group_id))) {
$ok = "Updated poller group";
} else {
$error = "Failed to update the poller group";
}
} else {
if (dbInsert(array('group_name' => $group_name, 'descr' => $descr), 'poller_groups') >= 0) {
$ok = "Added new poller group";
} else {
$error = "Failed to create new poller group";
}
}
} else {
$error = "You haven't given your poller group a name, it feels sad :( - $group_name";
}
if(!empty( $ok )) {
die("$ok");
} else {
die("ERROR: $error");
}
?>
+159
View File
@@ -0,0 +1,159 @@
<?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(is_admin() === false) {
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">
<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">
<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);
event.preventDefault();
});
$('#group-removal').click('', function(e) {
event.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
}
?>
+15
View File
@@ -446,6 +446,21 @@ if ($_SESSION['userlevel'] >= '10')
<li><a href="edituser/"><img src="images/16/user_edit.png" border="0" align="absmiddle" /> Edit User</a></li>
<li><a href="authlog/"><img src="images/16/lock.png" border="0" align="absmiddle" /> Authlog</a></li>
<li role="presentation" class="divider"></li>
');
if($config['distributed_poller'] === TRUE) {
echo('
<li class="dropdown-submenu">
<a href="#"><img src="images/16/clock.png" alt="Pollers" width="16" height="16" /> Pollers</a>
<ul class="dropdown-menu scrollable-menu">
<li><a href="/pollers/tab=pollers/"><img src="images/16/clock_link.png" alt="Pollers" width="16" height="16" /> Pollers</a></li>
<li><a href="/pollers/tab=groups/"><img src="images/16/clock_add.png" alt="Groups" width="16" height="16" /> Groups</a></li>
</ul>
</li>
<li role="presentation" class="divider"></li>
');
}
echo('
<li class="dropdown-submenu">
<a href="#"><img src="images/16/building.png" border="0" align="absmiddle" /> API</a>
<ul class="dropdown-menu scrollable-menu">
+25 -1
View File
@@ -50,7 +50,8 @@ if ($_POST['hostname'])
{
print_error("Unsupported SNMP Version. There was a dropdown menu, how did you reach this error ?");
}
$result = addHost($hostname, $snmpver, $port, $transport);
$poller_group = $_POST['poller_group'];
$result = addHost($hostname, $snmpver, $port, $transport,0,$poller_group);
if ($result)
{
print_message("Device added ($result)");
@@ -175,6 +176,29 @@ foreach ($config['snmp']['transports'] as $transport)
</div>
</div>
</div>
<?php
if ($config['distributed_poller'] === TRUE) {
echo('
<div class="form-group">
<label for="poller_group" class="col-sm-3 control-label">Poller Group</label>
<div class="col-sm-9">
<select name="poller_group" id="poller_group" class="form-control input-sm">
<option value="0"> Default poller group</option>
');
foreach (dbFetchRows("SELECT `id`,`group_name` FROM `poller_groups`") as $group) {
echo '<option value="' . $group['id'] . '">' . $group['group_name'] . '</option>';
}
echo('
</select>
</div>
</div>
');
}
?>
<button type="submit" class="btn btn-default input-sm" name="Submit">Add Host</button>
</div>
</form>
+28 -1
View File
@@ -10,6 +10,7 @@ if ($_POST['editing'])
$port = $_POST['port'] ? mres($_POST['port']) : $config['snmp']['port'];
$timeout = mres($_POST['timeout']);
$retries = mres($_POST['retries']);
$poller_group = mres($_POST['poller_group']);
$v3 = array (
'authlevel' => mres($_POST['authlevel']),
'authname' => mres($_POST['authname']),
@@ -24,7 +25,8 @@ if ($_POST['editing'])
'community' => $community,
'snmpver' => $snmpver,
'port' => $port,
'transport' => $transport
'transport' => $transport,
'poller_group' => $poller_group
);
if ($_POST['timeout']) { $update['timeout'] = $timeout; }
@@ -169,6 +171,31 @@ echo(" </select>
</div>
</div>");
if ($config['distributed_poller'] === TRUE) {
echo('
<div class="form-group">
<label for="poller_group" class="col-sm-2 control-label">Poller Group</label>
<div class="col-sm-4">
<select name="poller_group" id="poller_group" class="form-control input-sm">
<option value="0"> Default poller group</option>
');
foreach (dbFetchRows("SELECT `id`,`group_name` FROM `poller_groups`") as $group) {
echo '<option value="' . $group['id'] . '"';
if ($device['poller_group'] == $group['id']) {
echo ' selected';
}
echo '>' . $group['group_name'] . '</option>';
}
echo('
</select>
</div>
</div>
');
}
echo('
<button type="submit" name="Submit" class="btn btn-default">Save</button>
</form>
+34
View File
@@ -0,0 +1,34 @@
<?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.
*/
echo('<ul class="nav nav-tabs">');
$poll_tabs = array(array("name" => 'Pollers', 'icon' => 'clock_link'),
array("name" => 'Groups', 'icon' => 'clock_add'));
foreach ($poll_tabs as $tab) {
echo('
<li>
<a href="/pollers/tab='. lcfirst($tab["name"]) .'">
<img src="images/16/'. $tab["icon"] .'.png" align="absmiddle" border="0"> ' . $tab["name"] . '
</a>
</li>');
}
echo ('</ul>');
if (isset($vars['tab'])) {
require_once "pages/pollers/".mres($vars['tab']).".inc.php";
}
+48
View File
@@ -0,0 +1,48 @@
<?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.
*/
require_once "includes/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>Description</th>
<th>Action</th>
</tr>
<?php
$query = "SELECT * FROM `poller_groups`";
foreach (dbFetchRows($query) as $group) {
echo('
<tr id="'. $group['id'] .'">
<td>'.$group['id'].'</td>
<td>'.$group['group_name'].'</td>
<td>'.$group['descr'].'</td>
<td><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></td>
</tr>
');
}
?>
</table>
</div>
+52
View File
@@ -0,0 +1,52 @@
<?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.
*/
?>
<br />
<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>
</tr>
<?php
$query = "SELECT *,UNIX_TIMESTAMP(NOW()) AS `now`, UNIX_TIMESTAMP(`last_polled`) AS `then` FROM `pollers`";
foreach (dbFetchRows($query) as $poller) {
$old = $poller['now'] - $poller['then'];
if ($old >= 300) {
$row_class = 'danger';
} elseif ($old >= 280 && $old < 300) {
$row_class = 'warning';
} else {
$row_class = 'success';
}
echo('
<tr class="'.$row_class.'">
<td>'.$poller['poller_name'].'</td>
<td>'.$poller['devices'].'</td>
<td>'.$poller['time_taken'].' Seconds</td>
<td>'.$poller['last_polled'].'</td>
</tr>
');
}
?>
</table>
</div>
+6 -5
View File
@@ -266,7 +266,7 @@ function delete_device($id)
return $ret;
}
function addHost($host, $snmpver, $port = '161', $transport = 'udp', $quiet = '0')
function addHost($host, $snmpver, $port = '161', $transport = 'udp', $quiet = '0', $poller_group = '0')
{
global $config;
@@ -315,7 +315,7 @@ function addHost($host, $snmpver, $port = '161', $transport = 'udp', $quiet = '0
$snmphost = snmp_get($device, "sysName.0", "-Oqv", "SNMPv2-MIB");
if (empty($snmphost) or ($snmphost == $host || $hostshort = $host))
{
$device_id = createHost ($host, NULL, $snmpver, $port, $transport, $v3);
$device_id = createHost ($host, NULL, $snmpver, $port, $transport, $v3, $poller_group);
return $device_id;
} else {
if($quiet == '0') {print_error("Given hostname does not match SNMP-read hostname ($snmphost)!"); }
@@ -337,7 +337,7 @@ function addHost($host, $snmpver, $port = '161', $transport = 'udp', $quiet = '0
$snmphost = snmp_get($device, "sysName.0", "-Oqv", "SNMPv2-MIB");
if (empty($snmphost) || ($snmphost && ($snmphost == $host || $hostshort = $host)))
{
$device_id = createHost ($host, $community, $snmpver, $port, $transport);
$device_id = createHost ($host, $community, $snmpver, $port, $transport,array(),$poller_group);
return $device_id;
} else {
if($quiet == '0') { print_error("Given hostname does not match SNMP-read hostname ($snmphost)!"); }
@@ -520,7 +520,7 @@ function utime()
return $sec + $usec;
}
function createHost($host, $community = NULL, $snmpver, $port = 161, $transport = 'udp', $v3 = array())
function createHost($host, $community = NULL, $snmpver, $port = 161, $transport = 'udp', $v3 = array(), $poller_group='0')
{
$host = trim(strtolower($host));
@@ -530,7 +530,8 @@ function createHost($host, $community = NULL, $snmpver, $port = 161, $transport
'port' => $port,
'transport' => $transport,
'status' => '1',
'snmpver' => $snmpver
'snmpver' => $snmpver,
'poller_group' => $poller_group
);
$device = array_merge($device, $v3);
+1
View File
@@ -0,0 +1 @@
CREATE TABLE `poller_groups` (`id` INT NOT NULL AUTO_INCREMENT PRIMARY KEY ,`group_name` VARCHAR( 255 ) NOT NULL ,`descr` VARCHAR( 255 ) NOT NULL) ENGINE = INNODB;