mirror of
https://github.com/librenms/librenms.git
synced 2024-10-07 16:52:45 +00:00
Merge pull request #2623 from adaniels21487/issue-1650
Device Components.
This commit is contained in:
@@ -49,6 +49,8 @@ $app->group(
|
||||
// api/v0/devices/$hostname/graphs
|
||||
$app->get('/:hostname/ports', 'authToken', 'get_port_graphs')->name('get_port_graphs');
|
||||
// api/v0/devices/$hostname/ports
|
||||
$app->get('/:hostname/components', 'authToken', 'get_components')->name('get_components');
|
||||
// api/v0/devices/$hostname/components
|
||||
$app->get('/:hostname/groups', 'authToken', 'get_device_groups')->name('get_device_groups');
|
||||
$app->get('/:hostname/:type', 'authToken', 'get_graph_generic_by_hostname')->name('get_graph_generic_by_hostname');
|
||||
// api/v0/devices/$hostname/$type
|
||||
|
||||
@@ -13,6 +13,7 @@
|
||||
*/
|
||||
|
||||
require_once '../includes/functions.php';
|
||||
require_once '../includes/component.php';
|
||||
require_once '../includes/device-groups.inc.php';
|
||||
|
||||
function authToken(\Slim\Route $route) {
|
||||
@@ -507,6 +508,45 @@ function get_graph_by_portgroup() {
|
||||
}
|
||||
|
||||
|
||||
function get_components() {
|
||||
global $config;
|
||||
$code = 200;
|
||||
$status = 'ok';
|
||||
$message = '';
|
||||
$app = \Slim\Slim::getInstance();
|
||||
$router = $app->router()->getCurrentRoute()->getParams();
|
||||
$hostname = $router['hostname'];
|
||||
|
||||
// Do some filtering if the user requests.
|
||||
$options = array();
|
||||
// We need to specify the label as this is a LIKE query
|
||||
if (isset($_GET['label'])) {
|
||||
// set a label like filter
|
||||
$options['filter']['label'] = array('LIKE',$_GET['label']);
|
||||
unset ($_GET['label']);
|
||||
}
|
||||
// Add the rest of the options with an equals query
|
||||
foreach ($_GET as $k) {
|
||||
$options['filter'][$k] = array('=',$_GET[$k]);
|
||||
}
|
||||
|
||||
// use hostname as device_id if it's all digits
|
||||
$device_id = ctype_digit($hostname) ? $hostname : getidbyname($hostname);
|
||||
$COMPONENT = new component();
|
||||
$components = $COMPONENT->getComponents($device_id,$options);
|
||||
|
||||
$output = array(
|
||||
'status' => "$status",
|
||||
'err-msg' => $message,
|
||||
'count' => count($components[$device_id]),
|
||||
'components' => $components[$device_id],
|
||||
);
|
||||
$app->response->setStatus($code);
|
||||
$app->response->headers->set('Content-Type', 'application/json');
|
||||
echo _json_encode($output);
|
||||
}
|
||||
|
||||
|
||||
function get_graphs() {
|
||||
global $config;
|
||||
$code = 200;
|
||||
|
||||
84
html/includes/forms/component.inc.php
Normal file
84
html/includes/forms/component.inc.php
Normal file
@@ -0,0 +1,84 @@
|
||||
<?php
|
||||
|
||||
if (is_admin() === false) {
|
||||
$response = array(
|
||||
'status' => 'error',
|
||||
'message' => 'Need to be admin',
|
||||
);
|
||||
echo _json_encode($response);
|
||||
exit;
|
||||
}
|
||||
|
||||
$status = 'error';
|
||||
$message = 'Error with config';
|
||||
|
||||
// enable/disable components on devices.
|
||||
$device_id = intval($_POST['device']);
|
||||
|
||||
require_once "../includes/component.php";
|
||||
$OBJCOMP = new component();
|
||||
|
||||
// Go get the component array.
|
||||
$COMPONENTS = $OBJCOMP->getComponents($device_id);
|
||||
|
||||
// We only care about our device id.
|
||||
$COMPONENTS = $COMPONENTS[$device_id];
|
||||
|
||||
// Track how many updates we are making.
|
||||
$UPDATE = array();
|
||||
|
||||
foreach ($COMPONENTS as $ID => $AVP) {
|
||||
// Is the component disabled?
|
||||
if (isset($_POST['dis_'.$ID])) {
|
||||
// Yes it is, was it disabled before?
|
||||
if ($COMPONENTS[$ID]['disabled'] == 0) {
|
||||
// No it wasn't, best we disable it then..
|
||||
$COMPONENTS[$ID]['disabled'] = 1;
|
||||
$UPDATE[$ID] = true;
|
||||
}
|
||||
}
|
||||
else {
|
||||
// No its not, was it disabled before?
|
||||
if ($COMPONENTS[$ID]['disabled'] == 1) {
|
||||
// Yes it was, best we enable it then..
|
||||
$COMPONENTS[$ID]['disabled'] = 0;
|
||||
$UPDATE[$ID] = true;
|
||||
}
|
||||
}
|
||||
|
||||
// Is the component ignored?
|
||||
if (isset($_POST['ign_'.$ID])) {
|
||||
// Yes it is, was it ignored before?
|
||||
if ($COMPONENTS[$ID]['ignore'] == 0) {
|
||||
// No it wasn't, best we ignore it then..
|
||||
$COMPONENTS[$ID]['ignore'] = 1;
|
||||
$UPDATE[$ID] = true;
|
||||
}
|
||||
}
|
||||
else {
|
||||
// No its not, was it ignored before?
|
||||
if ($COMPONENTS[$ID]['ignore'] == 1) {
|
||||
// Yes it was, best we un-ignore it then..
|
||||
$COMPONENTS[$ID]['ignore'] = 0;
|
||||
$UPDATE[$ID] = true;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
if (count($UPDATE) > 0) {
|
||||
// Update our edited components.
|
||||
$STATUS = $OBJCOMP->setComponentPrefs($device_id,$COMPONENTS);
|
||||
|
||||
$message = count($UPDATE).' Device records updated.';
|
||||
$status = 'ok';
|
||||
}
|
||||
else {
|
||||
$message = 'Record unchanged. No update necessary.';
|
||||
$status = 'ok';
|
||||
}
|
||||
|
||||
$response = array(
|
||||
'status' => $status,
|
||||
'message' => $message,
|
||||
);
|
||||
echo _json_encode($response);
|
||||
@@ -1165,6 +1165,16 @@ function alert_details($details) {
|
||||
$fallback = false;
|
||||
}
|
||||
|
||||
if ($tmp_alerts['type'] && $tmp_alerts['label']) {
|
||||
if ($tmp_alerts['error'] == "") {
|
||||
$fault_detail .= ' '.$tmp_alerts['type'].' - '.$tmp_alerts['label'].'; ';
|
||||
}
|
||||
else {
|
||||
$fault_detail .= ' '.$tmp_alerts['type'].' - '.$tmp_alerts['label'].' - '.$tmp_alerts['error'].'; ';
|
||||
}
|
||||
$fallback = false;
|
||||
}
|
||||
|
||||
if ($fallback === true) {
|
||||
foreach ($tmp_alerts as $k => $v) {
|
||||
if (!empty($v) && $k != 'device_id' && (stristr($k, 'id') || stristr($k, 'desc') || stristr($k, 'msg')) && substr_count($k, '_') <= 1) {
|
||||
|
||||
59
html/includes/table/component.inc.php
Normal file
59
html/includes/table/component.inc.php
Normal file
@@ -0,0 +1,59 @@
|
||||
<?php
|
||||
|
||||
$row = 1;
|
||||
|
||||
$device_id = $_POST['device_id'];
|
||||
|
||||
require_once "../includes/component.php";
|
||||
$OBJCOMP = new component();
|
||||
|
||||
// Add a filter if supplied
|
||||
if (isset($searchPhrase) && !empty($searchPhrase)) {
|
||||
$options['filter']['label'] = array('LIKE', $searchPhrase);
|
||||
}
|
||||
|
||||
// Add a Sort option
|
||||
if (!isset($sort) || empty($sort)) {
|
||||
// Nothing supplied, default is id ASC.
|
||||
$options['sort'] = 'id asc';
|
||||
}
|
||||
else {
|
||||
$options['sort'] = $sort;
|
||||
}
|
||||
|
||||
// Define the Limit parameters
|
||||
if (isset($current)) {
|
||||
$start = (($current * $rowCount) - ($rowCount));
|
||||
}
|
||||
if ($rowCount != -1) {
|
||||
$options['limit'] = array($start,$rowCount);
|
||||
}
|
||||
|
||||
$COMPONENTS = $OBJCOMP->getComponents($device_id,$options);
|
||||
|
||||
$response[] = array(
|
||||
'id' => '<button type="submit" id="save-form" class="btn btn-success btn-sm" title="Save current component disable/ignore settings">Save</button><button type="submit" id="form-reset" class="btn btn-danger btn-sm" title="Reset form to when the page was loaded">Reset</button>',
|
||||
'label' => ' ',
|
||||
'status' => '<button type="submit" id="alert-select" class="btn btn-default btn-sm" title="Disable alerting on all currently-alerting components">Alerting</button>',
|
||||
'disable' => '<button type="submit" id="disable-toggle" class="btn btn-default btn-sm" title="Toggle polling for all components">Toggle</button><button type="button" id="disable-select" class="btn btn-default btn-sm" title="Disable polling on all components">Select All</button>',
|
||||
'ignore' => '<button type="submit" id="ignore-toggle" class="btn btn-default btn-sm" title="Toggle alerting for all components">Toggle</button><button type="button" id="ignore-select" class="btn btn-default btn-sm" title="Disable alerting on all components">Select All</button>',
|
||||
);
|
||||
|
||||
foreach ($COMPONENTS[$device_id] as $ID => $AVP) {
|
||||
$response[] = array(
|
||||
'id' => $ID,
|
||||
'type' => $AVP['type'],
|
||||
'label' => $AVP['label'],
|
||||
'status' => ($AVP['status'] ? "<span name='status_".$ID."' class='green'>Normal</span>" : "<span name='status_".$ID."' class='red'>Alert</span>"),
|
||||
'disable' => '<input type="checkbox" class="disable-check" name="dis_'.$ID.'"'.($AVP['disabled'] ? 'checked' : '').'>',
|
||||
'ignore' => '<input type="checkbox" class="ignore-check" name="ign_'.$ID.'"'.($AVP['ignore'] ? 'checked' : '').'>',
|
||||
);
|
||||
}//end foreach
|
||||
|
||||
$output = array(
|
||||
'current' => $current,
|
||||
'rowCount' => $rowCount,
|
||||
'rows' => $response,
|
||||
'total' => count($COMPONENTS[$device_id]),
|
||||
);
|
||||
echo _json_encode($output);
|
||||
@@ -38,6 +38,8 @@ else {
|
||||
$panes['storage'] = 'Storage';
|
||||
$panes['misc'] = 'Misc';
|
||||
|
||||
$panes['component'] = 'Components';
|
||||
|
||||
print_optionbar_start();
|
||||
|
||||
unset($sep);
|
||||
|
||||
102
html/pages/device/edit/component.inc.php
Normal file
102
html/pages/device/edit/component.inc.php
Normal file
@@ -0,0 +1,102 @@
|
||||
<span id="message"><small><div class="alert alert-danger">n.b For the first time, please click any button twice.</div></small></span>
|
||||
|
||||
<form id='components' class='form-inline' method='POST'>
|
||||
<table id='table' class='table table-condensed table-responsive table-striped'>
|
||||
<thead>
|
||||
<tr>
|
||||
<th data-column-id='id'>ID</th>
|
||||
<th data-column-id='type'>Type</th>
|
||||
<th data-column-id='label'>Label</th>
|
||||
<th data-column-id='status'>Status</th>
|
||||
<th data-column-id='disable' data-sortable='false'>Disable</th>
|
||||
<th data-column-id='ignore' data-sortable='false'>Ignore</th>
|
||||
</tr>
|
||||
</thead>
|
||||
</table>
|
||||
<input type='hidden' name='component' value='yes'>
|
||||
<input type='hidden' name='type' value='component'>
|
||||
<input type='hidden' name='device' value='<?php echo $device['device_id'];?>'>
|
||||
</form>
|
||||
<script>
|
||||
// Waiting for the document to be ready.
|
||||
$(document).ready(function() {
|
||||
|
||||
$('form#components').submit(function (event) {
|
||||
|
||||
$('#disable-toggle').click(function (event) {
|
||||
// invert selection on all disable buttons
|
||||
event.preventDefault();
|
||||
$('input[name^="dis_"]').trigger('click');
|
||||
});
|
||||
|
||||
$('#disable-select').click(function (event) {
|
||||
// select all disable buttons
|
||||
event.preventDefault();
|
||||
$('.disable-check').prop('checked', true);
|
||||
});
|
||||
$('#ignore-toggle').click(function (event) {
|
||||
// invert selection on all ignore buttons
|
||||
event.preventDefault();
|
||||
$('input[name^="ign_"]').trigger('click');
|
||||
});
|
||||
$('#ignore-select').click(function (event) {
|
||||
// select all ignore buttons
|
||||
event.preventDefault();
|
||||
$('.ignore-check').prop('checked', true);
|
||||
});
|
||||
$('#alert-select').click(function (event) {
|
||||
// select ignore buttons for all ports which are down
|
||||
event.preventDefault();
|
||||
$('[name^="status_"]').each(function () {
|
||||
var name = $(this).attr('name');
|
||||
var text = $(this).text();
|
||||
if (name && text == 'Alert') {
|
||||
// get the component number from the object name
|
||||
var id = name.split('_')[1];
|
||||
// find its corresponding checkbox and toggle it
|
||||
$('input[name="ign_' + id + '"]').trigger('click');
|
||||
}
|
||||
});
|
||||
});
|
||||
$('#form-reset').click(function (event) {
|
||||
// reset objects in the form to the value when the page was loaded
|
||||
event.preventDefault();
|
||||
$('#components')[0].reset();
|
||||
});
|
||||
$('#save-form').click(function (event) {
|
||||
event.preventDefault();
|
||||
$.ajax({
|
||||
type: "POST",
|
||||
url: "/ajax_form.php",
|
||||
data: $('form#components').serialize(),
|
||||
dataType: "json",
|
||||
success: function(data){
|
||||
if (data.status == 'ok') {
|
||||
$("#message").html('<div class="alert alert-info">' + data.message + '</div>')
|
||||
} else {
|
||||
$("#message").html('<div class="alert alert-danger">' + data.message + '</div>');
|
||||
}
|
||||
},
|
||||
error: function(){
|
||||
$("#message").html('<div class="alert alert-danger">Error creating config item</div>');
|
||||
}
|
||||
});
|
||||
});
|
||||
|
||||
event.preventDefault();
|
||||
});
|
||||
});
|
||||
|
||||
var grid = $("#table").bootgrid({
|
||||
ajax: true,
|
||||
rowCount: [50,100,250,-1],
|
||||
post: function ()
|
||||
{
|
||||
return {
|
||||
id: 'component',
|
||||
device_id: "<?php echo $device['device_id']; ?>"
|
||||
};
|
||||
},
|
||||
url: "/ajax_table.php"
|
||||
});
|
||||
</script>
|
||||
Reference in New Issue
Block a user