修复服务监控

This commit is contained in:
TinyAnts 2022-04-13 18:26:38 +08:00
parent ccefdde865
commit df682e01f6
4 changed files with 83 additions and 2 deletions

View File

@ -3,6 +3,7 @@ package com.hxkj.common.core;
import com.hxkj.common.entity.server.*; import com.hxkj.common.entity.server.*;
import com.hxkj.common.utils.ArithUtil; import com.hxkj.common.utils.ArithUtil;
import com.hxkj.common.utils.IpUtil; import com.hxkj.common.utils.IpUtil;
import com.hxkj.common.utils.TimeUtil;
import oshi.SystemInfo; import oshi.SystemInfo;
import oshi.hardware.CentralProcessor; import oshi.hardware.CentralProcessor;
import oshi.hardware.CentralProcessor.TickType; import oshi.hardware.CentralProcessor.TickType;
@ -13,6 +14,8 @@ import oshi.software.os.OSFileStore;
import oshi.software.os.OperatingSystem; import oshi.software.os.OperatingSystem;
import oshi.util.Util; import oshi.util.Util;
import java.lang.management.ManagementFactory;
import java.text.SimpleDateFormat;
import java.util.*; import java.util.*;
@ -81,13 +84,13 @@ public class ServerResult {
mem.setTotal(ArithUtil.div(memory.getTotal(), number, 2)); mem.setTotal(ArithUtil.div(memory.getTotal(), number, 2));
mem.setUsed(ArithUtil.div(memory.getTotal() - memory.getAvailable(), number, 2)); mem.setUsed(ArithUtil.div(memory.getTotal() - memory.getAvailable(), number, 2));
mem.setFree(ArithUtil.div(memory.getAvailable(), number, 2)); mem.setFree(ArithUtil.div(memory.getAvailable(), number, 2));
mem.setUsage(ArithUtil.mul(ArithUtil.div(mem.getUsed(), memory.getTotal(), 4), 100));
} }
/** /**
* 设置服务器信息 * 设置服务器信息
*/ */
private void setSysInfo() private void setSysInfo() {
{
Properties props = System.getProperties(); Properties props = System.getProperties();
sys.setComputerName(IpUtil.getHostName()); sys.setComputerName(IpUtil.getHostName());
sys.setComputerIp(IpUtil.getHostIp()); sys.setComputerIp(IpUtil.getHostIp());
@ -104,8 +107,13 @@ public class ServerResult {
jvm.setTotal(Runtime.getRuntime().totalMemory()); jvm.setTotal(Runtime.getRuntime().totalMemory());
jvm.setMax(Runtime.getRuntime().maxMemory()); jvm.setMax(Runtime.getRuntime().maxMemory());
jvm.setFree(Runtime.getRuntime().freeMemory()); jvm.setFree(Runtime.getRuntime().freeMemory());
jvm.setUsage(ArithUtil.mul(ArithUtil.div(jvm.getTotal() - jvm.getFree(), jvm.getTotal(), 4), 100));
jvm.setVersion(props.getProperty("java.version")); jvm.setVersion(props.getProperty("java.version"));
jvm.setHome(props.getProperty("java.home")); jvm.setHome(props.getProperty("java.home"));
jvm.setName(ManagementFactory.getRuntimeMXBean().getVmName());
jvm.setInputArgs(ManagementFactory.getRuntimeMXBean().getInputArguments().toString());
jvm.setRunTime(TimeUtil.datePoor(TimeUtil.nowDate(), TimeUtil.serverStartDate()));
jvm.setStartTime(new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(TimeUtil.serverStartDate()));
} }
/** /**

View File

@ -27,6 +27,11 @@ public class Jvm implements Serializable {
*/ */
private double free; private double free;
/**
* JVM内存使用率
*/
private double usage;
/** /**
* JDK版本 * JDK版本
*/ */
@ -37,4 +42,23 @@ public class Jvm implements Serializable {
*/ */
private String home; private String home;
/**
* JDK名称
*/
private String name;
/**
* 运行参数
*/
private String inputArgs;
/**
* JDK运行时间
*/
private String runTime;
/**
* JDK启动时间
*/
private String startTime;
} }

View File

@ -27,4 +27,9 @@ public class Mem implements Serializable {
*/ */
private double free; private double free;
/**
* 使用率
*/
private double usage;
} }

View File

@ -3,6 +3,7 @@ package com.hxkj.common.utils;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper; import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import java.lang.management.ManagementFactory;
import java.sql.Time; import java.sql.Time;
import java.text.DateFormat; import java.text.DateFormat;
import java.text.ParsePosition; import java.text.ParsePosition;
@ -192,6 +193,16 @@ public class TimeUtil {
return System.currentTimeMillis() / 1000; return System.currentTimeMillis() / 1000;
} }
/**
* 返回当前Date型日期
*
* @author fzr
* @return Date() 当前日期
*/
public static Date nowDate() {
return new Date();
}
/** /**
* 返回今日开始和结束的时间戳 * 返回今日开始和结束的时间戳
* *
@ -494,4 +505,37 @@ public class TimeUtil {
return data; return data;
} }
/**
* 返回服务器启动时间
*/
public static Date serverStartDate() {
long time = ManagementFactory.getRuntimeMXBean().getStartTime();
return new Date(time);
}
/**
* 计算两个时间差
*
* @author fzr
* @param endDate 结束时间
* @param nowDate 开始时间
* @return String
*/
public static String datePoor(Date endDate, Date nowDate) {
long nd = 1000 * 24 * 60 * 60;
long nh = 1000 * 60 * 60;
long nm = 1000 * 60;
// long ns = 1000;
// 获得两个时间的毫秒时间差异
long diff = endDate.getTime() - nowDate.getTime();
// 计算差多少天
long day = diff / nd;
// 计算差多少小时
long hour = diff % nd / nh;
// 计算差多少分钟
long min = diff % nd % nh / nm;
// 计算差多少秒//输出结果
// long sec = diff % nd % nh % nm / ns;
return day + "" + hour + "小时" + min + "分钟";
}
} }