Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

WIP - Feature: Custom OID polling and graphing #8738

Closed
wants to merge 14 commits into from
Closed
167 changes: 167 additions & 0 deletions html/includes/forms/customoid.inc.php
@@ -0,0 +1,167 @@
<?php

use LibreNMS\Authentication\LegacyAuth;

header('Content-type: application/json');

if (!LegacyAuth::user()->hasGlobalAdmin()) {
die(json_encode([
'status' => 'error',
'message' => 'You need to be admin.'
]));
}

$status = 'ok';
$message = '';

$device_id = $_POST['device_id'];
$id = $_POST['ccustomoid_id'];
$action = mres($_POST['action']);
$name = mres($_POST['name']);
$oid = mres($_POST['oid']);
$datatype = mres($_POST['datatype']);
if (empty(mres($_POST['unit']))) {
$unit = array('NULL');
} else {
$unit = mres($_POST['unit']);
}
if (!empty(mres($_POST['limit'])) && is_numeric(mres($_POST['limit']))) {
$limit = mres($_POST['limit']);
} else {
$limit = array('NULL');
}
if (!empty(mres($_POST['limit_warn'])) && is_numeric(mres($_POST['limit_warn']))) {
$limit_warn = mres($_POST['limit_warn']);
} else {
$limit_warn = array('NULL');
}
if (!empty(mres($_POST['limit_low'])) && is_numeric(mres($_POST['limit_low']))) {
$limit_low = mres($_POST['limit_low']);
} else {
$limit_low = array('NULL');
}
if (!empty(mres($_POST['limit_low_warn'])) && is_numeric(mres($_POST['limit_low_warn']))) {
$limit_low_warn = mres($_POST['limit_low_warn']);
} else {
$limit_low_warn = array('NULL');
}
if (mres($_POST['alerts']) == 'on') {
$alerts = 1;
} else {
$alerts = 0;
}
if (mres($_POST['passed']) == 'on') {
$passed = 1;
} else {
$passed = 0;
}
if (!empty(mres($_POST['divisor'])) && is_numeric(mres($_POST['divisor']))) {
$divisor = mres($_POST['divisor']);
} else {
$divisor = 1;
}
if (!empty(mres($_POST['multiplier'])) && is_numeric(mres($_POST['multiplier']))) {
$multiplier = mres($_POST['multiplier']);
} else {
$multiplier = 1;
}
if (!empty(mres($_POST['user_func']))) {
$user_func = mres($_POST['user_func']);
} else {
$user_func = array('NULL');
}

if ($action == "test") {
$query = "SELECT * FROM `devices` WHERE `device_id` = $device_id LIMIT 1";
$device = dbFetchRow($query);

$rawdata = snmp_get($device, $oid, '-Oqv');

if (is_numeric($rawdata)) {
if (dbUpdate(
array(
'customoid_passed' => 1,
),
'customoids',
'customoid_id=?',
array($id)
) >= 0) {
$message = "Test successful for <i>$name</i>, value $rawdata reveiced";
} else {
$status = 'error';
$message = "Failed to set pass on OID <i>$name</i>";
}
} else {
$status = 'error';
$message = "Invalid data in SNMP reply, value $rawdata reveiced";
}
} else {
if (is_numeric($id) && $id > 0) {
if (dbUpdate(
array(
'customoid_descr' => $name,
'customoid_oid' => $oid,
'customoid_datatype' => $datatype,
'customoid_unit' => $unit,
'customoid_divisor' => $divisor,
'customoid_multiplier' => $multiplier,
'customoid_limit' => $limit,
'customoid_limit_warn' => $limit_warn,
'customoid_limit_low' => $limit_low,
'customoid_limit_low_warn' => $limit_low_warn,
'customoid_alert' => $alerts,
'customoid_passed' => $passed,
'user_func' => $user_func
),
'customoids',
"`customoid_id` = ?",
array($id)
) >= 0) { //end if condition
$message = "Edited OID: <i>$name</i>";
} else {
$status = 'error';
$message = "Failed to edit OID <i>$name</i>";
}
} else {
if (empty($name)) {
$status = 'error';
$message = 'No OID name provided';
} else {
if (dbFetchCell('SELECT 1 FROM `customoids` WHERE `customoid_descr` = ? AND `device_id`=?', array($name, $device_id))) {
$status = 'error';
$message = "OID named <i>$name</i> on this device already exists";
} else {
$id = dbInsert(
array(
'device_id' => $device_id,
'customoid_descr' => $name,
'customoid_oid' => $oid,
'customoid_datatype' => $datatype,
'customoid_unit' => $unit,
'customoid_divisor' => $divisor,
'customoid_multiplier' => $multiplier,
'customoid_limit' => $limit,
'customoid_limit_warn' => $limit_warn,
'customoid_limit_low' => $limit_low,
'customoid_limit_low_warn' => $limit_low_warn,
'customoid_alert' => $alerts,
'customoid_passed' => $passed,
'user_func' => $user_func
),
'customoids'
);
if ($id) {
$message = "Added OID: <i>$name</i>";
} else {
$status = 'error';
$message = "Failed to add OID: <i>$name</i>";
}
}
}
}
}

die(json_encode([
'status' => $status,
'message' => $message,
]));
22 changes: 22 additions & 0 deletions html/includes/forms/delete-customoid.inc.php
@@ -0,0 +1,22 @@
<?php

use LibreNMS\Authentication\LegacyAuth;

