Added API call for listing devices by groups

This commit is contained in:
laf
2015-12-12 13:47:44 +00:00
parent 62b4c21a12
commit a73a997edc
4 changed files with 123 additions and 1 deletions

View File

@@ -22,6 +22,7 @@
- [`get_device_groups`](#api-route-get_device_groups)
- [`devicegroups`](#api-devicegroups)
- [`get_devicegroups`](#api-route-get_devicegroups)
- [`get_devices_by_group`](#api-route-get_devices_by_group)
- [`routing`](#api-routing)
- [`list_bgp`](#api-route-1)
- [`switching`](#api-switching)
@@ -472,6 +473,8 @@ Update devices field in the database.
Route: /api/v0/devices/:hostname
- hostname can be either the device hostname or id
Input (JSON):
- field: The column name within the database
@@ -499,6 +502,8 @@ List the device groups that a device is matched on.
Route: /api/v0/devices/:hostname/groups
- hostname can be either the device hostname or id
Input (JSON):
-
@@ -563,6 +568,82 @@ Output:
]
```
### <a name="api-route-get_devices_by_group">Function `get_devices_by_group`</a> [`top`](#top)
List all devices matching the group provided.
Route: /api/v0/devicegroups/:name
- name Is the name of the device group which can be obtained using [`get_devicegroups`](#api-route-get_devicegroups). Please ensure that the name is urlencoded if it needs to be (i.e Linux Servers would need to be urlencoded.
Input (JSON):
-
Examples:
```curl
curl -H 'X-Auth-Token: YOURAPITOKENHERE' https://librenms.org/api/v0/devicegroups/LinuxServers
```
Output:
```text
[
{
"status": "error",
"message": "Found 1 in group LinuxServers",
"count": 1,
"devices": [
{
"device_id": "1",
"hostname": "localhost",
"sysName": "hostname",
"community": "librenms",
"authlevel": null,
"authname": null,
"authpass": null,
"authalgo": null,
"cryptopass": null,
"cryptoalgo": null,
"snmpver": "v2c",
"port": "161",
"transport": "udp",
"timeout": null,
"retries": null,
"bgpLocalAs": null,
"sysObjectID": ".1.3.6.1.4.1.8072.3.2.10",
"sysDescr": "Linux li1045-133.members.linode.com 4.1.5-x86_64-linode61 #7 SMP Mon Aug 24 13:46:31 EDT 2015 x86_64",
"sysContact": "",
"version": "4.1.5-x86_64-linode61",
"hardware": "Generic x86 64-bit",
"features": "CentOS 7.1.1503",
"location": "",
"os": "linux",
"status": "1",
"status_reason": "",
"ignore": "0",
"disabled": "0",
"uptime": "4615964",
"agent_uptime": "0",
"last_polled": "2015-12-12 13:20:04",
"last_poll_attempted": null,
"last_polled_timetaken": "1.90",
"last_discovered_timetaken": "79.53",
"last_discovered": "2015-12-12 12:34:21",
"last_ping": "2015-12-12 13:20:04",
"last_ping_timetaken": "0.08",
"purpose": null,
"type": "server",
"serial": null,
"icon": null,
"poller_group": "0",
"override_sysLocation": "0",
"notes": "Nope"
}
]
}
]
```
## <a name="api-routing">`Routing`</a> [`top`](#top)
### <a name="api-route-1">Function: `list_bgp`</a> [`top`](#top)

View File

@@ -62,6 +62,12 @@ $app->group(
// api/v0/devices
$app->post('/devices', 'authToken', 'add_device')->name('add_device');
// api/v0/devices (json data needs to be passed)
$app->group(
'/devicegroups',
function () use ($app) {
$app->get('/:name', 'authToken', 'get_devices_by_group')->name('get_devices_by_group');
}
);
$app->get('/devicegroups', 'authToken', 'get_device_groups')->name('get_devicegroups');
$app->group(
'/portgroups',

View File

@@ -1048,3 +1048,38 @@ function get_device_groups() {
$app->response->headers->set('Content-Type', 'application/json');
echo _json_encode($output);
}
function get_devices_by_group() {
$app = \Slim\Slim::getInstance();
$router = $app->router()->getCurrentRoute()->getParams();
$status = 'error';
$code = 404;
$name = urldecode($router['name']);
$devices = array();
if (empty($name)) {
$message = 'No device group name provided';
}
else {
$group_id = dbFetchCell("SELECT `id` FROM `device_groups` WHERE `name`=?",array($name));
$devices = GetDevicesFromGroup($group_id);
$count = count($devices);
if (empty($devices)) {
$message = 'No devices found in group ' . $name;
}
else {
$message = "Found $count in group $name";
$code = 200;
}
}
$output = array(
'status' => $status,
'message' => $message,
'count' => $count,
'devices' => $devices,
);
$app->response->setStatus($code);
$app->response->headers->set('Content-Type', 'application/json');
echo _json_encode($output);
}

View File

@@ -66,7 +66,7 @@ function GenGroupSQL($pattern, $search='') {
$search .= ' &&';
}
$sql = 'SELECT DISTINCT('.str_replace('(', '', $tables[0]).'.device_id) FROM '.implode(',', $tables).' WHERE '.$search.' ('.str_replace(array('%', '@', '!~', '~'), array('', '.*', 'NOT REGEXP', 'REGEXP'), $pattern).')';
$sql = 'SELECT DISTINCT('.str_replace('(', '', $tables[0]).'.device_id),`devices`.* FROM '.implode(',', $tables).' WHERE '.$search.' ('.str_replace(array('%', '@', '!~', '~'), array('', '.*', 'NOT REGEXP', 'REGEXP'), $pattern).')';
return $sql;
}//end GenGroupSQL()