From 73a730b0c02738046be417776566a8e640fa8261 Mon Sep 17 00:00:00 2001 From: Thomas Vincent Date: Sun, 8 Nov 2026 11:37:36 -0800 Subject: [PATCH] fix: reject non-finite UPS dashboard metrics Signed-off-by: Thomas Vincent --- CHANGELOG.md | 1 + includes/dashboard.php | 6 +++++- includes/functions.php | 6 ++++-- phpunit.xml | 1 + tests/Unit/DashboardRrdStatsTest.php | 8 ++++++++ tests/Unit/EstimatedWattsTest.php | 6 ++++++ 6 files changed, 25 insertions(+), 3 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index cb49058..0e5f17b 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -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 --- diff --git a/includes/dashboard.php b/includes/dashboard.php index 49c1f38..11146ea 100644 --- a/includes/dashboard.php +++ b/includes/dashboard.php @@ -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; + } } } diff --git a/includes/functions.php b/includes/functions.php index 6ffdafa..884ee2e 100644 --- a/includes/functions.php +++ b/includes/functions.php @@ -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; } } diff --git a/phpunit.xml b/phpunit.xml index 9a1ec41..11242c1 100644 --- a/phpunit.xml +++ b/phpunit.xml @@ -28,6 +28,7 @@ setup.php + includes/dashboard.php includes/functions.php includes/helpers.php includes/nut.php diff --git a/tests/Unit/DashboardRrdStatsTest.php b/tests/Unit/DashboardRrdStatsTest.php index b74ca58..1bcb545 100644 --- a/tests/Unit/DashboardRrdStatsTest.php +++ b/tests/Unit/DashboardRrdStatsTest.php @@ -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([]); diff --git a/tests/Unit/EstimatedWattsTest.php b/tests/Unit/EstimatedWattsTest.php index 1fb4fb2..48eb599 100644 --- a/tests/Unit/EstimatedWattsTest.php +++ b/tests/Unit/EstimatedWattsTest.php @@ -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 () {