How to Query for All Monitoring Objects in an Error State

Updated: January 31, 2012

Applies To: System Center 2012 - Operations Manager

A MonitoringObject represents an instance of a that is defined in a management pack. You can query Operations Manager to obtain the existing monitoring objects in a management group. You can query for Monitoring Objects by defining criteria in the MonitoringObjectCriteria class constructor. The criteria syntax is defined in Criteria Expression Syntax.

The following code queries for all computers in an error state that are running Windows Server 2003:

/// <summary> 
/// Query for all computers in an error state that are running Windows Server 2003.
/// </summary>
using System;
using System.Collections.Generic;
using Microsoft.EnterpriseManagement;
using Microsoft.EnterpriseManagement.Common;
using Microsoft.EnterpriseManagement.Configuration;
using Microsoft.EnterpriseManagement.Monitoring;

namespace SDKSamples
{
	class Program
	{
		static void Main(string[] args)
		{
			ManagementGroup mg = new ManagementGroup("localhost");

			// The criteria specifies that you want to collect
			// computers running Windows Server 2003.
			ManagementPackClassCriteria classCriteria =
				new ManagementPackClassCriteria("Name = 'Microsoft.Windows.Server.2003.Computer'");

			// There should only be one item in the monitoringClasses collection.
			IList<ManagementPackClass> monitoringClasses =
				mg.EntityTypes.GetClasses(classCriteria);

			// The criteria specifies that you want to collect
			// all the monitoring objects (computers running Windows Server 2003) in an error state.
			MonitoringObjectCriteria objectCriteria =
				new MonitoringObjectCriteria(
				"HealthState = 3", monitoringClasses[0]);

			Console.WriteLine("Querying for data...");
			List<MonitoringObject> objects = new List<MonitoringObject>();
			IObjectReader<MonitoringObject> reader = mg.EntityObjects.GetObjectReader<MonitoringObject>(objectCriteria, ObjectQueryOptions.Default);
			objects.AddRange(reader);

			// Display information about each object.
			foreach (MonitoringObject monitoringObject in objects)
			{
				Console.WriteLine("Object name: " + monitoringObject.DisplayName);
				Console.WriteLine("Health State: " + monitoringObject.HealthState.ToString());
				Console.WriteLine("Last Modified: " + monitoringObject.LastModified +
					Environment.NewLine);
		}
	}
}
}

See Also