diff --git a/doc/API/API-Docs.md b/doc/API/API-Docs.md
index e879fe637..c9dd5c99e 100644
--- a/doc/API/API-Docs.md
+++ b/doc/API/API-Docs.md
@@ -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:
]
```
+### Function `get_device_groups` [`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\" &&"
+ }
+ ]
+ }
+]
+```
+
+## `Device Groups` [`top`](#top)
+
+### Function `get_devicegroups` [`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\" &&"
+ }
+ ]
+ }
+]
+```
+
## `Routing` [`top`](#top)
### Function: `list_bgp` [`top`](#top)
diff --git a/html/api_v0.php b/html/api_v0.php
index 426591162..d5de47c5e 100644
--- a/html/api_v0.php
+++ b/html/api_v0.php
@@ -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) {
diff --git a/html/includes/api_functions.inc.php b/html/includes/api_functions.inc.php
index 8469e1053..de6c307c3 100644
--- a/html/includes/api_functions.inc.php
+++ b/html/includes/api_functions.inc.php
@@ -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);
+}
diff --git a/includes/device-groups.inc.php b/includes/device-groups.inc.php
index 45d5afcd2..6f8104846 100644
--- a/includes/device-groups.inc.php
+++ b/includes/device-groups.inc.php
@@ -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