sebastian sebastian - 4 years ago 356
Python Question

Python WMI Hyper-v GetSummaryInformation result

Im trying to retrieve information from all the available VMs on a Hyper-V Server. The problem is that when I ask for the summary information, i get a list of useless COMObjects.

I can't find a way of getting the actual SummaryInformation values..

Here's the code:

import wmi

conn = wmi.connect_server(server="xxxx", user="xxxx", password="xxx", namespace=r"root\virtualization\v2")
client = wmi.WMI(wmi=conn)
mgs = client.Msvm_VirtualSystemManagementService()
summaries = mgs[0].GetSummaryInformation()

print summaries
# (0, [<COMObject <unknown>>, <COMObject <unknown>>, <COMObject <unknown>>])


So I tried retrieving one VirtualSystemData to pass as parameter to getSummary

vs = h.Msvm_VirtualSystemSettingData()
vs[0]
#Out[34]: <_wmi_object: \\WIN-Lxxxxxx\root\virtualization#\v2:Msvm_VirtualSystemSettingData.InstanceID="Microsoft:xxx-xxx-xxx-xxx">

mgs[0].GetSummaryInformation([vs[0].ole_object])
#Out[38]: (0, [<COMObject <unknown>>, <COMObject <unknown>>, <COMObject <unknown>>])


Any ideas?

Answer Source

Solution:

First, be careful about the objects selected to retrieve the virtual settings data. To avoid having the manager between de vms, is better to start like this:

vms = client.query("select * from Msvm_ComputerSystem where Caption=\"Virtual Machine\"")

From there we can iterate each vm, ask for its settings, and then use the manager to retrieve the summary information:

# SummaryInformation attributes
vm_attrs = {
    'Name': 0, 
    'ElementName': 1, 
    'CreationTime': 2, 
    'Notes': 3, 
    'NumberOfProcessors': 4, 
    'ThumbnailImage': 5, 
    'ThumbnailImage': 6, 
    'ThumbnailImage': 7, 
    'AllocatedGPU': 8, 
    'EnabledState': 100, 
    'ProcessorLoad': 101, 
    'ProcessorLoadHistory': 102, 
    'MemoryUsage': 103,
    'Heartbeat': 104, 
    'UpTime': 105, 
    'GuestOperatingSystem': 106, 
    'Snapshots': 107, 
    'AsynchronousTasks': 108, 
    'HealthState': 109, 
    'OperationalStatus': 110, 
    'StatusDescriptions': 111, 
    'MemoryAvailable': 112, 
    'AvailableMemoryBuffer': 113 
}

vms = client.query("select * from Msvm_ComputerSystem where Caption=\"Virtual Machine\"")
management_service = client.Msvm_VirtualSystemManagementService()[0]

for vm in vms:
    settings = vm.associators(wmi_result_class='Msvm_VirtualSystemSettingData')

    vm_original = filter(lambda x: 'Realized' in x.VirtualSystemType, settings)
    vm_snapshots = filter(lambda x: 'Snapshot' in x.VirtualSystemType, settings)

    # skipped retrieving snapshots to make it shorter, but is exactly the same        
    vm_data = {'snapshots': len(vm_snapshots)}
    paths = [vm_original[0].path_()]
    summary_info = management_service.GetSummaryInformation(vm_attrs.values(), paths)
    if summary_info[0] != 0:
        raise Exception("Error retrieving information from vm '%s'" % vm.Name)

    # Note, if you do dir() on the COMObject from the summary info list, you wont find any attribute from the docs, but, trust me, if you do summary[1][0].NumberOfProcessors or any other attribute, it will work. 
    vm_data.update({attr: getattr(summary_info[1][0], attr) for attr in vm_attrs.keys()})

    vms_data[vm.Name] = vm_data

It took me quite a while to figure it out... hope this will help someone else eventually. :)

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download