Creating overrides using the SDK


Here is some sample code that I wanted to share for creating overrides using the SDK. In scenarios where you need to automate the creation of overrides, this can become useful:


Creating a Rule Override:


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;


            MonitoringRuleCriteria              ruleCriteria;


            MonitoringRule                      rule;


            ManagementPackRulePropertyOverride  ruleOverride;


 


            mg = new ManagementGroup(“localhost”);


 


            mp = mg.GetManagementPacks(“OverrideTestMP”)[0];


 


            classCriteria = new MonitoringClassCriteria(“Name=’Microsoft.SQLServer.2005.Database'”);


 


            monitoringClass = mg.GetMonitoringClasses(classCriteria)[0];


 


            ruleCriteria = new MonitoringRuleCriteria(“DisplayName=’Collect Database Free Space (%)'”);


 


            rule = monitoringClass.GetMonitoringRules(ruleCriteria)[0];


 


            ruleOverride = new ManagementPackRulePropertyOverride(mp, “SampleRuleOverride”);


 


            ruleOverride.Rule           = rule;


            ruleOverride.Property       = ManagementPackWorkflowProperty.Enabled;


            ruleOverride.Value          = “false”;


            ruleOverride.Context        = monitoringClass;


            ruleOverride.DisplayName    = “SampleRuleOverride”;


 


            mp.Verify();


           


            //Save the changes into the management pack.


            mp.AcceptChanges();


        }       


    }


}


 


Creating a Monitor Override:


 


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;


            ManagementPackMonitor                   monitor;


            ManagementPackMonitorPropertyOverride   monitorOverride;


 


            mg = new ManagementGroup(“localhost”);


 


            mp = mg.GetManagementPacks(“OverrideTestMP”)[0];


 


            classCriteria = new MonitoringClassCriteria(“Name=’Microsoft.SQLServer.2005.Database'”);


 


            monitoringClass = mg.GetMonitoringClasses(classCriteria)[0];


 


            monitorCriteria = new MonitorCriteria(“DisplayName=’Database Status'”);


 


            monitor = mg.GetMonitors(monitorCriteria)[0];


 


            monitorOverride = new ManagementPackMonitorPropertyOverride(mp, “SampleMonitorOverride”);


 


            monitorOverride.Monitor     = monitor;


            monitorOverride.Property    = ManagementPackMonitorProperty.Enabled;


            monitorOverride.Value       = “false”;


            monitorOverride.Context     = monitoringClass;


            monitorOverride.DisplayName = “SampleMonitorOverride”;


 


            mp.Verify();


           


            //Save the changes into the management pack.


            mp.AcceptChanges();


        }       


    }


}


 


Creating a Discovery Override:


 


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;


            MonitoringDiscoveryCriteria                 discoveryCriteria;


            MonitoringDiscovery                         discovery;


            ManagementPackDiscoveryPropertyOverride     discoveryOverride;


 


            mg = new ManagementGroup(“localhost”);


 


            mp = mg.GetManagementPacks(“OverrideTestMP”)[0];


 


            classCriteria = new MonitoringClassCriteria(“Name=’Microsoft.SQLServer.2005.DBEngine'”);


 


            monitoringClass = mg.GetMonitoringClasses(classCriteria)[0];


 


            discoveryCriteria = new MonitoringDiscoveryCriteria(“Name=’Microsoft.SQLServer.2005.DatabaseDiscoveryRule'”);


 


            discovery = mg.GetMonitoringDiscoveries(discoveryCriteria)[0];


 


            discoveryOverride = new ManagementPackDiscoveryPropertyOverride(mp, “SampleDiscoveryOverride”);


 


            discoveryOverride.Discovery     = discovery;


            discoveryOverride.Property      = ManagementPackWorkflowProperty.Enabled;


            discoveryOverride.Value         = “false”;


            discoveryOverride.Context       = monitoringClass;


            discoveryOverride.DisplayName   = “SampleDiscoveryOverride”;


 


            mp.Verify();


           


            //Save the changes into the management pack.


            mp.AcceptChanges();


        }       


    }


}


 


