Notification Subscriptions

There have been a few requests recently for more granular notification subscriptions. While this is fully supported, our UI is limited in what it exposes for users to tweak. Here is a look at the SDK and how to use it to create a subscription (and related objects).

First of all, to use subscriptions, you need to setup some auxiliary objects. In my example below, the first thing I create is an endpoint. We support Sip, Sms and Smtp. The endpoint is stored as a ModuleType in the database, although this is abstracted away by the API. The fact that it's a module type, however, means that the name has to follow the naming restrictions for management pack elements. After the endpoint, I create an action (also a ModuleType). The action combines an endpoint with configuration that allows the format of the endpoint to be specified. In the Smtp case, this allows you to specify email properties. Finally, I create a recipient that the subscription will be targeted to.

The actual subscription involves combining all the aforementioned components and specifying the criteria by which to filter notifications. In SCOM 2007, notifications are based on alerts. You configure which alerts you want to trigger notifications by using the AlertNotChangedSubscriptionConfiguration and the AlertChangedSubscriptionConfiguration classes. These are also used for connector framework subscriptions to mark alerts for forwarding. These two classes represent criteria by which to match alerts. The first matches alert that have not changed that match the criteria while the latter matches alerts that have changed that match the criteria. Both work off a polling interval. If you look at the classes in the SDK, you will notice that you can specify groups and classes to filter by, but what I really wanted to outline here was the criteria property as this is what is really not exposed fully by the UI. The criteria has to match the schema as defined in the Microsoft.SystemCenter.Library called AlertCriteriaType. Note, you don't need the Expression tag, that is filled in for you. In terms of which columns you can query for in the criteria, it is anything that is defined on the Alert table in the db.

EDIT: Stefan Koell has put together a great powershell example to accomplish the same task.

Here's the code:

(Note - If the criteria is not all on one line, it doesn't work correctly, that's why the formatting is a bit weird below. If you select the code, the full criteria should select)

using System;

using Microsoft.EnterpriseManagement;

using Microsoft.EnterpriseManagement.Administration;

using Microsoft.EnterpriseManagement.Configuration;

using Microsoft.EnterpriseManagement.ConnectorFramework;

using Microsoft.EnterpriseManagement.Monitoring;

 

namespace Jakub_WorkSamples

{

    partial class Program

    {

        static void InsertSubscription()

        {

            // Connect to the sdk service on the local machine

            ManagementGroup localManagementGroup = new ManagementGroup("localhost);

 

            // Setup an Smtp Endpoint

            SmtpServer smtpServer = new SmtpServer("localhost");

            smtpServer.PortNumber = 42;

            smtpServer.AuthenticationType =

                SmtpNotificationAuthenticationProtocol.Anonymous;

 

            // The guid is here for a unique name so this can be rerun

            SmtpNotificationEndpoint smtpEndpoint =

                new SmtpNotificationEndpoint(

                "SmtpEndpoint_" + Guid.NewGuid().ToString().Replace('-', '_'),

                "smtp",

                smtpServer);

            smtpEndpoint.DisplayName = "My Smtp Endpoint";

            smtpEndpoint.Description = "This is my Smtp Endpoint";

            smtpEndpoint.MaxPrimaryRecipientsPerMail = 10;

            smtpEndpoint.PrimaryServerSwitchBackIntervalSeconds = 15;

 

            // This commits the endpoint into the system

            localManagementGroup.InsertNotificationEndpoint(smtpEndpoint);

 

            // Setup the Smtp Action (this includes email format)

            SmtpNotificationAction smtpNotificationAction =

                new SmtpNotificationAction(

                "SmtpAction" + Guid.NewGuid().ToString().Replace('-', '_'));

 

            smtpNotificationAction.Body = "Body";

            smtpNotificationAction.Description = "Description";

            smtpNotificationAction.DisplayName = "DisplayName";

            smtpNotificationAction.Endpoint = smtpEndpoint;

            smtpNotificationAction.From = "Test@Test.com";

            smtpNotificationAction.Headers.Add(

                new SmtpNotificationActionHeader("Name", "Value"));

            smtpNotificationAction.IsBodyHtml = false;

            smtpNotificationAction.ReplyTo = "replyto@test.com";

            smtpNotificationAction.Subject = "my subject";

 

            // This commits the action into the system

            localManagementGroup.InsertNotificationAction(smtpNotificationAction);

 

            // Setup a recipient

            NotificationRecipientScheduleEntry scheduleEntry =

                new NotificationRecipientScheduleEntry();

            scheduleEntry.ScheduleEntryType =

                NotificationRecipientScheduleEntryType.Inclusion;

            scheduleEntry.ScheduledDays =

                NotificationRecipientScheduleEntryDaysOfWeek.Weekdays;

            scheduleEntry.DailyStartTime =

                new NotificationRecipientScheduleEntryTime(8, 0);

            scheduleEntry.DailyEndTime =

                new NotificationRecipientScheduleEntryTime(17, 0);

 

            NotificationRecipientDevice recipientDevice =

                new NotificationRecipientDevice("smtp", "test@test.com");

            recipientDevice.Name = "TestDevice";

            recipientDevice.ScheduleEntries.Add(scheduleEntry);

 

            NotificationRecipient recipient =

                new NotificationRecipient("RecipientName" + DateTime.Now.ToString());

            recipient.Devices.Add(recipientDevice);

            recipient.ScheduleEntries.Add(scheduleEntry);

 

            // Commits the recipient

            localManagementGroup.InsertNotificationRecipient(recipient);

 

            // Alert configuration

            AlertNotChangedSubscriptionConfiguration config =

                new AlertNotChangedSubscriptionConfiguration(

                AlertSubscriptionConfigurationType.Any);

            config.Criteria = "<SimpleExpression><ValueExpression><Property>ResolutionState</Property></ValueExpression><Operator>Equal</Operator><ValueExpression><Value>255</Value></ValueExpression></SimpleExpression>";

            config.ExpirationStartTime = DateTime.Now;

            config.PollingIntervalMinutes = 1;

 

            // Subscription

            AlertNotificationSubscription alertChangeSubscription =

                new AlertNotificationSubscription(

                "MyNewAlertChangeSubscription" + Guid.NewGuid().ToString().Replace('-', '_'),

                config);

            alertChangeSubscription.DisplayName = "My Subscription";

            alertChangeSubscription.Description = "My Subscription Description";

            alertChangeSubscription.ToRecipients.Add(recipient);

            alertChangeSubscription.Actions.Add(smtpNotificationAction);

 

            // Commits the subscription

            localManagementGroup.InsertNotificationSubscription(alertChangeSubscription);

        }

    }

}