<?php
declare(strict_types = 1);
namespace App\Library;
class SystemInfo
{
private $disk = [
'total' => 0,
'free' => 0,
'used' => 0,
'usage_ratio' => 0,
'unit_symbol' => 'G',
];
public function getDisk()
{
if ( is_windows() ) { $this->disk['total'] = round(disk_total_space("C:") / 1024 / 1024 / 1024, 2);
$this->disk['free'] = round(disk_free_space("C:") / 1024 / 1024 / 1024, 2);
$this->disk['used'] = round($this->disk['total'] - $this->disk['free'], 2);
$this->disk['usage_ratio'] = round(round($this->disk['free'] / $this->disk['total'], 4) * 100, 2);
} else {
$fp = popen('df -lh | grep -E "^(/)"', "r");
$rs = fread($fp, 1024);
pclose($fp);
$rs = preg_replace('/\s{2,}/', ' ', $rs); $hd = explode(" ", $rs);
$this->disk['total'] = trim($hd[1], 'G') $this->disk['used'] = trim($hd[2], 'G') $this->disk['free'] = trim($hd[3], 'G') $this->disk['usage_ratio'] = trim($hd[4], '%') }
return $this->disk;
}
private $memory = [
'total' => 0,
'free' => 0,
'used' => 0,
'usage_ratio' => 0,
'unit_symbol' => 'M',
];
public function getMemory()
{
if ( is_windows() ) { $path = $this->getMemoryUsageVbsPathByWindows();
exec("cscript -nologo $path", $usage);
$memory = my_json_decode($usage[0], true);
$this->memory['total'] = round($memory['TotalVisibleMemorySize'] / 1024, 2);
$this->memory['free'] = round($memory['FreePhysicalMemory'] / 1024, 2);
$this->memory['used'] = round($this->memory['total'] - $this->memory['free'], 2);
$this->memory['usage_ratio'] = round(round($this->memory['used'] / $this->memory['total'], 4) * 100, 2);
} else {
$fp = popen('top -b -n 2 | grep -E "(Mem)"', "r");
$rs = fread($fp, 1024);
$sys_info = explode("\n", $rs);
$mem_info = explode(",", $sys_info[2]);
$this->memory['total'] = round(trim(trim($mem_info[0], 'KiB Mem : '), ' total'), 0);
$this->memory['used'] = round(trim(trim($mem_info[2], 'used')), 0);
$this->memory['buffer_cache'] = trim(trim($mem_info[3], 'buff/cache'));
$this->memory['free'] = round(trim(trim($mem_info[1], 'free')), 0);
$this->memory['usage_ratio'] = round($this->memory['used'] / $this->memory['total'], 4) * 100; }
return $this->memory;
}
private $cpu = [
'total' => 0,
'free' => 0,
'used' => 0,
'usage_ratio' => 0,
];
public function getCpu()
{
if ( is_windows() ) { $this->cpu['usage_ratio'] = (float)$this->getCpuUsage();
} else {
$fp = popen('top -b -n 2 | grep -E "(Cpu)"', "r");
$rs = fread($fp, 1024);
$sys_info = explode("\n", $rs);
$this->cpu['usage_ratio'] = (float)trim(current(explode(',', trim($sys_info[0], '%Cpu(s): '))), 'us'); }
return $this->cpu;
}
private function getMemoryUsageVbsPathByWindows()
{
return $this->getFilePath('memory_usage.vbs', "On Error Resume Next
Set objWMI = GetObject(\"winmgmts:\\\\.\\root\cimv2\")
Set colOS = objWMI.InstancesOf(\"Win32_OperatingSystem\")
For Each objOS in colOS
Wscript.Echo(\"{\"\"TotalVisibleMemorySize\"\":\" & objOS.TotalVisibleMemorySize & \",\"\"FreePhysicalMemory\"\":\" & objOS.FreePhysicalMemory & \"}\")
Next");
}
private function getFilePath($fileName, $content)
{
$path = dirname(__FILE__) . "\\$fileName";
if ( !file_exists($path) ) {
file_put_contents($path, $content);
}
return $path;
}
private function getCpuUsage()
{
$path = $this->getCupUsageVbsPathByWindows();
exec("cscript -nologo $path", $usage);
return $usage[0];
}
private function getCupUsageVbsPathByWindows()
{
return $this->getFilePath('cpu_usage.vbs', "On Error Resume Next
Set objProc = GetObject(\"winmgmts:\\\\.\\root\cimv2:win32_processor='cpu0'\")
WScript.Echo(objProc.LoadPercentage)");
}
}