Creating a Diagnostic Override:


 


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;


            MonitoringClass                             monitoringClass;


            MonitoringDiagnosticCriteria                diagnosticCriteria;


            MonitoringDiagnostic                        diagnostic;


            ManagementPackDiagnosticPropertyOverride    diagnosticOverride;


 


            mg = new ManagementGroup(“localhost”);


 


            mp = mg.GetManagementPacks(“OverrideTestMP”)[0];


 


            monitoringClass = mg.GetMonitoringClass(SystemMonitoringClass.WindowsComputer);


 


            diagnosticCriteria = new MonitoringDiagnosticCriteria(“Name=’Microsoft.SystemCenter.ManagedComputer.Ping.ICMPDiagnostic'”);


 


            diagnostic = mg.GetMonitoringDiagnostics(diagnosticCriteria)[0];


 


            diagnosticOverride = new ManagementPackDiagnosticPropertyOverride(mp, “SampleDiagnosticOverride”);


 


            diagnosticOverride.Diagnostic   = diagnostic;


            diagnosticOverride.Property     = ManagementPackWorkflowProperty.Enabled;


            diagnosticOverride.Value        = “false”;


            diagnosticOverride.Context      = monitoringClass;


            diagnosticOverride.DisplayName  = “SampleDiagnosticOverride”;


 


            mp.Verify();


           


            //Save the changes into the management pack.


            mp.AcceptChanges();


        }       


    }


}


 


Creating a recovery override:


 


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                     monitoringClassCriteria;


            MonitoringClass                             monitoringClass;


            MonitoringRecoveryCriteria                  recoveryCriteria;


            MonitoringRecovery                          recovery;


            ManagementPackRecoveryPropertyOverride      recoveryOverride;


 


            mg = new ManagementGroup(“localhost”);


 


            mp = mg.GetManagementPacks(“OverrideTestMP”)[0];


 


            monitoringClassCriteria = new MonitoringClassCriteria(“Name=’Microsoft.SystemCenter.HealthServiceWatcher'”);


 


            monitoringClass = mg.GetMonitoringClasses(monitoringClassCriteria)[0];


 


            recoveryCriteria = new MonitoringRecoveryCriteria(“Name=’Microsoft.SystemCenter.HealthService.Recovery.AutoReinstallAgent'”);


 


            recovery = mg.GetMonitoringRecoveries(recoveryCriteria)[0];


 


            recoveryOverride = new ManagementPackRecoveryPropertyOverride(mp, “SampleRecoveryOverride”);


 


            recoveryOverride.Recovery     = recovery;


            recoveryOverride.Property     = ManagementPackWorkflowProperty.Enabled;


            recoveryOverride.Value        = “false”;


            recoveryOverride.Context      = monitoringClass;


            recoveryOverride.DisplayName  = “SampleRecoveryOverride”;


 


            mp.Verify();


           


            //Save the changes into the management pack.


            mp.AcceptChanges();


        }       


    }


}


 


Comments (6)

  1. Craig Pero says:

    Boris,  the code is great… just what I needed.  I am running into trouble though since I want to apply the over-ride to a group instead of a class (I think a group is a type of class but I am still learning).  I changed the line

    classCriteria = new MonitoringClassCriteria("Name=’Microsoft.SQLServer.2005.Database’");

    to

    classCriteria = new MonitoringClassCriteria("DisplayName=’MyGroupName’");

    and it finds the group.  However on mp.verify() I get the following error

    An object of type MonitoringDiscovery with Id cd65c784-7a08-e26b-5710-2eee3dd91fdd was not found.

    Is a Group different than a class then?

  2. borisyan says:

    The problem is likely becaues the class called MyGroupName is not defined in the same MP as the override. Could that be in your case?

  3. Craig Pero says:

    Is that a requirement that the class/group is in the same MP as the override?

  4. Craig Pero says:

    I start with a Management pack that that contains overrides to disable every rule in a management pack.  then based on SLA, I enable only those that apply.

    the complicating factor here is that I need to apply the over-ride to a group that allows it’s memberships to change but you cannot do that with a group in an unsealed management pack..  What I did was create a management pack (call it "Child MP") which has a group "My Child Group" that I modify on a regular basis.  I then created a management pack (call it "Parent MP") in which I created "My Parent Group" and nested "My Child Group" in it.  Next I sealed the "Parent MP" and I use the groups in the "Parent MP" as the target of my over-rides. this works in the console.

    this allows me to modify members of group "My Child Group" and thus modifies the group membership of "My Parent Group" indirectly.  at the same time, it meets the requirements to target an over-ride to a sealed management pack.

    I need to control what rules are enabled or disabled based on groups with certain SLA’s and thus the requirement I face.  

  5. borisyan says:

    If you create an override and use a class in another management pack as the target of the override, you need to add reference to the MP which defines the class. This can only be done if the class is defined in a sealed MP.

  6. Craig Pero says:

    thank you for your reply.  I actually found what the problem was.  There is apparently a VERY LIMITED set of characters you can use when NAMING an override. the Example below only uses alpha and no spaces and thus is no issue (SampleruleOverride).

    ruleOverride = new ManagementPackRulePropertyOverride(mp, "SampleRuleOverride");)

    Alpha/Numeric and periods seem to be the only "safe" characters…  anything else kicks up an XSD error.

    I wrote a function to replace any chars other than the ones above with a period.  this fixed it for me.

    Again, thank you for taking the time to comment on this.