Skip to content

Commit

Permalink
Fix more divide by zero issues (#14954)
Browse files Browse the repository at this point in the history
* Fix more divide by zero issues
fixes: 14932

* Round to the nearest integer, then cast

* Fix up dhcpatriot
  • Loading branch information
murrant committed Apr 11, 2023
1 parent cde4d6a commit e9c08e2
Show file tree
Hide file tree
Showing 8 changed files with 37 additions and 34 deletions.
3 changes: 1 addition & 2 deletions LibreNMS/Modules/PrinterSupplies.php
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,6 @@
use LibreNMS\OS;
use LibreNMS\RRD\RrdDefinition;
use LibreNMS\Util\Number;
use Log;

class PrinterSupplies implements Module
{
Expand Down Expand Up @@ -235,7 +234,7 @@ private function discoveryPapers($device): Collection
// at least one piece of paper in tray
$current = 50;
} else {
$current = $current / $capacity * 100;
$current = Number::calculatePercent($current, $capacity);
}

$papers->push(new PrinterSupply([
Expand Down
3 changes: 2 additions & 1 deletion app/Http/Controllers/Widgets/GlobeController.php
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@
use Illuminate\Support\Collection;
use Illuminate\View\View;
use LibreNMS\Config;
use LibreNMS\Util\Number;

class GlobeController extends WidgetController
{
Expand Down Expand Up @@ -104,7 +105,7 @@ public function getView(Request $request)
$location->lat,
$location->lng,
$location->location,
(1 - $up / $count) * 100, // percent down
Number::calculatePercent($count - $up, $count), // percent down
$count,
$down_items->implode(',<br/> '),
]);
Expand Down
3 changes: 2 additions & 1 deletion app/Models/Mempool.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

use Illuminate\Support\Str;
use LibreNMS\Interfaces\Models\Keyable;
use LibreNMS\Util\Number;

class Mempool extends DeviceRelatedModel implements Keyable
{
Expand Down Expand Up @@ -86,7 +87,7 @@ public function fillUsage($used = null, $total = null, $free = null, $percent =
}

if ($percent == null) {
$this->mempool_perc = $this->mempool_used / $this->mempool_total * 100;
$this->mempool_perc = (int) Number::calculatePercent($this->mempool_used, $this->mempool_total, 0);
}

return $this;
Expand Down
2 changes: 1 addition & 1 deletion includes/discovery/sensors/load/dhcpatriot.inc.php
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,7 @@
$type = $value['type'];
$divisor = $pool_data[$value['size_oid']];
$descr = $value['description'] . ' (' . $pool_data[$value['oid']] . '/' . $divisor . ')';
$current = (($pool_data[$value['oid']] / $divisor) * 100);
$current = \LibreNMS\Util\Number::calculatePercent($pool_data[$value['oid']], $divisor);
$group = $value['group'];

discover_sensor(
Expand Down
2 changes: 1 addition & 1 deletion includes/html/pages/device/routing/mpls.inc.php
Original file line number Diff line number Diff line change
Expand Up @@ -402,7 +402,7 @@
$operstate_status_color = 'danger';
}

$fdb_usage_perc = $svc['svcTlsFdbNumEntries'] / $svc['svcTlsFdbTableSize'] * 100;
$fdb_usage_perc = Number::calculatePercent($svc['svcTlsFdbNumEntries'], $svc['svcTlsFdbTableSize']);
if ($fdb_usage_perc > 95) {
$fdb_status_color = 'danger';
} elseif ($fdb_usage_perc > 75) {
Expand Down
5 changes: 3 additions & 2 deletions includes/html/print-map.inc.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
*/

use LibreNMS\Config;
use LibreNMS\Util\Number;

$highlight_node = $vars['highlight_node'] ?? 0;
$group = $vars['group'] ?? 0;
Expand Down Expand Up @@ -276,8 +277,8 @@
} else {
$width = round(0.77 * pow($speed, 0.25));
}
$link_in_used = $items['local_ifspeed'] ? (($items['local_ifinoctets_rate'] * 8) / $items['local_ifspeed'] * 100) : 0;
$link_out_used = $items['local_ifspeed'] ? (($items['local_ifoutoctets_rate'] * 8) / $items['local_ifspeed'] * 100) : 0;
$link_in_used = Number::calculatePercent($items['local_ifinoctets_rate'], $items['local_ifspeed']);
$link_out_used = Number::calculatePercent($items['local_ifoutoctets_rate'], $items['local_ifspeed']);
if ($link_in_used > $link_out_used) {
$link_used = $link_in_used;
} else {
Expand Down
5 changes: 3 additions & 2 deletions scripts/collect-port-polling.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
use Illuminate\Support\Str;
use LibreNMS\Config;
use LibreNMS\Util\Debug;
use LibreNMS\Util\Number;

$install_dir = realpath(__DIR__ . '/..');
chdir($install_dir);
Expand Down Expand Up @@ -101,7 +102,7 @@ function print_help()
$device['port_count'] = $count;
$device['inactive_ratio'] = ($inactive == 0 ? 0 : ($inactive / $count));
$device['diff_sec'] = $device['selective_time_sec'] - $device['full_time_sec'];
$device['diff_perc'] = ($device['diff_sec'] / $device['full_time_sec']) * 100;
$device['diff_perc'] = Number::calculatePercent($device['diff_sec'], $device['full_time_sec']);

// $enable_sel_value is negative and we want to enable it for all devices with an even lower value.
// It also has to save more than 1 s, or we might enable it for devices with i.e. 100ms vs 50ms, which isn't needed.
Expand Down Expand Up @@ -160,7 +161,7 @@ function print_help()
$total_full_time = array_sum(array_column($devices, 'full_time_sec'));
$total_selective_time = array_sum(array_column($devices, 'selective_time_sec'));
$difference = $total_selective_time - $total_full_time;
$difference_perc = ($difference / $total_full_time) * 100;
$difference_perc = Number::calculatePercent($difference, $total_full_time);
$total_diff_color = ($difference > 0 ? "\033[0;31m" : "\033[0;32m");

printf(
Expand Down
48 changes: 24 additions & 24 deletions tests/data/dhcpatriot.json
Original file line number Diff line number Diff line change
Expand Up @@ -1137,7 +1137,7 @@
"group": "Authenticated DHCP",
"sensor_divisor": 7911,
"sensor_multiplier": 100,
"sensor_current": 88.914170142839,
"sensor_current": 88.91,
"sensor_limit": 100,
"sensor_limit_warn": 95,
"sensor_limit_low": null,
Expand All @@ -1162,7 +1162,7 @@
"group": "Authenticated DHCP",
"sensor_divisor": 1892,
"sensor_multiplier": 100,
"sensor_current": 34.989429175476,
"sensor_current": 34.99,
"sensor_limit": 100,
"sensor_limit_warn": 95,
"sensor_limit_low": null,
Expand All @@ -1187,7 +1187,7 @@
"group": "Authenticated DHCP",
"sensor_divisor": 123,
"sensor_multiplier": 100,
"sensor_current": 62.60162601626,
"sensor_current": 62.6,
"sensor_limit": 100,
"sensor_limit_warn": 95,
"sensor_limit_low": null,
Expand All @@ -1212,7 +1212,7 @@
"group": "Authenticated DHCP",
"sensor_divisor": 123,
"sensor_multiplier": 100,
"sensor_current": 9.7560975609756,
"sensor_current": 9.76,
"sensor_limit": 100,
"sensor_limit_warn": 95,
"sensor_limit_low": null,
Expand All @@ -1237,7 +1237,7 @@
"group": "Authenticated DHCP",
"sensor_divisor": 123,
"sensor_multiplier": 100,
"sensor_current": 4.8780487804878,
"sensor_current": 4.88,
"sensor_limit": 100,
"sensor_limit_warn": 95,
"sensor_limit_low": null,
Expand All @@ -1262,7 +1262,7 @@
"group": "Authenticated DHCP",
"sensor_divisor": 123,
"sensor_multiplier": 100,
"sensor_current": 7.3170731707317,
"sensor_current": 7.32,
"sensor_limit": 100,
"sensor_limit_warn": 95,
"sensor_limit_low": null,
Expand All @@ -1287,7 +1287,7 @@
"group": "Authenticated DHCP",
"sensor_divisor": 123,
"sensor_multiplier": 100,
"sensor_current": 8.130081300813,
"sensor_current": 8.13,
"sensor_limit": 100,
"sensor_limit_warn": 95,
"sensor_limit_low": null,
Expand All @@ -1312,7 +1312,7 @@
"group": "Authenticated DHCP",
"sensor_divisor": 251,
"sensor_multiplier": 100,
"sensor_current": 4.7808764940239,
"sensor_current": 4.78,
"sensor_limit": 100,
"sensor_limit_warn": 95,
"sensor_limit_low": null,
Expand All @@ -1337,7 +1337,7 @@
"group": "Authenticated DHCP",
"sensor_divisor": 123,
"sensor_multiplier": 100,
"sensor_current": 8.130081300813,
"sensor_current": 8.13,
"sensor_limit": 100,
"sensor_limit_warn": 95,
"sensor_limit_low": null,
Expand All @@ -1362,7 +1362,7 @@
"group": "Authenticated DHCP",
"sensor_divisor": 123,
"sensor_multiplier": 100,
"sensor_current": 3.2520325203252,
"sensor_current": 3.25,
"sensor_limit": 100,
"sensor_limit_warn": 95,
"sensor_limit_low": null,
Expand Down Expand Up @@ -1412,7 +1412,7 @@
"group": "Authenticated DHCP",
"sensor_divisor": 123,
"sensor_multiplier": 100,
"sensor_current": 0.8130081300813,
"sensor_current": 0.81,
"sensor_limit": 100,
"sensor_limit_warn": 95,
"sensor_limit_low": null,
Expand Down Expand Up @@ -1487,7 +1487,7 @@
"group": "Authenticated DHCP",
"sensor_divisor": 502,
"sensor_multiplier": 100,
"sensor_current": 92.03187250996,
"sensor_current": 92.03,
"sensor_limit": 100,
"sensor_limit_warn": 95,
"sensor_limit_low": null,
Expand Down Expand Up @@ -1948,7 +1948,7 @@
"sensor_custom": "No",
"entPhysicalIndex": null,
"entPhysicalIndex_measured": null,
"sensor_prev": 88.914170142839,
"sensor_prev": 88.91,
"user_func": null,
"rrd_type": "GAUGE",
"state_name": null
Expand All @@ -1973,7 +1973,7 @@
"sensor_custom": "No",
"entPhysicalIndex": null,
"entPhysicalIndex_measured": null,
"sensor_prev": 34.989429175476,
"sensor_prev": 34.99,
"user_func": null,
"rrd_type": "GAUGE",
"state_name": null
Expand All @@ -1998,7 +1998,7 @@
"sensor_custom": "No",
"entPhysicalIndex": null,
"entPhysicalIndex_measured": null,
"sensor_prev": 62.60162601626,
"sensor_prev": 62.6,
"user_func": null,
"rrd_type": "GAUGE",
"state_name": null
Expand All @@ -2023,7 +2023,7 @@
"sensor_custom": "No",
"entPhysicalIndex": null,
"entPhysicalIndex_measured": null,
"sensor_prev": 9.7560975609756,
"sensor_prev": 9.76,
"user_func": null,
"rrd_type": "GAUGE",
"state_name": null
Expand All @@ -2048,7 +2048,7 @@
"sensor_custom": "No",
"entPhysicalIndex": null,
"entPhysicalIndex_measured": null,
"sensor_prev": 4.8780487804878,
"sensor_prev": 4.88,
"user_func": null,
"rrd_type": "GAUGE",
"state_name": null
Expand All @@ -2073,7 +2073,7 @@
"sensor_custom": "No",
"entPhysicalIndex": null,
"entPhysicalIndex_measured": null,
"sensor_prev": 7.3170731707317,
"sensor_prev": 7.32,
"user_func": null,
"rrd_type": "GAUGE",
"state_name": null
Expand All @@ -2098,7 +2098,7 @@
"sensor_custom": "No",
"entPhysicalIndex": null,
"entPhysicalIndex_measured": null,
"sensor_prev": 8.130081300813,
"sensor_prev": 8.13,
"user_func": null,
"rrd_type": "GAUGE",
"state_name": null
Expand All @@ -2123,7 +2123,7 @@
"sensor_custom": "No",
"entPhysicalIndex": null,
"entPhysicalIndex_measured": null,
"sensor_prev": 4.7808764940239,
"sensor_prev": 4.78,
"user_func": null,
"rrd_type": "GAUGE",
"state_name": null
Expand All @@ -2148,7 +2148,7 @@
"sensor_custom": "No",
"entPhysicalIndex": null,
"entPhysicalIndex_measured": null,
"sensor_prev": 8.130081300813,
"sensor_prev": 8.13,
"user_func": null,
"rrd_type": "GAUGE",
"state_name": null
Expand All @@ -2173,7 +2173,7 @@
"sensor_custom": "No",
"entPhysicalIndex": null,
"entPhysicalIndex_measured": null,
"sensor_prev": 3.2520325203252,
"sensor_prev": 3.25,
"user_func": null,
"rrd_type": "GAUGE",
"state_name": null
Expand Down Expand Up @@ -2223,7 +2223,7 @@
"sensor_custom": "No",
"entPhysicalIndex": null,
"entPhysicalIndex_measured": null,
"sensor_prev": 0.8130081300813,
"sensor_prev": 0.81,
"user_func": null,
"rrd_type": "GAUGE",
"state_name": null
Expand Down Expand Up @@ -2298,7 +2298,7 @@
"sensor_custom": "No",
"entPhysicalIndex": null,
"entPhysicalIndex_measured": null,
"sensor_prev": 92.03187250996,
"sensor_prev": 92.03,
"user_func": null,
"rrd_type": "GAUGE",
"state_name": null
Expand Down

0 comments on commit e9c08e2

Please sign in to comment.