This article mainly introduces PHP in the Windows environment to get CPU memory real-time usage of relevant data, very good, with reference and learning PHP value, interested in PHP friends can refer to this article
Background real-time monitoring of the server's Cup and memory usage of the scene is very common, although not done, but before the handwriting code I did not expect to spend 2 hours to finally realize.
Although the Internet search PHP CUP memory usage of this kind of keywords come out of a lot of articles, but mostly in the Linux environment, under Windows only put forward by the CMD statement implementation of the point of view, but few practical directly feasible code, and tried again wmic, systeminfo A variety of not familiar with the cmd command, and did not find a viable solution, and finally through the Baidu know an answer to find the implementation using VBS, slightly improved, can be used directly in the Windows environment.
Class Code
class SystemInfoWindows
{
/ **
* Judge whether the specified file exists in the specified path, if not, create it
* @param string $ fileName file name
* @param string $ content file content
* @return string return file path
* /
private function getFilePath ($ fileName, $ content)
{
$ path = dirname (FILE). "\\ $ fileName";
if (! file_exists ($ path)) {
file_put_contents ($ path, $ content);
}
return $ path;
}
/ **
* Get CPU usage vbs file generation function
* @return string return vbs file path
* /
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) "
);
}
/ **
* Obtain total memory and available physical memory JSON vbs file generation function
* @return string return vbs file path
* /
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 "
);
}
/ **
* Get CPU usage
* @return Number
* /
public function getCpuUsage ()
{
$ path = $ this-> getCupUsageVbsPath ();
exec ("cscript -nologo $ path", $ usage);
return $ usage [0];
}
/ **
* Get memory usage array
* @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;
}
}
Invocation mode
$ info = new SystemInfoWindows ();
$ cpu = $ info-> getCpuUsage ();
$ memory = $ info-> getMemoryUsage ();
echo "Current system CPU usage: {$ cpu}%, memory usage {$ memory ['usage']}%";
Summarize
The above is a small part of the introduction of PHP in the Windows environment to get CPU memory real-time usage, I hope to help you!!
Recommended for PHP:
Php+redis for snapping features
Before we share with you the PHP and Redis implementation of the mall seconds Kill function code sharing, this article is mainly for everyone to introduce ...
How PHP handles high concurrent requests for snapping-like features
This article mainly and everybody introduced in detail the PHP processing snapping up class function high concurrent request, has certain reference value ...
Workaround to bypass vulnerability in PHP with offset feature
This article mainly introduces the information about the bypass vulnerability caused by the character offset feature in PHP, not only ...
PHP code to achieve a shopping cart storage cycle of 1 days
Shopping cart cookies are stored for 1 days. Note: Browsers must support cookies to be able to use them. This paper......