These example scripts demonstrate the SNMP API in use. All of these examples are written in JScript.
Initialize an SNMP object with error check from a device ID
The SnmpRqst.Initialize method returns a ComResult object that tells if the initialization succeeded or failed.
This script uses the Failed method to detect an error and logs an error message using GetErrorMsg if the initialization failed:
var oSnmpRqst = new ActiveXObject("CoreAsp.SnmpRqst");
var nDeviceID = 150;
var oComResult = oSnmpRqst.Initialize(nDeviceID);
if (oComResult.Failed)
{
Context.LogMessage(oComResult.GetErrorMsg);
}
Alternatively, initialization using a device address and an SNMP credential ID:
var oSnmpRqst = new ActiveXObject("CoreAsp.SnmpRqst");
var sAddress = "192.168.3.1";
var nCredentialID = 1;
var oComResult = oSnmpRqst.Initialize2(sAddress, nCredentialID);
if (oComResult.Failed)
{
Context.LogMessage(oComResult.GetErrorMsg);
}
Send a standard Get and log the polled value
var oSnmpRqst = new ActiveXObject("CoreAsp.SnmpRqst");
var nDeviceID = 150;
var oComResult = oSnmpRqst.Initialize(nDeviceID);
if (oComResult.Failed)
{
Context.LogMessage(oComResult.GetErrorMsg);
}
var oSnmpResponse = oSnmpRqst.Get("1.3.6.1.2.1.2.1.0");
if (oSnmpResponse.Failed)
{
Context.LogMessage("Failure. Error=" + oSnmpResponse.GetErrorMsg);
}
else
{
Context.LogMessage("Success. Polled " + oSnmpResponse.GetOid + ", got " + oSnmpResponse.GetValue);
}
Send a Get using non-standard port and timeout
var oSnmpRqst = new ActiveXObject("CoreAsp.SnmpRqst");
var nDeviceID = 150;
var oComResult = oSnmpRqst.Initialize(nDeviceID);
if (oComResult.Failed)
{
Context.LogMessage(oComResult.GetErrorMsg);
}
oComResult = oSnmpRqst.SetPort(1234);
oComResult = oSnmpRqst.SetTimeoutMs(5000); // 5 second timeout
var oSnmpResponse = oSnmpRqst.Get("1.3.6.1.2.1.2.1.0");
if (oSnmpResponse.Failed)
{
Context.LogMessage("Failure. Error=" + oSnmpResponse.GetErrorMsg);
}
else
{
Context.LogMessage("Success. Polled " + oSnmpResponse.GetOid + ", got " + oSnmpResponse.GetValue);
}
var oSnmpRqst = new ActiveXObject("CoreAsp.SnmpRqst");
var nDeviceID = 150;
var oComResult = oSnmpRqst.Initialize(nDeviceID);
if (oComResult.Failed)
{
Context.LogMessage(oComResult.GetErrorMsg);
}
var sOid = "1.3.6.1.2";
//get the next 10 objects
for (i=0; i<10; i++)
{
var oSnmpResponse = oSnmpRqst.GetNext(sOid);
if (oSnmpResponse.Failed)
{
Context.LogMessage("Failure. Error=" + oSnmpResponse.GetErrorMsg);
break;
}
else
{
sOid = oSnmpResponse.GetOid;
Context.LogMessage(sOid + "=" + oSnmpResponse.GetValue);
}
}
Read multiple objects in one request
var oSnmpRqst = new ActiveXObject("CoreAsp.SnmpRqst");
var nDeviceID = Context.GetProperty("DeviceID");
var oComResult = oSnmpRqst.Initialize(nDeviceID);
if (oComResult.Failed)
{
Context.LogMessage(oComResult.GetErrorMsg);
}
// Get three objects in one packet:
var oSnmpResponse = oSnmpRqst.GetMultiple("1.3.6.1.2.1.1.1.0,1.3.6.1.2.1.1.2.0,1.3.6.1.2.1.1.3.0");
if (oSnmpResponse.Failed)
{
Context.LogMessage("Failure. Error=" + oSnmpResponse.GetErrorMsg);
}
else
{
var sXML = oSnmpResponse.GetPayload;
var objXMLDocument = new ActiveXObject("Microsoft.XMLDOM");
objXMLDocument.async = false;
objXMLDocument.loadXML(sXML);
var oVarBinds = objXMLDocument.getElementsByTagName("SnmpVarBind");
// For each variable binding, log OID=VALUE
for (var i=0; i<oVarBinds.length; i++)
{
Context.LogMessage(oVarBinds(i).getAttribute("sOid") + "=" + oVarBinds(i).getAttribute("sValue"));
}
}
Reboot a Cisco device using Set
Note: As of WhatsUp Gold v14, SNMP values can be set using the built-in SNMP Set Action. For more information, see Using an SNMP Set Action.
var oSnmpRqst = new ActiveXObject("CoreAsp.SnmpRqst");
var nDeviceID = 150;
var oComResult = oSnmpRqst.Initialize(nDeviceID);
if (oComResult.Failed)
{
Context.LogMessage(oComResult.GetErrorMsg);
}
var oSnmpResponse = oSnmpRqst.Set("1.3.6.1.4.1.9.2.9.9.0", 'i', 2); /* reload */
if (oSnmpResponse.Failed)
{
Context.LogMessage("Failure. Error=" + oSnmpResponse.GetErrorMsg);
}
else
{
Context.LogMessage("Success. Polled " + oSnmpResponse.GetOid + ", got " + oSnmpResponse.GetValue);
}
See Also |