From e40042bd5c168e70f20f8f824cad0680ea015d52 Mon Sep 17 00:00:00 2001 From: jnewell Date: Tue, 5 Apr 2016 13:03:17 -0700 Subject: [PATCH 1/4] Added device/port_stack API endpoint --- html/api_v0.php | 1 + html/includes/api_functions.inc.php | 21 +++++++++++++++++++++ 2 files changed, 22 insertions(+) diff --git a/html/api_v0.php b/html/api_v0.php index 883b4609d..3ca6dc0ad 100644 --- a/html/api_v0.php +++ b/html/api_v0.php @@ -48,6 +48,7 @@ $app->group( $app->get('/:hostname/graphs', 'authToken', 'get_graphs')->name('get_graphs'); // api/v0/devices/$hostname/graphs $app->get('/:hostname/ports', 'authToken', 'get_port_graphs')->name('get_port_graphs'); + $app->get('/:hostname/port_stack', 'authToken', 'get_port_stack')->name('get_port_stack'); // api/v0/devices/$hostname/ports $app->get('/:hostname/components', 'authToken', 'get_components')->name('get_components'); $app->post('/:hostname/components/:type', 'authToken', 'add_components')->name('add_components'); diff --git a/html/includes/api_functions.inc.php b/html/includes/api_functions.inc.php index 834904fff..004138f16 100644 --- a/html/includes/api_functions.inc.php +++ b/html/includes/api_functions.inc.php @@ -721,6 +721,27 @@ function get_port_graphs() { } +function get_port_stack() { + global $config; + $app = \Slim\Slim::getInstance(); + $router = $app->router()->getCurrentRoute()->getParams(); + $hostname = $router['hostname']; + + // use hostname as device_id if it's all digits + $device_id = ctype_digit($hostname) ? $hostname : getidbyname($hostname); + $mappings = dbFetchRows("SELECT * FROM `ports_stack` WHERE `device_id` = ? AND `ifStackStatus` = 'active' ORDER BY `port_id_high` ASC", array($device_id)); + $total_mappings = count($mappings); + $output = array( + 'status' => 'ok', + 'err-msg' => '', + 'count' => $total_mappings, + 'mappings' => $mappings, + ); + $app->response->setStatus('200'); + $app->response->headers->set('Content-Type', 'application/json'); + echo _json_encode($output); +} + function list_alert_rules() { global $config; $app = \Slim\Slim::getInstance(); From 0e6093abfc4f8e22c79b138e79d5440955d03100 Mon Sep 17 00:00:00 2001 From: jnewell Date: Tue, 5 Apr 2016 14:25:45 -0700 Subject: [PATCH 2/4] Added device/port_stack API endpoint. Updated documentation --- doc/API/API-Docs.md | 42 +++++++++++++++++++++++++++++ html/includes/api_functions.inc.php | 9 +++++-- 2 files changed, 49 insertions(+), 2 deletions(-) diff --git a/doc/API/API-Docs.md b/doc/API/API-Docs.md index 185349b95..b23339ee8 100644 --- a/doc/API/API-Docs.md +++ b/doc/API/API-Docs.md @@ -13,6 +13,7 @@ - [`get_graphs`](#api-route-5) - [`get_graph_generic_by_hostname`](#api-route-6) - [`get_port_graphs`](#api-route-7) + - [`get_port_stack`](#api-route-29) - [`get_components`](#api-route-25) - [`add_components`](#api-route-26) - [`edit_components`](#api-route-27) @@ -275,6 +276,47 @@ Output: } ``` +### Function: `get_port_stack` [`top`](#top) + +Get a list of port mappings for a device. This is useful for showing physical ports that are in a virtual port-channel. + +Route: /api/v0/devices/:hostname/port_stack + +- hostname can be either the device hostname or id + +Input: + + - valid_mappings: Filter the result by only showing valid mappings ("0" values not shown). + +Example: +```curl +curl -H 'X-Auth-Token: YOURAPITOKENHERE' https://librenms.org/api/v0/devices/localhost/port_stack?valid_mappings +``` + +Output: + +```text +{ + "status": "ok", + "err-msg": "", + "count": 2, + "mappings": [ + { + "device_id": "3742", + "port_id_high": "1001000", + "port_id_low": "51001", + "ifStackStatus": "active" + }, + { + "device_id": "3742", + "port_id_high": "1001000", + "port_id_low": "52001", + "ifStackStatus": "active" + } + ] +} +``` + ### Function: `get_components` [`top`](#top) Get a list of components for a particular device. diff --git a/html/includes/api_functions.inc.php b/html/includes/api_functions.inc.php index 004138f16..9a2629890 100644 --- a/html/includes/api_functions.inc.php +++ b/html/includes/api_functions.inc.php @@ -726,10 +726,15 @@ function get_port_stack() { $app = \Slim\Slim::getInstance(); $router = $app->router()->getCurrentRoute()->getParams(); $hostname = $router['hostname']; - // use hostname as device_id if it's all digits $device_id = ctype_digit($hostname) ? $hostname : getidbyname($hostname); - $mappings = dbFetchRows("SELECT * FROM `ports_stack` WHERE `device_id` = ? AND `ifStackStatus` = 'active' ORDER BY `port_id_high` ASC", array($device_id)); + + if (isset($_GET['valid_mappings'])) { + $mappings = dbFetchRows("SELECT * FROM `ports_stack` WHERE (`device_id` = ? AND `ifStackStatus` = 'active' AND (`port_id_high` != '0' AND `port_id_low` != '0')) ORDER BY `port_id_high` ASC", array($device_id)); + } else { + $mappings = dbFetchRows("SELECT * FROM `ports_stack` WHERE `device_id` = ? AND `ifStackStatus` = 'active' ORDER BY `port_id_high` ASC", array($device_id)); + } + $total_mappings = count($mappings); $output = array( 'status' => 'ok', From 2d7bc78f7aa01138a78ac4111fdebe48aaa2c9a1 Mon Sep 17 00:00:00 2001 From: jnewell Date: Thu, 7 Apr 2016 15:56:27 -0700 Subject: [PATCH 3/4] update authors --- AUTHORS.md | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/AUTHORS.md b/AUTHORS.md index 2a6395a7a..ebd81652b 100644 --- a/AUTHORS.md +++ b/AUTHORS.md @@ -27,7 +27,7 @@ LibreNMS contributors: - Mohammad Al-Shami (mohshami) - Rudy Hardeman (zarya) - Freddie Cash (fjwcash) -- Thom Seddon (thomseddon) +- Thom Seddon (thomseddon) - Vitali Kari (vitalisator) - James Campbell (neokjames) - Steve Calvário (Calvario) @@ -93,6 +93,7 @@ LibreNMS contributors: - David Bell (geordish) - Ibrahim Tachijian (barhom) - Guillaume Coeugnet (gcoeugnet) +- Jared Newell (JaredN) [1]: http://observium.org/ "Observium web site" Observium was written by: @@ -100,4 +101,4 @@ Observium was written by: - Tom Laermans - various others as indicated in the file contents and commit logs - + From c2a62c1895f53d8581871c55cd6c5f88a533975f Mon Sep 17 00:00:00 2001 From: jnewell Date: Thu, 7 Apr 2016 16:07:11 -0700 Subject: [PATCH 4/4] I agree to the conditions of the Contributor Agreement contained in doc/General/Contributing.md. --- AUTHORS.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/AUTHORS.md b/AUTHORS.md index ebd81652b..65f1ecc3c 100644 --- a/AUTHORS.md +++ b/AUTHORS.md @@ -93,7 +93,7 @@ LibreNMS contributors: - David Bell (geordish) - Ibrahim Tachijian (barhom) - Guillaume Coeugnet (gcoeugnet) -- Jared Newell (JaredN) +- Jared Newell (Jaredn) [1]: http://observium.org/ "Observium web site" Observium was written by: