Skip to content

Commit 1fb8bb8

Browse files
committed
fix: CPU percentages
1 parent 8931e9e commit 1fb8bb8

File tree

1 file changed

+6
-2
lines changed

1 file changed

+6
-2
lines changed

app/Http/Controllers/API/Server/DetailsController.php

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -108,6 +108,8 @@ public function stats()
108108
}
109109

110110
if (server()->isLinux()) {
111+
$cores = str_replace("cpu cores\t: ", '', trim(explode("\n", Command::runSudo("cat /proc/cpuinfo | grep 'cpu cores'"))[0]));
112+
111113
$cpuPercent = Command::runSudo(
112114
"ps -eo %cpu --no-headers | grep -v 0.0 | awk '{s+=$1} END {print s/NR*10}'"
113115
);
@@ -124,7 +126,7 @@ public function stats()
124126
$secondUp = $this->calculateNetworkBytes(false);
125127

126128
return [
127-
'cpu' => round((float) $cpuPercent, 2),
129+
'cpu' => round((float) $cpuPercent / $cores, 2),
128130
'ram' => round((float) $ramPercent, 2),
129131
'io' => round((float) $ioPercent, 2),
130132
'network' => [
@@ -295,14 +297,16 @@ private function calculateNetworkBytes($download = true)
295297
*/
296298
private function parsePsOutput($output)
297299
{
300+
$cores = str_replace("cpu cores\t: ", '', trim(explode("\n", Command::runSudo("cat /proc/cpuinfo | grep 'cpu cores'"))[0]));
301+
298302
$data = [];
299303
foreach (explode("\n", (string) $output) as $row) {
300304
$row = explode('*-*', $row);
301305
$row[3] = str_replace('\\', '/', $row[3]);
302306
$fetch = explode('/', $row[3]);
303307
$data[] = [
304308
'pid' => $row[0],
305-
'percent' => $row[1],
309+
'percent' => round($row[1] / $cores, 1),
306310
'user' => $row[2],
307311
'cmd' => end($fetch),
308312
];

0 commit comments

Comments
 (0)