How to Create a Group

System Center

Updated: May 22, 2009

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

In Operations Manager 2007, groups are logical collections of objects, such as Windows-based computers, hard disks, or instances of Microsoft SQL Server. Groups are used to delegate authority, scope access to specific areas of the Operations Console, and override the default settings of Management Packs.

To create a group, you need to specify a name for the group and the Management Pack that the group will be added to. Next, you specify the type of objects (such as Windows computers) to add to the group. Then you specify the configuration for the group's data source, which dynamically discovers objects to include in the group based on specified criteria.

The following code example creates a group of SQL Server 2005 databases. In the CreateDiscoveryConfiguration method in the code example, the criteria that specifies which objects will be in the group is created. This example specifies that all instances of objects with a Monitoring Class equal to Microsoft.SQLServer.2005.Database will be included in the group.

using Microsoft.EnterpriseManagement;
using Microsoft.EnterpriseManagement.Common;
using Microsoft.EnterpriseManagement.Configuration;
using Microsoft.EnterpriseManagement.Monitoring;
using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Text;
using System.Xml;

namespace SDKSamples
{
    class Program
    {
        static void Main(string[] args)
        {
            ManagementGroup mg;

            mg = new ManagementGroup("localhost");

            try
            {
                CreateGroup(mg);
                Console.WriteLine("Successfully created the group.");
            }
            catch (MonitoringException error)
            {
                Console.WriteLine("Error: " + error.Message);
            }
        }

        // ---------------------------------------------------------------------
        private static void CreateGroup(ManagementGroup mg)
        {
            ManagementPackClass groupClass;
            ManagementPackDiscovery groupDiscovery;
            ManagementPack testManagementPack;
            MonitoringRelationshipClass relationshipClass;
            ManagementPackDiscoveryRelationship discoveryRelationship;

            testManagementPack = mg.GetManagementPacks("Test.Management.Pack")[0];

            groupClass = new ManagementPackClass(testManagementPack,
                "Samples.GroupsSample.AllDatabasesGroup",
                ManagementPackAccessibility.Public);
            groupClass.Singleton = true;
            groupClass.Base = mg.GetMonitoringClass(SystemMonitoringClass.InstanceGroup);
            groupClass.DisplayName = "SampleGroup";

            groupDiscovery = new ManagementPackDiscovery(testManagementPack,
                "Samples.GroupsSample.AllDatabasesGroup.Discovery");
            groupDiscovery.Remotable = true;

            discoveryRelationship = new ManagementPackDiscoveryRelationship();

            relationshipClass = mg.GetMonitoringRelationshipClass(
                SystemMonitoringRelationshipClass.InstanceGroupContainsEntities);

            discoveryRelationship.TypeID = relationshipClass;

            groupDiscovery.DiscoveryRelationshipCollection.Add(discoveryRelationship);
            groupDiscovery.Target = groupClass;
            
            groupDiscovery.DataSource = new ManagementPackDataSourceModule(
                groupDiscovery, "GroupPopulationDataSource");
            groupDiscovery.DataSource.TypeID = 
                (ManagementPackDataSourceModuleType)mg.GetMonitoringModuleTypes(
                "Microsoft.SystemCenter.GroupPopulator")[0];
            groupDiscovery.DataSource.Configuration = CreateDiscoveryConfiguration(mg,
                testManagementPack,
                groupClass.Name,
                "Microsoft.SQLServer.2005.Database");

            testManagementPack.AcceptChanges();

        }

