Repository: librenms/librenms (language: php)

Issue:
DivisionByZeroError on sensors
### The problem

In a very small, DEV env, it happen with checkpoint devices. Not sure at this stage of only checkpoint are affected.
```
Error discovering sensors module for X.X.X.X. DivisionByZeroError: Division by zero in /opt/librenms/includes/discovery/sensors/entity-sensor.inc.php:152
Stack trace:
#0 /opt/librenms/includes/discovery/sensors.inc.php(14): require()
#1 /opt/librenms/includes/discovery/functions.inc.php(183): include('...')
#2 /opt/librenms/discovery.php(108): discover_device()
#3 {main}
```

### Output of ./validate.php

```txt
librenms@localhost:~$ ./validate.php
===========================================
Component | Version
--------- | -------
LibreNMS  | 24.9.0 (2024-09-30T10:12:12+01:00)
DB Schema | 2024_08_12_232009_ent_physical_table_rev_length (300)
PHP       | 8.3.11
Python    | 3.9.2
Database  | MariaDB 10.11.8-MariaDB-ubu2204
RRDTool   | 1.9.0
SNMP      | 5.9
===========================================

[OK]    Installed from package; no Composer required
[OK]    Database connection successful
[OK]    Database connection successful
[OK]    Database Schema is current
[OK]    SQL Server meets minimum requirements
[OK]    lower_case_table_names is enabled
[OK]    MySQL engine is optimal
[OK]    Database and column collations are correct
[OK]    Database schema correct
[OK]    MySQL and PHP time match
[OK]    Active pollers found
[OK]    Dispatcher Service is enabled
[OK]    Locks are functional
[OK]    Python wrapper cron entry is not present
[WARN]  Using database for locking, you should set CACHE_DRIVER=redis
[OK]    rrd_dir is writable
[OK]    rrdtool version ok
[WARN]  Non-git install, updates are manual or from package
```


### What was the last working version of LibreNMS?

Unknown

### Anything in the logs that might be useful for us?

All the $entry's that generated the 0.
```
{"entPhySensorType":"voltsDC","entPhySensorScale":"zepto","entPhySensorPrecision":"0","entPhySensorValue":"4979","entPhySensorOperStatus":"ok"}

{"entPhySensorType":"voltsDC","entPhySensorScale":"zepto","entPhySensorPrecision":"0","entPhySensorValue":"1794","entPhySensorOperStatus":"ok"}

{"entPhySensorType":"voltsDC","entPhySensorScale":"zepto","entPhySensorPrecision":"0","entPhySensorValue":"3295","entPhySensorOperStatus":"ok"}
```



Discussion / hints:

Fix patch:
diff --git a/includes/discovery/sensors/entity-sensor.inc.php b/includes/discovery/sensors/entity-sensor.inc.php
index fe0f7b2dfd8d..7926e58ccc52 100644
--- a/includes/discovery/sensors/entity-sensor.inc.php
+++ b/includes/discovery/sensors/entity-sensor.inc.php
@@ -112,41 +112,23 @@
             }
             $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);


Test patch: