Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
* feature: Add a per-UPS Timeout (seconds) setting, 5 by default and limited to 1-120
* feature: Add support for monitoring UPSes through Network UPS Tools (NUT)
* feature: Support NUT TLS using self-signed certificates
* issue#66: Ignore non-finite RRD samples and power estimates in dashboard aggregates

--- 1.3.1 ---

Expand Down
6 changes: 5 additions & 1 deletion includes/dashboard.php
Original file line number Diff line number Diff line change
Expand Up @@ -475,7 +475,11 @@ function apcupsd_dashboard_rrd_series($local_data_id, string $ds_name, $start, $

foreach ($fetch['values'][$index] as $value) {
if (is_numeric($value)) {
$numbers[] = (float) $value;
$number = (float) $value;

if (is_finite($number)) {
$numbers[] = $number;
}
}
}

Expand Down
6 changes: 4 additions & 2 deletions includes/functions.php
Original file line number Diff line number Diff line change
Expand Up @@ -64,11 +64,13 @@ function apcupsd_estimate_watts($nominal_power, $load_percent): ?float {
$nominal = (float) $nominal_power;
$load = (float) $load_percent;

if ($nominal <= 0 || $load < 0) {
if (!is_finite($nominal) || !is_finite($load) || $nominal <= 0 || $load < 0) {
return null;
}

return $nominal * $load / 100;
$watts = $nominal * $load / 100;

return is_finite($watts) ? $watts : null;
}
}

Expand Down
1 change: 1 addition & 0 deletions phpunit.xml
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@
<source>
<include>
<file>setup.php</file>
<file>includes/dashboard.php</file>
<file>includes/functions.php</file>
<file>includes/helpers.php</file>
<file>includes/nut.php</file>
Expand Down
8 changes: 8 additions & 0 deletions tests/Unit/DashboardRrdStatsTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -161,6 +161,14 @@ function apcupsd_test_fleet(array $upses): void {
expect(apcupsd_dashboard_rrd_series(11, 'loadpct', -3600, 0, 'AVERAGE'))->toBe([10.0, 30.0]);
});

it('excludes non-finite numeric values from RRD series', function () {
apcupsd_test_rrd_setup(111, 'loadpct', ['AVERAGE' => [10, 20, 30, 40]]);
$GLOBALS['__rrd_data'][111]['AVERAGE']['values'][0][1700000300] = INF;
$GLOBALS['__rrd_data'][111]['AVERAGE']['values'][0][1700000600] = NAN;

expect(apcupsd_dashboard_rrd_series(111, 'loadpct', -3600, 0, 'AVERAGE'))->toBe([10.0, 40.0]);
});

it('returns nothing for a missing RRD, an unknown column or an empty fetch', function () {
expect(apcupsd_dashboard_rrd_series(0, 'loadpct', -3600, 0, 'AVERAGE'))->toBe([]);
expect(apcupsd_dashboard_rrd_series(12, 'loadpct', -3600, 0, 'AVERAGE'))->toBe([]);
Expand Down
6 changes: 6 additions & 0 deletions tests/Unit/EstimatedWattsTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,12 @@
expect(apcupsd_estimate_watts(-1500, 40))->toBeNull();
expect(apcupsd_estimate_watts(1500, -5))->toBeNull();
});

it('rejects non-finite inputs and estimates that overflow', function () {
expect(apcupsd_estimate_watts(INF, 40))->toBeNull();
expect(apcupsd_estimate_watts(1500, NAN))->toBeNull();
expect(apcupsd_estimate_watts(1.0e308, 1.0e308))->toBeNull();
});
});

describe('apcupsd_is_slave_status', function () {
Expand Down
Loading