Forum Discussion
Thanks. I managed to get the following together which seems to work. I want to use it to monitor some remote devices. Thanks for the assistance!
import com.santaba.agent.groovyapi.expect.Expect;
import com.santaba.agent.groovyapi.snmp.Snmp;
import com.santaba.agent.groovyapi.http.*;
import com.santaba.agent.groovyapi.jmx.*;
import org.xbill.DNS.*;
import com.jcraft.jsch.JSch;
import com.santaba.agent.util.Settings;
hostname = hostProps.get("system.hostname")
//hostname = "www.google.com"
mymtu = hostProps.get("custom.ping.mtu")?.toInteger() ?: 1472
pingargs = '-n 4 -f -l $mymtu'
timeout = 15000 // timeout in milliseconds
try {
def command_output = "ping ${hostname} ${pingargs}".execute().text
//command_output = command_output.toString()
//println command_output
packetsSent = (((command_output.findAll(~/(?<=Sent = )\d+/)).toString()).replaceAll(/\[(\d+)\]/, '$1')).toInteger()
packetsReceived = (((command_output.findAll(~/(?<=Received = )\d+/)).toString()).replaceAll(/\[(\d+)\]/, '$1')).toInteger()
packetsLost = (((command_output.findAll(~/(?<=Lost = )\d+/)).toString()).replaceAll(/\[(\d+)\]/, '$1')).toInteger()
roundtripMinimum = (((command_output.findAll(~/(?<=Minimum = )\d+/)).toString()).replaceAll(/\[(\d+)\]/, '$1')).toInteger()
roundtripMaximum = (((command_output.findAll(~/(?<=Maximum = )\d+/)).toString()).replaceAll(/\[(\d+)\]/, '$1')).toInteger()
roundtripAverage = (((command_output.findAll(~/(?<=Average = )\d+/)).toString()).replaceAll(/\[(\d+)\]/, '$1')).toInteger()
println 'packetsSent=' + packetsSent
println 'packetsReceived=' + packetsReceived
println 'packetsLost=' + packetsLost
println 'roundtripMinimum=' + roundtripMinimum
println 'roundtripMaximum=' + roundtripMaximum
println 'roundtripAverage=' + roundtripAverage
return 0
}
catch (Exception e) {
println "Unexpected Exception : " + e
return 1
}
FYI, you don't need any of those imports. You're not using any of them, so importing them is a waste of CPU cycles.
- Stephen_C9 months agoNeophyte
Thanks for the tip, I didn't actually know that.
- Anonymous9 months ago
Yeah, whenever you start a new DS, the UI inserts all the most common imports at the beginning of the script, whether you need them or not. In almost every case, you only need one not the others. It would be actually helpful if LM could prompt you for how you need to collect data and start the script. For example, if you know you need to SSH into a device and execute a command, it would be nice for it to start with a script like this:
import com.logicmonitor.common.sse.utils.GroovyScriptHelper as GSH import com.logicmonitor.mod.Snippets def azureHost = hostProps.get("auto.network.resolves") == "false" ? hostProps.get("system.azure.privateIpAddress") : null def loader = GSH.getInstance()._getScript("Snippets", Snippets.getLoader()) def remote = loader.load("lm.remote", "0").create(hostProps) .withHost(azureHost ?: hostProps.get("system.hostname")) .withTimeout(15000) def debug = false // To run in debug mode, set to true try { def command_output = remote.exec('echo YOUR COMMAND HERE') println(command_output) return 0 } catch (Exception e) {println(e);return 1}
This is the skeleton of most of LM's own Linux SSH scripts since they made a snippet that can be imported to do most of the tricky stuff. I'm sure the ME team has a bunch of monitoring recipes that can be used as starters for many scripted datasources. It would be nice if we could walk through a wizard that helps us find a template to get started with (or at least let us browse a repo through the UI and select a template to start with).
Related Content
- 10 months ago
- 2 years ago