Partager via


How to Display Information about a Monitoring Object

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."); }
        }
    }
}
' <summary> 
' Gather information about a Monitoring Object.
' </summary>
Imports System
Imports System.Collections.Generic
Imports System.Collections.ObjectModel
Imports Microsoft.EnterpriseManagement
Imports Microsoft.EnterpriseManagement.Configuration
Imports Microsoft.EnterpriseManagement.Common
Imports Microsoft.EnterpriseManagement.Monitoring


Namespace SDKSamples
    Class Program
        Public Overloads Shared Function Main(ByVal args() As String) As Integer

            Dim mg As ManagementGroup = New ManagementGroup("localhost")

            ' The criteria specifies that you want to collect
            ' computers running Windows Server 2003.
            Dim classCriteria As MonitoringClassCriteria = _
                New MonitoringClassCriteria("Name = 'Microsoft.Windows.Server.2003.Computer'")

            Console.WriteLine("Querying for data...")
            ' There should only be one item in the monitoringClasses collection.
            Dim monitoringClasses As ReadOnlyCollection(Of MonitoringClass) = _
                mg.GetMonitoringClasses(classCriteria)

            ' Get all instances of computers running Windows Server 2003 in the management group.
            Dim monitoringObjects As ReadOnlyCollection(Of MonitoringObject) = _
                mg.GetMonitoringObjects(monitoringClasses(0))

            If (monitoringObjects.Count > 0) Then

                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))
            End If

        End Function

        '---------------------------------------------------------------------------------
        ' Get the monitoring hierarchy and display information about the hierarchy.
        Public Shared Sub DisplayHierarchyInformation(ByVal mObject As MonitoringObject)

            Dim hierarchy As MonitoringHierarchyNode(Of ManagementPackMonitor) = _
            mObject.GetMonitorHierarchy()

            Console.WriteLine()
            Console.WriteLine("Monitoring hierarchy: ")

            ' Parent node information.
            If Not (hierarchy.ParentNode Is Nothing) Then

                Console.WriteLine("    Parent node: " + hierarchy.ParentNode.Item.DisplayName)

            Else
                Console.WriteLine("    No parent node.")
            End If

            ' Child node information.
            If (hierarchy.TotalChildNodeCount > 0) Then

                Console.WriteLine("    Child nodes: ")

                For Each child As MonitoringHierarchyNode(Of ManagementPackMonitor) _
                    In hierarchy.ChildNodes

                    Console.WriteLine("        " & child.Item.DisplayName)
                Next
            Else
                Console.WriteLine("    No child nodes available.")
            End If
        End Sub

        '-----------------------------------------------------------------------------------
        Public Shared Sub DisplayPerformanceData(ByVal mObject As MonitoringObject)

            Console.WriteLine()
            Console.WriteLine("Performance data information: ")

            For Each perfData As MonitoringPerformanceData In mObject.GetMonitoringPerformanceData()

                Console.WriteLine("Performance counter name: " & perfData.CounterName)
                Console.WriteLine("Last sampled value: " & perfData.LastSampledValue.ToString())
            Next
        End Sub

        '-----------------------------------------------------------------------------------
        Public Shared Sub DisplayPropertyValues(ByVal mObject As MonitoringObject)

            Console.WriteLine()
            Console.WriteLine("Property value information: ")
            For Each prop As MonitoringClassProperty In mObject.GetMonitoringProperties()

                Console.WriteLine("    " & prop.Name & " = " & _
                    mObject.GetMonitoringPropertyValue(prop))
            Next
        End Sub

        '-----------------------------------------------------------------------------------
        Public Shared Sub DisplayRelationshipInformation(ByVal mObject As MonitoringObject)

            Console.WriteLine()
            Console.WriteLine("Relationship object information: ")
            For Each relationshipObj As MonitoringRelationshipObject In mObject.GetMonitoringRelationshipObjects()

                Dim relClass As MonitoringRelationshipClass = _
                    relationshipObj.GetMonitoringRelationshipClass()
                Console.WriteLine("    " & relClass.DisplayName & ": " & relClass.Description)
                Console.WriteLine("        Target: " & relationshipObj.TargetMonitoringObject.DisplayName)
                Console.WriteLine("        Source: " & relationshipObj.SourceMonitoringObject.DisplayName)
                Console.WriteLine()
            Next

            Console.WriteLine("This object is the source for the following relationships: ")
            For Each relationshipObj As MonitoringRelationshipObject In _
                mObject.GetMonitoringRelationshipObjectsWhereSource()

                Dim relClass As MonitoringRelationshipClass = _
                    relationshipObj.GetMonitoringRelationshipClass()
                Console.WriteLine("    " & relClass.DisplayName)
            Next

            Console.WriteLine("This object is the target for the following relationships: ")
            For Each relationshipObj As MonitoringRelationshipObject In _
                mObject.GetMonitoringRelationshipObjectsWhereTarget()

                Dim relClass As MonitoringRelationshipClass = relationshipObj.GetMonitoringRelationshipClass()
                Console.WriteLine("    " & relClass.DisplayName)
            Next
        End Sub

        '-----------------------------------------------------------------------------------
        Public Shared Sub DisplayRelatedObjects(ByVal mObject As MonitoringObject)

            Console.WriteLine()
            Console.WriteLine("Related objects: ")
            For Each relatedObject As MonitoringObject In mObject.GetRelatedMonitoringObjects()

                Console.WriteLine("    " & relatedObject.ToString())
            Next
        End Sub

        '-----------------------------------------------------------------------------------
        Public Shared Sub DisplayMonitoringStateHierarchy(ByVal mObject As MonitoringObject)

            Console.WriteLine()
            Console.WriteLine("Monitoring state hierarchy: ")
            Dim state As MonitoringHierarchyNode(Of MonitoringState) = _
                mObject.GetMonitoringStateHierarchy()

            Console.WriteLine("    Monitor: " & state.Item.MonitorDisplayName & _
                " - " & state.Item.HealthState.ToString())
            Console.WriteLine("    Description: " & state.Item.MonitorDescription)

            ' Parent node information.
            If Not (state.ParentNode Is Nothing) Then

                Console.WriteLine("    Parent node: " & _
                    state.ParentNode.Item.MonitorDisplayName)

            Else
                Console.WriteLine("    No parent node.")
            End If

            ' Child node information.
            If (state.TotalChildNodeCount > 0) Then

                Console.WriteLine("    Child nodes: ")
                For Each child As MonitoringHierarchyNode(Of MonitoringState) In state.ChildNodes

                    Console.WriteLine("        " + child.Item.MonitorDisplayName & _
                        " - " + child.Item.HealthState)
                Next

            Else
                Console.WriteLine("    No child nodes available.")
            End If
        End Sub
    End Class
End Namespace