다음을 통해 공유


How to Query for All Monitoring Objects in an Error State

Applies To: Operations Manager 2007 R2, Operations Manager 2007 SP1, System Center Operations Manager 2007

A MonitoringObject represents an instance of a MonitoringClass 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.ObjectModel;
using Microsoft.EnterpriseManagement;
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.
            MonitoringClassCriteria classCriteria =
                new MonitoringClassCriteria("Name = 'Microsoft.Windows.Server.2003.Computer'");

            // There should only be one item in the monitoringClasses collection.
            ReadOnlyCollection<MonitoringClass> monitoringClasses =
                mg.GetMonitoringClasses(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...");
            ReadOnlyCollection<MonitoringObject> objects =
                mg.GetMonitoringObjects(objectCriteria);

            // 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);
            }
        }
    }
}