mirror of
https://github.com/librenms/librenms.git
synced 2024-10-07 16:52:45 +00:00
Added an option to rediscover a device from the web page
This commit is contained in:
30
html/forms/rediscover-device.inc.php
Normal file
30
html/forms/rediscover-device.inc.php
Normal 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['device_id'])) {
|
||||||
|
$status = "error";
|
||||||
|
$message = "Invalid device id";
|
||||||
|
} else {
|
||||||
|
$update = dbUpdate(array('last_discovered' => 'NULL'), 'devices', '`device_id` = ?', array($_POST['device_id']));
|
||||||
|
if(!empty($update) || $update == '0') {
|
||||||
|
$status = "ok";
|
||||||
|
$message = "Device will be rediscovered";
|
||||||
|
} else {
|
||||||
|
$status = "error";
|
||||||
|
$message = "Error rediscovering device";
|
||||||
|
}
|
||||||
|
}
|
||||||
|
$output = array("status" => $status, "message" => $message);
|
||||||
|
header("Content-type: application/json");
|
||||||
|
echo _json_encode($output);
|
@ -12,146 +12,7 @@
|
|||||||
* the source code distribution for details.
|
* the source code distribution for details.
|
||||||
*/
|
*/
|
||||||
|
|
||||||
if (!defined('JSON_UNESCAPED_SLASHES'))
|
require_once("../includes/functions.php");
|
||||||
define('JSON_UNESCAPED_SLASHES', 64);
|
|
||||||
if (!defined('JSON_PRETTY_PRINT'))
|
|
||||||
define('JSON_PRETTY_PRINT', 128);
|
|
||||||
if (!defined('JSON_UNESCAPED_UNICODE'))
|
|
||||||
define('JSON_UNESCAPED_UNICODE', 256);
|
|
||||||
|
|
||||||
function _json_encode($data, $options = 448)
|
|
||||||
{
|
|
||||||
if (version_compare(PHP_VERSION, '5.4', '>='))
|
|
||||||
{
|
|
||||||
return json_encode($data, $options);
|
|
||||||
}
|
|
||||||
|
|
||||||
return _json_format(json_encode($data), $options);
|
|
||||||
}
|
|
||||||
|
|
||||||
function _pretty_print_json($json)
|
|
||||||
{
|
|
||||||
return _json_format($json, JSON_PRETTY_PRINT);
|
|
||||||
}
|
|
||||||
|
|
||||||
function _json_format($json, $options = 448)
|
|
||||||
{
|
|
||||||
$prettyPrint = (bool) ($options & JSON_PRETTY_PRINT);
|
|
||||||
$unescapeUnicode = (bool) ($options & JSON_UNESCAPED_UNICODE);
|
|
||||||
$unescapeSlashes = (bool) ($options & JSON_UNESCAPED_SLASHES);
|
|
||||||
|
|
||||||
if (!$prettyPrint && !$unescapeUnicode && !$unescapeSlashes)
|
|
||||||
{
|
|
||||||
return $json;
|
|
||||||
}
|
|
||||||
|
|
||||||
$result = '';
|
|
||||||
$pos = 0;
|
|
||||||
$strLen = strlen($json);
|
|
||||||
$indentStr = ' ';
|
|
||||||
$newLine = "\n";
|
|
||||||
$outOfQuotes = true;
|
|
||||||
$buffer = '';
|
|
||||||
$noescape = true;
|
|
||||||
|
|
||||||
for ($i = 0; $i < $strLen; $i++)
|
|
||||||
{
|
|
||||||
// Grab the next character in the string
|
|
||||||
$char = substr($json, $i, 1);
|
|
||||||
|
|
||||||
// Are we inside a quoted string?
|
|
||||||
if ('"' === $char && $noescape)
|
|
||||||
{
|
|
||||||
$outOfQuotes = !$outOfQuotes;
|
|
||||||
}
|
|
||||||
|
|
||||||
if (!$outOfQuotes)
|
|
||||||
{
|
|
||||||
$buffer .= $char;
|
|
||||||
$noescape = '\\' === $char ? !$noescape : true;
|
|
||||||
continue;
|
|
||||||
}
|
|
||||||
elseif ('' !== $buffer)
|
|
||||||
{
|
|
||||||
if ($unescapeSlashes)
|
|
||||||
{
|
|
||||||
$buffer = str_replace('\\/', '/', $buffer);
|
|
||||||
}
|
|
||||||
|
|
||||||
if ($unescapeUnicode && function_exists('mb_convert_encoding'))
|
|
||||||
{
|
|
||||||
// http://stackoverflow.com/questions/2934563/how-to-decode-unicode-escape-sequences-like-u00ed-to-proper-utf-8-encoded-cha
|
|
||||||
$buffer = preg_replace_callback('/\\\\u([0-9a-f]{4})/i',
|
|
||||||
function ($match)
|
|
||||||
{
|
|
||||||
return mb_convert_encoding(pack('H*', $match[1]), 'UTF-8', 'UCS-2BE');
|
|
||||||
}, $buffer);
|
|
||||||
}
|
|
||||||
|
|
||||||
$result .= $buffer . $char;
|
|
||||||
$buffer = '';
|
|
||||||
continue;
|
|
||||||
}
|
|
||||||
elseif(false !== strpos(" \t\r\n", $char))
|
|
||||||
{
|
|
||||||
continue;
|
|
||||||
}
|
|
||||||
|
|
||||||
if (':' === $char)
|
|
||||||
{
|
|
||||||
// Add a space after the : character
|
|
||||||
$char .= ' ';
|
|
||||||
}
|
|
||||||
elseif (('}' === $char || ']' === $char))
|
|
||||||
{
|
|
||||||
$pos--;
|
|
||||||
$prevChar = substr($json, $i - 1, 1);
|
|
||||||
|
|
||||||
if ('{' !== $prevChar && '[' !== $prevChar)
|
|
||||||
{
|
|
||||||
// If this character is the end of an element,
|
|
||||||
// output a new line and indent the next line
|
|
||||||
$result .= $newLine;
|
|
||||||
for ($j = 0; $j < $pos; $j++)
|
|
||||||
{
|
|
||||||
$result .= $indentStr;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
else
|
|
||||||
{
|
|
||||||
// Collapse empty {} and []
|
|
||||||
$result = rtrim($result) . "\n\n" . $indentStr;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
$result .= $char;
|
|
||||||
|
|
||||||
// If the last character was the beginning of an element,
|
|
||||||
// output a new line and indent the next line
|
|
||||||
if (',' === $char || '{' === $char || '[' === $char)
|
|
||||||
{
|
|
||||||
$result .= $newLine;
|
|
||||||
|
|
||||||
if ('{' === $char || '[' === $char)
|
|
||||||
{
|
|
||||||
$pos++;
|
|
||||||
}
|
|
||||||
|
|
||||||
for ($j = 0; $j < $pos; $j++)
|
|
||||||
{
|
|
||||||
$result .= $indentStr;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
// If buffer not empty after formating we have an unclosed quote
|
|
||||||
if (strlen($buffer) > 0)
|
|
||||||
{
|
|
||||||
//json is incorrectly formatted
|
|
||||||
$result = false;
|
|
||||||
}
|
|
||||||
|
|
||||||
return $result;
|
|
||||||
}
|
|
||||||
|
|
||||||
function authToken(\Slim\Route $route)
|
function authToken(\Slim\Route $route)
|
||||||
{
|
{
|
||||||
@ -255,8 +116,6 @@ function get_device()
|
|||||||
$router = $app->router()->getCurrentRoute()->getParams();
|
$router = $app->router()->getCurrentRoute()->getParams();
|
||||||
$hostname = $router['hostname'];
|
$hostname = $router['hostname'];
|
||||||
|
|
||||||
require_once("../includes/functions.php");
|
|
||||||
|
|
||||||
// use hostname as device_id if it's all digits
|
// use hostname as device_id if it's all digits
|
||||||
$device_id = ctype_digit($hostname) ? $hostname : getidbyname($hostname);
|
$device_id = ctype_digit($hostname) ? $hostname : getidbyname($hostname);
|
||||||
|
|
||||||
@ -376,7 +235,6 @@ function add_device()
|
|||||||
}
|
}
|
||||||
if(empty($message))
|
if(empty($message))
|
||||||
{
|
{
|
||||||
require_once("../includes/functions.php");
|
|
||||||
$result = addHost($hostname, $snmpver, $port, $transport, 1);
|
$result = addHost($hostname, $snmpver, $port, $transport, 1);
|
||||||
if($result)
|
if($result)
|
||||||
{
|
{
|
||||||
@ -414,7 +272,6 @@ function del_device()
|
|||||||
}
|
}
|
||||||
else
|
else
|
||||||
{
|
{
|
||||||
require_once("../includes/functions.php");
|
|
||||||
|
|
||||||
// allow deleting by device_id or hostname
|
// allow deleting by device_id or hostname
|
||||||
$device_id = ctype_digit($hostname) ? $hostname : getidbyname($hostname);
|
$device_id = ctype_digit($hostname) ? $hostname : getidbyname($hostname);
|
||||||
|
@ -56,10 +56,19 @@ if ($updated && $update_message)
|
|||||||
|
|
||||||
?>
|
?>
|
||||||
|
|
||||||
<form id="delete_host" name="delete_host" method="post" action="delhost/" role="form">
|
<div class="row">
|
||||||
<input type="hidden" name="id" value="<?php echo($device['device_id']); ?>">
|
<div class="col-sm-1">
|
||||||
<button type="submit" class="btn btn-danger" name="Submit">Delete device</button>
|
<form id="delete_host" name="delete_host" method="post" action="delhost/" role="form">
|
||||||
</form>
|
<input type="hidden" name="id" value="<?php echo($device['device_id']); ?>">
|
||||||
|
<button type="submit" class="btn btn-danger" name="Submit">Delete device</button>
|
||||||
|
</form>
|
||||||
|
</div>
|
||||||
|
<div class="col-sm-1">
|
||||||
|
</div>
|
||||||
|
<div class="col-sm-1">
|
||||||
|
<button type="submit" id="rediscover" data-device_id="<?php echo($device['device_id']); ?>" class="btn btn-primary" name="rediscover">Rediscover device</button>
|
||||||
|
</div>
|
||||||
|
</div>
|
||||||
<br />
|
<br />
|
||||||
<form id="edit" name="edit" method="post" action="" role="form" class="form-horizontal">
|
<form id="edit" name="edit" method="post" action="" role="form" class="form-horizontal">
|
||||||
<input type=hidden name="editing" value="yes">
|
<input type=hidden name="editing" value="yes">
|
||||||
@ -124,6 +133,27 @@ foreach ($config['device_types'] as $type)
|
|||||||
<button type="submit" name="Submit" class="btn btn-default">Save</button>
|
<button type="submit" name="Submit" class="btn btn-default">Save</button>
|
||||||
</form>
|
</form>
|
||||||
<br />
|
<br />
|
||||||
|
<script>
|
||||||
|
$("#rediscover").click(function() {
|
||||||
|
var device_id = $(this).data("device_id");
|
||||||
|
$.ajax({
|
||||||
|
type: 'POST',
|
||||||
|
url: '/ajax_form.php',
|
||||||
|
data: { type: "rediscover-device", device_id: device_id },
|
||||||
|
dataType: "json",
|
||||||
|
success: function(data){
|
||||||
|
if(data['status'] == 'ok') {
|
||||||
|
toastr.success(data['message']);
|
||||||
|
} else {
|
||||||
|
toastr.error(data['message']);
|
||||||
|
}
|
||||||
|
},
|
||||||
|
error:function(){
|
||||||
|
toastr.error('An error occured setting this device to be rediscovered');
|
||||||
|
}
|
||||||
|
});
|
||||||
|
});
|
||||||
|
</script>
|
||||||
<?php
|
<?php
|
||||||
|
|
||||||
print_optionbar_start();
|
print_optionbar_start();
|
||||||
|
@ -941,4 +941,144 @@ function validate_device_id($id)
|
|||||||
return($return);
|
return($return);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
if (!defined('JSON_UNESCAPED_SLASHES'))
|
||||||
|
define('JSON_UNESCAPED_SLASHES', 64);
|
||||||
|
if (!defined('JSON_PRETTY_PRINT'))
|
||||||
|
define('JSON_PRETTY_PRINT', 128);
|
||||||
|
if (!defined('JSON_UNESCAPED_UNICODE'))
|
||||||
|
define('JSON_UNESCAPED_UNICODE', 256);
|
||||||
|
|
||||||
|
function _json_encode($data, $options = 448)
|
||||||
|
{
|
||||||
|
if (version_compare(PHP_VERSION, '5.4', '>='))
|
||||||
|
{
|
||||||
|
return json_encode($data, $options);
|
||||||
|
}
|
||||||
|
|
||||||
|
return _json_format(json_encode($data), $options);
|
||||||
|
}
|
||||||
|
|
||||||
|
function _pretty_print_json($json)
|
||||||
|
{
|
||||||
|
return _json_format($json, JSON_PRETTY_PRINT);
|
||||||
|
}
|
||||||
|
|
||||||
|
function _json_format($json, $options = 448)
|
||||||
|
{
|
||||||
|
$prettyPrint = (bool) ($options & JSON_PRETTY_PRINT);
|
||||||
|
$unescapeUnicode = (bool) ($options & JSON_UNESCAPED_UNICODE);
|
||||||
|
$unescapeSlashes = (bool) ($options & JSON_UNESCAPED_SLASHES);
|
||||||
|
|
||||||
|
if (!$prettyPrint && !$unescapeUnicode && !$unescapeSlashes)
|
||||||
|
{
|
||||||
|
return $json;
|
||||||
|
}
|
||||||
|
|
||||||
|
$result = '';
|
||||||
|
$pos = 0;
|
||||||
|
$strLen = strlen($json);
|
||||||
|
$indentStr = ' ';
|
||||||
|
$newLine = "\n";
|
||||||
|
$outOfQuotes = true;
|
||||||
|
$buffer = '';
|
||||||
|
$noescape = true;
|
||||||
|
|
||||||
|
for ($i = 0; $i < $strLen; $i++)
|
||||||
|
{
|
||||||
|
// Grab the next character in the string
|
||||||
|
$char = substr($json, $i, 1);
|
||||||
|
|
||||||
|
// Are we inside a quoted string?
|
||||||
|
if ('"' === $char && $noescape)
|
||||||
|
{
|
||||||
|
$outOfQuotes = !$outOfQuotes;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (!$outOfQuotes)
|
||||||
|
{
|
||||||
|
$buffer .= $char;
|
||||||
|
$noescape = '\\' === $char ? !$noescape : true;
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
elseif ('' !== $buffer)
|
||||||
|
{
|
||||||
|
if ($unescapeSlashes)
|
||||||
|
{
|
||||||
|
$buffer = str_replace('\\/', '/', $buffer);
|
||||||
|
}
|
||||||
|
|
||||||
|
if ($unescapeUnicode && function_exists('mb_convert_encoding'))
|
||||||
|
{
|
||||||
|
// http://stackoverflow.com/questions/2934563/how-to-decode-unicode-escape-sequences-like-u00ed-to-proper-utf-8-encoded-cha
|
||||||
|
$buffer = preg_replace_callback('/\\\\u([0-9a-f]{4})/i',
|
||||||
|
function ($match)
|
||||||
|
{
|
||||||
|
return mb_convert_encoding(pack('H*', $match[1]), 'UTF-8', 'UCS-2BE');
|
||||||
|
}, $buffer);
|
||||||
|
}
|
||||||
|
|
||||||
|
$result .= $buffer . $char;
|
||||||
|
$buffer = '';
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
elseif(false !== strpos(" \t\r\n", $char))
|
||||||
|
{
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (':' === $char)
|
||||||
|
{
|
||||||
|
// Add a space after the : character
|
||||||
|
$char .= ' ';
|
||||||
|
}
|
||||||
|
elseif (('}' === $char || ']' === $char))
|
||||||
|
{
|
||||||
|
$pos--;
|
||||||
|
$prevChar = substr($json, $i - 1, 1);
|
||||||
|
|
||||||
|
if ('{' !== $prevChar && '[' !== $prevChar)
|
||||||
|
{
|
||||||
|
// If this character is the end of an element,
|
||||||
|
// output a new line and indent the next line
|
||||||
|
$result .= $newLine;
|
||||||
|
for ($j = 0; $j < $pos; $j++)
|
||||||
|
{
|
||||||
|
$result .= $indentStr;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
// Collapse empty {} and []
|
||||||
|
$result = rtrim($result) . "\n\n" . $indentStr;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
$result .= $char;
|
||||||
|
|
||||||
|
// If the last character was the beginning of an element,
|
||||||
|
// output a new line and indent the next line
|
||||||
|
if (',' === $char || '{' === $char || '[' === $char)
|
||||||
|
{
|
||||||
|
$result .= $newLine;
|
||||||
|
|
||||||
|
if ('{' === $char || '[' === $char)
|
||||||
|
{
|
||||||
|
$pos++;
|
||||||
|
}
|
||||||
|
|
||||||
|
for ($j = 0; $j < $pos; $j++)
|
||||||
|
{
|
||||||
|
$result .= $indentStr;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
// If buffer not empty after formating we have an unclosed quote
|
||||||
|
if (strlen($buffer) > 0)
|
||||||
|
{
|
||||||
|
//json is incorrectly formatted
|
||||||
|
$result = false;
|
||||||
|
}
|
||||||
|
|
||||||
|
return $result;
|
||||||
|
}
|
||||||
?>
|
?>
|
||||||
|
Reference in New Issue
Block a user