def getHostGuestMapping(self): """ Returns dictionary containing a list of virt.Hypervisors Each virt.Hypervisor contains the hypervisor ID as well as a list of virt.Guest {'hypervisors': [Hypervisor1, ...] } """ hosts = {} nodes = self._client.get_nodes() vms = self._client.get_vms() for node in nodes['items']: status = node['status'] version = status['nodeInfo']['kubeletVersion'] name = node['metadata']['name'] uuid = status['nodeInfo']['machineID'] if self.config['hypervisor_id'] == 'uuid': host_id = uuid elif self.config['hypervisor_id'] == 'hostname': # set to uuid if hostname not available host_id = uuid for addr in status['addresses']: if addr['type'] == 'Hostname': host_id = addr['address'] facts = { virt.Hypervisor.CPU_SOCKET_FACT: self.parse_cpu(status['allocatable']["cpu"]), virt.Hypervisor.HYPERVISOR_TYPE_FACT: 'qemu', # this should be hardware uniqe identifier but k8s api gives us only machineID virt.Hypervisor.SYSTEM_UUID_FACT: status['nodeInfo']['machineID'], virt.Hypervisor.HYPERVISOR_VERSION_FACT: version } hosts[name] = virt.Hypervisor(hypervisorId=host_id, name=name, facts=facts) for vm in vms['items']: spec = vm['spec'] host_name = vm['status']['nodeName'] # a vm is not scheduled on any hosts if host_name is None: continue guest_id = spec['domain']['firmware']['uuid'] # a vm is always in running state status = virt.Guest.STATE_RUNNING hosts[host_name].guestIds.append( virt.Guest(guest_id, self.CONFIG_TYPE, status)) return {'hypervisors': list(hosts.values())}
def getHostGuestMapping(self): """ Get a dict of host to uvm mapping. Args: None. Returns: None. """ mapping = {'hypervisors': []} host_uvm_map = self._interface.build_host_to_uvm_map(self.version) for host_uuid in host_uvm_map: host = host_uvm_map[host_uuid] try: if self.config['hypervisor_id'] == 'uuid': hypervisor_id = host_uuid elif self.config['hypervisor_id'] == 'hostname': hypervisor_id = host['name'] except KeyError: self.logger.debug("Host '%s' doesn't have hypervisor_id property", host_uuid) continue guests = [] if 'guest_list' in host and len(host['guest_list']) > 0: for guest_vm in host['guest_list']: try: state = guest_vm['power_state'] except KeyError: self.logger.warning("Guest %s is missing power state. Perhaps they" " are powered off", guest_vm['uuid']) continue guests.append(Guest(guest_vm['uuid'], self.CONFIG_TYPE, state)) else: self.logger.debug("Host '%s' doesn't have any vms", host_uuid) cluster_uuid = self._interface.get_host_cluster_uuid(host) host_version = self._interface.get_host_version(host) host_name = host['name'] facts = { Hypervisor.CPU_SOCKET_FACT: str(host['num_cpu_sockets']), Hypervisor.HYPERVISOR_TYPE_FACT: host.get('hypervisor_type', 'AHV'), Hypervisor.HYPERVISOR_VERSION_FACT: str(host_version), Hypervisor.HYPERVISOR_CLUSTER: str(cluster_uuid)} mapping['hypervisors'].append(virt.Hypervisor(hypervisorId=hypervisor_id, guestIds=guests, name=host_name, facts=facts)) return mapping
def getHostGuestMapping(self): """ Returns dictionary containing a list of virt.Hypervisors Each virt.Hypervisor contains the hypervisor ID as well as a list of virt.Guest {'hypervisors': [Hypervisor1, ...] } """ hosts = {} nodes = self.get_nodes() vms = self.get_vms() for node in nodes.items: status = node.status version = status.node_info.kubelet_version name = node.metadata.name host_id = status.node_info.machine_id address = status.addresses[0].address facts = { virt.Hypervisor.CPU_SOCKET_FACT: status.allocatable["cpu"], virt.Hypervisor.HYPERVISOR_TYPE_FACT: 'qemu', virt.Hypervisor.HYPERVISOR_VERSION_FACT: version } hosts[name] = virt.Hypervisor(hypervisorId=host_id, name=address, facts=facts) for vm in vms.items: metadata = vm.metadata host_name = vm.status.node_name # a vm is not scheduled on any hosts if host_name is None: continue guest_id = metadata.namespace + '/' + metadata.name # a vm is always in running state status = virt.Guest.STATE_RUNNING hosts[host_name].guestIds.append( virt.Guest(guest_id, self.CONFIG_TYPE, status)) return {'hypervisors': list(hosts.values())}
def getHostGuestMapping(self): """ Returns dictionary containing a list of virt.Hypervisors Each virt.Hypervisor contains the hypervisor ID as well as a list of virt.Guest {'hypervisors': [Hypervisor1, ...] } """ hosts = {} nodes = self._client.get_nodes() vms = self._client.get_vms() for node in nodes['items']: status = node['status'] version = status['nodeInfo']['kubeletVersion'] name = node['metadata']['name'] host_id = status['nodeInfo']['machineID'] address = status['addresses'][0]['address'] facts = { virt.Hypervisor.CPU_SOCKET_FACT: status['allocatable']["cpu"], virt.Hypervisor.HYPERVISOR_TYPE_FACT: 'qemu', virt.Hypervisor.HYPERVISOR_VERSION_FACT: version } hosts[name] = virt.Hypervisor(hypervisorId=host_id, name=address, facts=facts) for vm in vms['items']: metadata = vm['metadata'] host_name = vm['status']['nodeName'] # a vm is not scheduled on any hosts if host_name is None: continue guest_id = metadata['namespace'] + '/' + metadata['name'] # a vm is always in running state status = virt.Guest.STATE_RUNNING hosts[host_name].guestIds.append( virt.Guest(guest_id, self.CONFIG_TYPE, status)) return {'hypervisors': list(hosts.values())}
def getHostGuestMapping(self): guests = [] connection = self.connect() hypervsoap = HyperVSoap(self.url, connection, self.logger) uuid = None if not self.useNewApi: try: # SettingType == 3 means current setting, 5 is snapshot - we don't want snapshots uuid = hypervsoap.Enumerate( "select BIOSGUID, ElementName " "from Msvm_VirtualSystemSettingData " "where SettingType = 3", "root/virtualization") except HyperVCallFailed: self.logger.debug( "Unable to enumerate using root/virtualization namespace, " "trying root/virtualization/v2 namespace") self.useNewApi = True if self.useNewApi: # Filter out Planned VMs and snapshots, see # http://msdn.microsoft.com/en-us/library/hh850257%28v=vs.85%29.aspx uuid = hypervsoap.Enumerate( "select BIOSGUID, ElementName " "from Msvm_VirtualSystemSettingData " "where VirtualSystemType = 'Microsoft:Hyper-V:System:Realized'", "root/virtualization/v2") # Get guest states guest_states = hypervsoap.Invoke_GetSummaryInformation( "root/virtualization/v2" if self. useNewApi else "root/virtualization") vmmsVersion = self.getVmmsVersion(hypervsoap) for instance in hypervsoap.Pull(uuid): try: uuid = instance["BIOSGUID"] except KeyError: self.logger.warning("Guest without BIOSGUID found, ignoring") continue try: elementName = instance["ElementName"] except KeyError: self.logger.warning("Guest %s is missing ElementName", uuid) continue try: state = guest_states[elementName] except KeyError: self.logger.warning("Unknown state for guest %s", elementName) state = virt.Guest.STATE_UNKNOWN guests.append(virt.Guest(HyperV.decodeWinUUID(uuid), self, state)) # Get the hostname hostname = None socket_count = None data = hypervsoap.Enumerate( "select DNSHostName, NumberOfProcessors from Win32_ComputerSystem", "root/cimv2") for instance in hypervsoap.Pull(data, "root/cimv2"): hostname = instance["DNSHostName"] socket_count = instance["NumberOfProcessors"] if self.config.hypervisor_id == 'uuid': uuid = hypervsoap.Enumerate( "select UUID from Win32_ComputerSystemProduct", "root/cimv2") host = None for instance in hypervsoap.Pull(uuid, "root/cimv2"): host = HyperV.decodeWinUUID(instance["UUID"]) elif self.config.hypervisor_id == 'hostname': host = hostname else: raise virt.VirtError( 'Invalid option %s for hypervisor_id, use one of: uuid, or hostname' % self.config.hypervisor_id) facts = { virt.Hypervisor.CPU_SOCKET_FACT: str(socket_count), virt.Hypervisor.HYPERVISOR_TYPE_FACT: 'hyperv', virt.Hypervisor.HYPERVISOR_VERSION_FACT: vmmsVersion, } hypervisor = virt.Hypervisor(hypervisorId=host, name=hostname, guestIds=guests, facts=facts) return {'hypervisors': [hypervisor]}
def getHostGuestMapping(self): """ Returns dictionary containing a list of virt.Hypervisors Each virt.Hypervisor contains the hypervisor ID as well as a list of virt.Guest {'hypervisors': [Hypervisor1, ...] } """ mapping = {} hosts = {} clusters = set() cluster_names = {} clusters_xml = self.get_xml(self.clusters_url) hosts_xml = self.get_xml(self.hosts_url) vms_xml = self.get_xml(self.vms_url) # Save ids of clusters that are "virt_service" for cluster in clusters_xml.findall('cluster'): cluster_id = cluster.get('id') virt_service = cluster.find('virt_service').text cluster_names[cluster_id] = cluster.find('name').text if virt_service.lower() == 'true': clusters.add(cluster_id) for host in hosts_xml.findall('host'): id = host.get('id') system_uuid = '' # Check if host is in cluster that is "virt_service" host_cluster = host.find('cluster') host_cluster_id = host_cluster.get('id') if host_cluster_id not in clusters: # Skip the host if it's cluster is not "virt_service" self.logger.debug( 'Cluster of host %s is not virt_service, skipped', id) continue try: system_uuid = host.find('hardware_information').find( 'uuid').text except AttributeError: # The error is not important yet self.logger.info("Unable to get hardware uuid for host %s ", id) if self.config['hypervisor_id'] == 'uuid': host_id = id elif self.config['hypervisor_id'] == 'hwuuid': if not system_uuid == '': host_id = system_uuid else: self.logger.error("Host %s doesn't have hardware uuid", id) continue elif self.config['hypervisor_id'] == 'hostname': host_id = host.find('address').text sockets = None try: sockets = host.find('cpu').find('topology').find( 'sockets').text except AttributeError: self.logger.warning("Unable to find socket count.") if not sockets: sockets = "unknown" facts = { virt.Hypervisor.CPU_SOCKET_FACT: sockets, virt.Hypervisor.HYPERVISOR_TYPE_FACT: 'qemu', virt.Hypervisor.SYSTEM_UUID_FACT: system_uuid, } try: cluster_name = cluster_names[host_cluster_id] except KeyError: pass else: facts[virt.Hypervisor.HYPERVISOR_CLUSTER] = cluster_name version = None try: version = host.find('version').find('full_version').text except AttributeError: self.logger.warning("Unable to find hypervisor version.") if version: facts[virt.Hypervisor.HYPERVISOR_VERSION_FACT] = version hosts[id] = virt.Hypervisor(hypervisorId=host_id, name=host.find('address').text, facts=facts) mapping[id] = [] for vm in vms_xml.findall('vm'): guest_id = vm.get('id') host = vm.find('host') if host is None: # Guest don't have any host continue host_id = host.get('id') if host_id not in mapping.keys(): self.logger.warning( "Guest %s claims that it belongs to host %s which doesn't exist", guest_id, host_id) continue try: status = vm.find('status') try: state_text = status.find('state').text.lower() except AttributeError: # RHEVM 4.0 reports the state differently state_text = status.text.lower() state = RHEVM_STATE_TO_GUEST_STATE.get( state_text, virt.Guest.STATE_UNKNOWN) except AttributeError: self.logger.warning("Guest %s doesn't report any status", guest_id) state = virt.Guest.STATE_UNKNOWN hosts[host_id].guestIds.append( virt.Guest(guest_id, self.CONFIG_TYPE, state)) return {'hypervisors': list(hosts.values())}
def getHostGuestMapping(self): mapping = {'hypervisors': []} for host_id, host in list(self.hosts.items()): parent = host['parent'].value if self.config[ 'exclude_host_parents'] is not None and parent in self.config[ 'exclude_host_parents']: self.logger.debug( "Skipping host '%s' because its parent '%s' is excluded", host_id, parent) continue if self.config[ 'filter_host_parents'] is not None and parent not in self.config[ 'filter_host_parents']: self.logger.debug( "Skipping host '%s' because its parent '%s' is not included", host_id, parent) continue guests = [] try: if self.config['hypervisor_id'] == 'uuid': uuid = host['hardware.systemInfo.uuid'] elif self.config['hypervisor_id'] == 'hwuuid': uuid = host_id elif self.config['hypervisor_id'] == 'hostname': uuid = host['config.network.dnsConfig.hostName'] domain_name = host['config.network.dnsConfig.domainName'] if domain_name: uuid = self._format_hostname(uuid, domain_name) except KeyError: self.logger.debug( "Host '%s' doesn't have hypervisor_id property", host_id) continue if host['vm']: for vm_id in host['vm'].ManagedObjectReference: if vm_id.value not in self.vms: self.logger.debug( "Host '%s' references non-existing guest '%s'", host_id, vm_id.value) continue vm = self.vms[vm_id.value] if 'config.uuid' not in vm: self.logger.debug( "Guest '%s' doesn't have 'config.uuid' property", vm_id.value) continue if not vm['config.uuid'].strip(): self.logger.debug( "Guest '%s' has empty 'config.uuid' property", vm_id.value) continue state = virt.Guest.STATE_UNKNOWN try: if vm['runtime.powerState'] == 'poweredOn': state = virt.Guest.STATE_RUNNING elif vm['runtime.powerState'] == 'suspended': state = virt.Guest.STATE_PAUSED elif vm['runtime.powerState'] == 'poweredOff': state = virt.Guest.STATE_SHUTOFF except KeyError: self.logger.debug( "Guest '%s' doesn't have 'runtime.powerState' property", vm_id.value) guests.append( virt.Guest(vm['config.uuid'], self.CONFIG_TYPE, state)) try: name = host['config.network.dnsConfig.hostName'] domain_name = host['config.network.dnsConfig.domainName'] if domain_name: name = self._format_hostname(name, domain_name) except KeyError: self.logger.debug( "Unable to determine hostname for host '%s'. Ommitting from report", uuid) continue facts = { virt.Hypervisor.CPU_SOCKET_FACT: str(host['hardware.cpuInfo.numCpuPackages']), virt.Hypervisor.HYPERVISOR_TYPE_FACT: host.get('config.product.name', 'vmware'), virt.Hypervisor.SYSTEM_UUID_FACT: host['hardware.systemInfo.uuid'] } if host['parent'] and host[ 'parent']._type == 'ClusterComputeResource': cluster_id = host['parent'].value # print('', self.clusters, cluster_id) cluster = self.clusters[cluster_id] facts[virt.Hypervisor.HYPERVISOR_CLUSTER] = cluster['name'] version = host.get('config.product.version', None) if version: facts[virt.Hypervisor.HYPERVISOR_VERSION_FACT] = version mapping['hypervisors'].append( virt.Hypervisor(hypervisorId=uuid, guestIds=guests, name=name, facts=facts)) return mapping
def getHostGuestMapping(self): assert hasattr(self, 'session'), "Login was not called" hosts = self.session.xenapi.host.get_all() mapping = { 'hypervisors': [], } for host in hosts: record = self.session.xenapi.host.get_record(host) guests = [] for resident in self.session.xenapi.host.get_resident_VMs(host): vm = self.session.xenapi.VM.get_record(resident) uuid = vm['uuid'] if vm.get('is_control_domain', False): if uuid not in self.ignored_guests: self.ignored_guests.add(uuid) self.logger.debug("Control Domain %s is ignored", uuid) continue if vm.get('is_a_snapshot', False) or vm.get( 'is_a_template', False): if uuid not in self.ignored_guests: self.ignored_guests.add(uuid) self.logger.debug( "Guest %s is snapshot or template, ignoring", uuid) continue if vm['power_state'] == 'Running': state = virt.Guest.STATE_RUNNING elif vm['power_state'] == 'Suspended': state = virt.Guest.STATE_PAUSED elif vm['power_state'] == 'Paused': state = virt.Guest.STATE_PAUSED elif vm['power_state'] == 'Halted': state = virt.Guest.STATE_SHUTOFF else: state = virt.Guest.STATE_UNKNOWN guests.append( virt.Guest(uuid=uuid, virt_type=self.CONFIG_TYPE, state=state)) facts = {} sockets = record.get('cpu_info', {}).get('socket_count') if sockets is not None: facts[virt.Hypervisor.CPU_SOCKET_FACT] = str(sockets) brand = record.get('software_version', {}).get('product_brand') if brand: facts[virt.Hypervisor.HYPERVISOR_TYPE_FACT] = brand version = record.get('software_version', {}).get('product_version') if version: facts[virt.Hypervisor.HYPERVISOR_VERSION_FACT] = version facts[virt.Hypervisor.SYSTEM_UUID_FACT] = record["uuid"] if self.config['hypervisor_id'] == 'uuid': uuid = record["uuid"] elif self.config['hypervisor_id'] == 'hostname': uuid = record["hostname"] mapping['hypervisors'].append( virt.Hypervisor(hypervisorId=uuid, guestIds=guests, name=record["hostname"], facts=facts)) return mapping
def getHostGuestMapping(self): """ Returns dictionary containing a list of virt.Hypervisors Each virt.Hypervisor contains the hypervisor ID as well as a list of virt.Guest {'hypervisors': [Hypervisor1, ...] } """ mapping = {} hosts = {} clusters = set() clusters_xml = self.get_xml(self.clusters_url) hosts_xml = self.get_xml(self.hosts_url) vms_xml = self.get_xml(self.vms_url) # Save ids of clusters that are "virt_service" for cluster in clusters_xml.findall('cluster'): cluster_id = cluster.get('id') virt_service = cluster.find('virt_service').text if virt_service.lower() == 'true': clusters.add(cluster_id) for host in hosts_xml.findall('host'): id = host.get('id') # Check if host is in cluster that is "virt_service" host_cluster = host.find('cluster') host_cluster_id = host_cluster.get('id') if host_cluster_id not in clusters: # Skip the host if it's cluster is not "virt_service" self.logger.debug( 'Cluster of host %s is not virt_service, skipped', id) continue if self.config.hypervisor_id == 'uuid': host_id = id elif self.config.hypervisor_id == 'hwuuid': try: host_id = host.find('hardware_information').find( 'uuid').text except AttributeError: self.logger.warn("Host %s doesn't have hardware uuid", id) continue elif self.config.hypervisor_id == 'hostname': host_id = host.find('name').text else: raise virt.VirtError( 'Invalid option %s for hypervisor_id, use one of: uuid, hwuuid, or hostname' % self.config.hypervisor_id) sockets = host.find('cpu').find('topology').get('sockets') if not sockets: sockets = host.find('cpu').find('topology').find( 'sockets').text facts = { virt.Hypervisor.CPU_SOCKET_FACT: sockets, virt.Hypervisor.HYPERVISOR_TYPE_FACT: 'qemu', } try: version = host.find('version').get('full_version') if version: facts[virt.Hypervisor.HYPERVISOR_VERSION_FACT] = version except AttributeError: pass hosts[id] = virt.Hypervisor(hypervisorId=host_id, name=host.find('name').text, facts=facts) mapping[id] = [] for vm in vms_xml.findall('vm'): guest_id = vm.get('id') host = vm.find('host') if host is None: # Guest don't have any host continue host_id = host.get('id') if host_id not in mapping.keys(): self.logger.warning( "Guest %s claims that it belongs to host %s which doesn't exist", guest_id, host_id) continue try: status = vm.find('status') try: state_text = status.find('state').text.lower() except AttributeError: # RHEVM 4.0 reports the state differently state_text = status.text.lower() state = RHEVM_STATE_TO_GUEST_STATE.get( state_text, virt.Guest.STATE_UNKNOWN) except AttributeError: self.logger.warning("Guest %s doesn't report any status", guest_id) state = virt.Guest.STATE_UNKNOWN hosts[host_id].guestIds.append(virt.Guest(guest_id, self, state)) return {'hypervisors': hosts.values()}
def getHostGuestMapping(self): mapping = {'hypervisors': []} for host_id, host in self.hosts.items(): parent = host['parent'].value if self.config.exclude_host_parents is not None and parent in self.config.exclude_host_parents: self.logger.debug( "Skipping host '%s' because its parent '%s' is excluded", host_id, parent) continue if self.config.filter_host_parents is not None and parent not in self.config.filter_host_parents: self.logger.debug( "Skipping host '%s' because its parent '%s' is not included", host_id, parent) continue guests = [] try: if self.config.hypervisor_id == 'uuid': uuid = host['hardware.systemInfo.uuid'] elif self.config.hypervisor_id == 'hwuuid': uuid = host_id elif self.config.hypervisor_id == 'hostname': uuid = host['config.network.dnsConfig.hostName'] domain_name = host['config.network.dnsConfig.domainName'] if domain_name: uuid = '{0}.{1}'.format(uuid, domain_name) else: raise virt.VirtError( 'Invalid option %s for hypervisor_id, use one of: uuid, hwuuid, or hostname' % self.config.hypervisor_id) except KeyError: self.logger.debug( "Host '%s' doesn't have hypervisor_id property", host_id) continue if host['vm']: for vm_id in host['vm'].ManagedObjectReference: if vm_id.value not in self.vms: self.logger.debug( "Host '%s' references non-existing guest '%s'", host_id, vm_id.value) continue vm = self.vms[vm_id.value] if 'config.uuid' not in vm: self.logger.debug( "Guest '%s' doesn't have 'config.uuid' property", vm_id.value) continue if not vm['config.uuid'].strip(): self.logger.debug( "Guest '%s' has empty 'config.uuid' property", vm_id.value) continue state = virt.Guest.STATE_UNKNOWN try: if vm['runtime.powerState'] == 'poweredOn': state = virt.Guest.STATE_RUNNING elif vm['runtime.powerState'] == 'suspended': state = virt.Guest.STATE_PAUSED elif vm['runtime.powerState'] == 'poweredOff': state = virt.Guest.STATE_SHUTOFF except KeyError: self.logger.debug( "Guest '%s' doesn't have 'runtime.powerState' property", vm_id.value) guests.append(virt.Guest(vm['config.uuid'], self, state)) try: name = host['config.network.dnsConfig.hostName'] domain_name = host['config.network.dnsConfig.domainName'] if domain_name: name = '{0}.{1}'.format(name, domain_name) except KeyError: self.logger.debug("Unable to determine hostname for host '%s'", uuid) name = '' facts = { virt.Hypervisor.CPU_SOCKET_FACT: str(host['hardware.cpuInfo.numCpuPackages']), virt.Hypervisor.HYPERVISOR_TYPE_FACT: host.get('config.product.name', 'vmware'), } version = host.get('config.product.version', None) if version: facts[virt.Hypervisor.HYPERVISOR_VERSION_FACT] = version mapping['hypervisors'].append( virt.Hypervisor(hypervisorId=uuid, guestIds=guests, name=name, facts=facts)) return mapping