Skip to content

Instantly share code, notes, and snippets.

@yunyu
Created May 23, 2017 18:22
Show Gist options
  • Save yunyu/7f389bc703392416b6835e2d0887f090 to your computer and use it in GitHub Desktop.
Save yunyu/7f389bc703392416b6835e2d0887f090 to your computer and use it in GitHub Desktop.
import io.vertx.core.Handler;
import io.vertx.core.json.JsonArray;
import io.vertx.core.json.JsonObject;
import io.vertx.ext.web.RoutingContext;
import java.lang.management.ManagementFactory;
import com.sun.management.OperatingSystemMXBean;
import java.lang.management.RuntimeMXBean;
public class SimpleJMXWriter implements Handler<RoutingContext> {
private static final OperatingSystemMXBean osBean = (OperatingSystemMXBean) ManagementFactory.getOperatingSystemMXBean();
private static final RuntimeMXBean runtimeBean = ManagementFactory.getRuntimeMXBean();
@Override
public void handle(RoutingContext ctx) {
JsonArray statisticsRequested = ctx.getBodyAsJson().getJsonArray("statistics");
JsonObject response = new JsonObject();
for (Object obj : statisticsRequested.getList()) {
if (obj instanceof String) {
writeStatistic((String) obj, response);
}
}
ctx.response().putHeader("Content-Type", "application/json").end(response.toString());
}
private void writeStatistic(String statistic, JsonObject jsonObject) {
// Autogenerated
// Could use cached reflection but slower
switch (statistic) {
case "availableProcessors":
jsonObject.put(statistic, osBean.getAvailableProcessors());
break;
case "arch":
jsonObject.put(statistic, osBean.getArch());
break;
case "name":
jsonObject.put(statistic, osBean.getName());
break;
case "systemLoadAverage":
jsonObject.put(statistic, osBean.getSystemLoadAverage());
break;
case "version":
jsonObject.put(statistic, osBean.getVersion());
break;
case "committedVirtualMemorySize":
jsonObject.put(statistic, osBean.getCommittedVirtualMemorySize());
break;
case "freePhysicalMemorySize":
jsonObject.put(statistic, osBean.getFreePhysicalMemorySize());
break;
case "freeSwapSpaceSize":
jsonObject.put(statistic, osBean.getFreeSwapSpaceSize());
break;
case "processCpuLoad":
jsonObject.put(statistic, osBean.getProcessCpuLoad());
break;
case "processCpuTime":
jsonObject.put(statistic, osBean.getProcessCpuTime());
break;
case "systemCpuLoad":
jsonObject.put(statistic, osBean.getSystemCpuLoad());
break;
case "totalPhysicalMemorySize":
jsonObject.put(statistic, osBean.getTotalPhysicalMemorySize());
break;
case "totalSwapSpaceSize":
jsonObject.put(statistic, osBean.getTotalSwapSpaceSize());
break;
case "startTime":
jsonObject.put(statistic, runtimeBean.getStartTime());
break;
case "uptime":
jsonObject.put(statistic, runtimeBean.getUptime());
break;
case "vmVersion":
jsonObject.put(statistic, runtimeBean.getVmVersion());
break;
default:
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment