Script Monitors and Rules
Updated: January 20, 2014
Applies To: System Center 2012 - Operations Manager, System Center 2012 R2 Operations Manager, System Center 2012 SP1 - Operations Manager
Monitoring scripts are used when the required data cannot be collected through other standard means such as an event or performance counter. The script collects data from information on the agent and creates a property bag by using the MOM.ScriptAPI object that is installed with the Operations Manager agent.
Monitoring scripts may be written in any script language that can access the MOM.ScriptAPI object that is installed on all Operations Manager agents. You can use the Operations console to create scripts in VBScript or JScript. To use a Windows PowerShell script, you must use another Authoring tool such as the Operations Manager R2 Authoring Console or MP Author.
The following code shows a sample monitoring script to illustrate the basic structure of a monitoring script. This sample script has the following characteristics.
Accepts arguments for the name of the computer that is running the script and a path of the location of the application.
Creates a property bag with the values named ComputerName, InstanceName, and PerfValue.
sComputerName = WScript.Arguments(0) sApplicationPath = WScript.Arguments(1) Set oAPI = CreateObject("MOM.ScriptAPI") Set oBag = oAPI.CreatePropertyBag() oBag.AddValue "ComputerName", sComputerName oBag.AddValue "InstanceName", "MyInstance" oBag.AddValue "Value", 1.0 oAPI.Return(oBag)
Details of each section of the script are discussed here.
sComputerName = WScript.Arguments(0) sApplicationPath = WScript.Arguments(1)
The first two lines of the script accept arguments. These values would be expected to be in the Arguments parameter of the rule or monitor running the script. The script can use any number of arguments that are required for the logic of the script.
Set oAPI = CreateObject("MOM.ScriptAPI") Set oBag = oAPI.CreatePropertyBag()
The next two lines create a property bag. These lines will also be unchanged in most monitoring scripts. The main purpose of the rest of the script will be to add values to the property bag by using data that is collected from the agent computer.
oBag.AddValue "ComputerName", sComputerName oBag.AddValue "InstanceName", "MyInstance" oBag.AddValue "Value", 1.0
After the property bag is created, any number of values can be added to it. You do this with the AddValue method on the property bag object by using the name of the item followed its value. This example uses explicit values. In actual monitoring script, additional code would be expected that would collect information from the agent computer to include in these values.
After all values are added to the property bag, it is returned into the workflow. This line is required, and without it the property bag is discarded when the script ends. This method is only used when the script creates only a single property bag. For more information about scripts that return multiple property bags and conditions when such a strategy is used, refer to the Cookdown section of the System Center Operations Manager 2007 R2 Authoring Guide.
Most scripts use arguments, which are values that are sent to the script from the command line when the script is run. Using arguments allows a single script to be used for multiple scenarios without modifying the script itself.
In a monitoring script, arguments are critical because there may be information that the script requires that will be different on each agent where the script runs. Any property of the target object for the monitor or rule can be used for the value of a script argument. This value is resolved individually on each agent at the time that the script is run.
Arguments are accessed in the Operations console from the Parameters button. Individual arguments should be separated by spaces in the order that they are accessed in the script. This is identical to the command line that would be provided if the script were run on a command line.
Each argument can be either an explicit value or a $Target variable to use the value of a property on the target object. Any $Target variables are resolved when the script is run so that the script is provided with the resolved values on the command line. You can type in the $Target variable if you know the proper syntax. It is easier though to select the property from Target button which will list all of the properties of the target object and its parents.
|Any $Target variable that might resolve to a value that includes a space should be enclosed with quotation marks. If a value includes spaces and does not have quotation marks, then it will be seen by the script as two separate arguments. The quotation marks will ensure that the value is seen as a single argument. If you select the property from Target menu, it will not include the quotation marks for you. You need to type these in after selecting the property.|
For example, the sample script earlier expects two arguments for the computer name and the application path. Assuming this was part of a monitor or rule targeted at a class hosted by the Windows Computer class, the computer name could be retrieved from the PrincipalName property. If the application path were a property on the target class, then the arguments might look similar to the following example. Notice the quotation marks around the ApplicationPath property, because this could resolve to a value that contains a space.
Assuming that you gave the script a name of MyScript.vbs, the computer name was MyServer01, and the application path was C:\Program Files\Contoso\My Application, the command line that would be run for this script would be:
MyScript.vbs MyServer01 "C:\Program Files\Contoso\My Application"
Script Monitors and Rules Topics
Script Collection Rules
Creating a rule that uses a script to collect performance or event data.
Creating a monitor that evaluates the results of a script to set its health state.
UNIX/Linux Shell Command Monitors
Creating a monitor that evaluates the output of execution of an UNIX/Linux command, script, or one-line sequence of multiple commands (using pipeline operators).
For additional resources, see Information and Support for System Center 2012.
Tip: Use this query to find online documentation in the TechNet Library for System Center 2012. For instructions and examples, see Search the System Center 2012 Documentation Library.