From c6a0c9d124564575fc88bf9162ec2879172e186d Mon Sep 17 00:00:00 2001 From: Neil Lathwood Date: Thu, 16 Nov 2017 21:21:52 +0000 Subject: [PATCH] api: Added support for Oxidized asking for a single host (#7705) --- doc/API/index.md | 2 +- html/api_v0.php | 2 +- html/includes/api_functions.inc.php | 13 +++++++++++-- 3 files changed, 13 insertions(+), 4 deletions(-) diff --git a/doc/API/index.md b/doc/API/index.md index 26ae5e5e1f..fbf475d8a1 100644 --- a/doc/API/index.md +++ b/doc/API/index.md @@ -768,7 +768,7 @@ Output: List devices for use with Oxidized. If you have group support enabled then a group will also be returned based on your config. -Route: `/api/v0/oxidized` +Route: `/api/v0/oxidized(/:hostname)` Input (JSON): diff --git a/html/api_v0.php b/html/api_v0.php index 09489a592c..75e38b5e61 100644 --- a/html/api_v0.php +++ b/html/api_v0.php @@ -42,7 +42,7 @@ $app->group( $app->get('/bgp', 'authToken', 'list_bgp')->name('list_bgp'); $app->get('/ospf', 'authToken', 'list_ospf')->name('list_ospf'); // api/v0/bgp - $app->get('/oxidized', 'authToken', 'list_oxidized')->name('list_oxidized'); + $app->get('/oxidized(/:hostname)', 'authToken', 'list_oxidized')->name('list_oxidized'); $app->group( '/devices', function () use ($app) { diff --git a/html/includes/api_functions.inc.php b/html/includes/api_functions.inc.php index 8e03d26edd..9d8aa1d807 100644 --- a/html/includes/api_functions.inc.php +++ b/html/includes/api_functions.inc.php @@ -1239,12 +1239,21 @@ function list_oxidized() { global $config; $app = \Slim\Slim::getInstance(); - $app->response->headers->set('Content-Type', 'application/json'); + $router = $app->router()->getCurrentRoute()->getParams(); + $hostname = $router['hostname']; $devices = array(); $device_types = "'".implode("','", $config['oxidized']['ignore_types'])."'"; $device_os = "'".implode("','", $config['oxidized']['ignore_os'])."'"; - foreach (dbFetchRows("SELECT hostname,sysname,os,location FROM `devices` LEFT JOIN devices_attribs AS `DA` ON devices.device_id = DA.device_id AND `DA`.attrib_type='override_Oxidized_disable' WHERE `disabled`='0' AND `ignore` = 0 AND (DA.attrib_value = 'false' OR DA.attrib_value IS NULL) AND (`type` NOT IN ($device_types) AND `os` NOT IN ($device_os))") as $device) { + + $sql = ''; + $params = array(); + if ($hostname) { + $sql = " AND hostname = ?"; + $params = array($hostname); + } + + foreach (dbFetchRows("SELECT hostname,sysname,os,location FROM `devices` LEFT JOIN devices_attribs AS `DA` ON devices.device_id = DA.device_id AND `DA`.attrib_type='override_Oxidized_disable' WHERE `disabled`='0' AND `ignore` = 0 AND (DA.attrib_value = 'false' OR DA.attrib_value IS NULL) AND (`type` NOT IN ($device_types) AND `os` NOT IN ($device_os)) $sql", $params) as $device) { if ($config['oxidized']['group_support'] == "true") { foreach ($config['oxidized']['group']['hostname'] as $host_group) { if (preg_match($host_group['regex'].'i', $device['hostname'])) {