반응형
@SuppressWarnings("restriction")
@RequestMapping(value = "/pie", method = RequestMethod.POST)
public @ResponseBody void getServerInfo(HttpServletRequest request, HttpServletResponse response)
        throws IOException {
    OperatingSystemMXBean osBean = (com.sun.management.OperatingSystemMXBean) ManagementFactory
            .getOperatingSystemMXBean();

    double load = 0;

    load = osBean.getSystemCpuLoad();
    File f = new File("/");

    JSONObject systemInfoJson = new JSONObject();

	/** CPU **/
    systemInfoJson.put("cpuUsage", Math.round(load * 100.0)); // 사용량
    systemInfoJson.put("cpuIdle", (100 - Math.round(load * 100.0))); // 남은 용량

	/** HDD **/
    systemInfoJson.put("hddUsage", Math.round((f.getTotalSpace() - f.getUsableSpace()) / (1024 * 1024) / 1000.0)); // 사용량
    systemInfoJson.put("hddIdle", Math.round((f.getFreeSpace()) / (1024 * 1024) / 1000.0)); // 남은 용량

	/** MEMORY **/
    systemInfoJson.put("memoryTotal", Math.round(osBean.getTotalPhysicalMemorySize() / (1024 * 1024) / 1000.0)); // 사용량
    systemInfoJson.put("memoryFree", Math.round(osBean.getFreePhysicalMemorySize() / (1024 * 1024) / 1000.0)); // 남은 용량

    response.setContentType("application/json; charset=UTF-8");
    PrintWriter writer = null;

    writer = response.getWriter();
    writer.print(systemInfoJson);
    writer.flush();

}
반응형