internal Dictionary <string, HostSystem> GetHostCached() { lock (_HVHostsLock) { foreach (HostSystem host in _HVHosts.Values) { if (!host.Loaded) { foreach (ManagementObject r in (new ManagementObjectSearcher(host.Scopes["CIMV2"], new ObjectQuery("SELECT caption, version FROM Win32_OperatingSystem"))).Get()) { string osver = r.GetPropertyValue("Caption").ToString(); osver = osver.Replace("Microsoft ", ""); osver = osver.Replace(" Evaluation", ""); host.Version = osver; host.Build = r.GetPropertyValue("Version").ToString(); } foreach (ManagementObject r in (new ManagementObjectSearcher(host.Scopes["CIMV2"], new ObjectQuery("SELECT uuid FROM Win32_ComputerSystemProduct"))).Get()) { host.Id = r.GetPropertyValue("uuid").ToString(); } foreach (ManagementObject r in (new ManagementObjectSearcher(host.Scopes["CIMV2"], new ObjectQuery("SELECT numberofprocessors, numberoflogicalprocessors, model, manufacturer FROM Win32_ComputerSystem"))).Get()) { host.Hardware.SystemInfo.Vendor = r.GetPropertyValue("Manufacturer").ToString(); host.Hardware.SystemInfo.Model = r.GetPropertyValue("Model").ToString(); host.Hardware.CpuInfo.NumCpuPackages = Convert.ToInt32(r.GetPropertyValue("NumberOfProcessors")); host.Hardware.CpuInfo.NumCpuCores = Convert.ToInt32(r.GetPropertyValue("NumberOfLogicalProcessors")); } // get vm count foreach (ManagementObject r in (new ManagementObjectSearcher(host.Scopes["CIMV2"], new ObjectQuery("SELECT capacity FROM Win32_PhysicalMemory"))).Get()) { host.Hardware.MemorySize = Convert.ToInt64(r.GetPropertyValue("Capacity")); } foreach (ManagementObject r in (new ManagementObjectSearcher(host.Scopes["Virtualization"], new ObjectQuery("SELECT caption, name, elementname FROM Msvm_ComputerSystem WHERE caption='Virtual Machine'"))).Get()) { VirtualMachine vm = new VirtualMachine(); vm.Id = r.GetPropertyValue("Name").ToString(); vm.Name = r.GetPropertyValue("ElementName").ToString(); host.Vm.Add(vm.Id, vm); } // get datastore count // only get information about mounted logical volumes foreach (ManagementObject r in (new ManagementObjectSearcher(host.Scopes["CIMV2"], new ObjectQuery("SELECT * FROM Win32_Volume WHERE drivetype='3'"))).Get()) { if (!r.GetPropertyValue("name").ToString().ToLower().Contains("?")) { Datastore ds = new Datastore(); ds.ID = r.GetPropertyValue("DeviceId").ToString(); ds.Name = r.GetPropertyValue("Name").ToString(); host.Datastore.Add(ds.ID, ds); } } host.Loaded = true; } } } return(_HVHosts); }
internal List <EntityViewBase> FindDatastores() { List <EntityViewBase> FoundEntities = new List <EntityViewBase> { }; foreach (HostSystem host in GetHostCached().Values) { // only get information about mounted logical volumes foreach (ManagementObject r in (new ManagementObjectSearcher(host.Scopes["CIMV2"], new ObjectQuery("SELECT * FROM Win32_Volume WHERE drivetype='3' AND NOT name LIKE '%?%'"))).Get()) { Datastore ds = null; string dsid = r.GetPropertyValue("DeviceId").ToString(); ds = GetDatastoreDiskCached(dsid); bool dsalreadyfound = false; foreach (Datastore entity in FoundEntities) { if (entity.ID == ds.ID) { dsalreadyfound = true; break; } } ds.Name = r.GetPropertyValue("Name").ToString(); long blocksize = 0, capacity = 0, freespace = 0; Int64.TryParse(r.GetPropertyValue("BlockSize").ToString(), out blocksize); Int64.TryParse(r.GetPropertyValue("Capacity").ToString(), out capacity); Int64.TryParse(r.GetPropertyValue("FreeSpace").ToString(), out freespace); ds.BlockSize = blocksize; ds.Capacity = capacity; ds.FreeSpace = freespace; ds.FileSystem = r.GetPropertyValue("FileSystem").ToString(); List <string> owners = new List <string> { }; if (ds.IsClustered) { foreach (HostSystem h in GetHostCached().Values) { owners.Add(h.Name); } } else { owners.Add(host.Name); } ds.Cluster = _ClusterName; ds.Host = CFunctions.StringJoin(";", owners); if (!dsalreadyfound) { FoundEntities.Add(ds); } } } return(FoundEntities); }
internal Datastore GetDatastoreDiskCached(string id) { // this function returns duplicate datastores placed at different mount points Datastore result = null; lock (_DatastoreDiskCacheLock) { if (DatastoreDiskCache.TryGetValue(id, out result)) { return(result); } else { foreach (HostSystem host in GetHostCached().Values) { ManagementObjectCollection volumeinfo = (new ManagementObjectSearcher(host.Scopes["MSStorage"], new ObjectQuery("SELECT * FROM MSFT_Volume"))).Get(); ManagementObjectCollection diskinfo = (new ManagementObjectSearcher(host.Scopes["MSStorage"], new ObjectQuery("SELECT * FROM MSFT_Disk"))).Get(); foreach (ManagementObject vol in volumeinfo) { string volpath = vol.GetPropertyValue("Path").ToString(); string volid = vol.GetPropertyValue("ObjectId").ToString(); volid = volid.Split('"')[1]; volid = volid.Split(':')[0]; if (!DatastoreDiskCache.ContainsKey(volpath)) { foreach (ManagementObject dis in diskinfo) { string disid = dis.GetPropertyValue("ObjectId").ToString(); disid = disid.Split('"')[1]; disid = disid.Split(':')[0]; if (disid.Contains(volid)) { Datastore ds = new Datastore { }; ds.ID = vol.GetPropertyValue("UniqueId").ToString(); ds.Model = dis.GetPropertyValue("Model").ToString().Trim(); ds.Vendor = dis.GetPropertyValue("Manufacturer").ToString().Replace(",", "").Trim(); int bustype = Convert.ToInt32(dis.GetPropertyValue("BusType")); ds.AccessMode = ((DsBusType)bustype).ToString().Replace("BusType", ""); int healthstatus = Convert.ToInt32(dis.GetPropertyValue("HealthStatus")); ds.HealthStatus = (new DsHealthState(healthstatus)).ToString(); ds.IsBoot = Convert.ToBoolean(dis.GetPropertyValue("IsBoot")); ds.IsClustered = Convert.ToBoolean(dis.GetPropertyValue("IsClustered")); ds.IsHighlyAvailable = Convert.ToBoolean(dis.GetPropertyValue("IsHighlyAvailable")); ds.IsReadOnly = Convert.ToBoolean(dis.GetPropertyValue("IsReadOnly")); ds.IsScaleout = Convert.ToBoolean(dis.GetPropertyValue("IsScaleout")); ds.IsSystem = Convert.ToBoolean(dis.GetPropertyValue("IsSystem")); ds.PhysicalSectorSize = Convert.ToInt32(dis.GetPropertyValue("PhysicalSectorSize")); ds.LogicalSectorSize = Convert.ToInt32(dis.GetPropertyValue("LogicalSectorSize")); int provisioningtype = Convert.ToInt32(dis.GetPropertyValue("ProvisioningType")); ds.ProvisioningType = (new DsProvisioningType(provisioningtype)).ToString(); DatastoreDiskCache.Add(volpath, ds); break; } } } } } DatastoreDiskCache.TryGetValue(id, out result); return(result); } } }