/// <summary> /// Creates an instance of the VMA proxy and establishes a connection /// </summary> /// <param name="url"></param> /// <param name="username"></param> /// <param name="password"></param> public void Connect(string url, string username, string password) { if (_service != null) { Disconnect(); } _service = GetVimService(url, username, password); _sic = _service.RetrieveServiceContent(_svcRef); if (_sic.sessionManager != null) { _service.Login(_sic.sessionManager, username, password, null); } _state = ConnectionState.Connected; if (AfterConnect != null) { AfterConnect(this, new ConnectionEventArgs()); } }
public static VMwareHostSystemInformation Retrieve(VMwareHostConnectionInfo hostConnectionInfo) { try { var sysinfo = new VMwareHostSystemInformation(); #warning comments #warning some system so the database don't get hammered too much sysinfo.timeStampInSecondsSinceEpochUtc = (long)(DateTime.UtcNow - _epochUtc).TotalSeconds; sysinfo.responsive = 1; sysinfo.comments = ""; sysinfo.ipOrHostname = hostConnectionInfo.ipOrHostname; sysinfo.vmHostnames = hostConnectionInfo.vmHostnames; VimPortType service = null; ServiceContent serviceContent = null; //Connect string url = "https://" + hostConnectionInfo.ipOrHostname + "/sdk"; ServicePointManager.SecurityProtocol |= SecurityProtocolType.Tls12; service = GetVimService(url, hostConnectionInfo.username, hostConnectionInfo.password); var svcRef = new ManagedObjectReference(); svcRef.type = "ServiceInstance"; svcRef.Value = "ServiceInstance"; serviceContent = service.RetrieveServiceContent(svcRef); //Finally connect, we do not need the user session later on. UserSession session = service.Login(serviceContent.sessionManager, hostConnectionInfo.username, hostConnectionInfo.password, null); //Get the host ref by IP or by host name. IPAddress address; ManagedObjectReference reference = IPAddress.TryParse(hostConnectionInfo.ipOrHostname, out address) ? service.FindByIp(serviceContent.searchIndex, null, hostConnectionInfo.ipOrHostname, false) : service.FindByDnsName(serviceContent.searchIndex, null, Dns.GetHostEntry(hostConnectionInfo.ipOrHostname).HostName, false); var systemInfo = GetPropertyContent(service, serviceContent, "HostSystem", "hardware.systemInfo", reference)[0].propSet[0].val as HostSystemInfo; sysinfo.system = systemInfo.vendor + " " + systemInfo.model; sysinfo.os = GetPropertyContent(service, serviceContent, "HostSystem", "summary.config.product.fullName", reference)[0].propSet[0].val.ToString(); var biosInfo = GetPropertyContent(service, serviceContent, "HostSystem", "hardware.biosInfo", reference)[0].propSet[0].val as HostBIOSInfo; sysinfo.bios = biosInfo.vendor + " " + biosInfo.biosVersion; var cpuPkgs = GetPropertyContent(service, serviceContent, "HostSystem", "hardware.cpuPkg", reference)[0].propSet[0].val as HostCpuPackage[]; var cpuDict = new SortedDictionary <string, int>(); foreach (var cpuPkg in cpuPkgs) { string[] candidateArr = cpuPkg.description.Split(new string[] { " " }, StringSplitOptions.RemoveEmptyEntries); string candidate = string.Join(" ", candidateArr); if (cpuDict.ContainsKey(candidate)) { ++cpuDict[candidate]; } else { cpuDict.Add(candidate, 1); } } sysinfo.processors = ComponentDictToString(cpuDict); var cpuInfo = GetPropertyContent(service, serviceContent, "HostSystem", "hardware.cpuInfo", reference)[0].propSet[0].val as HostCpuInfo; sysinfo.numCpuCores = cpuInfo.numCpuCores; sysinfo.numCpuThreads = cpuInfo.numCpuThreads; long memorySize = (long)GetPropertyContent(service, serviceContent, "HostSystem", "hardware.memorySize", reference)[0].propSet[0].val; sysinfo.memoryInGB = Convert.ToInt32(Math.Round(Convert.ToDouble(memorySize) / (1024 * 1024 * 1024), MidpointRounding.AwayFromZero)); //---- //First ask the childentity from the rootfolder (datacenter) ObjectContent[] oCont = GetPropertyContent(service, serviceContent, "Folder", "childEntity", serviceContent.rootFolder); ManagedObjectReference datacenter = (oCont[0].propSet[0].val as ManagedObjectReference[])[0]; //Then ask the datastore from the datacenter var datastoreRefs = GetPropertyContent(service, serviceContent, "Datacenter", "datastore", datacenter)[0].propSet[0].val as ManagedObjectReference[]; var hostMultipathInfo = GetPropertyContent(service, serviceContent, "HostSystem", "config.storageDevice.multipathInfo", reference)[0].propSet[0].val as HostMultipathInfo; var scsiLuns = GetPropertyContent(service, serviceContent, "HostSystem", "config.storageDevice.scsiLun", reference)[0].propSet[0].val as ScsiLun[]; string[] datastoreArr = new string[datastoreRefs.Length]; for (int i = 0; i != datastoreRefs.Length; i++) { var candidate = datastoreRefs[i]; var dsInfo = GetPropertyContent(service, serviceContent, "Datastore", "info", candidate)[0].propSet[0].val; string dataStoreName = null, diskName = null; if (dsInfo is VmfsDatastoreInfo) { dataStoreName = (dsInfo as VmfsDatastoreInfo).name; diskName = (dsInfo as VmfsDatastoreInfo).vmfs.extent[0].diskName; } else if (dsInfo is NasDatastoreInfo) { dataStoreName = (dsInfo as NasDatastoreInfo).name; } if (diskName == null) { diskName = "unknown"; } else { foreach (ScsiLun lun in scsiLuns) { if (lun.canonicalName == diskName) { diskName = lun.displayName; break; } } } if (dataStoreName == null) { dataStoreName = "Unknown"; } datastoreArr[i] = dataStoreName + " disk " + diskName; } sysinfo.datastores = string.Join("\t", datastoreArr); //Then ask the vm folder from the datacenter var vmFolder = GetPropertyContent(service, serviceContent, "Datacenter", "vmFolder", datacenter)[0].propSet[0].val as ManagedObjectReference; //finally get the list of the managed object from the vms. var vmRefs = GetPropertyContent(service, serviceContent, "Folder", "childEntity", vmFolder)[0].propSet[0].val as ManagedObjectReference[]; var vDiskPathsHs = new HashSet <string>(); foreach (var vmRef in vmRefs) { foreach (var dev in (GetPropertyContent(service, serviceContent, "VirtualMachine", "config.hardware", vmRef)[0].propSet[0].val as VirtualHardware).device) { if (dev is VirtualDisk) { if (dev.backing is VirtualDiskFlatVer2BackingInfo) { vDiskPathsHs.Add((dev.backing as VirtualDiskFlatVer2BackingInfo).fileName); } else if (dev.backing is VirtualDiskFlatVer1BackingInfo) { vDiskPathsHs.Add((dev.backing as VirtualDiskFlatVer1BackingInfo).fileName); } } } } sysinfo.vDiskPaths = string.Join("\t", vDiskPathsHs); //--- var physicalNics = GetPropertyContent(service, serviceContent, "HostSystem", "config.network.pnic", reference)[0].propSet[0].val as PhysicalNic[]; string[] pNicsArr = new string[physicalNics.Length]; for (int i = 0; i != physicalNics.Length; i++) { var candidate = physicalNics[i]; pNicsArr[i] = candidate.device + " " + candidate.driver + " driver (" + (candidate.linkSpeed == null ? "not connected)" : "connected)"); } sysinfo.nics = string.Join("\t", pNicsArr); var ipmiPropset = GetPropertyContent(service, serviceContent, "HostSystem", "config.ipmi", reference)[0].propSet; sysinfo.bmcIp = ipmiPropset == null ? "Unable to detect" : ipmiPropset[0].val.ToString(); return(sysinfo); } catch (Exception ex) { //Let IIS handle the errors, but using own logging. Loggers.Log(Level.Error, "Failed retrieving vhost system info", ex, new object[] { hostConnectionInfo }); throw; } }