Sorry, here's the original if you want to compare to @Austin Culbertson's.
Discovery Script:
import com.jcraft.jsch.JSch
import com.santaba.agent.util.Settings
host = hostProps.get("system.hostname")
user = hostProps.get("ssh.user")
pass = hostProps.get("ssh.pass")
port = hostProps.get("ssh.port") ?: 22
cert = hostProps.get("ssh.cert") ?: '~/.ssh/id_rsa'
timeout = 15000 // timeout in milliseconds
try {
def userCmd = getCommandOutput('ps ax -o pid,user,tty')
if (userCmd[0]==2) {
println(userCmd[1])
return 2
} else {
users = userCmd[1].readLines().collectEntries{[it.tokenize(" ")[0], [it.tokenize(" ")[1],it.tokenize(" ")[2]]]}
output = getCommandOutput('ps ax -o pid,command')
if (output[0] == 2){
return 3
} else {
def processes = output[1].readLines()
processes.subList(2,processes.size()-2).each{
e = it.tokenize(" ")
pid = e[0]
command = e[1..e.size()-1].join(" ")
print("${pid}##[${pid}]${command}######")
print("pid=${pid}")
if (users[pid]){
print("&tty=${users[pid][1]}")
print("&user=${users[pid][0]}")
} else {
print("&user=UnknownUser")
}
print("\n")
}
return 0
}
}
}
catch (Exception e) {println "Unexpected Exception : " + e; return 1}
def getCommandOutput(String input_command) {
try {
jsch = new JSch()
if (user && !pass) {jsch.addIdentity(cert)}
session = jsch.getSession(user, host, port)
session.setConfig("StrictHostKeyChecking", "no")
String authMethod = Settings.getSetting(Settings.SSH_PREFEREDAUTHENTICATION, Settings.DEFAULT_SSH_PREFEREDAUTHENTICATION)
session.setConfig("PreferredAuthentications", authMethod)
session.setTimeout(timeout)
if (pass) {session.setPassword(pass)}
session.connect()
channel = session.openChannel("exec")
channel.setCommand(input_command)
def commandOutput = channel.getInputStream()
channel.connect()
def output = commandOutput.text
channel.disconnect()
return [0,output]
}
catch (Exception e) {
e.printStackTrace()
return [2,"Error in SSH connection"]
}
finally {session.disconnect()}
}
Collection Script:
import com.jcraft.jsch.JSch
import com.santaba.agent.util.Settings
host = hostProps.get("system.hostname")
user = hostProps.get("ssh.user")
pass = hostProps.get("ssh.pass")
port = hostProps.get("ssh.port") ?: 22
cert = hostProps.get("ssh.cert") ?: '~/.ssh/id_rsa'
timeout = 15000 // timeout in milliseconds
try {
def output = getCommandOutput('ps aux')
if (output[0] == 2){
println(output[1])
return 2
} else {
output = output[1].readLines()
output.subList(2,output.size()-2).each{line ->
e = line.tokenize(" ")
println("${e[1]}.cpu: ${e[2]}")
println("${e[1]}.mem: ${e[3]}")
println("${e[1]}.vsz: ${e[4]}")
println("${e[1]}.rss: ${e[5]}")
}
return 0
}
}
catch (Exception e) {println "Unexpected Exception : " + e; return 1}
def getCommandOutput(String input_command) {
try {
jsch = new JSch()
if (user && !pass) {jsch.addIdentity(cert)}
session = jsch.getSession(user, host, port)
session.setConfig("StrictHostKeyChecking", "no")
String authMethod = Settings.getSetting(Settings.SSH_PREFEREDAUTHENTICATION, Settings.DEFAULT_SSH_PREFEREDAUTHENTICATION)
session.setConfig("PreferredAuthentications", authMethod)
session.setTimeout(timeout)
if (pass) {session.setPassword(pass)}
session.connect()
channel = session.openChannel("exec")
channel.setCommand(input_command)
def commandOutput = channel.getInputStream()
channel.connect()
def output = commandOutput.text
channel.disconnect()
return [0,output]
}
catch (Exception e) {
e.printStackTrace()
return [2,"Problem with SSH connection"]
}
finally {session.disconnect()}
}
I have these discovery filters setup to make it easy to pick and choose which processes are monitored based on properties set on the group/resource:
I also group by user because that's often helpful: