How to Create Discovery Data by Using a Script

Updated: January 31, 2012

Applies To: System Center 2012 - Operations Manager

You can create a custom runtime script to discover class instances that you want to monitor. A script can discover instances of a class that is defined in the management pack and that contains the script or in a referenced management pack. To discover class instances, a script can query Windows Management Instrumentation (WMI); query the Windows Registry; or perform other actions, such as searching for files on a target computer.

To perform discovery, a script creates a MOMDiscoveryData Object object and then submits it to Operations Manager for processing. The MOMDiscoveryData Object object contains the following:

  • Class instances—Objects that conform to a class that is defined in a management pack. The script defines the values of each object's properties and then adds the class objects to the discovery data.

  • Relationship instances—Objects that define the relationship between the discovered class instances. The script defines the source and target of each relationship and the values for each of the relationship's properties. The script then adds the relationship objects to the discovery data. For more information, see How to Create Relationship Instances by Using a Script.

A runtime script can create only one instance of a MOMDiscoveryData Object object, although that object can contain multiple class and relationship instances. After the script runs and submits the MOMDiscoveryData Object object for processing, monitoring objects (rules, monitors, or tasks) can monitor the health of the discovered class instances or perform actions on the instances.

To develop a discovery script

  1. Create an empty script in any script authoring environment. For more information about creating scripts, see Getting Started Developing Runtime Scripts.

  2. Determine the script's required arguments. Script arguments are values that are passed to the script by the discovery that runs the script. You can access argument values in the script by using the Windows Script Host's Arguments collection (WScript.Arguments).

    noteNote
    Discovery scripts must require the management pack ID and the target instance ID as script arguments. (Scripts use these values to submit discovery data to Operations Manager for processing.) Discovery scripts must also require an argument for each key property that is defined for hosting class instances. For example, if a database class is hosted by a database engine, which is hosted by a computer, a script to discover databases requires the key properties of both the database engine and the computer.
  3. Create an instance of the Operations Manager scripting object (MOMScriptAPI).

  4. Create a new discovery data instance by calling the MOMScriptAPI.CreateDiscoveryData method.

  5. Create new class instances by calling the MOMDiscoveryData.CreateClassInstance method for each instance.

  6. Define the properties of the class instances by calling the MOMClassInstance.AddProperty method for each property.

  7. Add the class instances to the discovery data by calling the MOMDiscoveryData.AddInstance method for each instance.

  8. If any class instances that you created are members of a containment or reference relationship, create the necessary relationship instances by calling the MOMDiscoveryData.CreateRelationshipInstance method for each instance.

  9. If you created any relationship instances, define the source, target, and properties of each instance by calling the member methods of the MOMRelationshipInstance object.

  10. If you created any relationship instances, add them to the discovery data by calling the MOMDiscoveryData.AddInstance method for each instance.

  11. Submit the discovery data to a management server for processing by calling the MOMScriptAPI.Return method.

  12. Add the script to a management pack for use in a discovery. For more information, see How to Use Discovery Data in a Management Pack.

Example

This section provides a simple example script that is written in VBScript. The example demonstrates how to discover instances of two classes:

  • An application that is hosted on a computer. For simplicity, the script discovers the application by searching for a specific folder (C:\AppY) on a target computer.

  • The application's components. In this example, the application's components are any files that are located within the C:\AppY folder.

The script requires three arguments:

  • SourceId—The globally unique identifier (GUID) of the discovery that runs the script.

  • ManagedEntityId—The GUID of the computer class that is targeted by the script.

  • TargetComputer—The fully qualified domain name (FQDN) of the computer that is targeted by the script.

Option Explicit

Dim oAPI
Set oAPI = CreateObject("MOM.ScriptAPI")

Dim oArgs
Set oArgs = WScript.Arguments
' Check for the required script arguments.
if oArgs.Count < 3 Then
	' If the script is called without the required arguments,
	' create an information event and then quit. 
	Call oAPI.LogScriptEvent("DiscoveryAppY.vbs",101,0, _
		"script was called with fewer than three arguments and was not executed.")
	Wscript.Quit -1
End If

Dim SourceID, ManagedEntityId, TargetComputer

SourceId = oArgs(0) ' The GUID of the discovery object that launched the script.
ManagedEntityId = oArgs(1) ' The GUID of the computer class targeted by the script.
TargetComputer = oArgs(2) ' The FQDN of the computer targeted by the script.

Dim oFso
Set oFso = CreateObject("Scripting.FileSystemObject")

Dim oDiscoveryData, oInst
Set oDiscoveryData = oAPI.CreateDiscoveryData(0, SourceId, ManagedEntityId)

If (oFso.FolderExists("C:\AppY")) Then

	' Discovered the application. Create the application instance.
	Set oInst = oDiscoveryData.CreateClassInstance("$MPElement[Name='Microsoft.Demo.Scripting.AppY']$")

	' Define the property values for this instance. Available 
	' properties are determined by the management pack that 
	' defines the class.
	Call oInst.AddProperty("$MPElement[Name='Windows!Microsoft.Windows.Computer']/PrincipalName$", TargetComputer)
	Call oInst.AddProperty("$MPElement[Name='Microsoft.Demo.Scripting.AppY']/Version$", "2.0")
	Call oInst.AddProperty("$MPElement[Name='Microsoft.Demo.Scripting.AppY']/Path$", "C:\AppY")
	Call oInst.AddProperty("$MPElement[Name='System!System.Entity']/DisplayName$", "Application Y")
	Call oDiscoveryData.AddInstance(oInst)

	' Discover the application's components.
	Dim oFolder, oFile
	Set oFolder = oFso.GetFolder("C:\AppY")

	' Create a separate class instance for each file in the folder.
	For each oFile in oFolder.Files
		Set oInst = oDiscoveryData.CreateClassInstance("$MPElement[Name='Microsoft.Demo.Scripting.AppYComponent']$")

		' Define the property values for this class instance. The 
		' available properties are determined by the 
		' management pack that defines the class.
			Call oInst.AddProperty("$MPElement[Name='Windows!Microsoft.Windows.Computer']/PrincipalName$", TargetComputer)
		Call oInst.AddProperty("$MPElement[Name='Microsoft.Demo.Scripting.AppYComponent']/ID$", StripExtension(oFile.Name))
		Call oInst.AddProperty("$MPElement[Name='Microsoft.Demo.Scripting.AppYComponent']/FileName$", oFile.Name)
		Call oInst.AddProperty("$MPElement[Name='System!System.Entity']/DisplayName$", StripExtension(oFile.Name))
		Call oDiscoveryData.AddInstance(oInst)
  Next

End If

' Submit the discovery data for processing.
Call oAPI.Return(oDiscoveryData) 

' A helper function to remove the extension from a file name.
Function StripExtension (sFile)
StripExtension = Left(sFile, Len(sFile) -4)
End Function

The script uses the management pack context parameter MPElement to reference various class and property names. For more information, see Using Management Pack Context Parameters in Scripts.

See Also