mirror of
https://github.com/librenms/librenms.git
synced 2024-10-07 16:52:45 +00:00
Add function to get information about a single device
This commit is contained in:
@ -247,6 +247,49 @@ function get_graph_generic_by_hostname()
|
||||
require("includes/graphs/graph.inc.php");
|
||||
}
|
||||
|
||||
function get_device()
|
||||
{
|
||||
// return details of a single device
|
||||
$app = \Slim\Slim::getInstance();
|
||||
$app->response->headers->set('Content-Type', 'application/json');
|
||||
$router = $app->router()->getCurrentRoute()->getParams();
|
||||
$hostname = $router['hostname'];
|
||||
|
||||
// if the hostname is all digits, use the device_id instead of the name
|
||||
$column = "hostname";
|
||||
if (ctype_digit($hostname)) {
|
||||
$column = "device_id";
|
||||
}
|
||||
|
||||
// find devices matching the name/id
|
||||
$devices = array();
|
||||
// FIXME: user-based permissions
|
||||
foreach (dbFetchRows("SELECT * FROM `devices` WHERE `".$column."`=?", array($hostname)) as $device)
|
||||
{
|
||||
$devices[] = $device;
|
||||
}
|
||||
|
||||
if (count($devices) == 0) {
|
||||
// not found
|
||||
$app->response->setStatus(404);
|
||||
$output = array("status" => "error", "message" => "Device $hostname does not exist");
|
||||
echo _json_encode($output);
|
||||
$app->stop();
|
||||
}
|
||||
elseif (count($devices) > 1) {
|
||||
// we got more than one device - something's weird
|
||||
$app->response->setStatus(500);
|
||||
$output = array("status" => "error", "message" => "Multiple devices matching $hostname", "devices" => $devices);
|
||||
echo _json_encode($output);
|
||||
$app->stop();
|
||||
}
|
||||
else {
|
||||
// not found
|
||||
$output = array("status" => "ok", "devices" => $devices);
|
||||
echo _json_encode($output);
|
||||
}
|
||||
}
|
||||
|
||||
function list_devices()
|
||||
{
|
||||
// This will return a list of devices
|
||||
|
Reference in New Issue
Block a user