Udostępnij za pośrednictwem


How to Display Information About a Monitoring Object

Applies To: System Center 2012 - Operations Manager

[This topic is pre-release documentation and is subject to change in future releases. Blank topics are included as placeholders.]

A monitoring object represents an instance of a management pack class, and a management pack class defines the type of the object. You can use the MonitoringObject class to gather information, such as property values, alerts, health states, and related objects, about each monitoring object. Each monitoring object can be a part of a hierarchy of related objects.

The following code example demonstrates how to display a variety of information about a single MonitoringObject object:

/// <summary> 
/// Gather information about a Monitoring Object.
/// </summary>
using System;
using System.Collections.ObjectModel;
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'");

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

            // Get all instances of computers running Windows Server 2003 in the management group.
            List<MonitoringObject> targets = new List<MonitoringObject>();
            IObjectReader<MonitoringObject> reader = mg.EntityObjects.GetObjectReader<MonitoringObject>(monitoringClasses[0], ObjectQueryOptions.Default);
            targets.AddRange(reader);



            if (targets.Count > 0)
            {
                Console.WriteLine("Monitoring Object name: " + targets[0].DisplayName +
                    Environment.NewLine);

                DisplayHierarchyInformation(targets[0], mg);
                DisplayPerformanceData(targets[0]);
                DisplayPropertyValues(targets[0]);
                DisplayRelationshipInformation(targets[0] , mg);
                DisplayRelatedObjects(targets[0]);
                DisplayMonitoringStateHierarchy(targets[0]);
            }

        }

        //---------------------------------------------------------------------------------
        // Get the monitoring hierarchy and display information about the hierarchy.
        static void DisplayHierarchyInformation(MonitoringObject mObject, ManagementGroup mg)
        {
          
            HierarchyNode<ManagementPackMonitor> hierarchy = mg.Monitoring.GetMonitorHierarchy(mObject);

            Console.WriteLine();
            Console.WriteLine("Monitoring hierarchy: ");
            
            // Parent node information.
            if (hierarchy.ParentNode != null)
            {
                Console.WriteLine("    Parent node: " + hierarchy.ParentNode.Item.DisplayName);
            }
            else { Console.WriteLine("    No parent node."); }

            // Child node information.
            if (hierarchy.TotalChildNodeCount > 0)
            {
                Console.WriteLine("    Child nodes: ");
                foreach (MonitoringHierarchyNode<ManagementPackMonitor> child in hierarchy.ChildNodes)
                {
                    Console.WriteLine("        " + child.Item.DisplayName);
                }
            }
            else { Console.WriteLine("    No child nodes available."); }
        }

        //-----------------------------------------------------------------------------------
        static void DisplayPerformanceData(MonitoringObject mObject)
        {
            Console.WriteLine();
            Console.WriteLine("Performance data information: ");
            foreach (MonitoringPerformanceData perfData in mObject.GetMonitoringPerformanceData())
            {
                Console.WriteLine("Performance counter name: " + perfData.CounterName);
                Console.WriteLine("Last sampled value: " + perfData.LastSampledValue);
            }
        }

        //-----------------------------------------------------------------------------------
        static void DisplayPropertyValues(MonitoringObject mObject)
        {
            Console.WriteLine();
            Console.WriteLine("Property value information: ");
            foreach(ManagementPackProperty property in mObject.GetProperties())
            {
               
                Console.WriteLine("    " + property.Name + " = " + mObject[property].Value.ToString());
            }                 
        }

        //-----------------------------------------------------------------------------------
        static void DisplayRelationshipInformation(MonitoringObject mObject, ManagementGroup mg)
        {
            Console.WriteLine();
            Console.WriteLine("Relationship object information: ");
       
            IList<EnterpriseManagementRelationshipObject<MonitoringObject>> relObjects = mg.EntityObjects.GetRelationshipObjects<MonitoringObject>(mObject.Id, ObjectQueryOptions.Default);

            foreach (EnterpriseManagementRelationshipObject<MonitoringObject> relObject in relObjects)
            {
                ManagementPackRelationship relClass = mg.EntityTypes.GetRelationshipClass(relObject.Id);
                Console.WriteLine("    " + relClass.DisplayName + ": " + relClass.Description
                    );
                Console.WriteLine("        Target: " + relObject.TargetObject.DisplayName);
                Console.WriteLine("        Source: " + relObject.SourceObject.DisplayName);
                Console.WriteLine();
            }

            Console.WriteLine("This object is the source for the following relationships: ");
            IList<EnterpriseManagementRelationshipObject<MonitoringObject>> sourceForRelObjects = mg.EntityObjects.GetRelationshipObjectsWhereSource<MonitoringObject>(mObject.Id, TraversalDepth.Recursive, ObjectQueryOptions.Default);
            foreach (EnterpriseManagementRelationshipObject<MonitoringObject> sourceForRelObject in sourceForRelObjects)
            {
                ManagementPackRelationship relClass = mg.EntityTypes.GetRelationshipClass(sourceForRelObject.Id);
                Console.WriteLine("    " + relClass.DisplayName); 
            }

            Console.WriteLine("This object is the target for the following relationships: ");
            IList<EnterpriseManagementRelationshipObject<MonitoringObject>> targetForRelObjects = mg.EntityObjects.GetRelationshipObjectsWhereTarget<MonitoringObject>(mObject.Id,  ObjectQueryOptions.Default);
          
            foreach (MonitoringRelationshipObject relationshipObj in targetForRelObjects)
            {
                ManagementPackRelationship relClass = mg.EntityTypes.GetRelationshipClass(relationshipObj.Id);
                Console.WriteLine("    " + relClass.DisplayName);
            }
        }

        //-----------------------------------------------------------------------------------
        static void DisplayRelatedObjects(MonitoringObject mObject)
        {
            Console.WriteLine();
            Console.WriteLine("Related objects: ");
            foreach (MonitoringObject relatedObject in mObject.GetRelatedMonitoringObjects())
            {
                Console.WriteLine("    " + relatedObject.ToString());
            }
        }

        //-----------------------------------------------------------------------------------
        static void DisplayMonitoringStateHierarchy(MonitoringObject mObject)
        {
            Console.WriteLine();
            Console.WriteLine("Monitoring state hierarchy: ");
            HierarchyNode<MonitoringState> state = mObject.GetStateHierarchy();
            
            Console.WriteLine("    Monitor: " + state.Item.MonitorDisplayName +
                " - " + state.Item.HealthState.ToString());
            Console.WriteLine("    Description: " + state.Item.MonitorDescription);

            // Parent node information.
            if (state.ParentNode != null)
            {
                Console.WriteLine("    Parent node: " +
                    state.ParentNode.Item.MonitorDisplayName);
            }
            else { Console.WriteLine("    No parent node."); }

            // Child node information.
            if (state.TotalChildNodeCount > 0)
            {
                Console.WriteLine("    Child nodes: ");
                foreach (MonitoringHierarchyNode<MonitoringState> child in state.ChildNodes)
                {
                    Console.WriteLine("        " + child.Item.MonitorDisplayName +
                        " - " + child.Item.HealthState);
                }
            }
            else { Console.WriteLine("    No child nodes available."); }
        }
    }
}

See Also

Other Resources

Automating Operations Manager Administration