改进 不上报 内存 使用情况

This commit is contained in:
iVampireSP.com 2023-01-05 01:51:34 +08:00
parent 50143f3ce4
commit 4f5acc3a04
No known key found for this signature in database
GPG Key ID: 2F7B001CA27A8132

View File

@ -90,12 +90,10 @@ private function report(): void
$this->info('正在报告此系统,请保持此命令一直运行。');
$cpu = $this->getCpuUsage();
$memory = $this->getMemoryUsage();
while (1) {
Cluster::publish('system_usage', [
'cpu' => $cpu,
'memory' => $memory,
]);
sleep(1);
@ -108,29 +106,4 @@ private function getCpuUsage(): float
$cpu = sys_getloadavg();
return $cpu[0];
}
private function getMemoryUsage(): float
{
// 检查 free 命令是否存在
if (exec('which free')) {
$free = exec('free');
} else {
// fake free
$free = <<<EOF
total used free shared buff/cache available
Mem: 1982 334 1121 126 527 1380
Swap: 0 0 0
EOF;
}
$free = trim($free);
$free_arr = explode("\n", $free);
$mem = explode(" ", $free_arr[1]);
$mem = array_filter($mem);
$mem = array_merge($mem);
return round($mem[2] / $mem[1] * 100, 2);
}
}