Php obtains real-time cpu memory usage in windows (recommended) and phpcpu

Source: Internet
Author: User

Php obtains real-time cpu memory usage in windows (recommended) and phpcpu

The CPU and memory usage of the backend Real-time Monitoring Server is very common. Although I have never done so, I did not expect it to take more than two hours to implement it before writing the code.

Although the keyword "php cup memory usage" is searched on the Internet, most of them are in the Linux environment. In windows, only the cmd statement is used for implementation, however, there are few actual and feasible code. I tried wmic and systeminfo a variety of unfamiliar cmd commands and did not find a feasible solution, finally, I found the implementation method obtained by using vbs through an answer that Baidu knows. It can be used directly in a windows environment with a slight improvement.

Class Code

Class SystemInfoWindows {/*** determines whether the specified file exists in the specified path, if not, create * @ param string $ fileName file name * @ param string $ content File content * @ return string returned file path */private function getFilePath ($ fileName, $ content) {$ path = dirname (_ FILE __). "\ $ fileName"; if (! File_exists ($ path) {file_put_contents ($ path, $ content);} return $ path ;} /*** obtain the cpu usage vbs file generation function * @ return string returns the 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 the 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 ob1_mi = GetObject (\" winmgmts :\\\\. \ root \ cimv2 \ ") Set colOS = ob1_mi. instancesOf (\ "Win32_OperatingSystem \") For Each objOS in colOS Wscript. echo (\ "{\" \ "TotalVisibleMemorySize \" \ ": \" & objOS. totalVisibleMemorySize & \ ", \" \ "FreePhysicalMemory \" \ ": \" & objOS. freePhysicalMemory & \ "} \") Next ");}/*** obtain CPU usage * @ return Number */public function getCpuUsage () {$ path = $ this-> getCupUsageVbsPath (); exec ("cscript-nologo $ path", $ usage); return $ usage [0];} /*** get the 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 ;}}

Call Method

$ Info = new SystemInfoWindows (); $ cpu = $ info-> getCpuUsage (); $ memory = $ info-> getMemoryUsage (); echo "current system CPU usage: {$ cpu }%, memory usage {$ memory ['usage']} % ";


The above section describes how to use php to obtain real-time cpu memory usage in windows. I hope it will be helpful to you. If you have any questions, please leave a message for me, the editor will reply to you in a timely manner. Thank you very much for your support for the help House website!

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.