Added API calls for device groups

This commit is contained in:
laf
2015-12-12 12:58:07 +00:00
parent 52647a92a0
commit 62b4c21a12
4 changed files with 127 additions and 2 deletions

View File

@@ -19,6 +19,9 @@
- [`add_device`](#api-route-11)
- [`list_oxidized`](#api-route-21)
- [`update_device_field`](#api-route-update_device_field)
- [`get_device_groups`](#api-route-get_device_groups)
- [`devicegroups`](#api-devicegroups)
- [`get_devicegroups`](#api-route-get_devicegroups)
- [`routing`](#api-routing)
- [`list_bgp`](#api-route-1)
- [`switching`](#api-switching)
@@ -490,6 +493,76 @@ Output:
]
```
### <a name="api-route-get_device_groups">Function `get_device_groups`</a> [`top`](#top)
List the device groups that a device is matched on.
Route: /api/v0/devices/:hostname/groups
Input (JSON):
-
Examples:
```curl
curl -H 'X-Auth-Token: YOURAPITOKENHERE' https://librenms.org/api/v0/devices/localhost/groups
```
Output:
```text
[
{
"status": "ok",
"message": "Found 1 device groups",
"count": 1,
"groups": [
{
"id": "1",
"name": "Testing",
"desc": "Testing",
"pattern": "%devices.status = \"1\" &&"
}
]
}
]
```
## <a name="api-devicegroups">`Device Groups`</a> [`top`](#top)
### <a name="api-route-get_devicegroups">Function `get_devicegroups`</a> [`top`](#top)
List all device groups.
Route: /api/v0/devicegroups
Input (JSON):
-
Examples:
```curl
curl -H 'X-Auth-Token: YOURAPITOKENHERE' https://librenms.org/api/v0/devicegroups
```
Output:
```text
[
{
"status": "ok",
"message": "Found 1 device groups",
"count": 1,
"groups": [
{
"id": "1",
"name": "Testing",
"desc": "Testing",
"pattern": "%devices.status = \"1\" &&"
}
]
}
]
```
## <a name="api-routing">`Routing`</a> [`top`](#top)
### <a name="api-route-1">Function: `list_bgp`</a> [`top`](#top)

View File

@@ -49,6 +49,7 @@ $app->group(
// api/v0/devices/$hostname/graphs
$app->get('/:hostname/ports', 'authToken', 'get_port_graphs')->name('get_port_graphs');
// api/v0/devices/$hostname/ports
$app->get('/:hostname/groups', 'authToken', 'get_device_groups')->name('get_device_groups');
$app->get('/:hostname/:type', 'authToken', 'get_graph_generic_by_hostname')->name('get_graph_generic_by_hostname');
// api/v0/devices/$hostname/$type
$app->get('/:hostname/ports/:ifname', 'authToken', 'get_port_stats_by_port_hostname')->name('get_port_stats_by_port_hostname');
@@ -61,6 +62,7 @@ $app->group(
// api/v0/devices
$app->post('/devices', 'authToken', 'add_device')->name('add_device');
// api/v0/devices (json data needs to be passed)
$app->get('/devicegroups', 'authToken', 'get_device_groups')->name('get_devicegroups');
$app->group(
'/portgroups',
function () use ($app) {

View File

@@ -13,7 +13,7 @@
*/
require_once '../includes/functions.php';
require_once '../includes/device-groups.inc.php';
function authToken(\Slim\Route $route) {
$app = \Slim\Slim::getInstance();
@@ -1014,3 +1014,37 @@ function update_device() {
$app->response->headers->set('Content-Type', 'application/json');
echo _json_encode($output);
}
function get_device_groups() {
$app = \Slim\Slim::getInstance();
$router = $app->router()->getCurrentRoute()->getParams();
$status = 'error';
$code = 404;
$hostname = $router['hostname'];
// use hostname as device_id if it's all digits
$device_id = ctype_digit($hostname) ? $hostname : getidbyname($hostname);
if (is_numeric($device_id)) {
$groups = GetFullGroupsFromDevice($device_id);
}
else {
$groups = GetDeviceGroups();
}
if (empty($groups)) {
$message = 'No device groups found';
}
else {
$status = 'ok';
$code = 200;
$message = 'Found ' . count($groups) . ' device groups';
}
$output = array(
'status' => $status,
'message' => $message,
'count' => count($groups),
'groups' => $groups,
);
$app->response->setStatus($code);
$app->response->headers->set('Content-Type', 'application/json');
echo _json_encode($output);
}

View File

@@ -99,7 +99,6 @@ function GetDeviceGroups() {
}//end GetDeviceGroups()
/**
* Get all groups of Device
* @param integer $device Device-ID
@@ -117,6 +116,23 @@ function GetGroupsFromDevice($device) {
}//end GetGroupsFromDevice()
/**
* Get all groups of Device
* @param integer $device Device-ID
* @return array
*/
function GetFullGroupsFromDevice($device) {
$ret = array();
foreach (GetDeviceGroups() as $group) {
if (dbFetchCell(GenGroupSQL($group['pattern'], 'device_id=?').' LIMIT 1', array($device)) == $device) {
$ret[] = $group;
}
}
return $ret;
}//end GetGroupsFromDevice()
/**
* Process Macros
* @param string $rule Rule to process