我是WMI Classes的新手,我想使用C#代码中的WMI类读取Hyper-V上不同虚拟机的CPU,RAM和磁盘利用率。
CPU利用率:可以使用属性ProcessorLoad和ProcessorLoadHistory来获取它
ManagementBaseObject outParams = virtualSystemService.InvokeMethod("GetSummaryInformation", inParams, null);
ManagementBaseObject[] summaryInformationArray = (ManagementBaseObject[])outParams["SummaryInformation"];
RAM利用率:无法获得动态和静态分配的当前RAM利用率的准确值。 'Msvm_MemorySettingData'类的VirtualQuantity和Reservation属性给出了动态RAM时的启动RAM值和静态RAM时分配的RAM的最小值。
HardDisk利用率:无法获得每个虚拟机的硬盘利用率。
我的代码是:
public static ManagementObject GetTargetComputer(string vmElementName, ManagementScope scope)
{
string query = string.Format("select * from Msvm_ComputerSystem Where ElementName = '{0}'", vmElementName);
ManagementObjectSearcher searcher = new ManagementObjectSearcher(scope, new ObjectQuery(query));
ManagementObjectCollection computers = searcher.Get();
ManagementObject computer = null;
foreach (ManagementObject instance in computers)
{
computer = instance;
break;
}
return computer;
}
void GetSummaryInformation(ManagementObject virtualSystemSettings, UInt32[] requestedInformation, ManagementObject virtualSystemService)
{
ManagementBaseObject inParams = virtualSystemService.GetMethodParameters("GetSummaryInformation");
string[] settingPaths = new string[1];
settingPaths[0] = virtualSystemSettings.Path.Path;
inParams["SettingData"] = settingPaths;
inParams["RequestedInformation"] = requestedInformation;
ManagementBaseObject outParams = virtualSystemService.InvokeMethod("GetSummaryInformation", inParams, null);
UInt32 v = (UInt32)outParams["ReturnValue"];
if ((UInt32)outParams["ReturnValue"] == 0)
{
ManagementBaseObject[] summaryInformationArray = (ManagementBaseObject[])outParams["SummaryInformation"];
foreach (ManagementBaseObject summaryInformation in summaryInformationArray)
{
foreach (UInt32 requested in requestedInformation)
{
string Name = summaryInformation["Name"].ToString();
switch (requested)//103, 106, 109, 104, 3, 4, 101, 105
{
case 103:
string MemoryUsage = summaryInformation["MemoryUsage"].ToString();
break;
case 112:
Int32 MemoryAvailable = (Int32)summaryInformation["MemoryAvailable"];
break;
case 113:
Int32 AvailableMemoryBuffer = (Int32)summaryInformation["AvailableMemoryBuffer"];
break;
}
}
}
}
else
{
Console.WriteLine("Failed to retrieve virtual system summary information");
}
inParams.Dispose();
outParams.Dispose();
}
ManagementObject virtualSystem = GetTargetComputer(vm["ElementName"].ToString(), Scope);
ManagementObjectCollection virtualSystemSettings = virtualSystem.GetRelated
(
"Msvm_VirtualSystemSettingData",
"Msvm_SettingsDefineState",
null,
null,
"SettingData",
"ManagedElement",
false,
null
);
ManagementObject virtualSystemSetting = null;
foreach (ManagementObject instance in virtualSystemSettings)
{
virtualSystemSetting = instance;
foreach (ManagementObject memorySettingData in virtualSystemSetting.GetRelated("Msvm_MemorySettingData"))
{
string HeartBeat = (string)memorySettingData["Heartbeat"];
string MemoryUsage = (string)memorySettingData["MemoryUsage"];
string VirtualQuantity = (string)memorySettingData["VirtualQuantity"];
string Limit = (string)memorySettingData["Limit"];
}
}
UInt32[] requestedInfo = { 103, 112, 113 };
GetSummaryInformation(virtualSystemSetting, requestedInfo, virtualSystemService);
是否可以使用WMI类获取Hyper-V中每个虚拟机的当前RAM利用率和硬盘利用率?
答案 0 :(得分:0)
要查找RAM利用率,您可能需要查看以下“Hyper-V动态内存”。例如:
https://code.msdn.microsoft.com/windowsdesktop/Hyper-V-dynamic-memory-9b0b1d05
它还可以为您提供有关如何获取您正在寻找的其他信息的见解。