API: list_arp search by MAC (#10803)

* API: Allow search ARP by MAC

* reformat mac

* Update docs
This commit is contained in:
Tony Murray 2019-11-13 02:21:07 +00:00 committed by GitHub
parent dc751123bc
commit 12773d125b
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 17 additions and 13 deletions

View File

@ -5,15 +5,16 @@ path: blob/master/doc/
Retrieve a specific ARP entry or all ARP enties for a device Retrieve a specific ARP entry or all ARP enties for a device
Route: `/api/v0/resources/ip/arp/:ip` Route: `/api/v0/resources/ip/arp/:query`
- ip is the specific IP you would like to query, if this is all then Query can be:
you need to pass ?device=_hostname_ (or device id) - An IP address
- This may also be a cidr network, for example 192.168.1.0/24 - A CIDR network (192.168.1.0/24)
- `all` and set ?device=_hostname_ (or device id)
Input: Input:
- device if you specify all for the IP then you need to populate this - device if you specify all for the query then you need to populate this
with the hostname or id of the device. with the hostname or id of the device.
Example: Example:

View File

@ -1996,22 +1996,22 @@ function list_ip_networks()
function list_arp(\Illuminate\Http\Request $request) function list_arp(\Illuminate\Http\Request $request)
{ {
$ip = $request->route('ip'); $query = $request->route('query');
$cidr = $request->route('cidr'); $cidr = $request->route('cidr');
$hostname = $request->get('device'); $hostname = $request->get('device');
if (empty($ip)) { if (empty($query)) {
return api_error(400, "No valid IP provided"); return api_error(400, "No valid IP/MAC provided");
} elseif ($ip === "all" && empty($hostname)) { } elseif ($query === "all" && empty($hostname)) {
return api_error(400, "Device argument is required when requesting all entries"); return api_error(400, "Device argument is required when requesting all entries");
} }
if ($ip === "all") { if ($query === "all") {
$device_id = ctype_digit($hostname) ? $hostname : getidbyname($hostname); $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` = ?", [$device_id]); $arp = dbFetchRows("SELECT `ipv4_mac`.* FROM `ipv4_mac` LEFT JOIN `ports` ON `ipv4_mac`.`port_id` = `ports`.`port_id` WHERE `ports`.`device_id` = ?", [$device_id]);
} elseif ($cidr) { } elseif ($cidr) {
try { try {
$ip = new IPv4("$ip/$cidr"); $ip = new IPv4("$query/$cidr");
$arp = dbFetchRows( $arp = dbFetchRows(
'SELECT * FROM `ipv4_mac` WHERE (inet_aton(`ipv4_address`) & ?) = ?', 'SELECT * FROM `ipv4_mac` WHERE (inet_aton(`ipv4_address`) & ?) = ?',
[ip2long($ip->getNetmask()), ip2long($ip->getNetworkAddress())] [ip2long($ip->getNetmask()), ip2long($ip->getNetworkAddress())]
@ -2019,8 +2019,11 @@ function list_arp(\Illuminate\Http\Request $request)
} catch (InvalidIpException $e) { } catch (InvalidIpException $e) {
return api_error(400, "Invalid Network Address"); return api_error(400, "Invalid Network Address");
} }
} elseif (filter_var($query, FILTER_VALIDATE_MAC)) {
$mac = \LibreNMS\Util\Rewrite::macToHex($query);
$arp = dbFetchRows("SELECT * FROM `ipv4_mac` WHERE `mac_address`=?", [$mac]);
} else { } else {
$arp = dbFetchRows("SELECT * FROM `ipv4_mac` WHERE `ipv4_address`=?", [$ip]); $arp = dbFetchRows("SELECT * FROM `ipv4_mac` WHERE `ipv4_address`=?", [$query]);
} }
return api_success($arp, 'arp'); return api_success($arp, 'arp');
} }

View File

@ -40,7 +40,7 @@ Route::group(['prefix' => 'v0', 'namespace' => '\App\Api\Controllers'], function
Route::get('links/{id}', 'LegacyApiController@get_link')->name('get_link'); Route::get('links/{id}', 'LegacyApiController@get_link')->name('get_link');
Route::get('locations', 'LegacyApiController@list_locations')->name('list_locations'); Route::get('locations', 'LegacyApiController@list_locations')->name('list_locations');
Route::get('ip/addresses', 'LegacyApiController@list_ip_addresses')->name('list_ip_addresses'); Route::get('ip/addresses', 'LegacyApiController@list_ip_addresses')->name('list_ip_addresses');
Route::get('ip/arp/{ip}/{cidr?}', 'LegacyApiController@list_arp')->name('list_arp'); Route::get('ip/arp/{query}/{cidr?}', 'LegacyApiController@list_arp')->name('list_arp');
Route::get('ip/networks', 'LegacyApiController@list_ip_networks')->name('list_ip_networks'); Route::get('ip/networks', 'LegacyApiController@list_ip_networks')->name('list_ip_networks');
Route::get('ip/networks/{id}/ip', 'LegacyApiController@get_network_ip_addresses')->name('get_network_ip_addresses'); Route::get('ip/networks/{id}/ip', 'LegacyApiController@get_network_ip_addresses')->name('get_network_ip_addresses');
}); });