try: domain = conn.lookupByID(domainID) except libvirt.libvirtError, lve: raise VirtualizationException, \ "Failed to obtain handle to domain %d: %s" % \ (domainID, repr(lve)), sys.exc_info()[2] uuid = binascii.hexlify(domain.UUID()) # SEE: http://libvirt.org/html/libvirt-libvirt.html#virDomainInfo # for more info. domain_info = domain.info() # Set the virtualization type. We can tell if the domain is fully virt # by checking the domain's OSType() attribute. virt_type = VirtualizationType.PARA if is_fully_virt(domain): virt_type = VirtualizationType.FULLY # we need to filter out the small per/minute KB changes # that occur inside a vm. To do this we divide by 1024 to # drop our precision down to megabytes with an int then # back up to KB memory = int(domain_info[2] / 1024); memory = memory * 1024; properties = { PropertyType.NAME : domain.name(), PropertyType.UUID : uuid, PropertyType.TYPE : virt_type, PropertyType.MEMORY : str(memory), # current memory PropertyType.VCPUS : domain_info[3], PropertyType.STATE : VIRT_STATE_NAME_MAP[domain_info[0]] }
def poll_hypervisor(): """ This function polls the hypervisor for information about the currently running set of domains. It returns a dictionary object that looks like the following: { uuid : { 'name' : '...', 'uuid' : '...', 'virt_type' : '...', 'memory_size' : '...', 'vcpus' : '...', 'state' : '...' }, ... } """ if not libvirt: return {} try: conn = libvirt.openReadOnly(None) except libvirt.libvirtError: # virConnectOpen() failed sys.stderr.write(rhncli.utf8_encode(_("Warning: Could not retrieve virtualization information!\n\t" + "libvirtd service needs to be running.\n"))) conn = None if not conn: # No connection to hypervisor made return {} domainIDs = conn.listDomainsID() state = {} for domainID in domainIDs: try: domain = conn.lookupByID(domainID) except libvirt.libvirtError: lve = sys.exc_info()[1] raise_with_tb(VirtualizationException("Failed to obtain handle to domain %d: %s" % (domainID, repr(lve)), sys.exc_info()[2])) uuid = binascii.hexlify(domain.UUID()) # SEE: http://libvirt.org/html/libvirt-libvirt.html#virDomainInfo # for more info. domain_info = domain.info() # Set the virtualization type. We can tell if the domain is fully virt # by checking the domain's OSType() attribute. virt_type = VirtualizationType.PARA if is_fully_virt(domain): virt_type = VirtualizationType.FULLY # we need to filter out the small per/minute KB changes # that occur inside a vm. To do this we divide by 1024 to # drop our precision down to megabytes with an int then # back up to KB memory = int(domain_info[2] / 1024); memory = memory * 1024; properties = { PropertyType.NAME : domain.name(), PropertyType.UUID : uuid, PropertyType.TYPE : virt_type, PropertyType.MEMORY : str(memory), # current memory PropertyType.VCPUS : domain_info[3], PropertyType.STATE : VIRT_STATE_NAME_MAP[domain_info[0]] } state[uuid] = properties if state: _log_debug("Polled state: %s" % repr(state)) return state
try: domain = conn.lookupByID(domainID) except libvirt.libvirtError, lve: raise VirtualizationException, \ "Failed to obtain handle to domain %d: %s" % \ (domainID, repr(lve)), sys.exc_info()[2] uuid = binascii.hexlify(domain.UUID()) # SEE: http://libvirt.org/html/libvirt-libvirt.html#virDomainInfo # for more info. domain_info = domain.info() # Set the virtualization type. We can tell if the domain is fully virt # by checking the domain's OSType() attribute. virt_type = VirtualizationType.PARA if is_fully_virt(domain): virt_type = VirtualizationType.FULLY # we need to filter out the small per/minute KB changes # that occur inside a vm. To do this we divide by 1024 to # drop our precision down to megabytes with an int then # back up to KB memory = int(domain_info[2] / 1024) memory = memory * 1024 properties = { PropertyType.NAME: domain.name(), PropertyType.UUID: uuid, PropertyType.TYPE: virt_type, PropertyType.MEMORY: str(memory), # current memory PropertyType.VCPUS: domain_info[3], PropertyType.STATE: VIRT_STATE_NAME_MAP[domain_info[0]]