From daa0773f4896d13240069f01cadcf10852dc0ddc Mon Sep 17 00:00:00 2001 From: Neil Lathwood Date: Wed, 6 Dec 2017 22:27:30 +0000 Subject: [PATCH] api: Added support for wireless sensors (#7846) * api: Added support for wireless sensors * Added docs for new api calls --- doc/API/Devices.md | 136 +++++++++++++++++++++++++++- html/api_v0.php | 3 +- html/includes/api_functions.inc.php | 43 ++++++++- 3 files changed, 179 insertions(+), 3 deletions(-) diff --git a/doc/API/Devices.md b/doc/API/Devices.md index 389c966d24..7f4e9f5811 100644 --- a/doc/API/Devices.md +++ b/doc/API/Devices.md @@ -118,7 +118,7 @@ Route: `/api/v0/devices/:hostname/health(/:type)(/:sensor_id)` - hostname can be either the device hostname or id - type (optional) is health type / sensor class - - sensor_id (optional) is the sensor id to retreive specific information. + - sensor_id (optional) is the sensor id to retrieve specific information. Input: @@ -212,6 +212,109 @@ Output: } ``` +### `list_available_wireless_graphs` +This function allows to do three things: + + - Get a list of overall wireless graphs available. + - Get a list of wireless graphs based on provided class. + - Get the wireless sensors information based on ID. + +Route: `/api/v0/devices/:hostname/wireless(/:type)(/:sensor_id)` + + - hostname can be either the device hostname or id + - type (optional) is wireless type / wireless class + - sensor_id (optional) is the sensor id to retrieve specific information. + +Input: + + - + +Example: +```curl +curl -H 'X-Auth-Token: YOURAPITOKENHERE' https://librenms.org/api/v0/devices/localhost/wireless +``` + +Output: +``` +{ + "status": "ok", + "graphs": [ + { + "desc": "Ccq", + "name": "device_wireless_ccq" + }, + { + "desc": "Clients", + "name": "device_wireless_clients" + } + ], + "count": 2 +} +``` + +Example: +```curl +curl -H 'X-Auth-Token: YOURAPITOKENHERE' https://librenms.org/api/v0/devices/localhost/wireless/device_wireless_ccq +``` + +Output: +``` +{ + "status": "ok", + "graphs": [ + { + "sensor_id": "791", + "desc": "SSID: bast (ng)" + }, + { + "sensor_id": "792", + "desc": "SSID: bast (na)" + } + ], + "count": 2 +} +``` + +Example: +```curl +curl -H 'X-Auth-Token: YOURAPITOKENHERE' https://librenms.org/api/v0/devices/localhost/health/device_wireless_ccq/1 +``` + +Output: +``` +{ + "status": "ok", + "graphs": [ + { + "sensor_id": "791", + "sensor_deleted": "0", + "sensor_class": "ccq", + "device_id": "381", + "sensor_index": "0", + "sensor_type": "unifi", + "sensor_descr": "SSID: bast (ng)", + "sensor_divisor": "10", + "sensor_multiplier": "1", + "sensor_aggregator": "sum", + "sensor_current": "100", + "sensor_prev": "100", + "sensor_limit": null, + "sensor_limit_warn": null, + "sensor_limit_low": null, + "sensor_limit_low_warn": null, + "sensor_alert": "1", + "sensor_custom": "No", + "entPhysicalIndex": null, + "entPhysicalIndex_measured": null, + "lastupdate": "2017-12-06 21:26:29", + "sensor_oids": "[\".1.3.6.1.4.1.41112.1.6.1.2.1.3.0\"]", + "access_point_id": null + } + ], + "count": 1 +} +``` + ### `get_health_graph` Get a particular health class graph for a device, if you provide a sensor_id as well then a single sensor graph @@ -245,7 +348,38 @@ Output: Output is the graph of the particular health type sensor provided. +### `get_wireless_graph` +Get a particular wireless class graph for a device, if you provide a sensor_id as well then a single sensor graph +will be provided. If no sensor_id value is provided then you will be sent a stacked wireless graph. + +Route: `/api/v0/devices/:hostname/graphs/wireless/:type(/:sensor_id)` + + - hostname can be either the device hostname or id + - type is the name of the wireless graph as returned by [`list_available_wireless_graphs`](#function-list_available_wireless_graphs) + - sensor_id (optional) restricts the graph to return a particular wireless sensor graph. + +Input: + + - + +Example: +```curl +curl -H 'X-Auth-Token: YOURAPITOKENHERE' https://librenms.org/api/v0/devices/localhost/graphs/wireless/device_wireless_ccq +``` + +Output: + +Output is a stacked graph for the wireless type provided. + +Example: +```curl +curl -H 'X-Auth-Token: YOURAPITOKENHERE' https://librenms.org/api/v0/devices/localhost/graphs/wireless/device_wireless_ccq/1 +``` + +Output: + +Output is the graph of the particular wireless type sensor provided. ### `get_graph_generic_by_hostname` diff --git a/html/api_v0.php b/html/api_v0.php index b734e3d858..a0b3322d29 100644 --- a/html/api_v0.php +++ b/html/api_v0.php @@ -57,7 +57,7 @@ $app->group( $app->get('/:hostname/graphs', 'authToken', 'get_graphs')->name('get_graphs'); // api/v0/devices/$hostname/graphs $app->get('/:hostname/health(/:type)(/:sensor_id)', 'authToken', 'list_available_health_graphs')->name('list_available_health_graphs'); - // api/v0/devices/$hostname/health + $app->get('/:hostname/wireless(/:type)(/:sensor_id)', 'authToken', 'list_available_wireless_graphs')->name('list_available_wireless_graphs'); $app->get('/:hostname/ports', 'authToken', 'get_port_graphs')->name('get_port_graphs'); $app->get('/:hostname/ip', 'authToken', 'get_ip_addresses')->name('get_device_ip_addresses'); $app->get('/:hostname/port_stack', 'authToken', 'get_port_stack')->name('get_port_stack'); @@ -68,6 +68,7 @@ $app->group( $app->delete('/:hostname/components/:component', 'authToken', 'delete_components')->name('delete_components'); $app->get('/:hostname/groups', 'authToken', 'get_device_groups')->name('get_device_groups'); $app->get('/:hostname/graphs/health/:type(/:sensor_id)', 'authToken', 'get_graph_generic_by_hostname')->name('get_health_graph'); + $app->get('/:hostname/graphs/wireless/:type(/:sensor_id)', 'authToken', 'get_graph_generic_by_hostname')->name('get_wireless_graph'); $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'); diff --git a/html/includes/api_functions.inc.php b/html/includes/api_functions.inc.php index bb52e6b85d..e43ce6f56d 100644 --- a/html/includes/api_functions.inc.php +++ b/html/includes/api_functions.inc.php @@ -207,7 +207,12 @@ function get_graph_generic_by_hostname() $vars['type'] = $router['type'] ?: 'device_uptime'; if (isset($sensor_id)) { $vars['id'] = $sensor_id; - $vars['type'] = str_replace('device_', 'sensor_', $vars['type']); + if (str_contains($vars['type'], '_wireless')) { + $vars['type'] = str_replace('device_', '', $vars['type']); + } else { + // If this isn't a wireless graph we need to fix the name. + $vars['type'] = str_replace('device_', 'sensor_', $vars['type']); + } } // use hostname as device_id if it's all digits @@ -743,6 +748,42 @@ function list_available_health_graphs() return api_success($graphs, 'graphs'); } +function list_available_wireless_graphs() +{ + $app = \Slim\Slim::getInstance(); + $router = $app->router()->getCurrentRoute()->getParams(); + $hostname = $router['hostname']; + $device_id = ctype_digit($hostname) ? $hostname : getidbyname($hostname); + check_device_permission($device_id); + if (isset($router['type'])) { + list(, , $type) = explode('_', $router['type']); + } + $sensor_id = $router['sensor_id'] ?: null; + $graphs = array(); + + if (isset($type)) { + if (isset($sensor_id)) { + $graphs = dbFetchRows('SELECT * FROM `wireless_sensors` WHERE `sensor_id` = ?', array($sensor_id)); + } else { + foreach (dbFetchRows('SELECT `sensor_id`, `sensor_descr` FROM `wireless_sensors` WHERE `device_id` = ? AND `sensor_class` = ? AND `sensor_deleted` = 0', array($device_id, $type)) as $graph) { + $graphs[] = array( + 'sensor_id' => $graph['sensor_id'], + 'desc' => $graph['sensor_descr'], + ); + } + } + } else { + foreach (dbFetchRows('SELECT `sensor_class` FROM `wireless_sensors` WHERE `device_id` = ? AND `sensor_deleted` = 0 GROUP BY `sensor_class`', array($device_id)) as $graph) { + $graphs[] = array( + 'desc' => ucfirst($graph['sensor_class']), + 'name' => 'device_wireless_'.$graph['sensor_class'], + ); + } + } + + return api_success($graphs, 'graphs'); +} + function get_port_graphs() { $app = \Slim\Slim::getInstance();