Embedded Groovy Scripting

Last updated on 25 January, 2024

Overview

LogicMonitor supports embedded scripting throughout our product using the Groovy programming language. Groovy can be used in DataSources for Scripted Active Discovery, Scripted Data Collection, and Complex Datapoint Post-Processing; in Scripted EventSources; ConfigSources; and Netscan Policies.

We favor Groovy primarily because—as an extension of Java—it is run entirely within the LogicMonitor Collector, so we can guarantee that it will run the same way across all collectors regardless of underlying OS platform and version. Also, we’ve bundled into our Collector a wide variety of helper classes specifically selected to help get instrumentation data out of systems and devices.

Using Device and Instance Properties in Groovy Scripts

Another advantage of Groovy is that we support the use of any device or instance property within your script. In other languages we can pass in device properties but not instance properties.

The following methods support ingest of properties.

  • hostProps.get() – Return the specified device property. For example: name = hostProps.get("system.hostname").
  • instanceProps.get() – Return the specified instance property. For example: instance = instanceProps.get("wildvalue").
  • taskProps.get() – Return the specified property from either the device property or instance property table; in case of a property name collision, return the value set at the instance level. For example: speed=taskProps.get("speed") will get the value of the speed property if set on the instance; else if it is not set it will get the value set on the device.

Note: The taskProps.get() and instanceProps.get() methods should not be used when BatchScript data collection method is specified (as opposed to the Script data collection method) because BatchScript context spans multiple instances and there is no way to indicate which instance for which you want to set the instance level property.

Example: Using Groovy for DataSource Active Discovery

The following is sample Groovy code used to perform scripted Active Discovery. In scripted Active Discovery, the goal is to return instance data of the form:

instance_id##instance_name

This particular script is used to discover power source instances on a Palo Alto firewall:

 
import com.santaba.agent.groovyapi.http.*;
apikey = hostProps.get("paloalto.apikey.pass")
host = hostProps.get("system.hostname")
command = java.net.URLEncoder.encode("")
url = "https://${host}/api/?type=op&key=${apikey}&cmd=${command}"
def response = new XmlSlurper().parseText(HTTP.body(url))
// loop through slots
response.result.power.children().each { slot ->
  slot_name = slot.name()
  // loop through each slot's components
  slot.entry.each { entry ->
    entry_desc    = entry.description.text()
    instance_name = slot_name + '/' + entry_desc
    println "${instance_name}##${instance_name}"
  }
}
return(0)

See Groovy Tips & Tricks and Groovy/Expect Text-Based Interaction for more examples and sample code.

Support for Groovy Lib v2 and v4

In addition to Groovy lib v2, collector also supports Groovy lib v4. Starting with EA Collector 34.500, you can use both Groovy lib v2 and v4. The first line is added as a comment (//) to define the groovy version to run the script.

Groovy lib v2

//!/lib-groovy/v2
println "version=" + GroovySystem.version

Groovy lib v4

//!/lib-groovy/v4
println "version=" + GroovySystem.version

Note: If you do not define the version comment, by default the script uses Groovy lib v2.

In This Article