1. 下载zabbix java gateway源码。
2. 修改zabbix-java源码
3.修改JMXItemChecker,增加如下代码:
/**
* @des get cpu usage
* @return
*/
private float getCpuUsage()
{
float cpuUsage = 0;
try
{
ObjectName operateObjectName = new ObjectName("java.lang:type=OperatingSystem");
ObjectName runtimeObjName = new ObjectName("java.lang:type=Runtime");
long processCpuTime = Long.parseLong(mbsc.getAttribute(operateObjectName, "ProcessCpuTime").toString());
int availableProcessors = Integer.parseInt(mbsc.getAttribute(operateObjectName, "AvailableProcessors").toString());
long upTime = Long.parseLong(mbsc.getAttribute(runtimeObjName, "Uptime").toString());
logger.info("===2======processCpuTime: " + processCpuTime);
logger.info("===2=====upTime: " + upTime);
if (prevUpTime > 0L && upTime > prevUpTime)
{
logger.info("===3=======");
long elapsedCpu = processCpuTime - prevProcessCpuTime;
long elapsedTime = upTime - prevUpTime;
// cpuUsage could go higher than 100% because elapsedTime
// and elapsedCpu are not fetched simultaneously. Limit to
// 99% to avoid Plotter showing a scale from 0% to 200%.
cpuUsage = Math.min(99F, elapsedCpu / (elapsedTime * 10000F * availableProcessors));
logger.info("===2=====cpuUsage: " + cpuUsage);
}
logger.info("===4=======");
prevUpTime = upTime;
prevProcessCpuTime = processCpuTime;
}
catch(Exception e)
{
e.printStackTrace();
}
return cpuUsage;
}