From b50dfd7a55e9281bda295abdc7a984a27cbc64fa Mon Sep 17 00:00:00 2001 From: ArnoHand Date: Thu, 21 Mar 2019 19:34:48 +0800 Subject: [PATCH] =?UTF-8?q?windows=E9=A6=96=E9=A1=B5=E5=BC=80=E5=90=AF?= =?UTF-8?q?=E7=9B=91=E5=90=AC=E4=BF=AE=E6=94=B9?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../jpom/controller/WelcomeController.java | 38 +-------------- .../cn/keepbx/jpom/socket/top/TopManager.java | 46 ++++++++++++++++++- 2 files changed, 45 insertions(+), 39 deletions(-) diff --git a/src/main/java/cn/keepbx/jpom/controller/WelcomeController.java b/src/main/java/cn/keepbx/jpom/controller/WelcomeController.java index 2773b00b8..9ad29a843 100644 --- a/src/main/java/cn/keepbx/jpom/controller/WelcomeController.java +++ b/src/main/java/cn/keepbx/jpom/controller/WelcomeController.java @@ -6,17 +6,12 @@ import cn.keepbx.jpom.common.BaseController; import cn.keepbx.jpom.common.commander.AbstractCommander; import cn.keepbx.jpom.model.UserModel; import cn.keepbx.jpom.socket.top.TopManager; -import com.alibaba.fastjson.JSONArray; -import com.alibaba.fastjson.JSONObject; -import com.sun.management.OperatingSystemMXBean; import org.springframework.http.MediaType; import org.springframework.stereotype.Controller; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RequestMethod; import org.springframework.web.bind.annotation.ResponseBody; -import java.lang.management.ManagementFactory; - /** * 欢迎页 * @@ -42,38 +37,7 @@ public class WelcomeController extends BaseController { String s = AbstractCommander.getInstance().execCommand("top -b -n 1"); topInfo = TopManager.getTopInfo(s); } else { -// https://docs.oracle.com/javase/7/docs/jre/api/management/extension/com/sun/management/OperatingSystemMXBean.html - OperatingSystemMXBean operatingSystemMXBean = (OperatingSystemMXBean) ManagementFactory.getOperatingSystemMXBean(); - long totalPhysicalMemorySize = operatingSystemMXBean.getTotalPhysicalMemorySize(); - long freePhysicalMemorySize = operatingSystemMXBean.getFreePhysicalMemorySize(); - //最近系统cpu使用量 - double systemCpuLoad = operatingSystemMXBean.getSystemCpuLoad(); - if (systemCpuLoad <= 0) { - systemCpuLoad = 0; - } - JSONObject jsonObject = new JSONObject(); - jsonObject.put("top", true); - JSONArray memory = new JSONArray(); - JSONObject mem1 = new JSONObject(); - mem1.put("name", "占用内存"); - mem1.put("value", totalPhysicalMemorySize - freePhysicalMemorySize); - JSONObject mem2 = new JSONObject(); - mem2.put("name", "空闲内存"); - mem2.put("value", freePhysicalMemorySize); - memory.add(mem1); - memory.add(mem2); - JSONObject cpu1 = new JSONObject(); - cpu1.put("name", "占用cpu"); - cpu1.put("value", systemCpuLoad); - JSONObject cpu2 = new JSONObject(); - cpu2.put("name", "空闲cpu"); - cpu2.put("value", 1 - systemCpuLoad); - JSONArray cpus = new JSONArray(); - cpus.add(cpu1); - cpus.add(cpu2); - jsonObject.put("memory", memory); - jsonObject.put("cpu", cpus); - topInfo = jsonObject.toJSONString(); + topInfo = TopManager.getWindowsTop(); } } catch (Exception e) { DefaultSystemLog.ERROR().error(e.getMessage(), e); diff --git a/src/main/java/cn/keepbx/jpom/socket/top/TopManager.java b/src/main/java/cn/keepbx/jpom/socket/top/TopManager.java index d2c7aa223..31787ad3e 100644 --- a/src/main/java/cn/keepbx/jpom/socket/top/TopManager.java +++ b/src/main/java/cn/keepbx/jpom/socket/top/TopManager.java @@ -10,9 +10,11 @@ import cn.keepbx.jpom.service.manage.CommandService; import cn.keepbx.jpom.socket.SocketSessionUtil; import com.alibaba.fastjson.JSONArray; import com.alibaba.fastjson.JSONObject; +import com.sun.management.OperatingSystemMXBean; import javax.websocket.Session; import java.io.IOException; +import java.lang.management.ManagementFactory; import java.util.HashSet; import java.util.Iterator; import java.util.Set; @@ -63,8 +65,13 @@ public class TopManager { CronUtil.remove(CRON_ID); CronUtil.schedule(CRON_ID, "0/5 * * * * ?", () -> { try { - String result = AbstractCommander.getInstance().execCommand("top -b -n 1"); - String topInfo = getTopInfo(result); + String topInfo; + if (AbstractCommander.OS_INFO.isLinux()) { + String result = AbstractCommander.getInstance().execCommand("top -b -n 1"); + topInfo = getTopInfo(result); + } else { + topInfo = getWindowsTop(); + } send(topInfo); } catch (Exception e) { DefaultSystemLog.ERROR().error(e.getMessage(), e); @@ -77,6 +84,41 @@ public class TopManager { watch = true; } + public static String getWindowsTop() { +// https://docs.oracle.com/javase/7/docs/jre/api/management/extension/com/sun/management/OperatingSystemMXBean.html + OperatingSystemMXBean operatingSystemMXBean = (OperatingSystemMXBean) ManagementFactory.getOperatingSystemMXBean(); + long totalPhysicalMemorySize = operatingSystemMXBean.getTotalPhysicalMemorySize(); + long freePhysicalMemorySize = operatingSystemMXBean.getFreePhysicalMemorySize(); + //最近系统cpu使用量 + double systemCpuLoad = operatingSystemMXBean.getSystemCpuLoad(); + if (systemCpuLoad <= 0) { + systemCpuLoad = 0; + } + JSONObject jsonObject = new JSONObject(); + jsonObject.put("top", true); + JSONArray memory = new JSONArray(); + JSONObject mem1 = new JSONObject(); + mem1.put("name", "占用内存"); + mem1.put("value", totalPhysicalMemorySize - freePhysicalMemorySize); + JSONObject mem2 = new JSONObject(); + mem2.put("name", "空闲内存"); + mem2.put("value", freePhysicalMemorySize); + memory.add(mem1); + memory.add(mem2); + JSONObject cpu1 = new JSONObject(); + cpu1.put("name", "占用cpu"); + cpu1.put("value", systemCpuLoad); + JSONObject cpu2 = new JSONObject(); + cpu2.put("name", "空闲cpu"); + cpu2.put("value", 1 - systemCpuLoad); + JSONArray cpus = new JSONArray(); + cpus.add(cpu1); + cpus.add(cpu2); + jsonObject.put("memory", memory); + jsonObject.put("cpu", cpus); + return jsonObject.toJSONString(); + } + public static String getTopInfo(String content) { if (StrUtil.isEmpty(content)) { return "top查询失败";