Skip to content

Instantly share code, notes, and snippets.

@mazocode
Created February 10, 2018 20:16
Show Gist options
  • Save mazocode/d6bd66a31a640e4e3e99b8f3418f81bf to your computer and use it in GitHub Desktop.
Save mazocode/d6bd66a31a640e4e3e99b8f3418f81bf to your computer and use it in GitHub Desktop.
Get performance counters for hyper-v VMs
public static ManagementObject GetObject(ManagementScope scope, string serviceName)
{
return GetObjects(scope, serviceName).FirstOrDefault();
}
public static IEnumerable<ManagementObject> GetObjects(ManagementScope scope, string serviceName)
{
return new ManagementClass(scope, new ManagementPath(serviceName), null)
.GetInstances()
.OfType<ManagementObject>();
}
void Main()
{
ManagementScope scope = new ManagementScope(@"root\virtualization\V2", null);
ManagementObject vsms = GetObject(scope, "Msvm_VirtualSystemManagementService");
using (var vmSearcher = new ManagementObjectSearcher(@"\root\virtualization\V2",
@"SELECT * FROM Msvm_ComputerSystem WHERE Description='Microsoft Virtual Machine'"))
{
using (var vmCollection = vmSearcher.Get())
{
// loop through the machines
foreach (ManagementObject vm in vmCollection)
{
var vmid = (string)vm["Name"];
var hostname = (string)vm["ElementName"];
Console.WriteLine();
Console.WriteLine("Hostname: " + hostname);
var settings = vm.GetRelated("Msvm_VirtualSystemSettingData", "Msvm_SettingsDefineState",
null, null, "SettingData", "ManagedElement", false, null);
foreach (ManagementObject setting in settings)
{
ManagementBaseObject inParams = vsms.GetMethodParameters("GetSummaryInformation");
inParams["SettingData"] = new string[] { setting.Path.Path };
inParams["RequestedInformation"] = new UInt32[] { 101, 102, 103, 105, 112,6 };
ManagementBaseObject outParams = (ManagementBaseObject)vsms.InvokeMethod("GetSummaryInformation", inParams, null);
ManagementBaseObject[] summaryInformationArray = (ManagementBaseObject[])outParams["SummaryInformation"];
foreach (ManagementBaseObject summaryInformation in summaryInformationArray)
{
Console.WriteLine("Processor Load: " + summaryInformation.GetPropertyValue("ProcessorLoad"));
var pHist = (UInt16[])summaryInformation.GetPropertyValue("ProcessorLoadHistory");
var cpuUsagePct = pHist == null ? 0 : Convert.ToInt16(pHist.Select(t => (Int32)t).Take(60).Sum() / (pHist.Count() > 60 ? (Int32)60 : pHist.Count()));
Console.WriteLine("Proc History: " + cpuUsagePct);
var memUsage = Convert.ToInt64(summaryInformation.GetPropertyValue("MemoryUsage"));
Console.WriteLine("Memory Usage: " + memUsage + " " + memUsage.GetType());
var memAvail = Convert.ToInt64(summaryInformation.GetPropertyValue("MemoryAvailable"));
Console.WriteLine("Memory memAvail: " + memAvail + " " + memAvail.GetType());
var imageBytes = (byte[])summaryInformation.GetPropertyValue("ThumbnailImage");
Console.WriteLine("Thumbnail: "+imageBytes);
var uptime = summaryInformation.GetPropertyValue("Uptime");
Console.WriteLine("Uptime: " + uptime + " " + uptime.GetType());
//Dump(summaryInformation);
}
}
}
}
}
}
// Define other methods and classes here
void Dump(ManagementBaseObject obj)
{
if (obj == null)
{
Console.WriteLine("(null)");
return;
}
Console.WriteLine("");
//Console.WriteLine("{0} —————–", obj.Path.Path);
foreach (var prop in obj.Properties)
{
switch (prop.Type)
{
case CimType.Boolean:
case CimType.Char16:
case CimType.Real32:
case CimType.Real64:
case CimType.SInt16:
case CimType.SInt32:
case CimType.SInt64:
case CimType.SInt8:
case CimType.String:
case CimType.UInt16:
case CimType.UInt32:
case CimType.UInt64:
case CimType.UInt8:
if (prop.IsArray && prop.Value != null)
foreach (var o in AsArray(prop))
Console.WriteLine(" {0} ({1}) = {2}", prop.Name, prop.Type.ToString(), o != null ? o.ToString() : "(null)");
else
Console.WriteLine(" {0} ({1}) = {2}", prop.Name, prop.Type.ToString(), prop.Value != null ? prop.Value.ToString() : "(null)");
break;
case CimType.DateTime:
var date = "(null)";
try
{
date = prop.Value != null ? System.Management.ManagementDateTimeConverter.ToDateTime((string)prop.Value).ToString() : "(null)";
}
catch (Exception ex)
{
date = ex.Message;
}
Console.WriteLine(" {0} ({1}) = {2}", prop.Name, prop.Type.ToString(), date);
break;
case CimType.Object:
Console.WriteLine(" {0} = (object)");
break;
case CimType.Reference:
Console.WriteLine(" {0} = (reference)");
break;
}
}
}
object[] AsArray(PropertyData prop)
{
switch (prop.Type)
{
case CimType.Boolean:
return ((bool[])prop.Value).Cast<object>().ToArray();
case CimType.Char16:
return ((char[])prop.Value).Cast<object>().ToArray();
case CimType.SInt16:
return ((Int16[])prop.Value).Cast<object>().ToArray();
case CimType.SInt32:
return ((Int32[])prop.Value).Cast<object>().ToArray();
case CimType.SInt64:
return ((Int64[])prop.Value).Cast<object>().ToArray();
case CimType.SInt8:
return ((byte[])prop.Value).Cast<object>().ToArray();
case CimType.String:
return ((string[])prop.Value).Cast<object>().ToArray();
case CimType.UInt16:
return ((UInt16[])prop.Value).Cast<object>().ToArray();
case CimType.UInt32:
return ((UInt32[])prop.Value).Cast<object>().ToArray();
case CimType.UInt64:
return ((UInt64[])prop.Value).Cast<object>().ToArray();
case CimType.UInt8:
return ((byte[])prop.Value).Cast<object>().ToArray();
case CimType.DateTime:
return ((string[])prop.Value).Cast<object>().ToArray();
case CimType.Object:
return ((object[])prop.Value).Cast<object>().ToArray();
default:
return new object[0];
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment