From 3e4a4940478b00b40e6fd796de72b0c6530f72df Mon Sep 17 00:00:00 2001 From: laf Date: Sun, 26 Oct 2014 15:48:51 +0000 Subject: [PATCH 1/2] Added bgp peers route to display all bgp peers --- html/api_v0.php | 1 + html/includes/api_functions.inc.php | 20 ++++++++++++++++++++ html/pages/api-docs.inc.php | 18 ++++++++++++++++++ 3 files changed, 39 insertions(+) diff --git a/html/api_v0.php b/html/api_v0.php index 613deae1c..346829c53 100644 --- a/html/api_v0.php +++ b/html/api_v0.php @@ -39,6 +39,7 @@ $app->group('/api', function() use ($app) { $app->get('/devices', 'authToken', 'list_devices')->name('list_devices');//api/v0/devices $app->post('/devices', 'authToken', 'add_device')->name('add_device');//api/v0/devices (json data needs to be passed) $app->delete('/devices/:hostname', 'authToken', 'del_device')->name('del_device');//api/v0/devices (json data needs to be passed) + $app->get('/bgp', 'authToken', 'list_bgp')->name('list_bgp');//api/v0/bpg }); $app->get('/v0', 'authToken', 'show_endpoints');//api/v0 }); diff --git a/html/includes/api_functions.inc.php b/html/includes/api_functions.inc.php index 9b4d4f2f1..5e1dcd532 100644 --- a/html/includes/api_functions.inc.php +++ b/html/includes/api_functions.inc.php @@ -349,3 +349,23 @@ function show_endpoints() { $app->response->headers->set('Content-Type', 'application/json'); echo _json_encode($output); } + +function list_bgp() { + global $config; + $app = \Slim\Slim::getInstance(); + $router = $app->router()->getCurrentRoute()->getParams(); + $code = 500; + $hostname = $_GET['hostname']; + $device_id = ctype_digit($hostname) ? $hostname : getidbyname($hostname); + if(is_numeric($device_id)) { + $sql = " AND `device_id`=?"; + $sql_params = array($device_id); + } + $bgp_sessions = dbFetchRows("SELECT * FROM bgpPeers WHERE `bgpPeerState` IS NOT NULL AND `bgpPeerState` != '' $sql", $sql_params); + $total_bgp_sessions = count($bgp_sessions); + $code = 200; + $output = array("status" => "ok", "count" => $total_bgp_sessions, "bgp_sessions" => $bgp_sessions); + $app->response->setStatus($code); + $app->response->headers->set('Content-Type', 'application/json'); + echo _json_encode($output); +} diff --git a/html/pages/api-docs.inc.php b/html/pages/api-docs.inc.php index 288cd26fc..5c6bb18df 100644 --- a/html/pages/api-docs.inc.php +++ b/html/pages/api-docs.inc.php @@ -250,6 +250,24 @@ if ($_SESSION['userlevel'] == '10') curl -H "X-Auth-Token: 91c60e737e342c205be5bba8e2954d27" \
"https://librenms.example.com/api/v0/devices/localhost/vlans"
+ + List BGP Sessions + + + /api + /v0 + /bgp + + + + + JSON + + + + curl -H "X-Auth-Token: 91c60e737e342c205be5bba8e2954d27" \
"https://librenms.example.com/api/v0/bgp"
+ From b4b160ead41ec92c8aed7f3efbf5fe93e5b33f09 Mon Sep 17 00:00:00 2001 From: laf Date: Wed, 29 Oct 2014 21:30:35 +0000 Subject: [PATCH 2/2] Updates from scrutinizer recommendations --- html/includes/api_functions.inc.php | 13 ++++++++++--- 1 file changed, 10 insertions(+), 3 deletions(-) diff --git a/html/includes/api_functions.inc.php b/html/includes/api_functions.inc.php index 5e1dcd532..5a0725f65 100644 --- a/html/includes/api_functions.inc.php +++ b/html/includes/api_functions.inc.php @@ -353,8 +353,11 @@ function show_endpoints() { function list_bgp() { global $config; $app = \Slim\Slim::getInstance(); - $router = $app->router()->getCurrentRoute()->getParams(); $code = 500; + $status = 'error'; + $message = 'Error retrieving bgpPeers'; + $sql = ''; + $sql_params = array(); $hostname = $_GET['hostname']; $device_id = ctype_digit($hostname) ? $hostname : getidbyname($hostname); if(is_numeric($device_id)) { @@ -363,8 +366,12 @@ function list_bgp() { } $bgp_sessions = dbFetchRows("SELECT * FROM bgpPeers WHERE `bgpPeerState` IS NOT NULL AND `bgpPeerState` != '' $sql", $sql_params); $total_bgp_sessions = count($bgp_sessions); - $code = 200; - $output = array("status" => "ok", "count" => $total_bgp_sessions, "bgp_sessions" => $bgp_sessions); + if(is_numeric($total_bgp_sessions)) { + $code = 200; + $status = 'ok'; + $message = ''; + } + $output = array("status" => "$status", "err-msg" => $message, "count" => $total_bgp_sessions, "bgp_sessions" => $bgp_sessions); $app->response->setStatus($code); $app->response->headers->set('Content-Type', 'application/json'); echo _json_encode($output);