Was this page helpful?
Your feedback about this content is important. Let us know what you think.
Additional feedback?
1500 characters remaining
Export (0) Print
Expand All

How to Create a Unit Monitor

System Center

Updated: May 22, 2009

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

Unit monitors are used to monitor specific counters, events, scripts, and services. Unit monitors can be rolled up to either dependency or aggregate rollup monitors. You have the option to set the monitor to generate an alert.

For an example of how to create a unit monitor to monitor an event log see, How to Create an Event Log Unit Monitor.

The following code example shows how to create a unit monitor that monitors the state of a service.

' Creates a unit monitor.
Imports System
Imports System.Collections.Generic
Imports System.Collections.ObjectModel
Imports System.Text
Imports Microsoft.EnterpriseManagement
Imports Microsoft.EnterpriseManagement.Administration
Imports Microsoft.EnterpriseManagement.Common
Imports Microsoft.EnterpriseManagement.Configuration
Imports Microsoft.EnterpriseManagement.Monitoring


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

            Dim mg As ManagementGroup
            Dim mp As ManagementPack
            Dim monitoringClass As MonitoringClass
            Dim monitoringClassCriteria As MonitoringClassCriteria
            Dim serviceMonitor As ManagementPackUnitMonitor
            Dim serviceMonitorType As ManagementPackUnitMonitorType

            mg = New ManagementGroup("localhost")

            mp = mg.GetManagementPacks("SampleManagementPack")(0)

            monitoringClassCriteria = New MonitoringClassCriteria( _
                "DisplayName='Windows Server 2003 Operating System'")
            monitoringClass = mg.GetMonitoringClasses(monitoringClassCriteria)(0)

            serviceMonitorType = mg.GetUnitMonitorTypes( _
                "Microsoft.Windows.CheckNTServiceStateMonitorType")(0)
            serviceMonitor = New ManagementPackUnitMonitor( _
                mp, "SampleServiceMonitor", ManagementPackAccessibility.Internal)

            serviceMonitor.DisplayName = "Sample Service Monitor"
            serviceMonitor.TypeID = serviceMonitorType
            serviceMonitor.Target = monitoringClass

            ConfigureAlertSettings(serviceMonitor, serviceMonitorType, mp)
            ConfigureHealthStates(serviceMonitor, serviceMonitorType)
            SpecifyMonitorConfiguration(serviceMonitor)
            SpecifyParentMonitor(serviceMonitor, mg)

            mp.Verify()

            ' Save the changes into the management pack.
            mp.AcceptChanges()
        End Function

        ' -------------------------------------------------------------------
        Private Shared Sub SpecifyParentMonitor( _
            ByVal serviceMonitor As ManagementPackUnitMonitor, _
            ByVal mg As ManagementGroup)

            Dim parentMonitor As ManagementPackAggregateMonitor
            Dim monitorCriteria As MonitorCriteria

            monitorCriteria = New MonitorCriteria("Name='System.Health.AvailabilityState'")
            parentMonitor = CType(mg.GetMonitors(monitorCriteria)(0), ManagementPackAggregateMonitor)
            serviceMonitor.ParentMonitorID = parentMonitor
        End Sub

        ' -------------------------------------------------------------------
        Private Shared Sub SpecifyMonitorConfiguration( _
            ByVal serviceMonitor As ManagementPackUnitMonitor)

            Dim monitorConfig As String

            monitorConfig = "<ComputerName>$Target/Host/Property[Type=""Windows!Microsoft.Windows.Computer""]/NetworkName$</ComputerName><ServiceName>Alerter</ServiceName>"

            serviceMonitor.Configuration = monitorConfig
        End Sub

        ' -------------------------------------------------------------------
        Private Shared Sub ConfigureHealthStates( _
            ByVal serviceMonitor As ManagementPackUnitMonitor, _
            ByVal serviceMonitorType As ManagementPackUnitMonitorType)

            Dim healthyState As ManagementPackUnitMonitorOperationalState
            Dim errorState As ManagementPackUnitMonitorOperationalState

            healthyState = New ManagementPackUnitMonitorOperationalState(serviceMonitor, "Success")
            errorState = New ManagementPackUnitMonitorOperationalState(serviceMonitor, "Error")

            healthyState.HealthState = HealthState.Success
            healthyState.MonitorTypeStateID = "Running"

            errorState.HealthState = HealthState.Error
            errorState.MonitorTypeStateID = "NotRunning"

            serviceMonitor.OperationalStateCollection.Add(healthyState)
            serviceMonitor.OperationalStateCollection.Add(errorState)
        End Sub

        ' -------------------------------------------------------------------
        Private Shared Sub ConfigureAlertSettings( _
            ByVal serviceMonitor As ManagementPackUnitMonitor, _
            ByVal unitMonitorType As ManagementPackUnitMonitorType, _
            ByVal mp As ManagementPack)

            serviceMonitor.AlertSettings = New ManagementPackMonitorAlertSettings()
            serviceMonitor.AlertSettings.AlertOnState = HealthState.Error
            serviceMonitor.AlertSettings.AutoResolve = True
            serviceMonitor.AlertSettings.AlertPriority = ManagementPackWorkflowPriority.Normal
            serviceMonitor.AlertSettings.AlertSeverity = ManagementPackAlertSeverity.Error
            serviceMonitor.AlertSettings.AlertParameter1 = _
                "$Target/Host/Property[Type=""Windows!Microsoft.Windows.Computer""]/NetworkName$" ' this points to the computer name            

            Dim alertMessage As ManagementPackStringResource

            alertMessage = New ManagementPackStringResource(mp, "SampleAlertMessage")

            alertMessage.DisplayName = "The Alerter service is stopped"
            alertMessage.Description = "The Alerter service is stopped on computer {0}."

            serviceMonitor.AlertSettings.AlertMessage = alertMessage
        End Sub
    End Class
End Namespace

Show:
© 2015 Microsoft