header('Content-type: text/plain');

if (!LegacyAuth::user()->hasGlobalAdmin()) {
die('ERROR: You need to be admin');
}

if (!is_numeric($_POST['customoid_id'])) {
echo 'ERROR: No alert selected';
exit;
} else {
if (dbDelete('customoids', '`customoid_id` = ?', array($_POST['customoid_id']))) {
echo 'Custom OID has been deleted.';
exit;
} else {
echo 'ERROR: Custom OID has not been deleted.';
exit;
}
}
45 changes: 45 additions & 0 deletions html/includes/forms/parse-customoid.inc.php
@@ -0,0 +1,45 @@
<?php

use LibreNMS\Authentication\LegacyAuth;

if (!LegacyAuth::user()->hasGlobalAdmin()) {
header('Content-type: text/plain');
die('ERROR: You need to be admin');
}

$customoid_id = $_POST['customoid_id'];

if (is_numeric($customoid_id) && $customoid_id > 0) {
$oid = dbFetchRow('SELECT * FROM `customoids` WHERE `customoid_id` = ? LIMIT 1', [$customoid_id]);

if ($oid['customoid_alert'] == 1) {
$alerts = true;
} else {
$alerts = false;
}
if ($oid['customoid_passed'] == 1) {
$cpassed = true;
$passed = 'on';
} else {
$cpassed = false;
$passed = '';
}

header('Content-type: application/json');
echo json_encode([
'name' => $oid['customoid_descr'],
'oid' => $oid['customoid_oid'],
'datatype' => $oid['customoid_datatype'],
'unit' => $oid['customoid_unit'],
'divisor' => $oid['customoid_divisor'],
'multiplier' => $oid['customoid_multiplier'],
'limit' => $oid['customoid_limit'],
'limit_warn' => $oid['customoid_limit_warn'],
'limit_low' => $oid['customoid_limit_low'],
'limit_low_warn' => $oid['customoid_limit_low_warn'],
'alerts' => $alerts,
'cpassed' => $cpassed,
'passed' => $passed,
'user_func' => $oid['user_func'],
]);
}
7 changes: 7 additions & 0 deletions html/includes/graphs/customoid/auth.inc.php
@@ -0,0 +1,7 @@
<?php

if ($auth || device_permitted($device['device_id'])) {
$title = generate_device_link($device);
$title .= ' :: Custom OID ';
$auth = true;
}
18 changes: 18 additions & 0 deletions html/includes/graphs/customoid/customoid.inc.php
@@ -0,0 +1,18 @@
<?php

$rrd_filename = rrd_name($device['hostname'], array($type, $subtype));

require 'includes/graphs/common.inc.php';

$scale_min = 0;
$graph_max = 1;
$unit_text = $unit;

$ds = 'oid_value';

$colour_area = '9999cc';
$colour_line = '0000cc';

$colour_area_max = '9999cc';

require 'includes/graphs/generic_simplex.inc.php';
3 changes: 3 additions & 0 deletions html/includes/graphs/graph.inc.php
Expand Up @@ -44,6 +44,9 @@

if ($auth && is_custom_graph($type, $subtype, $device)) {
include($config['install_dir'] . "/html/includes/graphs/custom.inc.php");
} elseif ($auth && is_customoid_graph($type, $subtype)) {
$unit = $vars['unit'];
include $config['install_dir']."/html/includes/graphs/$type/customoid.inc.php";
} elseif ($auth && is_mib_graph($type, $subtype)) {
include $config['install_dir']."/html/includes/graphs/$type/mib.inc.php";
} elseif ($auth && is_file($config['install_dir']."/html/includes/graphs/$type/$subtype.inc.php")) {
Expand Down
60 changes: 60 additions & 0 deletions html/includes/modal/delete_customoid.inc.php
@@ -0,0 +1,60 @@
<?php

use LibreNMS\Authentication\LegacyAuth;

if (!LegacyAuth::user()->hasGlobalAdmin()) {
die('ERROR: You need to be admin');
}

?>

<div class="modal fade" id="delete-oid-form" 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 this OID then please click Delete.</p>
</div>
<div class="modal-footer">
<form role="form" class="remove_oid_form">
<button type="button" class="btn btn-default" data-dismiss="modal">Cancel</button>
<button type="submit" class="btn btn-danger danger" id="delete-oid-button" data-target="delete-oid-button">Delete</button>
<input type="hidden" name="dcustomoid_id" id="dcustomoid_id" value="">
<input type="hidden" name="confirm" id="confirm" value="yes">
</form>
</div>
</div>
</div>
</div>

<script>
$('#delete-oid-form').on('show.bs.modal', function(event) {
customoid_id = $(event.relatedTarget).data('customoid_id');
$("#dcustomoid_id").val(customoid_id);
});

$('#delete-oid-button').click('', function(event) {
event.preventDefault();
var customoid_id = $("#dcustomoid_id").val();
$.ajax({
type: 'POST',
url: 'ajax_form.php',
data: { type: "delete-customoid", customoid_id: customoid_id },
dataType: "html",
success: function(msg) {
if(msg.indexOf("ERROR:") <= -1) {
$("#row_"+customoid_id).remove();
}
$("#message").html('<div class="alert alert-info">'+msg+'</div>');
$("#delete-oid-form").modal('hide');
},
error: function() {
$("#message").html('<div class="alert alert-info">This OID could not be deleted.</div>');
$("#delete-oid-form").modal('hide');
}
});
});
</script>