def get_current_session(self): # -> Mapping: currentSession = self.content.sessionManager.currentSession fields = [ "userName", # 'MyUserName' "fullName", # 'My FullName' "ipAddress", # '10.172.192.8' "userAgent", # 'pyvmomi Python/3.6.8 (Linux; 3.10.0-862.11.6.el7.x86_64; x86_64)' "locale", # 'en' "loginTime", # datetime.datetime(2019, 7, 22, 13, 8, 49, 529086, tzinfo=<pyVmomi.Iso8601.TZInfo object at 0x7f65aeab7128>) "callCount" ] data = { field: getattr(currentSession, field, None) for field in fields } data["loginTime"] = Iso8601.ISO8601Format(data["loginTime"]) # TODO: data["loginTime"] = arrow.get(login_time, "YYYY-MM-DD[T]HH:mm:ss.S").datetime return data
def parse_vim_property(vim_prop): """ Helper method to parse VIM properties of virtual machine """ prop_type = type(vim_prop).__name__ if prop_type.startswith(("vim", "vmodl", "Link")): if isinstance(vim_prop, DataObject): r = {} for prop in vim_prop._GetPropertyList(): # pylint: disable=protected-access if prop.name not in [ "dynamicProperty", "dynamicType", "managedObjectType", ]: sub_prop = getattr(vim_prop, prop.name) r[prop.name] = parse_vim_property(sub_prop) return r if isinstance(vim_prop, list): r = [] for prop in vim_prop: r.append(parse_vim_property(prop)) return r return vim_prop.__str__() elif prop_type == "datetime": return Iso8601.ISO8601Format(vim_prop) elif prop_type == "long": return int(vim_prop) elif prop_type == "long[]": return [int(x) for x in vim_prop] elif isinstance(vim_prop, list): return [parse_vim_property(x) for x in vim_prop] elif prop_type in ["bool", "int", "NoneType", "dict"]: return vim_prop elif prop_type in ["binary"]: return to_text(base64.b64encode(vim_prop)) return to_text(vim_prop)
def parse_vim_property(vim_prop): """ Helper method to parse VIM properties of virtual machine """ prop_type = type(vim_prop).__name__ if prop_type.startswith("vim") or prop_type.startswith("vmodl"): if isinstance(vim_prop, DataObject): r = {} for prop in vim_prop._GetPropertyList(): # pylint: disable=protected-access if prop.name not in [ 'dynamicProperty', 'dynamicType', 'managedObjectType' ]: sub_prop = getattr(vim_prop, prop.name) r[prop.name] = parse_vim_property(sub_prop) return r elif isinstance(vim_prop, list): r = [] for prop in vim_prop: r.append(parse_vim_property(prop)) return r return vim_prop.__str__() elif prop_type == "datetime": return Iso8601.ISO8601Format(vim_prop) elif prop_type == "long": return int(vim_prop) elif prop_type == "long[]": return [int(x) for x in vim_prop] elif isinstance(vim_prop, list): return [parse_vim_property(x) for x in vim_prop] elif prop_type in ['bool', 'int', 'NoneType']: return vim_prop return to_text(vim_prop)
def _get_vm_infos(self, vm: vim.VirtualMachine) -> Mapping: vmsum = { "boot_time": None, "create_date": None # Since vSphere API 6.7 } vmsum[ 'vm_path_name'] = vm.summary.config.vmPathName # '[LocalDS_0] DC0_H0_VM0/DC0_H0_VM0.vmx' vmsum[ 'name'] = vm.name # TODO: vérifier si vm.name et config.name sont toujours pareil vmsum['uuid'] = vm.config.instanceUuid vmsum['bios_uuid'] = vm.config.uuid vmsum['hostname'] = vm.guest.hostName vmsum['is_template'] = vm.config.template vmsum['diskGB'] = vm.summary.storage.committed / 1024**3 hardware = vm.config.hardware vmsum['cpu_count'] = hardware.numCPU vmsum['cpu_cores'] = hardware.numCoresPerSocket vmsum['mem_mb'] = hardware.memoryMB #vmsum['mem'] = vm.summary.config.memorySizeMB / 1024 vmsum['ostype'] = vm.config.guestFullName vmsum['state'] = vm.summary.runtime.powerState vmsum[ 'annotation'] = vm.config.annotation if vm.config.annotation else '' if not vm.config.template and getattr(vm.summary.runtime, "bootTime", None): vmsum['boot_time'] = Iso8601.ISO8601Format( vm.summary.runtime.bootTime) if getattr(vm.config, "createDate", None): vmsum['create_date'] = Iso8601.ISO8601Format(vm.config.createDate) vmsum['guestFamily'] = vm.guest.guestFamily # 'otherGuestFamily', vmsum['toolsVersion'] = vm.guest.toolsVersion # '2147483647', if getattr(vm.guest, 'toolsVersionStatus2', None): vmsum['toolsVersionStatus'] = vm.guest.toolsVersionStatus2 elif getattr(vm.guest, 'toolsVersionStatus', None): vmsum['toolsVersionStatus'] = vm.guest.toolsVersionStatus if hasattr(vm.guest, 'toolsStatus2'): vmsum['toolsStatus'] = vm.guest.toolsStatus2 # 'toolsOk', elif hasattr(vm.guest, 'toolsStatus'): vmsum['toolsStatus'] = vm.guest.toolsStatus # 'toolsOk', vmsum['toolsRunningStatus'] = vm.guest.toolsRunningStatus # ? vmsum['guestState'] = vm.guest.guestState # 'running', vmsum['guestOperationsReady'] = vm.guest.guestOperationsReady # true vmsum[ 'interactiveGuestOperationsReady'] = vm.guest.interactiveGuestOperationsReady # false, vmsum[ 'guestStateChangeSupported'] = vm.guest.guestStateChangeSupported # true, vmsum['net'] = self.getNICs(vm) vmsum['fields'] = self.get_custom_fields(vm) return vmsum