首页监控更新

This commit is contained in:
bwcx_jzy 2019-09-19 23:03:11 +08:00
parent 5d6cc523a8
commit 9e267a662c
5 changed files with 35 additions and 238 deletions

View File

@ -4,7 +4,6 @@ import cn.hutool.cache.impl.CacheObj;
import cn.hutool.core.date.DateField;
import cn.hutool.core.date.DateTime;
import cn.hutool.core.date.DateUtil;
import cn.hutool.core.util.StrUtil;
import cn.jiangzeyin.common.JsonMessage;
import cn.jiangzeyin.controller.base.AbstractController;
import com.alibaba.fastjson.JSONObject;
@ -37,18 +36,17 @@ public class WelcomeController extends AbstractController {
}
@RequestMapping(value = "getTop", method = RequestMethod.POST, produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
public String getTop() {
public String getTop(Long millis) {
Iterator<CacheObj<String, JSONObject>> cacheObjIterator = TopManager.get();
List<JSONObject> series = new ArrayList<>();
List<String> scale = new ArrayList<>();
int count = 60;
JSONObject value = null;
int minSize = 12;
while (cacheObjIterator.hasNext()) {
CacheObj<String, JSONObject> cacheObj = cacheObjIterator.next();
String key = cacheObj.getKey();
scale.add(key);
value = cacheObj.getValue();
JSONObject value = cacheObj.getValue();
series.add(value);
}
//限定数组最大数量
@ -61,7 +59,7 @@ public class WelcomeController extends AbstractController {
scale.add(getNowNextScale());
}
String time = scale.get(scale.size() - 1);
String newTime = getNextScaleTime(time);
String newTime = getNextScaleTime(time, millis);
scale.add(newTime);
}
JSONObject object = new JSONObject();
@ -71,27 +69,6 @@ public class WelcomeController extends AbstractController {
return JsonMessage.getString(200, "", object);
}
/**
* 补空将断开的监控填空
*/
private void filling(String startTime, String endTime, List<JSONObject> data, List<String> scale, int maxSize) {
for (int i = 0; i <= maxSize; i++) {
String nextScaleTime = getNextScaleTime(startTime);
if (nextScaleTime.equals(endTime)) {
return;
}
JSONObject object = new JSONObject();
object.put("time", nextScaleTime);
data.add(object);
startTime = nextScaleTime;
scale.add(nextScaleTime);
if (i == maxSize) {
data.clear();
scale.clear();
}
}
}
/**
* 当前时间的下一个刻度
*
@ -106,9 +83,12 @@ public class WelcomeController extends AbstractController {
*
* @return String
*/
private String getNextScaleTime(String time) {
private String getNextScaleTime(String time, Long millis) {
DateTime dateTime = DateUtil.parseTime(time);
DateTime newTime = dateTime.offsetNew(DateField.SECOND, 30);
if (millis == null) {
millis = 30 * 1000L;
}
DateTime newTime = dateTime.offsetNew(DateField.SECOND, (int) (millis / 1000));
return DateUtil.formatTime(newTime);
}

View File

@ -4,21 +4,12 @@ import cn.hutool.cache.impl.CacheObj;
import cn.hutool.cache.impl.TimedCache;
import cn.hutool.core.date.DateTime;
import cn.hutool.core.date.DateUtil;
import cn.hutool.cron.CronUtil;
import cn.jiangzeyin.common.DefaultSystemLog;
import cn.jiangzeyin.pool.ThreadPoolService;
import com.alibaba.fastjson.JSONObject;
import io.jpom.common.commander.AbstractSystemCommander;
import io.jpom.model.system.ProcessModel;
import io.jpom.util.CronUtils;
import io.jpom.util.SocketSessionUtil;
import javax.websocket.Session;
import java.io.IOException;
import java.util.*;
import java.util.concurrent.ExecutorService;
import java.util.Iterator;
import java.util.LinkedHashMap;
import java.util.concurrent.TimeUnit;
import java.util.concurrent.atomic.AtomicBoolean;
/**
* top命令管理保证整个服务器只获取一个top命令
@ -27,40 +18,11 @@ import java.util.concurrent.atomic.AtomicBoolean;
* @date 2018/10/2
*/
public class TopManager {
private static final Set<Session> SESSIONS = new HashSet<>();
private static final String CRON_ID = "topMonitor";
private static ExecutorService executorService = ThreadPoolService.newCachedThreadPool(TopManager.class);
/**
* 最近30分钟监控数据
*/
private static final TimedCache<String, JSONObject> MONITOR_CACHE = new TimedCache<>(TimeUnit.MINUTES.toMillis(30), new LinkedHashMap<>());
/**
* 是否开启首页监听自动刷新
*/
private static final AtomicBoolean WATCH = new AtomicBoolean(false);
// /**
// * 添加top 命令监听
// *
// * @param session 会话
// */
// public static void addMonitor(Session session) {
// SESSIONS.add(session);
// addCron();
// }
// /**
// * 移除top 命令监控
// *
// * @param session 会话
// */
// public static void removeMonitor(Session session) {
// SESSIONS.remove(session);
// close();
// }
public static Iterator<CacheObj<String, JSONObject>> get() {
JSONObject topInfo = AbstractSystemCommander.getInstance().getAllMonitor();
if (topInfo != null) {
@ -72,88 +34,4 @@ public class TopManager {
}
return MONITOR_CACHE.cacheObjIterator();
}
// /**
// * 创建定时执行top
// */
// private static void addCron() {
// if (WATCH.get()) {
// return;
// }
// CronUtil.remove(CRON_ID);
// CronUtil.schedule(CRON_ID, "0/30 * * * * ?", () -> {
// //发送监控信息
// try {
// JSONObject topInfo = AbstractSystemCommander.getInstance().getAllMonitor();
// if (topInfo != null) {
// DateTime date = DateUtil.date();
// String time = DateUtil.formatTime(date);
// topInfo.put("time", time);
// topInfo.put("monitorTime", date.getTime());
// MONITOR_CACHE.put(time, topInfo);
// send(topInfo.toString());
// }
// } catch (Exception e) {
// DefaultSystemLog.ERROR().error(e.getMessage(), e);
// }
// //发送首页进程列表信息
// sendProcessList();
// });
// CronUtils.start();
// WATCH.set(true);
// }
// /**
// * 发送首页进程列表信息
// */
// private static void sendProcessList() {
// executorService.execute(() -> {
// List<ProcessModel> array = AbstractSystemCommander.getInstance().getProcessList();
// if (array != null) {
// JSONObject jsonObject = new JSONObject();
// jsonObject.put("processList", array);
// send(jsonObject.toJSONString());
// }
// });
// }
// /**
// * 同步发送消息
// *
// * @param content 内容
// */
// private static void send(String content) {
// synchronized (TopManager.class) {
// String htmlContent = content.replaceAll("\n", "<br/>");
// htmlContent = htmlContent.replaceAll(" ", "&nbsp;&nbsp;");
// Iterator<Session> iterator = SESSIONS.iterator();
// while (iterator.hasNext()) {
// Session session = iterator.next();
// try {
// SocketSessionUtil.send(session, htmlContent);
// } catch (IOException e) {
// DefaultSystemLog.ERROR().error("消息失败", e);
// try {
// session.close();
// iterator.remove();
// } catch (IOException ignored) {
// }
// }
// }
// close();
// }
// }
// /**
// * 关闭top监听
// */
// private static void close() {
// // 如果没有队列就停止监听
// if (SESSIONS.isEmpty()) {
// //
// CronUtil.remove(CRON_ID);
// WATCH.set(false);
// }
// }
}

View File

@ -9,6 +9,9 @@ import cn.hutool.extra.servlet.ServletUtil;
import io.jpom.common.BaseServerController;
import io.jpom.common.forward.NodeForward;
import io.jpom.common.forward.NodeUrl;
import io.jpom.model.BaseEnum;
import io.jpom.model.Cycle;
import io.jpom.model.data.NodeModel;
import io.jpom.model.log.SystemMonitorLog;
import io.jpom.service.dblog.DbSystemMonitorLogService;
import org.springframework.http.MediaType;
@ -23,6 +26,7 @@ import java.io.UnsupportedEncodingException;
import java.net.URLEncoder;
import java.nio.charset.StandardCharsets;
import java.util.List;
import java.util.concurrent.TimeUnit;
/**
* 欢迎页
@ -38,6 +42,17 @@ public class NodeWelcomeController extends BaseServerController {
@RequestMapping(value = "welcome", method = RequestMethod.GET, produces = MediaType.TEXT_HTML_VALUE)
public String welcome() {
NodeModel node = getNode();
Cycle cycle = BaseEnum.getEnum(Cycle.class, node.getCycle());
long millis = cycle == null ? TimeUnit.SECONDS.toMillis(30) : cycle.getMillis();
if (millis <= 0) {
millis = TimeUnit.SECONDS.toMillis(30);
}
if (cycle != null && cycle != Cycle.none) {
//
setAttribute("monitorCycle", true);
}
setAttribute("cycleTime", millis);
return "node/welcome";
}

View File

@ -36,7 +36,10 @@ public enum Cycle implements BaseEnum {
if (code > 0) {
this.cronPattern = new CronPattern(String.format("0 0/%s * * * ?", code));
this.millis = TimeUnit.MINUTES.toMillis(code);
} else if (code < 0) {
} else if (code == 0) {
//
} else {
code = -code;
this.cronPattern = new CronPattern(String.format("0/%s * * * * ?", code));
this.millis = TimeUnit.SECONDS.toMillis(code);

View File

@ -35,7 +35,7 @@
<body>
<div style="position: relative">
<div class="export">
<div class="export" th:if="${monitorCycle}">
<div id="exportData" style="color: #FFFFFF;width: 1px;height: 1px"></div>
<button class="layui-icon layui-icon-export" id="exportBtn" title="导出监控数据" style="font-size: 18px"></button>
</div>
@ -44,6 +44,7 @@
<table class="layui-table" id="tab_monitor" lay-filter="tab_monitor" style="margin-top:10px"></table>
</body>
<script type="text/javascript">
var cycleTime = [[${cycleTime}]];
var ws, myEcharts, maxSize = 12;
var col = [{field: 'pid', title: '进程id', sort: true, width: '6%'},
{field: 'command', title: '进程名称'},
@ -74,7 +75,6 @@
tableRender(config);
loadProcessList();
layui.use(['laydate'], function () {
var laydate = layui.laydate;
@ -87,8 +87,7 @@
show: false,
done: function (value, date, endDate) {
if (value) {
let nodeId = getTopQueryString("nodeId");
self.location.href = './exportTop?nodeId=' + nodeId + '&time=' + value;
self.location.href = appendNodeId('./exportTop?time=' + value);
}
}
});
@ -96,15 +95,10 @@
}
function loadTop() {
// var bool = $("#hourData").hasClass("choose");
// let type = 'minute';
// if (bool) {
// type = 'hour';
// }
silentAjax({
url: './getTop',
data: {
// type: type
millis: cycleTime
},
success: function (data) {
if (200 === data.code && data.data) {
@ -112,7 +106,7 @@
loadEchartsData(data.data);
setTimeout(function () {
loadTop();
}, 2000);
}, cycleTime);
}
}
});
@ -158,79 +152,6 @@
loadEcharts(data.scales, array);
}
//拼接监控数据
function addTopData(topData) {
var bool = $("#hourData").hasClass("choose");
if (bool && !topData.hourTop) {
return;
}
let option = myEcharts.getOption();
let series = option.series;
let xAxis = option.xAxis;
let xAxisData = xAxis[0].data;
let cpuItem = series[0];
let memoryItem = series[1];
let diskItem = series[2];
cpuItem.data.push(parseFloat(topData.cpu));
diskItem.data.push(parseFloat(topData.disk));
memoryItem.data.push(parseFloat(topData.memory));
//删除过时数据
if (cpuItem.data.length > maxSize) {
cpuItem.data.splice(0, 1);
diskItem.data.splice(0, 1);
memoryItem.data.splice(0, 1);
xAxisData.splice(0, 1);
}
if (diskItem.data[0] === 0) {
cpuItem.data.splice(0, 1);
diskItem.data.splice(0, 1);
memoryItem.data.splice(0, 1);
}
if (cpuItem.data.length > xAxisData.length) {
xAxisData.push(topData.time);
}
let newSeries = [cpuItem, memoryItem, diskItem];
loadEcharts(xAxisData, newSeries);
}
// function linkSocket(status) {
// if (!('WebSocket' in window)) {
// layer.msg("不支持WebSocket");
// return;
// }
// var url = getSocketHost() + "/console?userId=[[${session.user.getUserMd5Key()}]]&projectId=system&nodeId=[[${node.id}]]&type=console";
// if (!ws) {
// ws = new WebSocket(url);
// }
// if (status) {
// if (ws.readyState !== 1 && ws.readyState !== 0) {
// ws = new WebSocket(url);
// }
// ws.onopen = function () {
// ws.send('{"op": "top", "projectInfo":{}}');
// };
// ws.onmessage = function (data) {
// try {
// if (data.data) {
// var top = JSON.parse(data.data);
// if (top.top) {
// addTopData(top);
// }
// }
// } catch (e) {
// return;
// }
// };
// ws.onerror = function (ev) {
// console.log(ev);
// layer.msg("socket 异常");
// }
// } else {
// ws.close();
// }
// }
//加载监控图
function loadEcharts(xAxis, series) {
if (!series) {
@ -287,7 +208,7 @@
tableRender(config);
setTimeout(function () {
loadProcessList();
}, 2000);
}, cycleTime);
} else if (data.msg && data.msg !== "") {
layer.msg(data.msg);
}