public String sampleServerResource() {
    String serverInfo = "";

    // storage
    String path = "/data";
    File file = new File(path);
    if (file.exists()) {
        serverInfo += String.format("Storage Info : path %s, total %s, free %s, usable %s <br>", 
                file.getAbsoluteFile(), addUnit(file.getTotalSpace()), addUnit(file.getFreeSpace()), addUnit(file.getUsableSpace()));
    }

    // CPU
    OperatingSystemMXBean osBean = ManagementFactory.getOperatingSystemMXBean();
    serverInfo += String.format("CPU Info : name %s, arch %s, version %s, avail %s, load average %s <br>", 
            osBean.getName(), osBean.getArch(), osBean.getVersion(), osBean.getAvailableProcessors(), osBean.getSystemLoadAverage());

    // Memory
    MemoryMXBean memoryMXBean = ManagementFactory.getMemoryMXBean();
    MemoryUsage heap = memoryMXBean.getHeapMemoryUsage();
    serverInfo += String.format("Heap Memory Info : init %s, used %s, commited %s, max %s <br>", 
            addUnit(heap.getInit()), addUnit(heap.getUsed()), addUnit(heap.getCommitted()), addUnit(heap.getMax()));
    MemoryUsage noneHeap = memoryMXBean.getNonHeapMemoryUsage();
    serverInfo += String.format("Heap None Memory Info : init %s, used %s, commited %s, max %s <br>", 
            addUnit(noneHeap.getInit()), addUnit(noneHeap.getUsed()), addUnit(noneHeap.getCommitted()), addUnit(noneHeap.getMax()));

    return serverInfo;
}

private String addUnit(long number) {
    String unitStr = "";
    if(number < 1024) return number + " byte";
    number /= 1024;
    if(number < 1024) return number + " Kb";
    number /= 1024;
    if(number < 1024) return number + " Mb";
    number /= 1024;
    if(number < 1024) return number + " Gb";
    number /= 1024;
    return number + " Tb";
}

 

시스템 자체의 CPU 사용량과 메모리 사용량을 보기 위해서는

oracle jdk 가 필요하다.

+ Recent posts