!59 windows首页开启监听修改

Merge pull request !59 from Arno/dyc
This commit is contained in:
Arno 2019-03-21 19:35:25 +08:00
commit 1160eec5f7
2 changed files with 45 additions and 39 deletions

View File

@ -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);

View File

@ -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查询失败";