Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to access wmi in python?

So I am trying to access the data from here

in Python. As you can see, it uses wmi. I have tried to use wmi in python before but I am having trouble interpreting the data they are giving me. Please be patient with me as I am a noob to how wmi works. It says that the wmi data is stored in root/OpenHardwareMontor and that it uses two different wmi classes(Hardware and Sensor). But all this information is going over my head.

could someone please give me some sample code to read some data from this?

For example, the code to check cpu core 1 frequency.

EDIT: i have sort of got it working. i run this code:

for Temperature in c.sensor():
    print Temperature.identifier
    print Temperature.value

and i get this:

/hdd/0/load/0
37.6608924866
/intelcpu/0/temperature/1
53.0
/intelcpu/0/temperature/0
42.0
/ram/data/1
2.88324356079
/intelcpu/0/load/2
1.53846144676
/hdd/0/temperature/0
43.0
/intelcpu/0/load/0
2.30768918991
/intelcpu/0/clock/1
1463.29663086
/intelcpu/0/clock/0
133.02696228
/intelcpu/0/clock/2
1463.29663086
/ram/load/0
49.224521637
/ram/data/0
2.79517364502
/intelcpu/0/load/1
3.07692289352

how can i request only the value associated with the identifier /intelcpu/0/temperature/1 ignoring all other values?

like image 277
ryan27968 Avatar asked Jan 12 '14 13:01

ryan27968


People also ask

How do I use WMI in Python?

Finding Properties and Methods of WMI Class In order to get properties and methods of as specific WMI class, create a WMI connection and use the ( '. ' ) Dot operator and 'Class Name' to access WMI namespace, then 'methods' or 'properties' attribute to return a Python List of property/method names.

What is WMI module?

Windows Management Instrumentation (WMI) is a complex set of proprietary extensions to the Windows Driver Model that provides an OS interface to allow instrumented components to provide information and notifications.

What is WMI used for?

Windows Management Instrumentation (WMI) is a subsystem of PowerShell that gives admins access to powerful system monitoring tools. Though this system has been designed to allow for fast, efficient system administration, it also has a spookier side: it can be abused by insiders as a tool to surveil other employees.


1 Answers

The most simple example to use WMI:

c = wmi.WMI()
wql = "Select * From Win32_SerialPort"
for item in c.query(wql):
    print item

Output Example:

instance of Win32_SerialPort
{
    Availability = 2;
    Binary = TRUE;
    Caption = "SpectrumAnalyzer1 (COM15)";
    ConfigManagerErrorCode = 0;
    ConfigManagerUserConfig = FALSE;
    CreationClassName = "Win32_SerialPort";
    Description = "SpectrumAnalyzer1";
    DeviceID = "COM15";
    MaxBaudRate = 128000;
    MaximumInputBufferSize = 0;
    MaximumOutputBufferSize = 0;
    Name = "SpectrumAnalyzer1 (COM15)";
    OSAutoDiscovered = TRUE;
    PNPDeviceID = "USB\\VID_10C4&PID_ED00\\1269376";
    PowerManagementCapabilities = {1};
    PowerManagementSupported = FALSE;
    ProviderType = "RS232 Serial Port";
    SettableBaudRate = TRUE;
    SettableDataBits = TRUE;
    SettableFlowControl = TRUE;
    SettableParity = TRUE;
    SettableParityCheck = TRUE;
    SettableRLSD = TRUE;
    SettableStopBits = TRUE;
    Status = "OK";
    StatusInfo = 3;
    Supports16BitMode = FALSE;
    SupportsDTRDSR = TRUE;
    SupportsElapsedTimeouts = TRUE;
    SupportsIntTimeouts = TRUE;
    SupportsParityCheck = TRUE;
    SupportsRLSD = TRUE;
    SupportsRTSCTS = TRUE;
    SupportsSpecialCharacters = TRUE;
    SupportsXOnXOff = TRUE;
    SupportsXOnXOffSet = TRUE;
    SystemCreationClassName = "Win32_ComputerSystem";
    SystemName = ".......";
};

You can access each item by:

myQuery = c.query(wql)
myQuery.Availability 

Output:

2

For more information, try the WMI cookbook.

Edit #1:

Using if statements and in you can do what you want.

for Temperature in c.sensor():
    if "/intelcpu/0/temperature/1" in Temperature.identifier:
        print Temperature.identifier
        print Temperature.value 
like image 128
Kobi K Avatar answered Sep 26 '22 00:09

Kobi K