Export (0) Print
Expand All
3 out of 3 rated this helpful - Rate this topic

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>
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.");
        }
    }
}

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.");
        }
    }
}

See Also

Did you find this helpful?
(1500 characters remaining)
Thank you for your feedback
Show:
© 2014 Microsoft. All rights reserved.