Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP

Loading…

Avoid dashboard divide by zero errors #865

Merged
merged 1 commit into from

2 participants

@phildd

Today I was having trouble with a 256MB Alix 2.1-RELEASE system that had too many OpenVPN servers defined, and some trouble with RRD data and bandwidthd... It was running out of memory badly during boot (another story).
Anyway, one of the side-effects was getting this on the dashboard System Information widget, where %ages should be displayed:
Warning: Division by zero in /usr/local/www/includes/functions.inc.php on line 155
The console had showed some errors about "sysctl" - and it seems the system didn't have enough spare memory to fork the various underlying "sysctl" etc commands that gather the amount of system memory etc. So these were zero, resulting in divide by zero errors when trying to calculate percentages.
Some other people have reported this in the forum in the past, but I guess it only happens when there is other bigger trouble on a system! I thought it was worth handling the "zero divisor" cases, because that saves someone from chasing this little code issue when they really need to be looking elsewhere to fix their system.
I have made the version for this pull request against master, but the code easily applies to 2.1 branch also.

@cbuechler cbuechler merged commit 0b5d55b into pfsense:master
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Commits on Dec 22, 2013
  1. @phildd
This page is out of date. Refresh to see the latest.
Showing with 12 additions and 3 deletions.
  1. +12 −3 usr/local/www/includes/functions.inc.php
View
15 usr/local/www/includes/functions.inc.php
@@ -146,7 +146,10 @@ function get_pfstate($percent=false) {
$curentries = $matches[1];
}
if ($percent)
- return round(($curentries / $maxstates) * 100, 0);
+ if ($maxstates > 0)
+ return round(($curentries / $maxstates) * 100, 0);
+ else
+ return "NA";
else
return $curentries . "/" . $maxstates;
}
@@ -166,7 +169,10 @@ function get_mbuf($percent=false) {
$mbufs_output=trim(`/usr/bin/netstat -mb | /usr/bin/grep "mbuf clusters in use" | /usr/bin/awk '{ print $1 }'`);
list( $mbufs_current, $mbufs_cache, $mbufs_total, $mbufs_max ) = explode( "/", $mbufs_output);
if ($percent)
- return round(($mbufs_total / $mbufs_max) * 100, 0);
+ if ($mbufs_max > 0)
+ return round(($mbufs_total / $mbufs_max) * 100, 0);
+ else
+ return "NA";
else
return "{$mbufs_total}/{$mbufs_max}";
}
@@ -211,7 +217,10 @@ function mem_usage() {
$totalMem = $memory[0];
$availMem = $memory[1] + $memory[2] + $memory[3];
$usedMem = $totalMem - $availMem;
- $memUsage = round(($usedMem * 100) / $totalMem, 0);
+ if ($totalMem > 0)
+ $memUsage = round(($usedMem * 100) / $totalMem, 0);
+ else
+ $memUsage = "NA";
return $memUsage;
}
Something went wrong with that request. Please try again.