diff --git a/doc/API/API-Docs.md b/doc/API/API-Docs.md index 55e28c5db8..fed298f941 100644 --- a/doc/API/API-Docs.md +++ b/doc/API/API-Docs.md @@ -49,6 +49,8 @@ - [`bills`](#api-bills) - [`list_bills`](#api-route-22) - [`get_bill`](#api-route-23) + - [`resources`](#api-resources) + - [`list_arp`](#api-resources-list_arp) Describes the API structure. # `Structure` [`top`](#top) @@ -1403,3 +1405,37 @@ Output: } ``` +### Function: `list_arp` [`top`](#top) + +Retrieve a specific ARP entry or all ARP enties for a device + +Route: /api/v0/resources/ip/arp/:ip + + - ip is the specific IP you would like to query, if this is all then you need to pass ?device=_hostname_ (or device id) + +Input: + + - device if you specify all for the IP then you need to populate this with the hostname or id of the device. + +Example: +```curl +curl -H 'X-Auth-Token: YOURAPITOKENHERE' https://librenms.org/api/v0/resources/ip/arp/1.1.1.1 +curl -H 'X-Auth-Token: YOURAPITOKENHERE' https://librenms.org/api/v0/resources/ip/arp/1.1.1.1?device=localhost +``` + +Output: +```text +{ + "status": "ok", + "err-msg": "", + "count": 1, + "arp": [ + { + "port_id": "229", + "mac_address": "da160e5c2002", + "ipv4_address": "1.1.1.1", + "context_name": "" + } + ] +} +``` diff --git a/html/api_v0.php b/html/api_v0.php index 021cbb327c..676cca8967 100644 --- a/html/api_v0.php +++ b/html/api_v0.php @@ -141,6 +141,19 @@ $app->group( } ); // End Routing + // Resources section + $app->group( + '/resources', + function () use ($app) { + $app->group( + '/ip', + function () use ($app) { + $app->get('/arp/:ip', 'authToken', 'list_arp')->name('list_arp'); + } + ); + } + ); + // End Resources } ); $app->get('/v0', 'authToken', 'show_endpoints'); diff --git a/html/includes/api_functions.inc.php b/html/includes/api_functions.inc.php index 75aa966bbf..46fd86bc30 100644 --- a/html/includes/api_functions.inc.php +++ b/html/includes/api_functions.inc.php @@ -1323,3 +1323,37 @@ function list_ipsec() { $app->response->headers->set('Content-Type', 'application/json'); echo _json_encode($output); } + +function list_arp() { + $app = \Slim\Slim::getInstance(); + $router = $app->router()->getCurrentRoute()->getParams(); + $status = 'error'; + $code = 404; + $message = ''; + $ip = $router['ip']; + if (empty($ip)) { + $message = "No valid IP provided"; + } + else { + $code = 200; + $status = 'ok'; + if ($ip === "all") { + $hostname = mres($_GET['device']); + $device_id = ctype_digit($hostname) ? $hostname : getidbyname($hostname); + $arp = dbFetchRows("SELECT `ipv4_mac`.* FROM `ipv4_mac` LEFT JOIN `ports` ON `ipv4_mac`.`port_id` = `ports`.`port_id` WHERE `ports`.`device_id` = ?", array($device_id)); + } + else { + $arp = dbFetchRows("SELECT * FROM `ipv4_mac` WHERE `ipv4_address`=?", array($ip)); + } + $total = count($arp); + } + $output = array( + 'status' => $status, + 'err-msg' => $message, + 'count' => $total, + 'arp' => $arp, + ); + $app->response->setStatus($code); + $app->response->headers->set('Content-Type', 'application/json'); + echo _json_encode($output); +}