How to Configure Maintenance Mode for Monitoring Objects

Updated: January 31, 2012

Applies To: System Center 2012 - Operations Manager

You can use the Operations Manager class libraries 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'";
			ManagementPackClassCriteria criteria = new ManagementPackClassCriteria(query);
			IList<ManagementPackClass> monClasses = 
				mg.EntityTypes.GetClasses(criteria);
			List<MonitoringObject> monObjects = new List<MonitoringObject>();
			foreach (ManagementPackClass monClass in monClasses)
			{
				monObjects.AddRange(mg.EntityObjects.GetObjectReader<MonitoringObject>(monClass,ObjectQueryOptions.Default));
		}

			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'";
			ManagementPackClassCriteria criteria = new ManagementPackClassCriteria(query);
			IList<ManagementPackClass> monClasses =
				mg.EntityTypes.GetClasses(criteria);
			List<MonitoringObject> monObjects = new List<MonitoringObject>();
		 
			foreach (ManagementPackClass monClass in monClasses)
			{
				IObjectReader<MonitoringObject> reader = mg.EntityObjects.GetObjectReader<MonitoringObject>(monClass, ObjectQueryOptions.Default);
				monObjects.AddRange(reader);
		}

			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