Share via


How to Insert Custom Event and Performance Data

Applies To: System Center 2012 - Operations Manager

[This topic is pre-release documentation and is subject to change in future releases. Blank topics are included as placeholders.]

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.
            ManagementPackClassCriteria DBEngineClassCriteria = new ManagementPackClassCriteria(query);
            IList<ManagementPackClass> classes = mg.EntityTypes.GetClasses(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.
            List<MonitoringObject> SQLDBEngines = new List<MonitoringObject>();
            IObjectReader<MonitoringObject> reader = mg.EntityObjects.GetObjectReader<MonitoringObject>(classes[0], ObjectQueryOptions.Default);
            SQLDBEngines.AddRange(reader);

            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.
            ManagementPackClassCriteria DBEngineClassCriteria = new ManagementPackClassCriteria(query);
            IList<ManagementPackClass> classes =
                mg.EntityTypes.GetClasses(DBEngineClassCriteria);
            if (classes.Count != 1)
                throw new InvalidOperationException("Expected one monitoring class with: " + query);

            // Get the database-engine monitoring object for the specified computer.
            List<MonitoringObject> SQLDBEngines = new List<MonitoringObject>();
            IObjectReader<MonitoringObject> reader = mg.EntityObjects.GetObjectReader<MonitoringObject>(classes[0], ObjectQueryOptions.Default);
            SQLDBEngines.AddRange(reader);

            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

Tasks

How to Display Operations Manager Data

Other Resources

Automating Operations Manager Administration