How to Insert Custom Event and Performance Data

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>
using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using Microsoft.EnterpriseManagement;
using Microsoft.EnterpriseManagement.Administration;
using Microsoft.EnterpriseManagement.Common;
using Microsoft.EnterpriseManagement.Configuration;
using Microsoft.EnterpriseManagement.Monitoring;
using System.Text;

namespace SDKSamples
{
    class Program
    {
        static void Main(string[] args)
        {
            // Define the name of the agent-managed computer that was manually installed.
            string fullAgentName = "EnterFullyQualifiedAgentNameHere";

            ManagementGroup mg = new ManagementGroup("localhost");
            Console.WriteLine("Inserting custom event for " + fullAgentName);

            CustomMonitoringEvent monitoringEvent = 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.

            string query = "Name = 'Microsoft.SQLServer.DBEngine'";
            // Get the SQL Server Database Engine monitoring class.
            MonitoringClassCriteria DBEngineClassCriteria = new MonitoringClassCriteria(query);
            ReadOnlyCollection<MonitoringClass> classes = mg.GetMonitoringClasses(DBEngineClassCriteria);
            if (classes.Count != 1)
                throw new InvalidOperationException("Expected one monitoring class with: " + query);

            // Get the SQL Server Database Engine monitoring object for the specified computer.
            ReadOnlyCollection<MonitoringObject> SQLDBEngines = mg.GetMonitoringObjects(classes[0]);
            int MOIndex = 0;
            foreach (MonitoringObject DBEngine in SQLDBEngines)
            {
                // Find the monitoring object for a specific agent-managed computer.
                if ((DBEngine.Path) == fullAgentName)
                    break;
                ++MOIndex;
            }
            if (MOIndex > SQLDBEngines.Count - 1)
                throw new InvalidOperationException(
                    "The monitoring object for the agent's database engine was not found.");

            // Insert the event in the Operations Manager database.
            SQLDBEngines[MOIndex].InsertCustomMonitoringEvent(monitoringEvent);

            Console.WriteLine("Successfully inserted custom event.");
        }
    }
}
' <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>
using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using Microsoft.EnterpriseManagement;
using Microsoft.EnterpriseManagement.Administration;
using Microsoft.EnterpriseManagement.Common;
using Microsoft.EnterpriseManagement.Configuration;
using Microsoft.EnterpriseManagement.Monitoring;
using System.Text;

namespace SDKSamples
{
    class Program
    {
        static void Main(string[] args)
        {
            // Define the name of the agent-managed computer that was manually installed.
            string fullAgentName = "EnterFullyQualifiedAgentNameHere";

            ManagementGroup mg = new ManagementGroup("localhost");
            Console.WriteLine("Inserting performance data for " + fullAgentName);

            int minValue = 100;
            int maxValue = 200;
            int timeSpanMins = 3;
            int intervalSecs = 30;

            int numberOfData = (int)((timeSpanMins * 60) / intervalSecs);
            int variation = maxValue - minValue;
            Random random = new Random();

            string query = "Name = 'Microsoft.SQLServer.DBEngine'";
            // Get the SQL Server database-engine monitoring class.
            MonitoringClassCriteria DBEngineClassCriteria = new MonitoringClassCriteria(query);
            ReadOnlyCollection<MonitoringClass> classes = 
                mg.GetMonitoringClasses(DBEngineClassCriteria);
            if (classes.Count != 1)
                throw new InvalidOperationException("Expected one monitoring class with: " + query);

            // Get the database-engine monitoring object for the specified computer.
            ReadOnlyCollection<MonitoringObject> SQLDBEngines = mg.GetMonitoringObjects(classes[0]);
            int MOIndex = 0;
            foreach (MonitoringObject DBEngine in SQLDBEngines)
            {
                // Find the monitoring object for a specific agent-managed computer.
                if ((DBEngine.Path) == fullAgentName)
                    break;
                ++MOIndex;
            }
            if (MOIndex > SQLDBEngines.Count - 1)
                throw new InvalidOperationException(
                    "The monitoring object for the agent's database engine was not found.");

            // Insert the performance data.
            for (int i = 0; i < numberOfData; i++)
            {
                // Generate the random data.
                double value = (random.NextDouble() * variation) + minValue;
                CustomMonitoringPerformanceData perf = 
                    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.");
            }

            Console.WriteLine("Successfully inserted performance data.");
        }
    }
}
' <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

Tasks

How to Display Operations Manager Data

Other Resources

Automating Operations Manager Administration