The biggest hurdle to using WMI I found was knowing where the information you want is stored. There are various browsers available but one quick and simple entry point is Microsoft’s WMI Code Creator. It extracts to an runnable executable so no installer needed. While it is designed for VB and C# users, if you ignore most of the code it creates and just grab WQL statement I found it one of the quickest ways of getting up and running.
What lead to code creator was a need to monitor the process usage. I suspected correctly I could get this information from the WMI but had no idea how.
Starting WMI Code Creator and using the default namespace of root\CIMV2 you can use the drop down list of classes to browse what is available. The first one I tried was Win32_Processor. Selecting the class gives you the properties. Select the properties (I just select every one) and clicking Search for Property Values gives you a list of the values. Needless to say Win32_Processor gives you the processors installed, not quite what I want.
Next class to try is the long winded Win32_PerfFormattedData_PerfOS_Processor. This looked more promising, as the thumbnail to the right shows (click for larger image). Getting the properties values brought back far more data than I expected and a slight surprise; I had forgot I had a dual core processor.
Because I have more than one core, the query returns 3 sets of data, one for each processor and a total. WMI Code Creator indents the even sets which helps a little but there are better methods.
The code created in the right hand pane is of little use in Python but you can copy and paste the WQL query to save some time. Time to get this query into Python so we can have a play.
import wmi mywmi = wmi.WMI() wql = "SELECT * FROM Win32_PerfFormattedData_PerfOS_Processor" for item in mywmi.query(wql): print(item)
Wow, that’s a lot of information. At this point the documentation comes to the rescue (enter the class name into a search engine). It looks like PercentProcessorTime is the property I need. So with a quick update gives me a very crude processor monitor
import wmi, time mywmi = wmi.WMI() wql = 'SELECT * FROM Win32_PerfFormattedData_PerfOS_Processor WHERE Name = "_Total"' while True: for item in mywmi.query(wql): print(item.PercentProcessorTime) time.sleep(1)