Export (0) Print
Expand All

How to Display Information about a Monitoring Object

System Center

Updated: May 22, 2009

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

A monitoring object represents an instance of a monitoring class, and a monitoring class defines the type of the object. You can use the MonitoringObject class to gather information about each monitoring object such as property values, alerts, health states, and related objects. 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 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.
            MonitoringClassCriteria classCriteria =
                new MonitoringClassCriteria("Name = 'Microsoft.Windows.Server.2003.Computer'");

            Console.WriteLine("Querying for data...");
            // There should only be one item in the monitoringClasses collection.
            ReadOnlyCollection<MonitoringClass> monitoringClasses =
                mg.GetMonitoringClasses(classCriteria);

            // Get all instances of computers running Windows Server 2003 in the management group.
            ReadOnlyCollection<MonitoringObject> monitoringObjects =
                mg.GetMonitoringObjects(monitoringClasses[0]);

            if (monitoringObjects.Count > 0)
            {
                Console.WriteLine("Monitoring Object name: " + monitoringObjects[0].DisplayName +
                    Environment.NewLine);
                
                DisplayHierarchyInformation(monitoringObjects[0]);
                DisplayPerformanceData(monitoringObjects[0]);
                DisplayPropertyValues(monitoringObjects[0]);
                DisplayRelationshipInformation(monitoringObjects[0]);
                DisplayRelatedObjects(monitoringObjects[0]);
                DisplayMonitoringStateHierarchy(monitoringObjects[0]);
            }

        }

        //---------------------------------------------------------------------------------
        // Get the monitoring hierarchy and display information about the hierarchy.
        static void DisplayHierarchyInformation(MonitoringObject mObject)
        {
            MonitoringHierarchyNode<ManagementPackMonitor> hierarchy = mObject.GetMonitorHierarchy();
            
            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(MonitoringClassProperty property in mObject.GetMonitoringProperties())
            {
                Console.WriteLine("    " + property.Name + " = " + mObject.GetMonitoringPropertyValue(property));
            }                 
        }

        //-----------------------------------------------------------------------------------
        static void DisplayRelationshipInformation(MonitoringObject mObject)
        {
            Console.WriteLine();
            Console.WriteLine("Relationship object information: ");
            foreach (MonitoringRelationshipObject relationshipObj in mObject.GetMonitoringRelationshipObjects())
            {
                MonitoringRelationshipClass relClass = relationshipObj.GetMonitoringRelationshipClass();
                Console.WriteLine("    " + relClass.DisplayName + ": " + relClass.Description
                    );
                Console.WriteLine("        Target: " + relationshipObj.TargetMonitoringObject.DisplayName);
                Console.WriteLine("        Source: " + relationshipObj.SourceMonitoringObject.DisplayName);
                Console.WriteLine();
            }

            Console.WriteLine("This object is the source for the following relationships: ");
            foreach (MonitoringRelationshipObject relationshipObj in 
                mObject.GetMonitoringRelationshipObjectsWhereSource())
            {
                MonitoringRelationshipClass relClass = relationshipObj.GetMonitoringRelationshipClass();
                Console.WriteLine("    " + relClass.DisplayName); 
            }

            Console.WriteLine("This object is the target for the following relationships: ");
            foreach (MonitoringRelationshipObject relationshipObj in
                mObject.GetMonitoringRelationshipObjectsWhereTarget())
            {
                MonitoringRelationshipClass relClass = relationshipObj.GetMonitoringRelationshipClass();
                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: ");
            MonitoringHierarchyNode<MonitoringState> state = mObject.GetMonitoringStateHierarchy();
            
            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."); }
        }
    }
}

Show:
© 2014 Microsoft