Creating and Updating Groups

Creating and Updating Groups

Rate This
  • Comments 22

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();

        }

    }

}

 

Leave a Comment
  • Please add 1 and 5 and type the answer here:
  • Post
  • How do I add sub groups to a group?  Is there any documentation on the formula?

  • 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.

  • 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

  • oh, another question , I add the

    <IncludeList>                    <MonitoringObjectId>******</MonitoringObjectId></IncludeList> into the managementpack, then I insert a computer to the group, but I can see the computer only in the group properity[explicit members],  then right-click the group , view group members... , see no computers, and view group state..., also cannot find the computer, and On The Monitoring , its all the same .

    Is this normal ? when I insert a computer to a group , I can only see it on the explicit members of the group properity?

  • Depending on how you inserted the computer explicitly, it will take some time for group calculation to rerun again, but your computer should show up as a member of the group when it is in the explicit list.

    I don't understand your first question about inserting computers into the group. That is what this post and the other referenced post are all about. Can you be more specific as to what problem you are having?

  • Thanks Jakub, sorry for my pool English, I used the <includelist><monitoringobjectid>******</monitoringobjectid></includelist>  to insert a computer into the group, this is one method to insert computer into group , but is this the only method?How about other methods?

    And when I insert successful, I can see the computer only in explicit list , it didnt show as a member of the group from now on ( about 24 hours ) , what's wrong?

  • The post I referenced in my comment earlier has all the various ways to add group members, including computers and goes into great detail about how group calculation works. Please read through it as it should answer all your questions. If your computer is not showing up, either it does not match the relationship type supported by the group calculation formula or is an invalid id, I can't think of another reason it wouldn't work.

    Did you check the event log on the RMS to see if there are any errors related to this rule?

  • But if it is an invalid id , it shouldn't show up in the explicit list also

  • 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?

  • 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?

  • 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.

  • "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.

  • FW: Advanced example using PowerShell and the OpsMgr SDK: Creating and Updating Groups Feed: System Center

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

  • 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

Page 1 of 2 (22 items) 12