From 7b4c63a2f415d2de96f99e49a99c3008c2a2bb28 Mon Sep 17 00:00:00 2001 From: Misha Komarovskiy Date: Tue, 9 Apr 2019 08:51:01 -0400 Subject: [PATCH] Added API call to list all ports FDB (#10020) Add FDB API calls to resolve this [forum request](https://community.librenms.org/t/fdb-table-search-via-api-mac-ip-switch-port/1772/4) DO NOT DELETE THIS TEXT #### Please note > Please read this information carefully. You can run `./scripts/pre-commit.php` to check your code before submitting. - [x] Have you followed our [code guidelines?](http://docs.librenms.org/Developing/Code-Guidelines/) #### Testers If you would like to test this pull request then please run: `./scripts/github-apply `, i.e `./scripts/github-apply 5926` After you are done testing, you can remove the changes with `./scripts/github-remove`. If there are schema changes, you can ask on discord how to revert. --- doc/API/Devices.md | 31 ++++++++++++++++++ doc/API/Switching.md | 38 ++++++++++++++++++++++ html/includes/api_functions.inc.php | 49 +++++++++++++++++++++++++++++ html/legacy_api_v0.php | 4 +++ 4 files changed, 122 insertions(+) diff --git a/doc/API/Devices.md b/doc/API/Devices.md index 884b409a89..258a03b88d 100644 --- a/doc/API/Devices.md +++ b/doc/API/Devices.md @@ -445,6 +445,37 @@ Output: ] } ``` + +### `get_device_fdb` + +Get a list of FDB entries associated with a device. + +Route: `/api/v0/devices/:hostname/fdb` + + - hostname can be either the device hostname or id + +Example: +```curl +curl -H 'X-Auth-Token: YOURAPITOKENHERE' https://librenms.org/api/v0/devices/localhost/fdb +``` + +Output: + +```json +{ + "status": "ok", + "ports_fdb": { + "ports_fdb_id": 10, + "port_id": 10000, + "mac_address": "1aaa2bbb3ccc", + "vlan_id": 20000, + "device_id": 1, + "created_at": "2019-01-1 01:01:01", + "updated_at": "2019-01-1 01:01:01" + } +} +``` + ### `get_device_ip_addresses` Get a list of IP addresses (v4 and v6) associated with a device. diff --git a/doc/API/Switching.md b/doc/API/Switching.md index bb54068224..fcacdcc8e6 100644 --- a/doc/API/Switching.md +++ b/doc/API/Switching.md @@ -187,3 +187,41 @@ Output: "count": 1 } ``` + +### `list_fdb` + +Get a list of all ports FDB. + +Route: `/api/v0/resources/fdb/:mac` + + - mac is the specific MAC address you would like to query + +Input: + +- + +Example: +```curl +curl -H 'X-Auth-Token: YOURAPITOKENHERE' https://librenms.org/api/v0/resources/fdb +curl -H 'X-Auth-Token: YOURAPITOKENHERE' https://librenms.org/api/v0/resources/fdb/1aaa2bbb3ccc +``` + +Output: +```json +{ + "status": "ok", + "ports_fdb": [ + { + "ports_fdb_id": 10, + "port_id": 10000, + "mac_address": "1aaa2bbb3ccc", + "vlan_id": 20000, + "device_id": 1, + "created_at": "2019-01-1 01:01:01", + "updated_at": "2019-01-1 01:01:01" + }, + ... + ], + "count": 100 +} +``` diff --git a/html/includes/api_functions.inc.php b/html/includes/api_functions.inc.php index 1b7d740e2a..709fb1c1b1 100644 --- a/html/includes/api_functions.inc.php +++ b/html/includes/api_functions.inc.php @@ -2025,6 +2025,55 @@ function get_link() } +function get_fdb() +{ + $app = \Slim\Slim::getInstance(); + $router = $app->router()->getCurrentRoute()->getParams(); + $hostname = $router['hostname']; + + if (empty($hostname)) { + api_error(500, 'No hostname has been provided'); + } + + $device_id = ctype_digit($hostname) ? $hostname : getidbyname($hostname); + $device = null; + if ($device_id) { + // save the current details for returning to the client on successful delete + $device = device_by_id_cache($device_id); + } + + if (!$device) { + api_error(404, "Device $hostname not found"); + } + check_device_permission($device_id); + + $fdb = \App\Models\PortsFdb::find($device_id); + api_success($fdb, 'ports_fdb'); +} + + +function list_fdb() +{ + check_is_read(); + + $app = \Slim\Slim::getInstance(); + $router = $app->router()->getCurrentRoute()->getParams(); + $mac = $router['mac']; + + if (empty($mac)) { + $fdb = \App\Models\PortsFdb::hasAccess(Auth::user())->get(); + } else { + $fdb = \App\Models\PortsFdb::find($mac); + } + $total_fdb = $fdb->count(); + if ($total_fdb == 0) { + api_error(404, 'Fdb do not exist'); + } + + api_success($fdb, 'ports_fdb'); +} + + function list_sensors() { check_is_read(); diff --git a/html/legacy_api_v0.php b/html/legacy_api_v0.php index 3b5b801525..d0a885b88e 100644 --- a/html/legacy_api_v0.php +++ b/html/legacy_api_v0.php @@ -64,6 +64,8 @@ $app->group( // api/v0/devices/$hostname/links $app->get('/:hostname/graphs', 'authToken', 'get_graphs')->name('get_graphs'); // api/v0/devices/$hostname/graphs + $app->get('/:hostname/fdb', 'authToken', 'get_fdb')->name('get_fdb'); + // api/v0/devices/$hostname/fdb $app->get('/:hostname/health(/:type)(/:sensor_id)', 'authToken', 'list_available_health_graphs')->name('list_available_health_graphs'); $app->get('/:hostname/wireless(/:type)(/:sensor_id)', 'authToken', 'list_available_wireless_graphs')->name('list_available_wireless_graphs'); $app->get('/:hostname/ports', 'authToken', 'get_port_graphs')->name('get_port_graphs'); @@ -185,6 +187,8 @@ $app->group( $app->group( '/resources', function () use ($app) { + $app->get('/fdb/', 'authToken', 'list_fdb')->name('list_fdb'); + $app->get('/fdb/:mac', 'authToken', 'list_fdb')->name('list_fdb'); $app->get('/links', 'authToken', 'list_links')->name('list_links'); $app->get('/links/:id', 'authToken', 'get_link')->name('get_link'); $app->get('/locations', 'authToken', 'list_locations')->name('list_locations');