Add function to get information about a single device

This commit is contained in:
Paul Gear
2014-09-27 15:25:43 +10:00
parent a58c857ed2
commit 76f5ab87a9
3 changed files with 62 additions and 0 deletions

View File

@@ -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

View File

@@ -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

View File

@@ -90,6 +90,24 @@ if ($_SESSION['userlevel'] == '10')
<tr>
<td colspan="5"><code>curl -H "Content-Type: application/json" -H "X-Auth-Token: 91c60e737e342c205be5bba8e2954d27" \<br/> "https://librenms.example.com/api/v0/devices/localhost/ports/eth0/port_bits?width=1024&height=768&from=1405457456&to=1405543856" > /tmp/graph.png</code></td>
</tr>
<a name="general_info"></a>
<tr class="success">
<td colspan="5"><strong>General Info</strong></td>
</tr>
<tr>
<td>/api</td>
<td>/v0</td>
<td>/devices/$hostname</td>
<td>
<ul class="list-unstyled">
<li>$hostname = the hostname of the device you want the information about</li>
</ul>
</td>
<td>JSON</td>
</tr>
<tr>
<td colspan="5"><code>curl -H "Content-Type: application/json" -H "X-Auth-Token: 91c60e737e342c205be5bba8e2954d27" \<br/> "https://librenms.example.com/api/v0/devices/localhost" > localhost.json</code></td>
</tr>
<a name="general_graphs"></a>
<tr class="success">
<td colspan="5"><strong>General Graphs</strong></td>