Added list devices api call

This commit is contained in:
laf
2014-09-16 22:19:28 +01:00
parent dabe45a257
commit b8df0596a9
2 changed files with 48 additions and 0 deletions
+3
View File
@@ -48,6 +48,9 @@ $app->group('/api', function() use ($app) {
});
});
});
$app->group('/list', function() use ($app) {
$app->get('/devices(/:order)(/:type)(/)', 'authToken', 'list_devices');//api/v1/list/devices (order can be any device column) (types = all, ignored, up, down, disabled)
});
});
});
+45
View File
@@ -188,3 +188,48 @@ function get_graph_generic_by_hostname()
$app->response->headers->set('Content-Type', 'image/png');
require("includes/graphs/graph.inc.php");
}
function list_devices()
{
// This will return a list of devices
global $config;
$app = \Slim\Slim::getInstance();
$router = $app->router()->getCurrentRoute()->getParams();
$order = $router['order'];
$type = $router['type'];
if(empty($order))
{
$order = "hostname";
}
if(stristr($order,' desc') === FALSE && stristr($order, ' asc') === FALSE)
{
$order .= ' ASC';
}
if($type == 'all' || empty($type))
{
$sql = "1";
}
elseif($type == 'ignored')
{
$sql = "ignore='1' AND disabled='0'";
}
elseif($type == 'up')
{
$sql = "status='1' AND ignore='0' AND disabled='0'";
}
elseif($type == 'down')
{
$sql = "status='0' AND ignore='0' AND disabled='0'";
}
elseif($type == 'disabled')
{
$sql = "disabled='1'";
}
foreach (dbFetchRows("SELECT * FROM `devices` WHERE $sql ORDER by $order") as $device)
{
$devices[] = $device;
}
$output = array("status" => "ok", "devices" => $devices);
$app->response->headers->set('Content-Type', 'application/json');
echo json_encode($output);
}