Groovy SNMP Access

Last updated on 09 November, 2023

For some types of SNMP monitoring, you’ll need to resort to Groovy to get at your SNMP data. For example, a typical use case is a DataSource that requires aggregation of SNMP values across various nodes.

A complete reference of LogicMonitor’s Groovy SNMP methods can be found at the bottom of this page, but here are a few examples to demonstrate how this feature works.

Example 1 – Obtain a device’s sysDescr using SNMP v1/v2c

// import the logicmonitor snmp helper class
import com.santaba.agent.groovyapi.snmp.Snmp;

// get the snmp host from the device properties
def host = hostProps.get('system.hostname');

// retrieve the value for the specified OID
def oid_value = Snmp.get(host, ".1.3.6.1.2.1.1.1.0");

// output the value
println oid_value;

// return with a response code that indicates we ran successfully
return (0);

This script will output something like the following:

Linux localhost.localdomain 2.6.18-194.26.1.el5 #1 SMP Tue Nov 9 12:54:40 EST 2010 i686

Example 2 – List all Network interfaces of a Linux Device using SNMP v1

// import the logicmonitor snmp helper class
import com.santaba.agent.groovyapi.snmp.Snmp;

// get the snmp host & community from the device properties
def host = hostProps.get('system.hostname');
def comm = hostProps.get('snmp.community');

// walk the oid tree
def snmp_walk = Snmp.walk(host, comm, "v1", ".1.3.6.1.2.1.2.2.1.2");

// display the raw output
println "Raw output from Snmp.walk:\n" + snmp_walk;

// display each network interface
println "\nNICs:"

// iterate over each line returned by the oid tree
snmp_walk.eachLine 
{ line ->
    // tokenize the line, separating the leaf oid from its value
    tokens = line.split(/ = /, 2);

    // print the value
    println tokens[1];
}

// return with a response code that indicates we ran successfully
return (0);

The output from the above looks something like:

Raw output from Snmp.walk:
.1.3.6.1.2.1.2.2.1.2.3 = sit0
.1.3.6.1.2.1.2.2.1.2.2 = eth0
.1.3.6.1.2.1.2.2.1.2.1 = lo

NICs:
sit0
eth0
lo

Example 3 – List all Network Interfaces of a Linux Device using SNMP v3

// import the logicmonitor snmp helper class
import com.santaba.agent.groovyapi.snmp.Snmp;

// get the snmp host from the device properties
def host = hostProps.get('system.hostname');

// populate a hashmap of snmpv3 parameters as key/value pairs
snmp_options = new HashMap();
snmp_options.put("snmp.version","v3");
snmp_options.put("snmp.security","LogicMonitor");
snmp_options.put("snmp.auth","MD5");
snmp_options.put("snmp.authToken","exampleauth");
snmp_options.put("snmp.priv","DES");
snmp_options.put("snmp.privToken","examplepriv");

// do the snmp walk
def snmp_walk = Snmp.walk(host, ".1.3.6.1.2.1.2.2.1.2", snmp_options, 3000)

// do something with the output...

The above script behaves just like the preceding example, but uses SNMP v3 authentication & encryption.

SNMP Method Reference

SNMP v1/v2

Snmp.get(host, [community], [version], oid, [timeout]) – get a single SNMP OID value from a v1 or v2 agent

  • @param string host – device hostname or ip address
  • @param string community – optional snmp community name. If omitted, LogicMonitor will attempt to determine the appropriate value from the snmp.community property on the corresponding device record and otherwise default to “public”
  • @param string version – optional snmp version; required only if community is provided. If omitted, LogicMonitor will attempt to determine the appropriate value from the snmp.version property on the corresponding device record
  • @param string oid – the snmp oid to query
  • @param int timeout – connection timeout in milliseconds
  • @return string oid_value the value of this SNMP OID as returned by the agent

Snmp.walk(host, [community], [version], tree_oid, [timeout], port) – walk an SNMP OID tree from a v1 or v2 agent

  • @param string host – device hostname or ip address
  • @param string community – (optional) snmp community name. If omitted, LogicMonitor will attempt to determine the appropriate value from the snmp.community property on the corresponding device record and otherwise default to “public”
  • @param string version – (optional) snmp version; required only if community is provided. If omitted, LogicMonitor will attempt to determine the appropriate value from the snmp.version property on the corresponding device record
  • @param string tree_oid – the snmp tree oid to walk
  • @param int timeout –  (optional) connection timeout in milliseconds
  • @return string walk_output the OIDs and values returned by the SNMP walk separated by “=” with one entry per line
  • @param string port– (optional) the port to which you want access.

SNMP v3

Snmp.get(host, oid, [options], [timeout]) – get a single SNMP OID value from a v3 agent

  • @param string host – device hostname or ip address
  • @param string oid – the snmp oid to query
  • @param map options – optional snmp v3 options map name. If omitted, LogicMonitor will attempt to determine the appropriate values from properties on the device record matching the hostname supplied in the first parameter.
  • @param int timeout – connection timeout in milliseconds
  • @return string oid_value the value of this SNMP OID as returned by the agent

Snmp.walk(host, tree_oid, [options], [timeout]) – walk an SNMP OID tree from a v3 agent

  • @param string host – device hostname or ip address
  • @param string tree_oid – the snmp tree oid to walk
  • @param map options – optional snmp v3 options map name. If omitted, LogicMonitor will attempt to determine the appropriate values from properties on the device record matching the hostname supplied in the first parameter.
  • @param int timeout – connection timeout in milliseconds
  • @return string walk_output the OIDs and values returned by the SNMP walk separated by “=” with one entry per line
  • @param string port– (optional) the port to which you want access.

The SNMP v3 options map requires the following parameters:

  • snmp.version – the version used (v3)
  • snmp.security – the snmp v3 security name
  • snmp.auth – the snmp v3 authentcation type (MD5|SHA)
  • snmp.authToken – the snmp v3 authentcation passphrase
  • snmp.priv – the snmp v3 privacy type (AES|DES)
  • snmp.privToken – the snmp v3 privacy encryption key
In This Article