Using the Active Script Performance Monitor context object

The context object is available to the script programmer when scripts are executing. It delivers context aspects of the device that it is operating upon. All methods and properties are retrieved using the "Context" namespace.

All scripts used in the Active Script Monitor can be used in the Active Script Performance Monitor by making one small change:

At the end, instead of setting the status to either true or false using Context.SetResult, you need to call Context.SetValue(YourNumericValue). This numeric value will be stored in the statistical table and will be used for custom performance monitor reports and graphs.

We have provided several code samples for you to create useful Active Script Performance Monitors for your devices.

Methods

LogMessage(sText);

This method allows for a message to be written to the WhatsUp Gold debug log.

Example:

JScript:

Context.LogMessage( "Checking Monitor name using Context.GetProperty()");

VBScript:

Context.LogMessage "Checking Address using Context.GetProperty()"

PutProperty(sPropertyName);

This method allows you to store a value in the INMSerialize object. This value is retained across polls.

Example:

JScript:

var nCount = parselnt(nNum) +1;
Context.PutProperty("MyNumeric",nCount);

SetResult(nCode, sText);

This method allows for a result code and result message to be set. This is how you can tell  the WhatsUp Gold system if the monitor succeeded or not.

Important: Every script should have a result, otherwise it will report back positively.

Example:

JScript:

Context.SetResult(0, "    Everything is OK"); //Success
Context.SetResult(1, "    Really big big error"); //Failure

VBScript:

Context.SetResult 1, "    Really big big error"

Context.GetReferenceVariable( );

This method allows the code to grab a reference variable to be used in the monitor.

Example:

JScript:

Context.GetReferenceVariable("A")

A reference variable "A" would have had to have been created.

Context.SetValue( );

This method allows you to graph a value.

Example:

JScript:

Context.SetValue(245)

Properties

GetProperty(sPropertyName);

This property offers access to many device specific aspects. You obtain access to these items using the names listed. These names are case sensitive.

"StatisticalMonitorTypeName"

The performance monitor display name

"Address"

The IP address of the device

"DeviceID"

The device ID

"Mode"

1 =  doing discovery
2 =  polling
3 =  test

"CredSnmpV1:ReadCommunity"

SNMP V1 Read community

"CredSnmpV1:WriteCommunity"

SNMP V1 Write community

"CredSnmpV2:ReadCommunity"

SNMP V2 Read community

"CredSnmpV2:WriteCommunity"

SNMP V2 Write community

"CredSnmpV3:Username"

SNMP V3 Username

"CredSnmpV3:Context"

SNMP V3 Context

"CredSnmpV3:AuthPassword"

SNMP V3 Authentication password

"CredSnmpV3:AuthProtocol"

SNMP V3 Authentication protocol

"CredSnmpV3:EncryptPassword"

SNMP V3 Encrypt password

"CredSnmpV3:EncryptProtocol"

SNMP V3 Encrypt protocol

"CredWindows:DomainAndUserid"

Windows NT Domain and User ID

"CredWindows:Password"

Windows NT Password

Examples:

JScript:

var sAddress = Context.GetProperty("Address");
var sReadCommunity = Context.GetProperty("CredSnmpV1:ReadCommunity");
var nDeviceID = Context.GetProperty("DeviceID");

JScript:

//Sending log message to the WhatsUp Event Viewer
Context.LogMessage ( "Checking Mode flag");

var nFlag = Context.GetProperty("Mode");

if (nFlag == 1)
{
Context.LogMessage ("Doing a discovery");
}
else if (nFlag == 2)
{
Context.LogMessage ("Doing a poll");
}
else if (nFlag == 3)
{
Context.LogMessage ("Must be just a test.");
}
else
{
Context.LogMessage ("Do not know the mode.");
}

//Set the result code of the check (0=Success, 1=Error)
Context.SetResult (0, "No error");

(GetDB);

This property returns an open connection to the WhatsUp Gold database.

Examples:

This example gets the Open connection and reads some values out of the WhatsUp Gold "Device" table using the deviceID context. Please refer to the WhatsUp Gold Database Schema for more information about the WhatsUp Gold schema.

var oDb = Context.GetDB;
if (null == oDb)
{
Context.SetResult( 1, "  Problem creating the PRO DB object");
}
else
{
var oRs = new ActiveXObject("ADODB.Recordset");
// Get the device ID
var nDeviceID = Context.GetProperty("DeviceID");
var sSql = "SELECT * from Device WHERE nDeviceID = " + nDeviceID;
oRs = oDb.Execute(sSql);
if ( !oRs.EOF )
{
   var sDisplay;
   sDisplay = "" + oRs("sDisplayName");
   Context.LogMessage("Display Name=" + sDisplay);
   sDisplay = "" + oRs("nWorstStateID");
   Context.LogMessage("WorstStateID=" + sDisplay);  
   sDisplay = "" + oRs("sNote");
   Context.LogMessage("Note=" + sDisplay);   
   sDisplay = "" + oRs("sStatus");
   Context.LogMessage("Status=" + sDisplay);
}   
Context.SetResult( 0, "   Ok");
}