Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added resources/sensors api call to list all sensors #9837

Merged
merged 5 commits into from Mar 19, 2019
Merged
Changes from 1 commit
Commits
File filter...
Filter file types
Jump to…
Jump to file or symbol
Failed to load files and symbols.

Always

Just for now

@@ -758,6 +758,58 @@ Output:
}
```

### `list_sensors`

Get a list of all Sensors.

Route: `/api/v0/resources/sensors`

Input:

-

Example:
```curl
curl -H 'X-Auth-Token: YOURAPITOKENHERE' https://librenms.org/api/v0/resources/sensors
```

Output:
```json
{
"status": "ok",
"sensors": [
{
"sensor_id": 218810,
"sensor_deleted": 0,
"sensor_class": "dbm",
"device_id": 136,
"poller_type": "snmp",
"sensor_oid": ".1.3.6.1.4.1.2636.3.60.1.1.1.1.7.919",
"sensor_index": "tx-919",
"sensor_type": "junos",
"sensor_descr": "xe-2/1/4 Tx Power",
"group": null,
"sensor_divisor": 100,
"sensor_multiplier": 1,
"sensor_current": -1.81,
"sensor_limit": 2,
"sensor_limit_warn": 0.5,
"sensor_limit_low": -9.7,
"sensor_limit_low_warn": -8.21,
"sensor_alert": 1,
"sensor_custom": "No",
"entPhysicalIndex": "919",
"entPhysicalIndex_measured": "ports",
"lastupdate": "2019-02-18 02:47:09",
"sensor_prev": -1.77,
"user_func": null
},
...
],
"count": 100
}
```

### `list_devices`

Return a list of devices.
@@ -2027,6 +2027,42 @@ function get_link()
}
function list_sensors()
{
$app = \Slim\Slim::getInstance();
$router = $app->router()->getCurrentRoute()->getParams();
$sql = '';
$sql_params = array();
$hostname = $router['hostname'];
$device_id = ctype_digit($hostname) ? $hostname : getidbyname($hostname);
if (is_numeric($device_id)) {
check_device_permission($device_id);
$sql = " AND `sensors`.`device_id`=?";
$sql_params = array($device_id);
}
if (!LegacyAuth::user()->hasGlobalRead()) {
$sql .= " AND `sensors`.`device_id` IN (SELECT device_id FROM devices_perms WHERE user_id = ?)";
$sql_params[] = LegacyAuth::id();
}
$sensors = array();
foreach (dbFetchRows("SELECT `sensors`.* FROM `sensors` LEFT JOIN `devices` ON `sensors`.`device_id` = `devices`.`device_id` WHERE `sensors`.`sensor_id` IS NOT NULL $sql", $sql_params) as $sensor) {
$host_id = get_vm_parent_id($device);

Check notice on line 2050 in html/includes/api_functions.inc.php

Scrutinizer / Inspection

html/includes/api_functions.inc.php#L2050

The variable ``$device`` seems to be defined later in this ``foreach`` loop on line ``2051``. Are you sure it is defined here?

This comment has been minimized.

Copy link
@murrant

murrant Feb 19, 2019

Member

$device is not defined yet...

This comment has been minimized.

Copy link
@zombah

zombah Feb 19, 2019

Author Contributor

Yes, missed that. Now i see that i wrongly duplicated this construct, it looks devices table specific with it's ip and parent_id stuff. Maybe to just use check_is_read for such list functions?

$device['ip'] = inet6_ntop($device['ip']);
if (is_numeric($host_id)) {
$device['parent_id'] = $host_id;
}
$sensors[] = $sensor;
}
$total_sensors = count($sensors);
if ($total_sensors == 0) {
api_error(404, 'Sensors do not exist');
}
api_success($sensors, 'sensors');
}
function list_ip_addresses()
{
check_is_read();
@@ -188,6 +188,7 @@ function () use ($app) {
$app->get('/links', 'authToken', 'list_links')->name('list_links');
$app->get('/links/:id', 'authToken', 'get_link')->name('get_link');
$app->get('/locations', 'authToken', 'list_locations')->name('list_locations');
$app->get('/sensors', 'authToken', 'list_sensors')->name('list_sensors');
$app->get('/vlans', 'authToken', 'list_vlans')->name('list_vlans');
$app->group(
'/ip',
ProTip! Use n and p to navigate between commits in a pull request.
You can’t perform that action at this time.