diff --git a/doc/API/API-Docs.md b/doc/API/API-Docs.md index 3a7db50851bc..82f57b9f76d6 100644 --- a/doc/API/API-Docs.md +++ b/doc/API/API-Docs.md @@ -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", diff --git a/html/includes/api_functions.inc.php b/html/includes/api_functions.inc.php index 3c2c7fab156f..637e7f21997d 100644 --- a/html/includes/api_functions.inc.php +++ b/html/includes/api_functions.inc.php @@ -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'; } @@ -166,28 +169,46 @@ function list_devices() { if ($type == 'all' || empty($type)) { $sql = '1'; } - else if ($type == 'ignored') { + elseif ($type == 'ignored') { $sql = "ignore='1' AND disabled='0'"; } - else if ($type == 'up') { + elseif ($type == 'up') { $sql = "status='1' AND ignore='0' AND disabled='0'"; } - else if ($type == 'down') { + elseif ($type == 'down') { $sql = "status='0' AND ignore='0' AND disabled='0'"; } - else if ($type == 'disabled') { + 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'); @@ -211,7 +232,7 @@ function add_device() { $message = 'No information has been provided to add this new device'; } - else if (empty($data['hostname'])) { + elseif (empty($data['hostname'])) { $message = 'Missing the device hostname'; } @@ -227,7 +248,7 @@ function add_device() { $snmpver = mres($data['version']); } - else if ($data['version'] == 'v3') { + elseif ($data['version'] == 'v3') { $v3 = array( 'authlevel' => mres($data['authlevel']), 'authname' => mres($data['authname']), @@ -625,7 +646,7 @@ function add_edit_rule() { if (empty($device_id) && !isset($rule_id)) { $message = 'Missing the device id or global device id (-1)'; } - else if ($device_id == 0) { + elseif ($device_id == 0) { $device_id = '-1'; } @@ -687,7 +708,7 @@ function add_edit_rule() { $message = 'Failed to update existing alert rule'; } } - else if (dbInsert(array('name' => $name, 'device_id' => $device_id, 'rule' => $rule, 'severity' => $severity, 'disabled' => $disabled, 'extra' => $extra_json), 'alert_rules')) { + elseif (dbInsert(array('name' => $name, 'device_id' => $device_id, 'rule' => $rule, 'severity' => $severity, 'disabled' => $disabled, 'extra' => $extra_json), 'alert_rules')) { $status = 'ok'; $code = 200; }