How to Create a Management Pack

Updated: January 31, 2012

Applies To: System Center 2012 - Operations Manager

You can use the Operations Manager class libraries to create a management pack and add it to a management group.

The following code example demonstrates how to create a management pack. This example creates a management pack for a router and the ports that the router uses. A object is created for the router and the ports, and the class is used to set up a relationship that shows that the router hosts the ports. The management pack that is created references types in the following set of management packs; therefore, these management packs must be installed for the example code to run correctly:

  • C:\Program Files\System Center Operations Manager 2007\System.Library.mp

  • C:\Program Files\System Center Operations Manager 2007\Microsoft.SystemCenter.Library.mp

  • C:\Program Files\System Center Operations Manager 2007\System.Health.Library.mp

The management pack is saved to an XML file after it is created.

using System;
using System.Collections.Generic;
using System.Text;
using Microsoft.EnterpriseManagement.Configuration;
using Microsoft.EnterpriseManagement.Configuration.IO;
 
namespace MPCreation
{
	class Program
	{
		static void Main(string[] args)
		{
			MPCreator mpCreator = new MPCreator();
 
			mpCreator.AddReferences();
		
			mpCreator.AddClasses();
			mpCreator.AddRelationships();
			mpCreator.AddViews();
			mpCreator.AddTasks();
			mpCreator.AddRules();
		
			mpCreator.AddMonitorType(); 
 
			mpCreator.SaveMPToDisk(@"C:\");
	}
}

	class MPCreator
	{
		ManagementPack				m_mp;
		ManagementPack				m_libraryMp;
		ManagementPack				m_windowsLibraryMp;
		ManagementPack				m_healthLibraryMp;
		ManagementPackFileStore		 m_mpStore;
		ManagementPackClass			 m_routerClass;
		ManagementPackClass			 m_portClass;
		ManagementPackUnitMonitorType   m_unitMonitorType;

		//---------------------------------------------------------------------
		public MPCreator()
		{
			m_mpStore = new ManagementPackFileStore();

			m_mpStore.AddDirectory(@"C:\Program Files\System Center Operations Manager 2007");
 
			m_mp = new ManagementPack("SampleMP", "Sample Management Pack", new Version(1, 0), m_mpStore);

			m_libraryMp = new ManagementPack(@"C:\Program Files\System Center Operations Manager 2007\System.Library.mp", m_mpStore);
			m_windowsLibraryMp = new ManagementPack(@"C:\Program Files\System Center Operations Manager 2007\Microsoft.SystemCenter.Library.mp", m_mpStore);
			m_healthLibraryMp = new ManagementPack(@"C:\Program Files\System Center Operations Manager 2007\System.Health.Library.mp", m_mpStore);
			m_mp.Description	= "Sample Description";
			m_mp.DisplayName	= "Sample Management Pack";
 
			m_mp.DefaultLanguageCode = "ENU";
	}
 
		//---------------------------------------------------------------------
		public void AddClasses()
		{ 	
			m_routerClass = new ManagementPackClass(m_mp,
													"SampleMP.Router",
													ManagementPackAccessibility.Public);
 
			m_portClass = new ManagementPackClass(m_mp,
												"SampleMP.Port",
												ManagementPackAccessibility.Public);
 
			m_routerClass.Abstract  = false;
			m_routerClass.Base	= m_libraryMp.GetClass("System.NetworkDevice");
 
			m_portClass.Abstract	= false;
			m_portClass.Hosted	= true;
			m_portClass.Base		= m_libraryMp.GetClass("System.LogicalEntity");
 
			AddRouterClassProperties();
			AddPortClassProperties();
	}
 
		//---------------------------------------------------------------------
		public void AddRelationships()
		{
			ManagementPackRelationship routerHostsPort;
		
			routerHostsPort = new ManagementPackRelationship(m_mp,
															 "SampleMP.RouterHostsPort",
															 ManagementPackAccessibility.Public);
			routerHostsPort.Abstract	= false;
			routerHostsPort.Base		= m_libraryMp.GetRelationship("System.Hosting");
			routerHostsPort.Description = "Defines the hosting relationship between a router and its ports";
			routerHostsPort.DisplayName = "Router Hosts Port";
			routerHostsPort.Source = new ManagementPackRelationshipEndpoint(m_routerClass, "Source");
			routerHostsPort.Target = new ManagementPackRelationshipEndpoint(m_portClass, "Target"); ;
 
			routerHostsPort.Status = ManagementPackElementStatus.PendingAdd;
	}
 
