From 1a3f5504d0200660a35b12fed304e8a56b0c436f Mon Sep 17 00:00:00 2001 From: laf Date: Sun, 30 Nov 2014 15:45:10 +0000 Subject: [PATCH 1/2] Added new API route to obtain available ports for a device --- 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 a80370a98d..2127287dbc 100644 --- a/html/api_v0.php +++ b/html/api_v0.php @@ -34,6 +34,7 @@ $app->group('/api', function() use ($app) { $app->delete('/:hostname', 'authToken', 'del_device')->name('del_device');//api/v0/devices/$hostname $app->get('/:hostname', 'authToken', 'get_device')->name('get_device');//api/v0/devices/$hostname $app->get('/:hostname/vlans', 'authToken', 'get_vlans')->name('get_vlans');//api/v0/devices/$hostname/vlans + $app->get('/:hostname/ports', 'authToken', 'get_port_graphs')->name('get_port_graphs');//api/v0/devices/$hostname/ports $app->get('/:hostname/:type', 'authToken', 'get_graph_generic_by_hostname')->name('get_graph_generic_by_hostname');//api/v0/devices/$hostname/$type $app->get('/:hostname/ports/:ifname', 'authToken', 'get_port_stats_by_port_hostname')->name('get_port_stats_by_port_hostname');//api/v0/devices/$hostname/ports/$ifName $app->get('/:hostname/ports/:ifname/:type', 'authToken', 'get_graph_by_port_hostname')->name('get_graph_by_port_hostname');//api/v0/devices/$hostname/ports/$ifName/$type diff --git a/html/includes/api_functions.inc.php b/html/includes/api_functions.inc.php index 607c4cacd0..9c037e60bf 100644 --- a/html/includes/api_functions.inc.php +++ b/html/includes/api_functions.inc.php @@ -419,3 +419,24 @@ function get_graph_by_portgroup() { $app->response->headers->set('Content-Type', 'image/png'); require("includes/graphs/graph.inc.php"); } + +function get_port_graphs() { + global $config; + $app = \Slim\Slim::getInstance(); + $router = $app->router()->getCurrentRoute()->getParams(); + $hostname = $router['hostname']; + if(isset($_GET['columns'])) { + $columns = $_GET['columns']; + } else { + $columns = 'ifName'; + } + + // use hostname as device_id if it's all digits + $device_id = ctype_digit($hostname) ? $hostname : getidbyname($hostname); + $ports = dbFetchRows("SELECT $columns FROM `ports` WHERE `device_id` = ? AND `deleted` = '0' ORDER BY `ifIndex` ASC", array($device_id)); + $total_ports = count($ports); + $output = array("status" => "ok", "err-msg" => '', "count" => $total_ports, "ports" => $ports); + $app->response->setStatus('200'); + $app->response->headers->set('Content-Type', 'application/json'); + echo _json_encode($output); +} From 0a7a1a0ef6ab33d2d2c4f7b98a29505a896ec1ba Mon Sep 17 00:00:00 2001 From: laf Date: Sun, 30 Nov 2014 17:11:08 +0000 Subject: [PATCH 2/2] Added docs for the route --- html/pages/api-docs.inc.php | 23 +++++++++++++++++++++++ 1 file changed, 23 insertions(+) diff --git a/html/pages/api-docs.inc.php b/html/pages/api-docs.inc.php index bddb64729b..02d9330b8e 100644 --- a/html/pages/api-docs.inc.php +++ b/html/pages/api-docs.inc.php @@ -296,6 +296,29 @@ if ($_SESSION['userlevel'] == '10') curl -H "X-Auth-Token: 91c60e737e342c205be5bba8e2954d27" \
"https://librenms.example.com/api/v0/bgp"
+ + List ports for a device + + + /api + /v0 + /devices/$hostname/ports + + + + + JSON + + + + curl -H "X-Auth-Token: 91c60e737e342c205be5bba8e2954d27" \
"https://librenms.example.com/api/v0/devices/localhost/ports"
+ + + curl -H "X-Auth-Token: 91c60e737e342c205be5bba8e2954d27" \
"https://librenms.example.com/api/v0/devices/localhost/ports?columns=ifDescr,ifName"
+