diff --git a/doc/API/API-Docs.md b/doc/API/API-Docs.md
index c9dd5c99e..3e96ebe70 100644
--- a/doc/API/API-Docs.md
+++ b/doc/API/API-Docs.md
@@ -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:
]
```
+### Function `get_devices_by_group` [`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"
+ }
+ ]
+ }
+]
+```
+
## `Routing` [`top`](#top)
### Function: `list_bgp` [`top`](#top)
diff --git a/html/api_v0.php b/html/api_v0.php
index d5de47c5e..e46c0a3ea 100644
--- a/html/api_v0.php
+++ b/html/api_v0.php
@@ -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',
diff --git a/html/includes/api_functions.inc.php b/html/includes/api_functions.inc.php
index de6c307c3..7f89b37cb 100644
--- a/html/includes/api_functions.inc.php
+++ b/html/includes/api_functions.inc.php
@@ -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);
+
+}
diff --git a/includes/device-groups.inc.php b/includes/device-groups.inc.php
index 6f8104846..bb703f2cd 100644
--- a/includes/device-groups.inc.php
+++ b/includes/device-groups.inc.php
@@ -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()