diff --git a/html/api_v0.php b/html/api_v0.php index e5f8aa71b..6549a9904 100644 --- a/html/api_v0.php +++ b/html/api_v0.php @@ -30,6 +30,7 @@ $app->setName('api'); $app->group('/api', function() use ($app) { $app->group('/v0', function() use ($app) { $app->group('/devices', function() use ($app) { + $app->get('/:hostname', 'authToken', 'get_device');//api/v0/devices/$hostname $app->get('/:hostname/ports/:ifname/:type', 'authToken', 'get_graph_by_port_hostname');//api/v0/devices/$hostname/ports/$ifName/$type $app->get('/:hostname/:type', 'authToken', 'get_graph_generic_by_hostname');//api/v0/devices/$hostname/$type $app->get('/:hostname/ports/:ifname', 'authToken', 'get_port_stats_by_port_hostname');//api/v0/devices/$hostname/ports/$ifName diff --git a/html/includes/api_functions.inc.php b/html/includes/api_functions.inc.php index 4a657f754..17f0e291a 100644 --- a/html/includes/api_functions.inc.php +++ b/html/includes/api_functions.inc.php @@ -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 diff --git a/html/pages/api-docs.inc.php b/html/pages/api-docs.inc.php index db822bb9c..96888b1ce 100644 --- a/html/pages/api-docs.inc.php +++ b/html/pages/api-docs.inc.php @@ -90,6 +90,24 @@ if ($_SESSION['userlevel'] == '10') curl -H "Content-Type: application/json" -H "X-Auth-Token: 91c60e737e342c205be5bba8e2954d27" \
"https://librenms.example.com/api/v0/devices/localhost/ports/eth0/port_bits?width=1024&height=768&from=1405457456&to=1405543856" > /tmp/graph.png
+ + + General Info + + + /api + /v0 + /devices/$hostname + + + + JSON + + + curl -H "Content-Type: application/json" -H "X-Auth-Token: 91c60e737e342c205be5bba8e2954d27" \
"https://librenms.example.com/api/v0/devices/localhost" > localhost.json
+ General Graphs