public static ServerInfoFormMap usage(Sigar sigar) { ServerInfoFormMap monitorMap = new ServerInfoFormMap(); try { Runtime r = Runtime.getRuntime(); monitorMap.put( "jvmUsage", Math.round( Common.div(r.totalMemory() - r.freeMemory(), r.totalMemory(), 2) * 100)); // JVM使用率 Mem mem = sigar.getMem(); // 内存总量 monitorMap.put( "ramUsage", Math.round(Common.div(mem.getUsed(), mem.getTotal(), 2) * 100)); // 内存使用率 List<ServerInfoFormMap> cpu = cpuInfos(sigar); double b = 0.0; for (ServerInfoFormMap m : cpu) { b += Double.valueOf(m.get("cpuTotal") + ""); } monitorMap.put("cpuUsage", Math.round(b / cpu.size())); // cpu使用率 } catch (Exception e) { } return monitorMap; }
public static ServerInfoFormMap memory(Sigar sigar) { ServerInfoFormMap monitorMap = new ServerInfoFormMap(); try { Runtime r = Runtime.getRuntime(); monitorMap.put("jvmTotal", Common.div(r.totalMemory(), (1024 * 1024), 2) + "M"); // java总内存 monitorMap.put( "jvmUse", Common.div(r.totalMemory() - r.freeMemory(), (1024 * 1024), 2) + "M"); // JVM使用内存 monitorMap.put("jvmFree", Common.div(r.freeMemory(), (1024 * 1024), 2) + "M"); // JVM剩余内存 monitorMap.put( "jvmUsage", Common.div(r.totalMemory() - r.freeMemory(), r.totalMemory(), 2)); // JVM使用率 Mem mem = sigar.getMem(); // 内存总量 monitorMap.put("ramTotal", Common.div(mem.getTotal(), (1024 * 1024 * 1024), 2) + "G"); // 内存总量 monitorMap.put("ramUse", Common.div(mem.getUsed(), (1024 * 1024 * 1024), 2) + "G"); // 当前内存使用量 monitorMap.put( "ramFree", Common.div(mem.getFree(), (1024 * 1024 * 1024), 2) + "G"); // 当前内存剩余量 monitorMap.put("ramUsage", Common.div(mem.getUsed(), mem.getTotal(), 2)); // 内存使用率 Swap swap = sigar.getSwap(); // 交换区总量 monitorMap.put("swapTotal", Common.div(swap.getTotal(), (1024 * 1024 * 1024), 2) + "G"); // 当前交换区使用量 monitorMap.put("swapUse", Common.div(swap.getUsed(), (1024 * 1024 * 1024), 2) + "G"); // 当前交换区剩余量 monitorMap.put("swapFree", Common.div(swap.getFree(), (1024 * 1024 * 1024), 2) + "G"); monitorMap.put("swapUsage", Common.div(swap.getUsed(), swap.getTotal(), 2)); // } catch (Exception e) { } return monitorMap; }