Ive modified a GroovyEpaAgent script that I got, to monitor every JVM process on a unix machine.This one was tested on a PP64 (suse), and send the CPU usage of every JAVA process listening on the server.
Hopefully will be usefull for someone too.
/*
* cpuWebLogic.groovy
*
* Author: Michael F. Aube, CA Wily
* Modified by: Gustavo Benitez gabenitez@gmail.com
* Created: 17 December, 2008
*
* This EPAgent Groovy script was originally designed to monitor the CPU Utilization of
* each and every WebLogic.
*
* Then, it was modified to monitor the CPU usage of all the JAVA processes on the server.
* This script was tested on a PPC64.
* ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
* IMPORTANT NOTE:
* ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
* Since Introscope can only capture INTEGER VALUES for these CPU metrics,
* a given JVM may have a CPU Utilization metric value of ZERO, when in
* fact it has a "real" value between 0.0 and 0.4 percent CPU Utilization.
* ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
*/
class osCommand
{
String cmd
String output
List lines
/** Answers a new O/S command; ready to be invoked. **/
osCommand (String aCommand)
{
this.cmd = aCommand
}
/** Answers a list of the output lines generated by this command. **/
List execute()
{
this.output = ""
this.lines = []
Process process = cmd.execute()
process.waitFor()
if (process.exitValue())
print process.err.text // this can't be good...
else
output = process.text
//print "Resultado de la ejecucion:" + output
this.lines = output.split(/\n/)
return this.lines
}
}
class JvmMetric
{
String cpuPct // CPU Utilization %, rounded to nearest integer
String pid // the JVM's O/S Process ID
private String nameAttr // an XML attribute for an Introscope Metric
private String valueAttr // an XML attribute for an Introscope Metric
private String typeAttr // an XML attribute for an Introscope Metric
JvmMetric (String aLine)
{
cpuPct = "0"
pid = this.extractPID(aLine)
def port = this.extractPort(aLine)
def name=this.extractName(aLine)
nameAttr =/name='$name'/
typeAttr = "type='IntAverage'"
valueAttr = "" // TBD - need to know the cpuPct first!
}
String asXML() // answer this metric as XML suitable for the Introscope EM
{
valueAttr = "value='$cpuPct'" // assumes cpuPct has been set!
return ""
}
private String extractPID(String aLine)
{
def fields = aLine.split(/\s+/)
return fields[1]
}
private String extractName(String aLine)
{
def texto=aLine
def start = 62
def end = (texto.size())
def tFinal=texto.substring(start,end)
//Just add here a better formating.
//A good one may be to and a Dummy parameter to the JVM startup script called AP_NAME, and extrat that value to show at the
//metric tree.
tFinal=tFinal.replaceAll("\\.",".")
tFinal=tFinal.replaceAll("\\=",".")
tFinal=tFinal.replaceAll("\\:",".")
tFinal=tFinal.replaceAll("\\/",".")
tFinal=tFinal.replaceAll("\\-",".")
return tFinal
}
private String extractPort(String aLine)
{
def pattern = '-Dweblogic.Name='
def start = aLine.indexOf(pattern) + pattern.size()
def stop = aLine.indexOf(' ', start) - 1 // ASSUMES a space at the end!
return aLine.getAt(start..stop)
}
}
// **************************
// BEGIN AGENT SCRIPT HERE...
// **************************
//Etract all the PIDs that belongs to a JVM instance on the server.
def commandStr= /netstat -lnptu|grep java|awk -F' ' '{print $7}' |awk -F\/ '{print $1}'|awk '{RS="\n";puertos[$1]=$1;}END{for(i in puertos){ORS=",";print puertos[i]}}'|cut -c2-|sed 's\/\(.*\).\/\1\/'/
command = ["sh", "-c", commandStr]
process = command.execute();
process.waitFor()
pids = process.text
//Gets the CPU usage of all the java proceses that we got eearlier
prstat = ["sh", "-c", "top -c -b -n 1 -p "+ pids]
Process process = prstat.execute()
process.waitFor()
cpuLines = process.text
lines=cpuLines.split(/\n/)
//Send the metrics to the EM
(7..lines.size() -1 ).each { index ->
metric = new JvmMetric(lines[index].trim())
fields = lines[index].trim().split(/\s+/)
cpu = fields[8]
n = Math.round(new Float(cpu))
metric.setCpuPct(n.toString())
// send the metric XML to the Introscope Enterprise Manager
EMOUTPUT.println metric.asXML()
}
// **************************
// END AGENT SCRIPT HERE.
// **************************