<?php /** * Created by PhpStorm. * User: 25754 * Date: 2019/5/4 * Time: 13:42 */ class SystemInfoWindows { /** * 判斷指定路徑下指定文件是否存在,如不存在則建立 * @param string $fileName 文件名 * @param string $content 文件內容 * @return string 返回文件路徑 */ private function getFilePath($fileName, $content) { $path = dirname(__FILE__) . "\\$fileName"; if (!file_exists($path)) { file_put_contents($path, $content); } return $path; } /** * 得到cpu使用率vbs文件生成函數 * @return string 返回vbs文件路徑 */ private function getCupUsageVbsPath() { return $this->getFilePath( 'cpu_usage.vbs', "On Error Resume Next Set objProc = GetObject(\"winmgmts:\\\\.\\root\cimv2:win32_processor='cpu0'\") WScript.Echo(objProc.LoadPercentage)" ); } /** * 得到總內存及可用物理內存JSON vbs文件生成函數 * @return string 返回vbs文件路徑 */ private function getMemoryUsageVbsPath() { 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" ); } /** * 得到CPU使用率 * @return Number */ public function getCpuUsage() { $path = $this->getCupUsageVbsPath(); exec("cscript -nologo $path", $usage); return $usage[0]; } /** * 得到內存使用率數組 * @return array */ public function getMemoryUsage() { $path = $this->getMemoryUsageVbsPath(); exec("cscript -nologo $path", $usage); $memory = json_decode($usage[0], true); $memory['usage'] = Round((($memory['TotalVisibleMemorySize'] - $memory['FreePhysicalMemory']) / $memory['TotalVisibleMemorySize']) * 100); return $memory; } }