Skip to content

Commit 7bd424e

Browse files
Fix health API returning empty results for processor, storage and mempool (librenms#19953)
* Fix health API returning empty results for processor, storage and mempool * Fix code quality issues * Address review comments - removing soft delete reference for mempool
1 parent f6ad9fe commit 7bd424e

2 files changed

Lines changed: 51 additions & 1 deletion

File tree

doc/API/Devices.md

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -250,6 +250,11 @@ Route: `/api/v0/devices/:hostname/health(/:type)(/:sensor_id)`
250250
- type (optional) is health type / sensor class
251251
- sensor_id (optional) is the sensor id to retrieve specific information.
252252

253+
`type` may be a sensor class (e.g. `device_voltage`) or one of the special
254+
classes `device_processor`, `device_storage` and `device_mempool`, which are
255+
stored in their own tables rather than the `sensors` table. The `device_`
256+
prefix is optional, so `processor` and `device_processor` are equivalent.
257+
253258
Input:
254259

255260
-
@@ -348,6 +353,28 @@ Output:
348353
}
349354
```
350355

356+
Example (processor list):
357+
358+
```curl
359+
curl -H 'X-Auth-Token: YOURAPITOKENHERE' https://foo.example/api/v0/devices/localhost/health/processor
360+
```
361+
362+
Output:
363+
364+
```
365+
{
366+
"status": "ok",
367+
"message": "",
368+
"count": 1,
369+
"graphs": [
370+
{
371+
"sensor_id": "1",
372+
"desc": "Processor"
373+
}
374+
]
375+
}
376+
```
377+
351378
### `list_available_wireless_graphs`
352379

353380
This function allows to do three things:

includes/html/api_functions.inc.php

Lines changed: 24 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@
3030
use App\Models\Ipv6Network;
3131
use App\Models\Link;
3232
use App\Models\Location;
33+
use App\Models\Mempool;
3334
use App\Models\MplsSap;
3435
use App\Models\MplsService;
3536
use App\Models\OspfPort;
@@ -43,8 +44,10 @@
4344
use App\Models\PortSecurity;
4445
use App\Models\PortsFdb;
4546
use App\Models\PortsNac;
47+
use App\Models\Processor;
4648
use App\Models\Sensor;
4749
use App\Models\ServiceTemplate;
50+
use App\Models\Storage;
4851
use App\Models\UserPref;
4952
use App\Models\Vlan;
5053
use App\Models\Vrf;
@@ -1045,8 +1048,28 @@ function list_available_health_graphs(Illuminate\Http\Request $request)
10451048
$sensor_id = $request->route('sensor_id');
10461049
$graphs = [];
10471050

1051+
// processors, storage and mempools are not stored in the `sensors` table,
1052+
// so they need to be looked up via their own models.
1053+
$health_models = [
1054+
'processor' => ['model' => Processor::class, 'id' => 'processor_id', 'descr' => 'processor_descr'],
1055+
'storage' => ['model' => Storage::class, 'id' => 'storage_id', 'descr' => 'storage_descr'],
1056+
'mempool' => ['model' => Mempool::class, 'id' => 'mempool_id', 'descr' => 'mempool_descr'],
1057+
];
1058+
10481059
if (isset($type)) {
1049-
if (isset($sensor_id)) {
1060+
if (isset($health_models[$type])) {
1061+
$health = $health_models[$type];
1062+
if (isset($sensor_id)) {
1063+
$graphs = $health['model']::where($health['id'], $sensor_id)->get()->toArray();
1064+
} else {
1065+
foreach ($health['model']::where('device_id', $device_id)->get() as $graph) {
1066+
$graphs[] = [
1067+
'sensor_id' => $graph->{$health['id']},
1068+
'desc' => $graph->{$health['descr']},
1069+
];
1070+
}
1071+
}
1072+
} elseif (isset($sensor_id)) {
10501073
$graphs = dbFetchRows('SELECT * FROM `sensors` WHERE `sensor_id` = ?', [$sensor_id]);
10511074
} else {
10521075
foreach (dbFetchRows('SELECT `sensor_id`, `sensor_descr` FROM `sensors` WHERE `device_id` = ? AND `sensor_class` = ? AND `sensor_deleted` = 0', [$device_id, $type]) as $graph) {

0 commit comments

Comments
 (0)