QueryMetricCollectionEnabledForVirtualMachine( string name) { ManagementScope scope = new ManagementScope(@"root\virtualization\v2"); // // Retrieve the Msvm_ComputerSystem and the CIM_BaseMetricDefinition derived instance // that we want to query the MetricCollectionEnabled state for. // SelectQuery metricDefForMeQuery; const string metricDefinitionName = "Aggregated Average CPU Utilization"; using (ManagementObject vm = WmiUtilities.GetVirtualMachine(name, scope)) using (ManagementObject metricDefinition = MetricUtilities.GetMetricDefinition(metricDefinitionName, scope)) { // // Build the WQL query used to retrieve the Msvm_MetricDefForME association between // these two objects. It is the one that contains the MetricCollectionEnabled // property. // string metricDefForMeQueryWql = string.Format(CultureInfo.InvariantCulture, "SELECT * FROM Msvm_MetricDefForME WHERE Antecedent=\"{0}\" AND Dependent=\"{1}\"", WmiUtilities.EscapeObjectPath(vm.Path.Path), WmiUtilities.EscapeObjectPath(metricDefinition.Path.Path)); metricDefForMeQuery = new SelectQuery(metricDefForMeQueryWql); } using (ManagementObjectSearcher metricDefForMeSearcher = new ManagementObjectSearcher(scope, metricDefForMeQuery)) using (ManagementObjectCollection metricDefForMeCollection = metricDefForMeSearcher.Get()) { // // There will always only be one Msvm_MetricDefForME for a given managed element and // metric definition. // if (metricDefForMeCollection.Count != 1) { throw new ManagementException(string.Format(CultureInfo.CurrentCulture, "A single Msvm_MetricDefForME could not be found for virtual machine " + "\"{0}\" and metric definition \"{1}\"", name, metricDefinitionName)); } foreach (ManagementObject metricDefForMe in metricDefForMeCollection) { using (metricDefForMe) { string metricCollectionState = Enum.Parse(typeof(MetricEnabledState), metricDefForMe["MetricCollectionEnabled"].ToString()).ToString(); Console.WriteLine("MetricCollectionEnabled = {0}", metricCollectionState); } } } }
ConfigureMetricsFlushInterval( TimeSpan interval) { ManagementScope scope = new ManagementScope(@"root\virtualization\v2"); // // Create an instance of the Msvm_MetricServiceSettingData class and set the specified // metrics flush interval. Note that the TimeSpan must be converted to a DMTF time // interval first. // string dmtfTimeInterval = ManagementDateTimeConverter.ToDmtfTimeInterval(interval); string serviceSettingDataEmbedded; using (ManagementClass serviceSettingDataClass = new ManagementClass( "Msvm_MetricServiceSettingData")) { serviceSettingDataClass.Scope = scope; using (ManagementObject serviceSettingData = serviceSettingDataClass.CreateInstance()) { serviceSettingData["MetricsFlushInterval"] = dmtfTimeInterval; serviceSettingDataEmbedded = serviceSettingData.GetText(TextFormat.WmiDtd20); } } // // Call the Msvm_MetricService::ModifyServiceSettings method. Note that the // Msvm_MetricServiceSettingData instance must be passed as an embedded instance. // using (ManagementObject metricService = MetricUtilities.GetMetricService(scope)) { using (ManagementBaseObject inParams = metricService.GetMethodParameters("ModifyServiceSettings")) { inParams["SettingData"] = serviceSettingDataEmbedded; using (ManagementBaseObject outParams = metricService.InvokeMethod("ModifyServiceSettings", inParams, null)) { WmiUtilities.ValidateOutput(outParams, scope); Console.WriteLine(string.Format(CultureInfo.CurrentCulture, "The MetricsFlushInterval was successfully configured to interval \"{0}\".", interval.ToString())); } } } }
DisableMetricsForNetworkAdapter( string macAddress, string ipAddress) { ManagementScope scope = new ManagementScope(@"root\virtualization\v2"); // // 1. Retrieve the Msvm_SyntheticEthernetPortSettingData associated with the specified // MAC address. // 2. Retrieve the associated Msvm_EthernetPortAllocationSettingData, which // corresponds to the connection. // 3. Retrieve the associated Msvm_EthernetSwitchPortAclSettingData with the specified // IP address. This is the object that metrics are associated with. // string portAclSettingDataPath; using (ManagementObject portSettingData = MetricUtilities.GetSyntheticEthernetPortSettingData(macAddress, scope)) using (ManagementObject connectionSettingData = MetricUtilities.GetEthernetPortAllocationSettingData(portSettingData, scope)) using (ManagementObject portAclSettingData = MetricUtilities.GetEthernetSwitchPortAclSettingData(connectionSettingData, ipAddress, scope)) { portAclSettingDataPath = portAclSettingData.Path.Path; } // // Retrieve the Msvm_BaseMetricDefinition for the Filtered Incoming Network Traffic. // string metricDefinitionPath; using (ManagementObject metricDefinition = MetricUtilities.GetMetricDefinition("Filtered Incoming Network Traffic", scope)) { metricDefinitionPath = metricDefinition.Path.Path; } // // Call the Msvm_MetricService::ControlMetrics method. // ControlMetrics(portAclSettingDataPath, metricDefinitionPath, MetricOperation.Disable, scope); Console.WriteLine(string.Format(CultureInfo.CurrentCulture, "The metric was successfully disabled for network adapter \"{0}\" (\"{1}\")", macAddress, ipAddress)); }
ControlMetrics( string managedElementPath, string metricDefinitionPath, MetricOperation operation, ManagementScope scope) { using (ManagementObject metricService = MetricUtilities.GetMetricService(scope)) { using (ManagementBaseObject inParams = metricService.GetMethodParameters("ControlMetrics")) { inParams["Subject"] = managedElementPath; inParams["Definition"] = metricDefinitionPath; inParams["MetricCollectionEnabled"] = (uint)operation; using (ManagementBaseObject outParams = metricService.InvokeMethod("ControlMetrics", inParams, null)) { WmiUtilities.ValidateOutput(outParams, scope); } } } }