简体   繁体   中英

How to get CPU and RAM usage without psutil

For security reasons, I am not allowed to use any packages downloaded from pypi, including PSUTIL. Is there a way to be able to retrieve the CPU Usage and RAM Usage without it?

I believe there is already an existing question for this with a solution where I can read /proc/stat , but that is not an option for me, because I am using Windows server, not linux.

I was thinking maybe I can execute a "get CPU/RAM Usage" shell command and printing the output in Python using subprocess module, but I'm getting an error. Here is my code:

import subprocess
result = subprocess.run(['Get-WmiObject -Class Win32_Processor | Select LoadPercentage', '-l'], stdout=subprocess.PIPE)

result.stdout

I'm getting a "FileNotFoundError: [WinError 2] The system cannot find the file specified" error.

More detailed error message:

  File "C:\Users\acer\Documents\Test.py", line 2, in <module>
    result = subprocess.run(['Get-WmiObject -Class Win32_Processor | Select LoadPercentage', '-l'], stdout=subprocess.PIPE)
  File "C:\Users\acer\AppData\Local\Programs\Python\Python310\lib\subprocess.py", line 501, in run
    with Popen(*popenargs, **kwargs) as process:
  File "C:\Users\acer\AppData\Local\Programs\Python\Python310\lib\subprocess.py", line 969, in __init__
    self._execute_child(args, executable, preexec_fn, close_fds,
  File "C:\Users\acer\AppData\Local\Programs\Python\Python310\lib\subprocess.py", line 1438, in _execute_child
    hp, ht, pid, tid = _winapi.CreateProcess(executable, args,
FileNotFoundError: [WinError 2] The system cannot find the file specified

subprocess.run runs your command in the windows command prompt. To run it in powershell, you need to first invoke powershell in the command prompt. You can do this simply by prepending "powershell" to your list of arguments

result = subprocess.run(['powershell', 'Get-WmiObject -Class Win32_Processor | Select LoadPercentage'], stdout=subprocess.PIPE)

print(result.stdout)

prints

b'\r\nLoadPercentage\r\n--------------\r\n             5\r\n\r\n\r\n'

Since this output is of type bytes , you'll have to decode it and parse the number out of it. I split the result and selected the last element, but you can use any other method you prefer:

result_s = result.stdout.decode("ascii")
cpu_load = int(result_s.split()[-1])        # gives cpu_load = 5

Note that I removed your '-l' argument because that causes an error on my computer. I am not familiar with the Select command (or any powershell, really) to tell you how to fix it, but it seems irrelevant to the question anyway.

Select-Object : Missing an argument for parameter 'Last'. Specify a parameter of type 'System.Int32' and try again.
At line:1 char:62
+ Get-WmiObject -Class Win32_Processor | Select LoadPercentage -l
+                                                              ~~
    + CategoryInfo          : InvalidArgument: (:) [Select-Object], ParameterBindingException
    + FullyQualifiedErrorId : MissingArgument,Microsoft.PowerShell.Commands.SelectObjectCommand

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM