Export (0) Print
Expand All

How to Insert Custom Event and Performance Data

System Center

Updated: May 22, 2009

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

You can use Operations Manager class libraries to programmatically generate and insert custom event and performance data into the Operations Manager database on behalf of specific monitoring objects.

Example

Example 1—Generating Event Data

This example demonstrates how to generate event data and insert it into the Operations Manager database on behalf of the monitoring object for an instance of SQL Server 2005 Database Engine on an agent-managed computer.

' <summary> 
' Create a custom event for an instance of SQL Server Database Engine.
' </summary>
Imports System
Imports System.Collections.Generic
Imports System.Collections.ObjectModel
Imports System.Text
Imports Microsoft.EnterpriseManagement
Imports Microsoft.EnterpriseManagement.Administration
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")

            ' Define the name of the agent-managed computer that was manually installed.
            Dim fullAgentName As String = "EnterFullyQualifiedAgentNameHere"
            Console.WriteLine("Inserting custom event for " + fullAgentName)

            Dim monitoringEvent As CustomMonitoringEvent = _
               New CustomMonitoringEvent("EventPublisherName", 187)
            monitoringEvent.Channel = "Application"
            monitoringEvent.LoggingComputer = "AnyMachine"
            monitoringEvent.Message = New CustomMonitoringEventMessage("Sample Event Message.")
            monitoringEvent.User = "AnyUser"
            monitoringEvent.LevelId = 4
            ' 1-Error, 2-Warning, 4-Information, 8-Success Audit, 16-Failure Audit.

            Dim query As String = "Name = 'Microsoft.SQLServer.DBEngine'"
            ' Get the SQL Server Database Engine monitoring class.
            Dim DBEngineClassCriteria As MonitoringClassCriteria = New MonitoringClassCriteria(query)
            Dim classes As ReadOnlyCollection(Of MonitoringClass) = _
                mg.GetMonitoringClasses(DBEngineClassCriteria)
            If (classes.Count <> 1) Then
                Throw New InvalidOperationException("Expected one monitoring class with: " & query)
            End If

            ' Get the SQL Server Database Engine monitoring object for the specified computer.
            Dim SQLDBEngines As ReadOnlyCollection(Of MonitoringObject) = mg.GetMonitoringObjects(classes(0))
            Dim MOIndex As Integer = 0
            For Each DBEngine As MonitoringObject In SQLDBEngines

                ' Find the monitoring object for a specific agent-managed computer.
                If ((DBEngine.Path) = fullAgentName) Then
                    Exit For
                End If

                MOIndex = MOIndex + 1
            Next

            If (MOIndex > SQLDBEngines.Count - 1) Then
                Throw New InvalidOperationException( _
                    "The monitoring object for the agent's database engine was not found.")
            End If

            ' Insert the event in the Operations Manager database.
            SQLDBEngines(MOIndex).InsertCustomMonitoringEvent(monitoringEvent)

            Console.WriteLine("Successfully inserted custom event.")
        End Function
    End Class
End Namespace

Example 2—Generating Performance Data

This example demonstrates how to generate performance data and insert it into the Operations Manager database on behalf of the monitoring object for an instance of SQL Server 2005 Database Engine on an agent-managed computer.

' <summary> 
' Create custom performance data for an instance of SQL Server Database Engine.
' </summary>
Imports System
Imports System.Collections.Generic
Imports System.Collections.ObjectModel
Imports System.Text
Imports Microsoft.EnterpriseManagement
Imports Microsoft.EnterpriseManagement.Administration
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")

            ' Define the name of the agent-managed computer that was manually installed.
            Dim fullAgentName As String = "EnterFullyQualifiedAgentNameHere"

            Console.WriteLine("Inserting performance data for " & fullAgentName)

            Dim minValue As Integer = 100
            Dim maxValue As Integer = 200
            Dim timeSpanMins As Integer = 3
            Dim intervalSecs As Integer = 30

            Dim numberOfData As Integer = CInt((timeSpanMins * 60) / intervalSecs)
            Dim variation As Integer = maxValue - minValue
            Dim random As Random = New Random()

            Dim query As String = "Name = 'Microsoft.SQLServer.DBEngine'"

            ' Get the SQL Server database-engine monitoring class.
            Dim DBEngineClassCriteria As MonitoringClassCriteria = New MonitoringClassCriteria(query)
            Dim classes As ReadOnlyCollection(Of MonitoringClass) = _
                mg.GetMonitoringClasses(DBEngineClassCriteria)
            If (classes.Count <> 1) Then
                Throw New InvalidOperationException("Expected one monitoring class with: " & query)
            End If

            ' Get the database-engine monitoring object for the specified computer.
            Dim SQLDBEngines As ReadOnlyCollection(Of MonitoringObject) = _
                mg.GetMonitoringObjects(classes(0))
            Dim MOIndex As Integer = 0

            For Each DBEngine As MonitoringObject In SQLDBEngines

                ' Find the monitoring object for a specific agent-managed computer.
                If (DBEngine.Path.Equals(fullAgentName)) Then
                    Exit For
                End If
                MOIndex += 1

            Next

            If (MOIndex > SQLDBEngines.Count - 1) Then
                Throw New InvalidOperationException( _
                    "The monitoring object for the agent's database engine was not found.")
            End If

            ' Insert the performance data.
            Dim i As Integer
            For i = 0 To numberOfData Step 1

                ' Generate the random data.
                Dim value As Double = (random.NextDouble() * variation) + minValue
                Dim perf As CustomMonitoringPerformanceData = _
                    New CustomMonitoringPerformanceData("My object", "My counter", value)

                ' Insert the data into the Operations Manager database.
                SQLDBEngines(MOIndex).InsertCustomMonitoringPerformanceData(perf)
                Console.Write("Inserted perfData. Now sleeping for " & intervalSecs & " secs... ")

                ' Wait until the next interval.
                System.Threading.Thread.Sleep(intervalSecs * 1000)
                Console.WriteLine("OK.")

            Next

            Console.WriteLine("Successfully inserted performance data.")
        End Function
    End Class
End Namespace

See Also

Show:
© 2015 Microsoft