package nz.org.geonet.metrics.collector;

import java.util.HashMap;

/**
 * Collects basic JVM metrics.
 *
 * @author Geoff Clitheroe
 *         Date: 8/16/13
 *         Time: 12:30 PM
 */
public class JVMCollector implements Collector {

    private final MetricsClient metricsClient;
    private final long collectionInterval;

    public JVMCollector(MetricsClient metricsClient, long collectionInterval) {
        this.metricsClient = metricsClient;
        this.collectionInterval = collectionInterval;
    }

    public HashMap<String, Number> gather() {
        HashMap<String, Number> metrics = jvmHeapMemoryUsage();
        metrics.putAll(jvmPermGen());
        metrics.putAll(jvmThreads());
        metrics.putAll(classLoading());

        return metrics;
    }

    public long collectionInterval() {
        return collectionInterval;
    }

    public String serverType() {
        return ServerType.UnknownJVM.name();
    }

    HashMap<String, Number> jvmHeapMemoryUsage() {

        HashMap<String, Number> responseMap = metricsClient.read("java.lang:type=Memory", "HeapMemoryUsage");

        HashMap<String, Number> result = new HashMap<String, Number>();

            if (responseMap.containsKey("used")) {
                result.put("HeapMemoryUsage.used", responseMap.get("used"));
            }

            if (responseMap.containsKey("max")) {
                result.put("HeapMemoryUsage.max", responseMap.get("max"));
            }

        return result;
    }

    HashMap<String, Number> jvmPermGen() {

        HashMap<String, Number> responseMap = metricsClient.read("java.lang:type=MemoryPool,name=PS Perm Gen", "Usage");

        HashMap<String, Number> result = new HashMap<String, Number>();

            if (responseMap.containsKey("used")) {
                result.put("PermGen.used", responseMap.get("used"));
            }

            if (responseMap.containsKey("max")) {
                result.put("PermGen.max", responseMap.get("max"));
            }

        return result;
    }

    HashMap<String, Number> jvmThreads() {

        HashMap<String, Number> responseMap = metricsClient.read("java.lang:type=Threading", null);

        HashMap<String, Number> result = new HashMap<String, Number>();

        if (responseMap.containsKey("ThreadCount")) {
            result.put("Threading.used", responseMap.get("ThreadCount"));
        }

        if (responseMap.containsKey("DaemonThreadCount")) {
            result.put("Threading.max", responseMap.get("DaemonThreadCount"));
        }

        return result;
    }

    HashMap<String, Number> classLoading() {

        HashMap<String, Number> responseMap = metricsClient.read("java.lang:type=ClassLoading", null);

        HashMap<String, Number> result = new HashMap<String, Number>();

        if (responseMap.containsKey("TotalLoadedClassCount")) {
            result.put("Classes.totalLoaded", responseMap.get("TotalLoadedClassCount"));
        }

        return result;
    }

}

@@ -733,16 +733,51 @@
 ding());
+%0A        metrics.putAll(cpuLoad());
 %0A%0A      
@@ -959,32 +959,595 @@
 .name();%0A    %7D%0A%0A
+    HashMap%3CString, Number%3E cpuLoad() %7B%0A        HashMap%3CString, Number%3E responseMap = metricsClient.read(%22java.lang:type=OperatingSystem%22, %22ProcessCpuLoad,SystemCpuLoad%22);%0A%0A        HashMap%3CString, Number%3E result = new HashMap%3CString, Number%3E();%0A%0A        if (responseMap.containsKey(%22ProcessCpuLoad%22)) %7B%0A            result.put(%22ProcessCpuLoad%22, responseMap.get(%22ProcessCpuLoad%22));%0A        %7D%0A%0A        if (responseMap.containsKey(%22SystemCpuLoad%22)) %7B%0A            result.put(%22SystemCpuLoad%22, responseMap.get(%22SystemCpuLoad%22));%0A        %7D%0A%0A        return result;%0A    %7D%0A%0A
     HashMap%3CStri

Add system and process CPU load.