Поделиться через


Creating and Updating Groups

I've received several questions over the past few days about how to create and update groups in the SDK. Below is a commented sample of how to do it.

What InsertCustomMonitoringObject group does, is create a new singleton class that derives from Microsoft.SystemCenter.InstanceGroup. It also creates a rule that populates this group which essentially defines the formula and inclusion and exlusion lists for the group. Hopefully the sample and comments in the code below explain this in more detail.

Note: A quick apology on the formating of the strings for formulas below. I found that when I had extra spaces, the schema validation failed and caused some exceptions to be thrown. This way, a direct cut and paste should work.

Edit: If you receive an ArgumentException for the references collection, this means that your default management pack already has a reference for the given management pack. When this happens, replace the alias I used with the existing alias for the management pack in the default management pack and don't add it to the newReferences collection.

using System;

using Microsoft.EnterpriseManagement;

using Microsoft.EnterpriseManagement.Configuration;

using Microsoft.EnterpriseManagement.ConnectorFramework;

using Microsoft.EnterpriseManagement.Monitoring;

 

namespace Jakub_WorkSamples

{

    partial class Program

    {

        static void InsertAndUpdatingGroups()

        {

            // Connect to the sdk service on the local machine

            ManagementGroup localManagementGroup = new ManagementGroup("localhost");

 

            // Create the formula

            // There can be multiple membership rules, no need for a root tag.

            // The monitoring class is the class of instance you want in the group.

            // The Relationship class is a relationhip whose source type needs to

            // be in the base class hierarchy of the class of your group (in this

            // case we actually create a new class using the insert method on

            // ManagementPack and this class will derive from InstanceGroup

            // which is the source of InstanceGroupContainsEntities) and the target

            // class needs to be in the base class hierarchy of the of the

            // aforementioned MonitoringClass.

            // You can also have an include and exclude list of specific entities that

            // also must match the relationship class and monitoring class critiera.

            // So in the example below, you could only include or exclude instances

            // that derive from Microsoft.Windows.Computer.

            // These look like this: (if both are specified the include list is first)

            // <IncludeList>

            // <MonitoringObjectId>f5E5F15E-3D7D-1839-F4C6-13E36BCD982a

            // </MonitoringObjectId>

            // </IncludeList>

            // <ExcludeList>

            // <MonitoringObjectId>f5E5F15E-3D7D-1839-F4C6-13E36BCD982a

            // </MonitoringObjectId>

            // </ExcludeList>

            // Prior to the include list, you can also add an expression to include

            // instances by. The element is defined as Expression and the schema for

            // it (as well as this entire MembershipRule schema) is defined in the

            // Microsoft.SystemCenter.Library management pack.

            // An example of an expression:

            // <Expression>

            // <SimpleExpression>

            // <ValueExpression>

            // <Property>

            // $MPElement[Name="Microsoft.SystemCenter.HealthService"]/IsRHS$

            // </Property>

            // </ValueExpression>

            // <Operator>Equal</Operator>

            // <ValueExpression>

            // <Value>False</Value>

            // </ValueExpression>

            // </SimpleExpression>

            // </Expression>

            // This expression can reference properties of the class of the membership

            // rule and in this case would include any health services that are not

            // the root health service.

            // Note: this example doesn't work with the rule I have below, it is simple

            // for illustrative purposes. I would need to filter by a

            // Microsoft.Windows.Computer property in order to use it below.

 

            string formula =

                @"<MembershipRule>

                <MonitoringClass>$MPElement[Name=""Windows!Microsoft.Windows.Computer""]$</MonitoringClass>

                <RelationshipClass>$MPElement[Name=""InstanceGroup!Microsoft.SystemCenter.InstanceGroupContainsEntities""]$</RelationshipClass>

                </MembershipRule>";

 

            // Create the custom monitoring object group

            CustomMonitoringObjectGroup allComputersGroup =

                new CustomMonitoringObjectGroup("Jakub.At.Work.Namespace",

                "AllComputerGroup",

                "Jakub@Work Sample All Computers Group",

                formula);

 

            // Get the default management pack.

            ManagementPack defaultManagementPack =

                localManagementGroup.GetManagementPacks(

                "Microsoft.SystemCenter.OperationsManager.DefaultUser")[0];

 

            // Get the management packs for references

            ManagementPack windowsManagementPack = localManagementGroup.

                GetManagementPack(SystemManagementPack.Windows);

            ManagementPack instanceGroupManagementPack = localManagementGroup.

                GetManagementPack(SystemManagementPack.Group);

            ManagementPackReferenceCollection newReferences =

                new ManagementPackReferenceCollection();

            newReferences.Add("Windows", windowsManagementPack);

            newReferences.Add("InstanceGroup", instanceGroupManagementPack);

 

            defaultManagementPack.InsertCustomMonitoringObjectGroup(allComputersGroup,

                newReferences);

 

            // Get the class that represents my new group

            MonitoringClass myNewGroup = localManagementGroup.

                GetMonitoringClasses("Jakub.At.Work.Namespace.AllComputerGroup")[0];

 

            // Get the discovery rule that populates this group

            // For the purposes of this sample, I know there is only 1 in the template

            MonitoringDiscovery groupPopulateDiscovery = myNewGroup.

                GetMonitoringDiscoveries()[0];

 

            // This is the full configuration of the discovery of which the

            // membership rule is one part that you can configure and update

            Console.WriteLine("The discovery configuration: {0}",

                groupPopulateDiscovery.DataSource.Configuration);

 

            // Update the configuration in some fasion

            string newConfiguration =

                @"<RuleId>$MPElement$</RuleId>

                <GroupInstanceId>$MPElement[Name=""Jakub.At.Work.Namespace.AllComputerGroup""]$</GroupInstanceId>

                <MembershipRules>

                    <MembershipRule>

                <MonitoringClass>$MPElement[Name=""Windows!Microsoft.Windows.Computer""]$</MonitoringClass>

                <RelationshipClass>$MPElement[Name=""InstanceGroup!Microsoft.SystemCenter.InstanceGroupContainsEntities""]$</RelationshipClass>

                <ExcludeList>

                    <MonitoringObjectId>f5E5F15E-3D7D-1839-F4C6-13E36BCD982a</MonitoringObjectId>

                </ExcludeList>

                </MembershipRule>

                </MembershipRules>";

 

            // Now we want to update the group membership for this group

            groupPopulateDiscovery.Status = ManagementPackElementStatus.PendingUpdate;

            groupPopulateDiscovery.DataSource.Configuration = newConfiguration;

            groupPopulateDiscovery.GetManagementPack().AcceptChanges();

        }

    }

}

