Export (0) Print
Expand All

How to Configure Maintenance Mode for Monitoring Objects

System Center

Updated: May 22, 2009

Applies To: Operations Manager 2007 R2, Operations Manager 2007 SP1, System Center Operations Manager 2007

The Operations Manager class libraries allow you to control the scheduling of maintenance windows. A maintenance window is the period of time during which a monitored object is placed in maintenance mode and, consequently, does not generate alerts. Scheduling maintenance windows is useful when you need to place a large number of monitored objects in maintenance mode at one time (for example, to apply updates to the monitored objects) or when you need to run a scheduled task to automatically control the maintenance window.

Example

Example 1—Starting Maintenance Mode

The following example demonstrates how to place the monitoring objects for all instances of SQL Server 2005 Database Engine in maintenance mode for an hour.

/// <summary>
/// This example places all instances of SQL Server 2005 Database
/// Engines in maintenance mode for one hour.
/// </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)
        {
            ManagementGroup mg = new ManagementGroup("localhost");

            Console.WriteLine("Creating a maintenance window...");

            string query = "DisplayName = 'SQL 2005 DB Engine'";
            MonitoringClassCriteria criteria = new MonitoringClassCriteria(query);
            ReadOnlyCollection<MonitoringClass> monClasses = 
                mg.GetMonitoringClasses(criteria);
            List<MonitoringObject> monObjects = new List<MonitoringObject>();
            foreach (MonitoringClass monClass in monClasses)
            {
                monObjects.AddRange(mg.GetMonitoringObjects(monClass));
            }

            foreach (MonitoringObject monObject in monObjects)
            {
                if (!monObject.InMaintenanceMode)
                {
                    DateTime startTime = DateTime.UtcNow;
                    DateTime schedEndTime = DateTime.UtcNow.AddHours(1);
                    MaintenanceModeReason reason = MaintenanceModeReason.SecurityIssue;
                    String comment = "Need to apply the latest update to all SQL 2005 servers.";
                    monObject.ScheduleMaintenanceMode(startTime, schedEndTime, reason, comment);
                    Console.WriteLine(monObject.DisplayName + " set in maintenance mode for an hour.");
                }
                else
                {
                    MaintenanceWindow window = monObject.GetMaintenanceWindow();
                    DateTime schedEndTime = window.ScheduledEndTime;
                    Console.WriteLine(monObject.DisplayName + " already in maintenance mode until " + schedEndTime.ToShortTimeString() + ".");
                }
            }
        }
    }
}

Example 2—Ending Maintenance Mode

  • The following example demonstrates how to immediately end the maintenance mode of monitoring objects for all instances of SQL Server 2005 Database Engine.

/// <summary>
/// Immediately takes all instances of SQL Server 2005 Database
/// Engines out of maintenance mode.
/// </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)
        {
            ManagementGroup mg = new ManagementGroup("localhost");

            Console.WriteLine("Stopping maintenance mode...");

            string query = "DisplayName = 'SQL 2005 DB Engine'";
            MonitoringClassCriteria criteria = new MonitoringClassCriteria(query);
            ReadOnlyCollection<MonitoringClass> monClasses =
                mg.GetMonitoringClasses(criteria);
            List<MonitoringObject> monObjects = new List<MonitoringObject>();
            foreach (MonitoringClass monClass in monClasses)
            {
                monObjects.AddRange(mg.GetMonitoringObjects(monClass));
            }

            foreach (MonitoringObject monObject in monObjects)
            {
                if (monObject.InMaintenanceMode)
                {
                    monObject.StopMaintenanceMode(DateTime.UtcNow);
                    Console.WriteLine(monObject.DisplayName + " set out of maintenance mode.");
                }
                else
                {
                    Console.WriteLine(monObject.DisplayName + " not in maintenance mode.");
                }
            }
        }
    }
}

See Also

Show:
© 2014 Microsoft