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>
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

Show:
© 2014 Microsoft