From c7e0a144896aeed41f395a90e6c1698e44b5843a Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Steve=20Calv=C3=A1rio?= <12759754+Calvario@users.noreply.github.com> Date: Tue, 1 Oct 2024 16:51:40 +0100 Subject: [PATCH] Avoid DivisionByZeroError (#16464) * Avoid DivisionByZeroError * Use match for entPhySensorPrecision * Fix style * Add zepto * Update match condition --- .../discovery/sensors/entity-sensor.inc.php | 52 ++++++------------- 1 file changed, 17 insertions(+), 35 deletions(-) diff --git a/includes/discovery/sensors/entity-sensor.inc.php b/includes/discovery/sensors/entity-sensor.inc.php index fe0f7b2dfd..7926e58ccc 100644 --- a/includes/discovery/sensors/entity-sensor.inc.php +++ b/includes/discovery/sensors/entity-sensor.inc.php @@ -112,41 +112,23 @@ if (! empty($entity_oids)) { } $valid_sensor = check_entity_sensor($descr, $device); $type = $entitysensor[$entry['entPhySensorType']]; - // FIXME this stuff is foul - if ($entry['entPhySensorScale'] == 'nano') { - $divisor = '1000000000'; - $multiplier = '1'; - } - if ($entry['entPhySensorScale'] == 'micro') { - $divisor = '1000000'; - $multiplier = '1'; - } - if ($entry['entPhySensorScale'] == 'milli') { - $divisor = '1000'; - $multiplier = '1'; - } - if ($entry['entPhySensorScale'] == 'units') { - $divisor = '1'; - $multiplier = '1'; - } - if ($entry['entPhySensorScale'] == 'kilo') { - $divisor = '1'; - $multiplier = '1000'; - } - if ($entry['entPhySensorScale'] == 'mega') { - $divisor = '1'; - $multiplier = '1000000'; - } - if ($entry['entPhySensorScale'] == 'giga') { - $divisor = '1'; - $multiplier = '1000000000'; - } - if ($entry['entPhySensorScale'] == 'yocto') { - $divisor = '1'; - $multiplier = '1'; - } - if (is_numeric($entry['entPhySensorPrecision']) && $entry['entPhySensorPrecision'] > '0') { - $divisor = $divisor . str_pad('', $entry['entPhySensorPrecision'], '0'); + + // Try to handle the scale + [$divisor, $multiplier] = match ($entry['entPhySensorScale']) { + 'zepto' => [1000000000000000000, 1], + 'nano' => [1000000000, 1], + 'micro' => [1000000, 1], + 'milli' => [1000, 1], + 'units' => [1, 1], + 'kilo' => [1, 1000], + 'mega' => [1, 1000000], + 'giga' => [1, 1000000000], + 'yocto' => [1, 1], + default => [1, 1], + }; + + if (is_numeric($entry['entPhySensorPrecision']) && $entry['entPhySensorPrecision'] > 0) { + $divisor .= str_pad('', $entry['entPhySensorPrecision'], '0'); } $current = ($current * $multiplier / $divisor);