mirror of
https://github.com/stylersnico/librenms.git
synced 2026-08-01 16:26:55 +02:00
Extended support for list devices to support mac/ipv4 and ipv6 filtering
This commit is contained in:
+34
-2
@@ -333,8 +333,16 @@ Route: /api/v0/devices
|
||||
Input:
|
||||
|
||||
- order: How to order the output, default is by hostname. Can be prepended by DESC or ASC to change the order.
|
||||
- type: can be one of the following, all, ignored, up, down, disabled to filter by that device status.
|
||||
|
||||
- type: can be one of the following to filter or search by:
|
||||
- all: All devices
|
||||
- ignored: Only ignored devices
|
||||
- up: Only devices that are up
|
||||
- down: Only devices that are down
|
||||
- disabled: Disabled devices
|
||||
- mac: search by mac address
|
||||
- ipv4: search by IPv4 address
|
||||
- ipv6: search by IPv6 address (compressed or uncompressed)
|
||||
- query: If searching by, then this will be used as the input.
|
||||
Example:
|
||||
```curl
|
||||
curl -H 'X-Auth-Token: YOURAPITOKENHERE' https://librenms.org/api/v0/devices?order=hostname%20DESC&type=down
|
||||
@@ -345,6 +353,30 @@ Output:
|
||||
```text
|
||||
{
|
||||
"status": "ok",
|
||||
"count": 1,
|
||||
"devices": [
|
||||
{
|
||||
"device_id": "1",
|
||||
"hostname": "localhost",
|
||||
...
|
||||
"serial": null,
|
||||
"icon": null
|
||||
}
|
||||
]
|
||||
}
|
||||
```
|
||||
|
||||
Example:
|
||||
```curl
|
||||
curl -H 'X-Auth-Token: YOURAPITOKENHERE' https://librenms.org/api/v0/devices?type=mac&query=00000c9ff013
|
||||
```
|
||||
|
||||
Output:
|
||||
|
||||
```text
|
||||
{
|
||||
"status": "ok",
|
||||
"count": 1,
|
||||
"devices": [
|
||||
{
|
||||
"device_id": "1",
|
||||
|
||||
@@ -155,6 +155,9 @@ function list_devices() {
|
||||
$app = \Slim\Slim::getInstance();
|
||||
$order = $_GET['order'];
|
||||
$type = $_GET['type'];
|
||||
$query = mres($_GET['query']);
|
||||
$param = array();
|
||||
$join = '';
|
||||
if (empty($order)) {
|
||||
$order = 'hostname';
|
||||
}
|
||||
@@ -178,16 +181,34 @@ function list_devices() {
|
||||
elseif ($type == 'disabled') {
|
||||
$sql = "disabled='1'";
|
||||
}
|
||||
elseif ($type == 'mac') {
|
||||
$join = " LEFT JOIN `ports` ON `devices`.`device_id`=`ports`.`device_id` LEFT JOIN `ipv4_mac` ON `ports`.`port_id`=`ipv4_mac`.`port_id` ";
|
||||
$sql = "`ipv4_mac`.`mac_address`=?";
|
||||
$param[] = $query;
|
||||
}
|
||||
elseif ($type == 'ipv4') {
|
||||
$join = " LEFT JOIN `ports` ON `devices`.`device_id`=`ports`.`device_id` LEFT JOIN `ipv4_addresses` ON `ports`.`port_id`=`ipv4_addresses`.`port_id` ";
|
||||
$sql = "`ipv4_addresses`.`ipv4_address`=?";
|
||||
$param[] = $query;
|
||||
}
|
||||
elseif ($type == 'ipv6') {
|
||||
$join = " LEFT JOIN `ports` ON `devices`.`device_id`=`ports`.`device_id` LEFT JOIN `ipv6_addresses` ON `ports`.`port_id`=`ipv6_addresses`.`port_id` ";
|
||||
$sql = "`ipv6_addresses`.`ipv6_address`=? OR `ipv6_addresses`.`ipv6_compressed`=?";
|
||||
$param = array($query,$query);
|
||||
}
|
||||
else {
|
||||
$sql = '1';
|
||||
}
|
||||
$devices = array();
|
||||
foreach (dbFetchRows("SELECT * FROM `devices` WHERE $sql ORDER by $order") as $device) {
|
||||
foreach (dbFetchRows("SELECT * FROM `devices` $join WHERE $sql ORDER by $order", $param) as $device) {
|
||||
$devices[] = $device;
|
||||
}
|
||||
|
||||
$count = count($devices);
|
||||
|
||||
$output = array(
|
||||
'status' => 'ok',
|
||||
'count' => $count,
|
||||
'devices' => $devices,
|
||||
);
|
||||
$app->response->headers->set('Content-Type', 'application/json');
|
||||
|
||||
Reference in New Issue
Block a user