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

Feature: Custom OID polling and graphing #10945

Merged
merged 16 commits into from Dec 19, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
49 changes: 49 additions & 0 deletions database/migrations/2019_12_16_140000_create_customoids_table.php
@@ -0,0 +1,49 @@
<?php

use Illuminate\Support\Facades\Schema;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Database\Migrations\Migration;

class CreateCustomoidsTable extends Migration
{
/**
* Run the migrations.
*
* @return void
*/
public function up()
{
Schema::create('customoids', function (Blueprint $table) {
$table->increments('customoid_id');
$table->unsignedInteger('device_id')->default(0);
$table->string('customoid_descr', 255)->nullable()->default('');
$table->tinyInteger('customoid_deleted')->default(0);
$table->double('customoid_current')->nullable();
$table->double('customoid_prev')->nullable();
$table->string('customoid_oid', 255)->nullable();
$table->string('customoid_datatype', 20)->default('GAUGE');
$table->string('customoid_unit', 20)->nullable();
$table->unsignedInteger('customoid_divisor')->default(1);
$table->unsignedInteger('customoid_multiplier')->default(1);
$table->double('customoid_limit')->nullable();
$table->double('customoid_limit_warn')->nullable();
$table->double('customoid_limit_low')->nullable();
$table->double('customoid_limit_low_warn')->nullable();
$table->tinyInteger('customoid_alert')->default(0);
$table->tinyInteger('customoid_passed')->default(0);
$table->timestamp('lastupdate')->default(DB::raw('CURRENT_TIMESTAMP on update CURRENT_TIMESTAMP'));
$table->string('user_func', 100)->nullable();
});
}


/**
* Reverse the migrations.
*
* @return void
*/
public function down()
{
Schema::dropIfExists('customoids');
}
}
23 changes: 23 additions & 0 deletions includes/common.php
Expand Up @@ -861,6 +861,13 @@ function is_custom_graph($type, $subtype, $device)
return false;
} // is_custom_graph

function is_customoid_graph($type, $subtype)
{
if (!empty($subtype) && $type == 'customoid') {
return true;
}
return false;
} // is_customoid_graph

/*
* FIXME: Dummy implementation
Expand Down Expand Up @@ -1539,6 +1546,22 @@ function fahrenheit_to_celsius($value, $scale = 'fahrenheit')
return sprintf('%.02f', $value);
}

/**
* Converts celsius to fahrenheit (with 2 decimal places)
* if $scale is not celsius, it assumes celsius and returns the value
*
* @param float $value
* @param string $scale fahrenheit or celsius
* @return string (containing a float)
*/
function celsius_to_fahrenheit($value, $scale = 'celsius')
{
if ($scale === 'celsius') {
$value = ($value * 1.8) + 32;
}
return sprintf('%.02f', $value);
}

/**
* Converts uW to dBm
* $value must be positive
Expand Down
167 changes: 167 additions & 0 deletions includes/html/forms/customoid.inc.php
@@ -0,0 +1,167 @@
<?php

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

if (!Auth::user()->hasGlobalAdmin()) {
$response = array(
'status' => 'error',
'message' => 'Need to be admin',
);
echo _json_encode($response);
exit;
}

$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 received";
} else {
$status = 'error';
$message = "Failed to set pass on OID <i>$name</i>";
}
} else {
$status = 'error';
$message = "Invalid data in SNMP reply, value $rawdata received";
}
} 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,
]));
25 changes: 25 additions & 0 deletions includes/html/forms/delete-customoid.inc.php
@@ -0,0 +1,25 @@
<?php

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

if (!Auth::user()->hasGlobalAdmin()) {
$response = array(
'status' => 'error',
'message' => 'Need to be admin',
);
echo _json_encode($response);
exit;
}

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;
}
}
46 changes: 46 additions & 0 deletions includes/html/forms/parse-customoid.inc.php
@@ -0,0 +1,46 @@
<?php

if (!Auth::user()->hasGlobalAdmin()) {
$response = array(
'status' => 'error',
'message' => 'Need to be admin',
);
echo _json_encode($response);
exit;
}
$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 includes/html/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 includes/html/graphs/customoid/customoid.inc.php
@@ -0,0 +1,18 @@
<?php

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

require 'includes/html/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/html/graphs/generic_simplex.inc.php';
3 changes: 3 additions & 0 deletions includes/html/graphs/graph.inc.php
Expand Up @@ -36,6 +36,9 @@

if ($auth && is_custom_graph($type, $subtype, $device)) {
include(Config::get('install_dir') . "/includes/html/graphs/custom.inc.php");
} elseif ($auth && is_customoid_graph($type, $subtype)) {
$unit = $vars['unit'];
include(Config::get('install_dir') . "/includes/html/graphs/customoid/customoid.inc.php");
} elseif ($auth && is_mib_graph($type, $subtype)) {
include Config::get('install_dir') . "/includes/html/graphs/$type/mib.inc.php";
} elseif ($auth && is_file(Config::get('install_dir') . "/includes/html/graphs/$type/$subtype.inc.php")) {
Expand Down