diff --git a/src/www/widgets/api/plugins/system.inc b/src/www/widgets/api/plugins/system.inc index c3f4cd0b3..98281861a 100644 --- a/src/www/widgets/api/plugins/system.inc +++ b/src/www/widgets/api/plugins/system.inc @@ -31,21 +31,31 @@ function system_api_cpu_stats() $cpustats = array(); // take a short snapshot to calculate cpu usage $diff = array('user', 'nice', 'sys', 'intr', 'idle'); - $cpuTicks1 = array_combine($diff, explode(" ", get_single_sysctl('kern.cp_time'))); + $cpuTicks1 = array_combine($diff, array_slice(explode(" ", get_single_sysctl('kern.cp_time')),0, 5)); usleep(100000); - $cpuTicks2 = array_combine($diff, explode(" ", get_single_sysctl('kern.cp_time'))); + $cpuTicks2 = array_combine($diff, array_slice(explode(" ", get_single_sysctl('kern.cp_time')),0, 5)); $totalStart = array_sum($cpuTicks1); $totalEnd = array_sum($cpuTicks2); if ($totalEnd <= $totalStart) { // if for some reason the measurement is invalid, assume nothing has changed (all 0) $totalEnd = $totalStart; } - $cpustats['used'] = floor(100 * (($totalEnd - $totalStart) - ($cpuTicks2['idle'] - $cpuTicks1['idle'])) / ($totalEnd - $totalStart)); - $cpustats['user'] = floor(100 * (($cpuTicks2['user'] - $cpuTicks1['user'])) / ($totalEnd - $totalStart)); - $cpustats['nice'] = floor(100 * (($cpuTicks2['nice'] - $cpuTicks1['nice'])) / ($totalEnd - $totalStart)); - $cpustats['sys'] = floor(100 * (($cpuTicks2['sys'] - $cpuTicks1['sys'])) / ($totalEnd - $totalStart)); - $cpustats['intr'] = floor(100 * (($cpuTicks2['intr'] - $cpuTicks1['intr'])) / ($totalEnd - $totalStart)); - $cpustats['idle'] = floor(100 * (($cpuTicks2['idle'] - $cpuTicks1['idle'])) / ($totalEnd - $totalStart)); + $totalDiff = $totalEnd - $totalStart; + if ($totalDiff != 0) { + $cpustats['used'] = floor(100 * ($totalDiff - ($cpuTicks2['idle'] - $cpuTicks1['idle'])) / $totalDiff); + $cpustats['user'] = floor(100 * (($cpuTicks2['user'] - $cpuTicks1['user'])) / $totalDiff); + $cpustats['nice'] = floor(100 * (($cpuTicks2['nice'] - $cpuTicks1['nice'])) / $totalDiff); + $cpustats['sys'] = floor(100 * (($cpuTicks2['sys'] - $cpuTicks1['sys'])) / $totalDiff); + $cpustats['intr'] = floor(100 * (($cpuTicks2['intr'] - $cpuTicks1['intr'])) / $totalDiff); + $cpustats['idle'] = floor(100 * (($cpuTicks2['idle'] - $cpuTicks1['idle'])) / $totalDiff); + } else { + $cpustats['used'] = "0"; + $cpustats['user'] = "0"; + $cpustats['nice'] = "0"; + $cpustats['sys'] = "0"; + $cpustats['intr'] = "0"; + $cpustats['idle'] = "0"; + } // cpu model and count $cpustats['model'] = get_single_sysctl("hw.model");