예제 #1
0
 private VmSettings GetVmSettingsCached(string id)
 {
     lock (_VMSettingsCacheLock)
     {
         VmSettings result = null;
         if (VMSettingsCache.TryGetValue(id, out result))
         {
             return(result);
         }
         else
         {
             foreach (HostSystem host in GetHostCached().Values)
             {
                 foreach (ManagementObject r in (new ManagementObjectSearcher(host.Scopes["Virtualization"], new ObjectQuery("SELECT * FROM Msvm_VirtualSystemSettingData WHERE NOT instanceid LIKE '%Definition%'"))).Get())
                 {
                     string     _id      = ((r.GetPropertyValue("InstanceId").ToString()).Split('\\')[0]).Replace("Microsoft:", "");
                     VmSettings settings = new VmSettings();
                     settings.Version      = Convert.ToDouble(r.GetPropertyValue("Version"));
                     settings.CreationTime = r.GetPropertyValue("CreationTime").ToString();
                     if (!VMSettingsCache.ContainsKey(_id))
                     {
                         VMSettingsCache.Add(_id, settings);
                     }
                 }
             }
             if (VMSettingsCache.TryGetValue(id, out result))
             {
                 return(result);
             }
         }
         return(result);
     }
 }
예제 #2
0
        // need to improve caching logic and processing speed
        internal List <EntityViewBase> FindVirtualMachines()
        {
            List <EntityViewBase> FoundEntities = new List <EntityViewBase> {
            };

            foreach (HostSystem host in GetHostCached().Values)
            {
                foreach (ManagementObject r in (new ManagementObjectSearcher(host.Scopes["Virtualization"], new ObjectQuery("SELECT * FROM Msvm_ComputerSystem WHERE caption='Virtual Machine'"))).Get())
                {
                    // get related vm object or initialize it
                    VirtualMachine vm   = null;
                    string         vmid = r.GetPropertyValue("Name").ToString();
                    host.Vm.TryGetValue(vmid, out vm);
                    if (vm == null)
                    {
                        vm = new VirtualMachine(); vm.Name = r.GetPropertyValue("ElementName").ToString(); vm.Id = vmid; host.Vm.Add(vmid, vm);
                    }

                    vm.Host  = host.Name;
                    vm.State = Convert.ToInt32(r.GetPropertyValue("EnabledState"));


                    // possible KVP dictionary values are (only works when powered on)
                    // FullyQualifiedDomainName, IntegrationServicesVersion, NetworkAddressIPv4, NetworkAddressIPv6
                    // OSBuildNumber, OSDistributionData, OSDistributionName, OSKernelVersion, OSMajorVersion, OSMinorVersion, OSName, OSPlatformId, OSVersion
                    // ProcessorArchitecture
                    ManagementObjectCollection  vmGuestKVPCollection = r.GetRelated("Msvm_KvpExchangeComponent"); // have to use select * for above query as otherwise getrelated does not work
                    Dictionary <string, string> vmGuestKeyValues     = new Dictionary <string, string> {
                    };
                    foreach (ManagementObject element in vmGuestKVPCollection)
                    {
                        string[] xmlitems = element.Properties["GuestIntrinsicExchangeItems"].Value as string[];
                        vmGuestKeyValues = GetWmiXmlDictionary(xmlitems);
                    }

                    VmSettings guestsettings = GetVmSettingsCached(vm.Id);
                    vm.Version = guestsettings.Version.ToString();

                    if (vmGuestKeyValues.Count > 0)
                    {
                        System.Net.IPAddress ipaddr = null;
                        System.Net.IPAddress.TryParse(vmGuestKeyValues["NetworkAddressIPv4"].Split(',')[0], out ipaddr);
                        vm.Guest.IP           = ipaddr;
                        vm.Guest.ToolsVersion = vmGuestKeyValues["IntegrationServicesVersion"];
                        vm.Guest.OS           = vmGuestKeyValues["OSName"] + " " + vmGuestKeyValues["OSVersion"];
                        vm.Guest.Name         = vmGuestKeyValues["FullyQualifiedDomainName"];
                    }
                    vm.Hardware.MemoryMB          = GetVmMemoryCached(vm.Id);
                    vm.Hardware.NumCoresPerSocket = 1;
                    vm.Hardware.NumCPU            = GetVmCpuCached(vm.Id);

                    vm.Storage = GetVmDiskInfo(vm.Id);
                    vm.Disks   = GetVmDiskCached(vm.Id);

                    if (host.IsClusterMember)
                    {
                        vm.Cluster = _ClusterName;
                    }

                    FoundEntities.Add(vm);
                }
            }
            return(FoundEntities);
        }