How to Create Relationship Instances by Using a Script

Updated: January 31, 2012

Applies To: System Center 2012 - Operations Manager

A management pack models an application, device, or service as a collection of class instances of various types. For example, a management pack might monitor a database server object that hosts one or more database objects. When you discover class instances that you want to monitor, you must also discover the relationships between the instances.

The System.Library management pack defines three abstract relationship types, which are described in the following table. All relationship types are directional and require both a source and a target class instance.

 

Relationship type Description

System.Containment

A containment relationship indicates that a class instance (the source) can contain another class instance (the target). The contained instance does not depend on the containing instance for its existence. For example, a database server group named "Production Servers" might contain a database server named "ProdSQLServer1." Although the server is a member of the group, it does not depend on the group to exist.

In a containment relationship, the same target can be contained by any number of sources, and a single source can contain any number of targets.

System.Hosting

A hosting relationship indicates that a class instance (the source) hosts another class instance (the target). The hosted instance depends on the hosting instance for its existence. For example, if a database server hosts a database instance and the server shuts down, the database is no longer accessible.

In a hosting relationship, a target can be hosted by only one source and it cannot belong to any other hosting relationship. However, a single source can host any number of targets.

System.Reference

In a reference relationship, two class instances (the source and target) are related, but neither instance is contained by the other and neither instance depends on the other for its existence. For example, if a database server replicates data to a remote server, the two servers have a reference relationship.

In a reference relationship, the same target can reference any number of sources and a source can reference any number of targets.

To use one of these abstract base types to define a relationship between class instances, a management pack must declare a new relationship type that is based on the abstract base type. The custom relationship type can include properties that describe the relationship. For example, a replication interval might be a property of a reference relationship between two database server instances.

When a runtime script discovers class instances, it must also discover any containment-based or reference-based relationships between those instances. Hosting relationships are implied by the class definitions and are automatically created when discovery data is processed.

Example

In this example, a management pack defines two custom relationship types. The first type is containment-based and has no properties. The second type is reference-based and has a single string property, as follows:

<RelationshipType ID="Microsoft.Example.RelAContainsB" Accessibility="Public" Base="System!System.Containment">
	<Source>Microsoft.Example.ClassA</Source>
	<Target>Microsoft.Example.ClassB</Target>
</RelationshipType>
<RelationshipType ID="Microsoft.Example.RelCReferencesD" Accessibility="Public" Base="System!System.Reference">
	<Source>Microsoft.Example.ClassC</Source>
	<Target>Microsoft.Example.ClassD</Target>
..  <Property ID="CommentProperty" Type="string"/>
</RelationshipType>

The following segment from a discovery script, written in VBScript, shows how to create relationship instances for each custom relationship type:

Set oRelCon = oDiscData.CreateRelationshipInstance("$MPElement[Name='Microsoft.Example.RelAContainsB']$")
oRelCon.Source = oClassInstanceA
oRelCon.Target = oClassInstanceB
Call oDiscData.AddInstance(oRelCon)
Set oRelRef = oDiscData.CreateRelationshipInstance("$MPElement[Name='Microsoft.Example.RelCReferencesD']$")
oRelRef.Source = oClassInstanceC
oRelRef.Target = oClassInstanceD
Set oRelComment = "A comment about the relationship."
Call oRelRef.AddProperty(oRelComment)
Call oDiscData.AddInstance(oRelRef)

The example script creates a containment-based relationship between the discovered class instances oClassInstanceA and oClassInstanceB. It then creates a reference-based relationship between the discovered class instances oClassInstanceC and oClassInstanceD. The script adds the relationship instances to the discovery data by calling the MOMDiscoveryData.AddInstance method.

See Also