Skip to content

Commit

Permalink
Merge pull request #66 from netyum/master
Browse files Browse the repository at this point in the history
修复获取值
  • Loading branch information
kanyxmo committed Jun 15, 2023
2 parents 895958f + 6330ff8 commit f2a7dff
Showing 1 changed file with 17 additions and 4 deletions.
21 changes: 17 additions & 4 deletions app/System/Service/ServerMonitorService.php
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,7 @@ public function getCpuInfo(): array
];
} catch (\Throwable $e) {
$res = '无法获取';
echo $e->getMessage(), "\n";
return [
'name' => $res, 'cores' => $res, 'cache' => $res, 'usage' => $res, 'free' => $res,
];
Expand Down Expand Up @@ -91,7 +92,7 @@ public function getCpuName(): string
}
return $matches[1] ?? "未知";
} else if ($this->isCygwin()) {
$name = shell_exec('wmic cpu get Name | findstr /V "Name"');
$name = shell_exec('wmic cpu get Name | findstr /V "Name" | head -n1');
return trim($name);
}else {
return shell_exec('sysctl -n machdep.cpu.brand_string');
Expand All @@ -107,8 +108,14 @@ public function getCpuPhysicsCores(): string
$num = str_replace("\n", '', shell_exec('cat /proc/cpuinfo |grep "physical id"|sort |uniq|wc -l'));
return intval($num) == 0 ? '1' : $num;
} else if ($this->isCygwin()) {
$num = shell_exec('wmic cpu get NumberOfCores | findstr /V "NumberOfCores"');
return trim($num ?? '1');
$num = shell_exec('wmic cpu get NumberOfCores | findstr /V "NumberOfCores"');
$num = trim($num ?? '1');
$nums = explode("\n", $num);
$num = 0;
foreach($nums as $n) {
$num += intval(trim($n));
}
return strval($num);
} else {
return trim(shell_exec('sysctl -n hw.physicalcpu'));
}
Expand All @@ -123,7 +130,13 @@ public function getCpuLogicCores(): string
return str_replace("\n", '', shell_exec('cat /proc/cpuinfo |grep "processor"|wc -l'));
} else if ($this->isCygwin()) {
$num = shell_exec('wmic cpu get NumberOfLogicalProcessors | findstr /V "NumberOfLogicalProcessors"');
return trim($num ?? '1');
$num = trim($num ?? '1');
$nums = explode("\n", $num);
$num = 0;
foreach($nums as $n) {
$num += intval(trim($n));
}
return strval($num);
} else {
return trim(shell_exec('sysctl -n hw.logicalcpu'));
}
Expand Down

0 comments on commit f2a7dff

Please sign in to comment.