widgets/api/plugins/system.inc some php warnings, for https://github.com/opnsense/core/issues/3400

This commit is contained in:
Ad Schellevis 2019-04-09 11:42:48 +02:00
parent 8bc683d295
commit d082d266c9

View File

@ -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");