api: Added support for Oxidized asking for a single host (#7705)

This commit is contained in:
Neil Lathwood
2017-11-16 21:21:52 +00:00
committed by GitHub
parent 37d005ebd8
commit c6a0c9d124
3 changed files with 13 additions and 4 deletions

View File

@ -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):

View File

@ -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) {

View File

@ -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'])) {