        // ---------------------------------------------------------------------
        private static string CreateDiscoveryConfiguration(
            ManagementGroup mg,
            ManagementPack groupMp,
            string groupTypeName,
            string className
            )
        {
            /*
            Here is the XML document that is being created in this function. 
            <RuleId>$MPElement$</RuleId>
            <GroupInstanceId>$MPElement[Name="Samples.GroupsSample.AllDatabasesGroup"]$</GroupInstanceId>
            <MembershipRules>
                <MembershipRule>
                    <MonitoringClass>$MPElement[Name="MicrosoftSQLServer2005Discovery6050000!Microsoft.SQLServer.2005.Database"]$</MonitoringClass>
                    <RelationshipClass>$MPElement[Name="MicrosoftSystemCenterInstanceGroupLibrary6050000!Microsoft.SystemCenter.InstanceGroupContainsEntities"]$</RelationshipClass>
                </MembershipRule>
            </MembershipRules>
            */

            XmlDocument document = new XmlDocument();
            XmlElement rootElement = document.CreateElement("Configuration");
            XmlElement ruleIdElement;
            XmlElement groupInstanceElement;
            XmlElement membershipRulesElement;
            XmlElement membershipRuleElement;
            XmlElement membershipClassElement;
            XmlElement membershipRelationshipElement;

            document.AppendChild(rootElement);

            ruleIdElement = document.CreateElement("RuleId");
            membershipRulesElement = document.CreateElement("MembershipRules");
            membershipRuleElement = document.CreateElement("MembershipRule");
            membershipClassElement = document.CreateElement("MonitoringClass");
            membershipRelationshipElement = document.CreateElement("RelationshipClass");
            groupInstanceElement = document.CreateElement("GroupInstanceId");

            ruleIdElement.InnerText = "$MPElement$";
            groupInstanceElement.InnerText = string.Format(@"$MPElement[Name=""{0}""]$", groupTypeName);
            membershipClassElement.InnerText = GetClassReference(mg, groupMp, className);
            membershipRelationshipElement.InnerText = GetMPElementReference(mg,
                groupMp,
                mg.GetMonitoringRelationshipClass(SystemMonitoringRelationshipClass.InstanceGroupContainsEntities));

            rootElement.AppendChild(ruleIdElement);
            rootElement.AppendChild(groupInstanceElement);
            rootElement.AppendChild(membershipRulesElement);
            membershipRulesElement.AppendChild(membershipRuleElement);
            membershipRuleElement.AppendChild(membershipClassElement);
            membershipRuleElement.AppendChild(membershipRelationshipElement);

            return (rootElement.InnerXml);
        }

        // ---------------------------------------------------------------------
        private static string GetMPElementReference(
            ManagementGroup mg,
            ManagementPack groupMp,
            ManagementPackElement element
            )
        {
            string formatString = @"$MPElement[Name=""{0}!{1}""]$";
            string mpAlias = string.Empty;

            mpAlias = GetMPAlias(groupMp, element);

            return (string.Format(formatString, mpAlias, element.Name));
        }

        // ---------------------------------------------------------------------
        private static string GetClassReference(
            ManagementGroup mg,
            ManagementPack groupMp,
            string className
            )
        {
            ReadOnlyCollection<MonitoringClass> monitoringClasses;

            monitoringClasses = mg.GetMonitoringClasses(className);

            if (monitoringClasses.Count == 0)
            {
                throw new ApplicationException("The specified class was not found");
            }

            if (monitoringClasses.Count > 1)
            {
                throw new ApplicationException("Multiple classes with the specified name were found");
            }

            return (GetMPElementReference(mg, groupMp, monitoringClasses[0]));
        }

        // ---------------------------------------------------------------------
        private static string GetMPAlias(
            ManagementPack groupMp,
            ManagementPackElement element
            )
        {
            ManagementPack elementMp = element.GetManagementPack();
            string mpAlias = string.Empty;
            bool addRefRequired = true;

            if (elementMp.Id == groupMp.Id)
            {
                // The type is defined in the same MP as the new override MP.
                // No need to add a dependency.
                addRefRequired = false;
                return (string.Empty);
            }

            foreach (KeyValuePair<string, ManagementPackReference> existingMPRef in groupMp.References)
            {
                if (existingMPRef.Value.KeyToken == elementMp.KeyToken &&
                    existingMPRef.Value.Version == elementMp.Version &&
                    existingMPRef.Value.Name == elementMp.Name)
                {
                    addRefRequired = false;
                    mpAlias = existingMPRef.Key;
                }
            }

            if (addRefRequired)
            {
                ManagementPackReference mpRef = new ManagementPackReference(elementMp);

                mpAlias = elementMp.Name + elementMp.Version.ToString();
                mpAlias = mpAlias.Replace(".", "");

                groupMp.References.Add(mpAlias, mpRef);
            }

            return (mpAlias);
        }
    }
}

Show: