[OpsMgr] How to Create an Override for a Monitor (updated SDK sample)

Posted: October 9, 2011 in SCOM, SCOM 2007

here is slightly updated SCOM SDK sample showing how to create an override choosing an object as an override target.
How to Create an Override for a Monitor

//creating override for "Agent processor utilization" monitor for first found Health Service object in Error state.
using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Text;
using Microsoft.EnterpriseManagement;
using Microsoft.EnterpriseManagement.Administration;
using Microsoft.EnterpriseManagement.Common;
using Microsoft.EnterpriseManagement.Configuration;
using Microsoft.EnterpriseManagement.Monitoring;

namespace SDKSamples
{
    class Program
    {
        //---------------------------------------------------------------------
        static void Main(string[] args)
        {
            ManagementGroup mg;
            ManagementPack mp;
            MonitoringClassCriteria classCriteria;
            MonitoringClass monitoringClass;
            MonitorCriteria monitorCriteria;
            MonitoringObject monitoringObject;
            MonitoringObjectCriteria objectCriteria;
            ManagementPackMonitor monitor;
            ManagementPackMonitorPropertyOverride monitorOverride;

            //connecting to management group
            mg = new ManagementGroup("localhost");
            //getting reference to unsealed management pack
            //code will fail if no MP with 'OverrideTestMP' name is found 
            mp = mg.GetManagementPacks("OverrideTestMP")[0];
            //preparing criteria to get 'HealthService' class C# object
            //Criteria syntax http://msdn.microsoft.com/en-us/library/bb437603.aspx
            classCriteria = new MonitoringClassCriteria("Name='Microsoft.SystemCenter.HealthService'");
            monitoringClass = mg.GetMonitoringClasses(classCriteria)[0];

            objectCriteria = new MonitoringObjectCriteria(string.Format("HealthState='{0}'", (int)HealthState.Error), monitoringClass);
            monitoringObject = mg.GetMonitoringObjects(objectCriteria)[0];

            monitorCriteria = new MonitorCriteria("DisplayName='Agent processor utilization'");
            monitor = mg.GetMonitors(monitorCriteria)[0];

            monitorOverride = new ManagementPackMonitorPropertyOverride(mp, "SampleMonitorOverride");

            monitorOverride.Monitor = monitor;
            monitorOverride.Property = ManagementPackMonitorProperty.Enabled;
            monitorOverride.Value = "false";
            monitorOverride.Context = monitoringClass;
            monitorOverride.ContextInstance = monitoringObject.Id; //Here is key difference between MS example and this one 
            monitorOverride.DisplayName = "SampleMonitorOverride";

            mp.Verify();

            //Save the changes into the management pack.
            mp.AcceptChanges();
        }
    }
}
Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s