Export (0) Print
Expand All

How to Create a Group

System Center

Updated: January 31, 2012

Applies To: System Center 2012 - Operations Manager

[This topic is pre-release documentation and is subject to change in future releases. Blank topics are included as placeholders.]

In Operations Manager, 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 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 specify which objects will be in the group are 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 (EnterpriseManagementException error)
            {
                Console.WriteLine("Error: " + error.Message);
            }
        }

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

            testManagementPack = mg.ManagementPacks.GetManagementPacks(new ManagementPackCriteria("ID='Test.Management.Pack'"))[0];

           //create new group
            groupClass = new ManagementPackClass(testManagementPack, "Samples.GroupsSample.AllDatabasesGroup", ManagementPackAccessibility.Public);
            groupClass.Singleton = true;
            groupClass.Base = mg.EntityTypes.GetClass(SystemClass.Group);
            groupClass.DisplayName = "SampleGroup";


            //create discovery for group
            groupDiscovery = new ManagementPackDiscovery(testManagementPack, "Samples.GroupsSample.AllDatabasesGroup.Discovery");
            groupDiscovery.Remotable = true;

            //relationship type is containment
            discoveryRelationship = new ManagementPackDiscoveryRelationship();
            relationshipClass = mg.EntityTypes.GetRelationshipClass(SystemRelationship.Containment);
            discoveryRelationship.TypeID = relationshipClass;

            groupDiscovery.DiscoveryRelationshipCollection.Add(discoveryRelationship);
            groupDiscovery.Target = groupClass;
            
            groupDiscovery.DataSource = new ManagementPackDataSourceModule(groupDiscovery, "GroupPopulationDataSource");
            groupDiscovery.DataSource.TypeID = 
                (ManagementPackDataSourceModuleType)mg.Monitoring.GetModuleTypes(
                new ManagementPackModuleTypeCriteria("ID='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.EntityTypes.GetClass(SystemClass.Group));

            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
            )
        {
            IList<ManagementPackClass> mpClasses;

            mpClasses = mg.EntityTypes.GetClasses(new ManagementPackClassCriteria("ID='" +className + "'"));

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

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

            return (GetMPElementReference(mg, groupMp, mpClasses[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);
        }
    }
}

See Also

 

Community Additions

ADD
Show:
© 2014 Microsoft