Comments

  • Anonymous
    May 30, 2007
    How do I add sub groups to a group?  Is there any documentation on the formula?

  • Anonymous
    May 30, 2007
    Take a look at the most recent groups post for more examples of formulas: http://blogs.msdn.com/jakuboleksy/archive/2007/05/22/more-on-group-membership-and-calculations.aspx In order to create a sub-group, you need to add your group as a member of the group you want it to be a sub-group of.

  • Anonymous
    June 03, 2007
    I have create a group, and how to find it from all groups , if finded ,how to insert a computer to it? Thanks a lot

  • Anonymous
    June 03, 2007
    The comment has been removed

  • Anonymous
    June 04, 2007
    The comment has been removed

  • Anonymous
    June 04, 2007
    The comment has been removed

  • Anonymous
    June 04, 2007
    The comment has been removed

  • Anonymous
    June 04, 2007
    The comment has been removed

  • Anonymous
    June 04, 2007
    I check it again, it also match the relationship type , but it still didnt show as the member. when I add a computer manually ,then it shows as a member with the computer manually added. What's the matter?The SCOM2007 didnt refresh the computer inserted by SDK?

  • Anonymous
    June 06, 2007
    Sorry for the delay, I was following up with the appropriate developer for the group calculation feature. He basically said the same thing I said, in that there are two reasons why this might not work. The first is the Id is not right; perhaps the instance is deleted or it is not an instance to begin with. The other reason is the instance is not of a type that matches the relationship type as defined by the rule. Would it be possible for you to attach your group calculation rule and results from a query on the TypedManagedEntity table where the BaseManagedEntityId equals the Guid you are adding to your include list?

  • Anonymous
    February 28, 2008
    Hi Jakub, I'm trying to add dynamically some Computers to a group based on a seperated Database. Furthermore I would like to add all classes from the entity, based on a Computer FQDN. Iv'e tried to use your solution, but if I choose the System.Entity, it shows me just the classes of a computer and not the HealthService, Exchange objects and so on. The other question is: Make it sense to  to set some attributes at the top (System.Entity) to fill the groups dynamically based on such attributes. If you could provide me a solution I would really appriciate.

  • Anonymous
    February 28, 2008
    "Furthermore I would like to add all classes from the entity, based on a Computer FQDN." What does that mean, can you elaborate? Also, the public newsgroups would be a better place to ask this specific question; I am not an expert on group calculation configuration.

  • Anonymous
    December 03, 2008
    FW: Advanced example using PowerShell and the OpsMgr SDK: Creating and Updating Groups Feed: System Center

  • Anonymous
    December 06, 2008
    I&#39;ve got a new post up HERE on the System Center Forum site. The title is &quot;Advanced example

  • Anonymous
    September 30, 2010
    Hi Jakub - This is great. Is there anyway to use this type of approach so MPs are restricted to a set of servers.  Our challenge today is we have a lot of MPs and any time they are modified they are requested by all servers (putting a large load on the MS), the discovery runs and only the servers that the MP is supposed to be on start using it.  However, the discovery itself puts a large load on all the agent servers and the RMS.  We dynamically update a large number of management packs (each pack only applies to 1-10 servers), however we have 1000+ servers with managed agents.  We have made optimizations to the initial discovery spikes on the target machines with SpreadInitializationOverInterval mechanism, but this does not solve the problem on the MS. Do you have any advice how we can restrict MPs to be only be requested by a specified set of servers?   Thank you, Alex

  • Anonymous
    September 30, 2010
    Well, an MP will not get distributed to an agent that doesn't need it, overrides not withstanding. So if the only reason an agent doesn't need an MP is because an override has effectively disabled it, the MP will still be sent. There is nothing that can be done about this though.

  • Anonymous
    January 17, 2011
    Jakub- Can you tell me what the priority of Expression, IncludeList and ExcludeList are? For instance, if an Expression doesn't match an instance, but it is explicitly specified in the IncludeList, does it get added to the group? Likewise, if Expression matches, or an instance is in the IncludeList, if the same instance is in the ExcludeList, does it get added to the group? (There is nothing suggesting that Expression, IncludeList and ExcludeList are mutually exclusive.) Thanks, Mike

  • Anonymous
    January 18, 2011
    (Formula – ExcludeList) + IncludeList is what you get.

  • Anonymous
    April 19, 2012
    How can I implement this to 2012? I can't find much classes declared in this sample. Please help.

  • Anonymous
    April 20, 2012
    Should work in 2012, the assuming you recompile against the new DLLs.

  • Anonymous
    April 23, 2012
    Can I still use this to scsm 2012?

  • Anonymous
    April 24, 2012
    Yes and no. The underlying concepts are similar, but in SM you would not use some of these classes as they are OM specific. For example, CustomMonitoringObjectGroup does not exist, instead you would use EnterpriseManagementGroup.