		//---------------------------------------------------------------------
		public void AddReferences()
		{
			m_mp.References.Add("SCLibrary", new ManagementPackReference(m_libraryMp));
			m_mp.References.Add("SCWinLibrary", new ManagementPackReference(m_windowsLibraryMp));
			m_mp.References.Add("SCHealth", new ManagementPackReference(m_healthLibraryMp));
	}
 
		//---------------------------------------------------------------------
		public void AddViews()
		{
			ManagementPackFolderItem	routerStateFolderItem;
			ManagementPackFolder		viewFolder;
			ManagementPackView		routerStateView;
		
			routerStateView = new ManagementPackView(m_mp,"SampleMP.RouterStateView",ManagementPackAccessibility.Public);
			viewFolder	= new ManagementPackFolder(m_mp, "SampleMP.ViewFolder", ManagementPackAccessibility.Public);
 
			viewFolder.Description  = "Sample MP View Folder";
			viewFolder.DisplayName  = "Sample MP";
			viewFolder.Status	 = ManagementPackElementStatus.PendingAdd;
			viewFolder.ParentFolder = m_windowsLibraryMp.GetFolder("Microsoft.SystemCenter.Monitoring.ViewFolder.Root");
 
			routerStateView.Target		= m_routerClass;
			routerStateView.TypeID		= m_windowsLibraryMp.GetViewType("Microsoft.SystemCenter.StateViewType");
			routerStateView.Description	 = "Router State View";
			routerStateView.DisplayName	 = "Router State View";
			routerStateView.Category		= "Operations";
			routerStateView.Configuration   = @"<Criteria><InMaintenanceMode>false</InMaintenanceMode></Criteria>";
 
			routerStateFolderItem = new ManagementPackFolderItem(routerStateView, viewFolder);
 
			routerStateFolderItem.Status	= ManagementPackElementStatus.PendingAdd;
			routerStateView.Status		= ManagementPackElementStatus.PendingAdd;
	}
 
		//---------------------------------------------------------------------
		public void AddTasks()
		{
			ManagementPackTask task;
			ManagementPackProbeActionModule probeAction;
		
			task		= new ManagementPackTask(m_mp, "ResetRouter", ManagementPackAccessibility.Public);
			probeAction = new ManagementPackProbeActionModule(task, "ResetRouterProbe");
 
			task.Description	= "Resets a router";
			task.DisplayName	= "Reset Router";
			task.Target		 = m_routerClass;
			task.Status		 = ManagementPackElementStatus.PendingAdd;
			task.ProbeAction	= probeAction;
 
			probeAction.TypeID		= (ManagementPackProbeActionModuleType)m_libraryMp.GetModuleType("System.CommandExecuterProbe");
			probeAction.Configuration   = @"<ApplicationName>%WINDIR%\System32\NET.EXE</ApplicationName><WorkingDirectory/><CommandLine>USER</CommandLine><TimeoutSeconds>30</TimeoutSeconds><RequireOutput>true</RequireOutput><Files/>";
	}
 
