In the last blog post, it was described how overrides for AEM applications/error groups can be changed via submitting discovery data for Microsoft.SystemCenter.CM.AEM.MonitorOverride through a management pack. Same can be achieved via writing code against the SDK service. Below is a snippet of code that can help achieve the same.

namespace AEM_Samples

{

  using System;

  using ObjectModel = System.Collections.ObjectModel;

 

  using Microsoft.EnterpriseManagement;

  using Configuration = Microsoft.EnterpriseManagement.Configuration;

  using ConnectorFramework = Microsoft.EnterpriseManagement.ConnectorFramework;

  using Monitoring = Microsoft.EnterpriseManagement.Monitoring;

 

  public class AEMDemoClass

  {

 

    private void SetOverrides()

    {

      // Connect to the sdk service on the local machine

      ManagementGroup localManagementGroup = new ManagementGroup("komalk-test");

 

      // Retrieving information about ClientMonitoring library MP

      Configuration.ManagementPack aemLibraryMp = localManagementGroup.GetManagementPack(

          "Microsoft.SystemCenter.ClientMonitoring.Library",

          "9396306c2be7fcc4",

          new Version("6.0.5000.0"));

 

      // We need to create as many CustomMonitoringObjects as we have overrides

      // to be defined

      // So, let's figure out how many applications as of now the

      // OpsMgr system knows that meet the given requirement (appName = 'sample.exe'

      // appVersion like '%'.

 

      // Let's 1st get class type for application

      Configuration.ManagementPackClass applicationClass =

          aemLibraryMp.GetClass("Microsoft.SystemCenter.CM.AEM.Application");

 

      Configuration.MonitoringClass applicationMonitoringClass =

          localManagementGroup.GetMonitoringClass(applicationClass.Id);

 

      ObjectModel.ReadOnlyCollection<Monitoring.MonitoringObject> apps =

          localManagementGroup.GetMonitoringObjects(

          new Monitoring.MonitoringObjectCriteria(

              "ApplicationName = 'sample.exe'",

              applicationMonitoringClass));

 

      if (apps.Count == 0)

      {

        // there are no Apps whose override value can be set;

        return;

      }

 

      // Let's get class type id for Microsoft.SystemCenter.CM.AEM.MonitorOverride

      // defined in MP: Microsoft.SystemCenter.ClientMonitoring.Library

      Configuration.ManagementPackClass monitorOverrideClass =

          aemLibraryMp.GetClass("Microsoft.SystemCenter.CM.AEM.MonitorOverride");

 

      Configuration.MonitoringClass monitorOverrideMonitoringClass =

          localManagementGroup.GetMonitoringClass(monitorOverrideClass.Id);

 

      // Let's get information about various elements on this class

      Configuration.MonitoringClassProperty managedEntityIdProperty =

          monitorOverrideMonitoringClass.GetMonitoringProperty("ManagedEntityId");

 

      Configuration.MonitoringClassProperty errorCountProperty =

          monitorOverrideMonitoringClass.GetMonitoringProperty("HitCountThresholdValue");

 

      Configuration.MonitoringClassProperty userCountProperty =

          monitorOverrideMonitoringClass.GetMonitoringProperty("UniqueUserThresholdValue");

 

      Configuration.MonitoringClassProperty computerCountProperty =

          monitorOverrideMonitoringClass.GetMonitoringProperty("UniqueComputerThresholdValue");

 

      // For all these applications, lets set new override values for all 3

      // basic threshold parameters

      // we achieve this by submitting a MonitorOverride information about each

      // to submit we need to add each instance of CustomMonitoringObject to

      // discovery data below

 

      // Discovery Data would contain class Instance for AEM Crash Listener

      ConnectorFramework.IncrementalMonitoringDiscoveryData discoveryData =

          new ConnectorFramework.IncrementalMonitoringDiscoveryData();

 

      for (Int32 idx = 0; idx < apps.Count; idx++)

      {

        Monitoring.CustomMonitoringObject aemMonitorOverride =

            new Monitoring.CustomMonitoringObject(monitorOverrideMonitoringClass);

 

        // Key:

        aemMonitorOverride.SetMonitoringPropertyValue(

            managedEntityIdProperty,

            apps[idx].Id);

 

        // Non-Keys:

        aemMonitorOverride.SetMonitoringPropertyValue(errorCountProperty, 100);

        aemMonitorOverride.SetMonitoringPropertyValue(userCountProperty, 150);

        aemMonitorOverride.SetMonitoringPropertyValue(computerCountProperty, 200);

 

        discoveryData.Add(aemMonitorOverride);

      }

 

      // Committing makes sure the data is submitted to the system now

      discoveryData.CommitForUserDiscoverySource(localManagementGroup);

    }

  } // end of class

} // end of namespace

More about inserting discovery data can be read here.

 

It should be noted that this approach only takes care of submitting overrides for applications that are known to OpsMgr and match given criteria. Where as the MP approach takes care of applications new to OpsMgr have overrides defined/submitted periodically.