		public void AddRules()
		{
			ManagementPackDataSourceModule		sdkEventDS;
			ManagementPackWriteActionModule		 writeEventToDBMWA;
			ManagementPackConditionDetectionModule  conditionDetection;
			ManagementPackRule					rule;
 
			rule = new ManagementPackRule(m_mp, "CollectEventsRule");
 
			writeEventToDBMWA			 = new ManagementPackWriteActionModule(rule, "WriteToDB");
			writeEventToDBMWA.TypeID		= (ManagementPackWriteActionModuleType)m_windowsLibraryMp.GetModuleType("Microsoft.SystemCenter.CollectEvent");
			writeEventToDBMWA.Description   = "Write Event to DB";
			writeEventToDBMWA.DisplayName   = "Write Event to DB";
		
			sdkEventDS		= new ManagementPackDataSourceModule(rule, "CollectSDKEvent");
			sdkEventDS.TypeID   = (ManagementPackDataSourceModuleType)m_windowsLibraryMp.GetModuleType("Microsoft.SystemCenter.SdkEventProvider");
			sdkEventDS.DisplayName = "Collect SDK Event";
			sdkEventDS.Description = "Collect SDK Event";
 
			conditionDetection = new ManagementPackConditionDetectionModule(rule, "EventConditionDetection");
 
			conditionDetection.TypeID = (ManagementPackConditionDetectionModuleType)m_libraryMp.GetModuleType("System.ExpressionFilter");
			conditionDetection.Configuration = @"<Expression><SimpleExpression><ValueExpression><XPathQuery>EventNumber</XPathQuery></ValueExpression><Operator>Equal</Operator><ValueExpression><Value>1</Value></ValueExpression></SimpleExpression></Expression>";
  
			rule.ConfirmDelivery	= true;
			rule.Description		= "Collect Events Rule";
			rule.DisplayName		= "Collect Router Events";
			rule.Priority		 = ManagementPackWorkflowPriority.Normal;
			rule.Target			 = m_routerClass;
 
			rule.DataSourceCollection.Add(sdkEventDS);
			rule.WriteActionCollection.Add(writeEventToDBMWA);
		
			rule.ConditionDetection = conditionDetection;
 
			rule.Status = ManagementPackElementStatus.PendingAdd;
	}
 
		//---------------------------------------------------------------------
		private void AddRouterClassProperties()
		{
			AddPropertyToClass("Id",
							 "Id",
							 "Contains the Id of the router",
							 true,
							 false,
							 1,
							 ManagementPackEntityPropertyTypes.@string,
							 m_routerClass);
 
			AddPropertyToClass("Name",
							 "Name",
							 "Contains the name of the router",
							 false,
							 false,
							 1,
							 ManagementPackEntityPropertyTypes.@string,
							 m_routerClass);
 
			AddPropertyToClass("ModelNumber",
							 "Model Number",
							 "Contains the model number of the router",
							 false,
							 false,
							 1,
							 ManagementPackEntityPropertyTypes.@string,
							 m_routerClass);
 
			m_routerClass.Status = ManagementPackElementStatus.PendingAdd;
	}
 
		//---------------------------------------------------------------------
		private void AddPortClassProperties()
		{
			AddPropertyToClass("Id",
							 "Id",
							 "Contains the Id of the port",
							 true,
							 false,
							 1,
							 ManagementPackEntityPropertyTypes.@string,
							 m_portClass);
 
			AddPropertyToClass("PortType",
							 "Port Type",
							 "Contains the type of the port",
							 false,
							 false,
							 1,
							 ManagementPackEntityPropertyTypes.@string,
							 m_portClass);
 
			AddPropertyToClass("PortStatus",
							 "Port Status",
							 "Determines whether the port is on or off",
							 false,
							 false,
							 1,
							 ManagementPackEntityPropertyTypes.@string,
							 m_portClass);
 
			m_portClass.Status = ManagementPackElementStatus.PendingAdd;
	}
 
		//---------------------------------------------------------------------
		internal void SaveMPToDisk(string directoryName)
		{
			ManagementPackXmlWriter mpXmlWriter = new ManagementPackXmlWriter(directoryName);
 
			m_mp.AcceptChanges();
 
			mpXmlWriter.WriteManagementPack(m_mp);
			Console.WriteLine("Management pack created in the " + directoryName + m_mp.Name + ".xml file.");
	}
 
		//---------------------------------------------------------------------
		private void AddPropertyToClass(
			string							name,
			string							displayName,
			string							description,
			bool								isKey,
			bool								isCaseSensitive,
			int								 minLength,
			ManagementPackEntityPropertyTypes   type,
			ManagementPackClass				 mpClass
			)
		{
			ManagementPackProperty property = new ManagementPackProperty(mpClass, name);
 
			property.CaseSensitive  = isCaseSensitive;
			property.Description	= description;
			property.Type		 = type;
			property.MinLength	= minLength;
			property.Key			= isKey;
			property.DisplayName	= displayName;
 
			mpClass.PropertyCollection.Add(property);
	}
 
		internal void AddMonitorType()
		{
			ManagementPackConfigurationSchemaType   monitorSchemaType   = new ManagementPackConfigurationSchemaType();
			ManagementPackModuleTypeReference	 dsRef;
			ManagementPackModuleTypeReference	 conditionDetectionUnderThresholdRef;
			ManagementPackModuleTypeReference	 conditionDetectionOverThresholdRef;
			ManagementPackMonitorTypeState		underThresholdState;
			ManagementPackMonitorTypeState		overThresholdState;
			ManagementPackMonitorTypeDetection	underThresholdDetection;
			ManagementPackMonitorTypeDetection	overThresholdDetection;
			ManagementPackModuleCompositionNodeType dataSourceNodeType;
 
			m_unitMonitorType = new ManagementPackUnitMonitorType(m_mp, "SdkPerfDataUnitMonitor", ManagementPackAccessibility.Public);
 
			monitorSchemaType.Schema = @"<xsd:element name=""CounterName"" type=""xsd:string"" /><xsd:element name=""ObjectName"" type=""xsd:string"" /><xsd:element name=""Threshold"" type=""xsd:string""/><xsd:element name=""InstanceName"" type=""xsd:string"" minOccurs=""0"" maxOccurs=""1"" />";
 
			m_unitMonitorType.Configuration = monitorSchemaType;
 
			dsRef = new ManagementPackModuleTypeReference(m_unitMonitorType, "Datasource");
			dsRef.TypeID	= m_windowsLibraryMp.GetModuleType("Microsoft.SystemCenter.SdkPerformanceDataProvider");
 
			conditionDetectionUnderThresholdRef				 = new ManagementPackModuleTypeReference(m_unitMonitorType, "UnderThresholdFilter");
			conditionDetectionUnderThresholdRef.TypeID		= m_libraryMp.GetModuleType("System.ExpressionFilter");
			conditionDetectionUnderThresholdRef.Configuration   = @"<Expression><SimpleExpression><ValueExpression><XPathQuery Type=""Double"">Value</XPathQuery></ValueExpression><Operator>LessEqual</Operator><ValueExpression><Value Type=""Double"">$Config/Threshold$</Value></ValueExpression></SimpleExpression></Expression>";
 
			conditionDetectionOverThresholdRef				= new ManagementPackModuleTypeReference(m_unitMonitorType, "OverThresholdFilter");
			conditionDetectionOverThresholdRef.TypeID		 = m_libraryMp.GetModuleType("System.ExpressionFilter");
			conditionDetectionOverThresholdRef.Configuration	= @"<Expression><SimpleExpression><ValueExpression><XPathQuery Type=""Double"">Value</XPathQuery></ValueExpression><Operator>Greater</Operator><ValueExpression><Value Type=""Double"">$Config/Threshold$</Value></ValueExpression></SimpleExpression></Expression>";
 
			m_unitMonitorType.DataSourceCollection.Add(dsRef);
			m_unitMonitorType.ConditionDetectionCollection.Add(conditionDetectionUnderThresholdRef);
			m_unitMonitorType.ConditionDetectionCollection.Add(conditionDetectionOverThresholdRef);
 
			underThresholdState = new ManagementPackMonitorTypeState(m_unitMonitorType, "UnderThreshold");
			overThresholdState = new ManagementPackMonitorTypeState(m_unitMonitorType, "OverThreshold");
 
			m_unitMonitorType.MonitorTypeStateCollection.Add(underThresholdState);
			m_unitMonitorType.MonitorTypeStateCollection.Add(overThresholdState);
 
			underThresholdDetection = new ManagementPackMonitorTypeDetection();
			overThresholdDetection  = new ManagementPackMonitorTypeDetection();
 
			underThresholdDetection.MonitorTypeStateID  = "UnderThreshold";
			overThresholdDetection.MonitorTypeStateID   = "OverThreshold"; 
 
			dataSourceNodeType = new ManagementPackModuleCompositionNodeType();
 
			dataSourceNodeType.ID = "Datasource";
 
			underThresholdDetection.Node.ID = "UnderThresholdFilter";
			underThresholdDetection.Node.NodeCollection.Add(dataSourceNodeType);
 
			overThresholdDetection.Node.ID = "OverThresholdFilter";
			overThresholdDetection.Node.NodeCollection.Add(dataSourceNodeType);
 
			m_unitMonitorType.RegularDetectionCollection.Add(underThresholdDetection);
			m_unitMonitorType.RegularDetectionCollection.Add(overThresholdDetection);
 
			m_unitMonitorType.Status = ManagementPackElementStatus.PendingAdd;
	}
 
	
}
}

See Also