The results after the front-end docking are shown as follows:
1. Entity class of CPU related information
/** * CPU related information **@author csp
*/
public class Cpu
{
/** * number of cores */
private int cpuNum;
/** * Total CPU usage */
private double total;
/** * CPU system usage */
private double sys;
/** * CPU usage */
private double used;
/** * Current CPU wait rate */
private double wait;
/** * Current CPU idle rate */
private double free;
public int getCpuNum(a)
{
return cpuNum;
}
public void setCpuNum(int cpuNum)
{
this.cpuNum = cpuNum;
}
public double getTotal(a)
{
return Arith.round(Arith.mul(total, 100), 2);
}
public void setTotal(double total)
{
this.total = total;
}
public double getSys(a)
{
return Arith.round(Arith.mul(sys / total, 100), 2);
}
public void setSys(double sys)
{
this.sys = sys;
}
public double getUsed(a)
{
return Arith.round(Arith.mul(used / total, 100), 2);
}
public void setUsed(double used)
{
this.used = used;
}
public double getWait(a)
{
return Arith.round(Arith.mul(wait / total, 100), 2);
}
public void setWait(double wait)
{
this.wait = wait;
}
public double getFree(a)
{
return Arith.round(Arith.mul(free / total, 100), 2);
}
public void setFree(double free)
{
this.free = free; }}Copy the code
2, memory related information entity class
/** * Memory related information **@author csp
*/
public class Mem
{
/** * Total memory */
private double total;
/** ** Used memory */
private double used;
/** ** Free memory */
private double free;
public double getTotal(a)
{
return Arith.div(total, (1024 * 1024 * 1024), 2);
}
public void setTotal(long total)
{
this.total = total;
}
public double getUsed(a)
{
return Arith.div(used, (1024 * 1024 * 1024), 2);
}
public void setUsed(long used)
{
this.used = used;
}
public double getFree(a)
{
return Arith.div(free, (1024 * 1024 * 1024), 2);
}
public void setFree(long free)
{
this.free = free;
}
public double getUsage(a)
{
return Arith.mul(Arith.div(used, total, 4), 100); }}Copy the code
3. JVM related information entity class
/** * JVM information **@author csp
*/
public class Jvm
{
/** * Total memory currently occupied by the JVM (M) */
private double total;
/** * Maximum available memory for JVM (M) */
private double max;
/** * JVM free memory (M) */
private double free;
/** * JDK version */
private String version;
/** * JDK path */
private String home;
public double getTotal(a)
{
return Arith.div(total, (1024 * 1024), 2);
}
public void setTotal(double total)
{
this.total = total;
}
public double getMax(a)
{
return Arith.div(max, (1024 * 1024), 2);
}
public void setMax(double max)
{
this.max = max;
}
public double getFree(a)
{
return Arith.div(free, (1024 * 1024), 2);
}
public void setFree(double free)
{
this.free = free;
}
public double getUsed(a)
{
return Arith.div(total - free, (1024 * 1024), 2);
}
public double getUsage(a)
{
return Arith.mul(Arith.div(total - free, total, 4), 100);
}
/** * get the JDK name */
public String getName(a)
{
return ManagementFactory.getRuntimeMXBean().getVmName();
}
public String getVersion(a)
{
return version;
}
public void setVersion(String version)
{
this.version = version;
}
public String getHome(a)
{
return home;
}
public void setHome(String home)
{
this.home = home;
}
/** * JDK startup time */
public String getStartTime(a)
{
return DateUtils.parseDateToStr(DateUtils.YYYY_MM_DD_HH_MM_SS, DateUtils.getServerStartDate());
}
/** * JDK runtime */
public String getRunTime(a)
{
returnDateUtils.getDatePoor(DateUtils.getNowDate(), DateUtils.getServerStartDate()); }}Copy the code
Here to fill the DateUtils tool class, in fact, their own packaging can also!
import org.apache.commons.lang3.time.DateFormatUtils;
import java.lang.management.ManagementFactory;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
/** * time tools **@author csp
*/
public class DateUtils extends org.apache.commons.lang3.time.DateUtils {
public static String YYYY = "yyyy";
public static String YYYY_MM = "yyyy-MM";
public static String YYYY_MM_DD = "yyyy-MM-dd";
public static String YYYYMMDDHHMMSS = "yyyyMMddHHmmss";
public static String YYYY_MM_DD_HH_MM_SS = "yyyy-MM-dd HH:mm:ss";
private static String[] parsePatterns = {
"yyyy-MM-dd"."yyyy-MM-dd HH:mm:ss"."yyyy-MM-dd HH:mm"."yyyy-MM"."yyyy/MM/dd"."yyyy/MM/dd HH:mm:ss"."yyyy/MM/dd HH:mm"."yyyy/MM"."yyyy.MM.dd"."yyyy.MM.dd HH:mm:ss"."yyyy.MM.dd HH:mm"."yyyy.MM"};
/** * gets the current Date of type Date **@returnDate() Current Date */
public static Date getNowDate(a) {
return new Date();
}
/** * Gets the current date. The default format is YYYY-MM-dd **@return String
*/
public static String getDate(a) {
return dateTimeNow(YYYY_MM_DD);
}
public static final String getTime(a) {
return dateTimeNow(YYYY_MM_DD_HH_MM_SS);
}
public static final String dateTimeNow(a) {
return dateTimeNow(YYYYMMDDHHMMSS);
}
public static final String dateTimeNow(final String format) {
return parseDateToStr(format, new Date());
}
public static final String dateTime(final Date date) {
return parseDateToStr(YYYY_MM_DD, date);
}
public static final String parseDateToStr(final String format, final Date date) {
return new SimpleDateFormat(format).format(date);
}
public static final Date dateTime(final String format, final String ts) {
try {
return new SimpleDateFormat(format).parse(ts);
} catch (ParseException e) {
throw newRuntimeException(e); }}/** * The date path is year/month/day, such as 2018/08/08 */
public static final String datePath(a) {
Date now = new Date();
return DateFormatUtils.format(now, "yyyy/MM/dd");
}
/** * The date path is year/month/day, such as 20180808 */
public static final String dateTime(a) {
Date now = new Date();
return DateFormatUtils.format(now, "yyyyMMdd");
}
/** * The date string is converted to the date format */
public static Date parseDate(Object str) {
if (str == null) {
return null;
}
try {
return parseDate(str.toString(), parsePatterns);
} catch (ParseException e) {
return null; }}/** * Get the server startup time */
public static Date getServerStartDate(a) {
long time = ManagementFactory.getRuntimeMXBean().getStartTime();
return new Date(time);
}
/** * calculate two time difference */
public static String getDatePoor(Date endDate, Date nowDate) {
long nd = 1000 * 24 * 60 * 60;
long nh = 1000 * 60 * 60;
long nm = 1000 * 60;
// long ns = 1000;
// Get the millisecond difference between the two times
long diff = endDate.getTime() - nowDate.getTime();
// Calculate the difference of days
long day = diff / nd;
// Calculate the difference in hours
long hour = diff % nd / nh;
// Calculate the difference in minutes
long min = diff % nd % nh / nm;
// Calculate the difference in seconds // print the result
// long sec = diff % nd % nh % nm / ns;
return day + "Day" + hour + "Hour" + min + "Minutes"; }}Copy the code
4. Host system related information entity class
/** * System information **@author csp
*/
public class Sys
{
/** * Server name */
private String computerName;
/** * Server Ip address */
private String computerIp;
/** * Project path */
private String userDir;
/** * operating system */
private String osName;
/** * System architecture */
private String osArch;
public String getComputerName(a)
{
return computerName;
}
public void setComputerName(String computerName)
{
this.computerName = computerName;
}
public String getComputerIp(a)
{
return computerIp;
}
public void setComputerIp(String computerIp)
{
this.computerIp = computerIp;
}
public String getUserDir(a)
{
return userDir;
}
public void setUserDir(String userDir)
{
this.userDir = userDir;
}
public String getOsName(a)
{
return osName;
}
public void setOsName(String osName)
{
this.osName = osName;
}
public String getOsArch(a)
{
return osArch;
}
public void setOsArch(String osArch)
{
this.osArch = osArch; }}Copy the code
5, system file related information entity class
/** * System file information **@author csp
*/
public class SysFile
{
/** * Drive letter path */
private String dirName;
/** * Drive letter type */
private String sysTypeName;
/** * File type */
private String typeName;
/** * total size */
private String total;
/** * The remaining size */
private String free;
/** * Already used */
private String used;
/** * Resource usage */
private double usage;
public String getDirName(a)
{
return dirName;
}
public void setDirName(String dirName)
{
this.dirName = dirName;
}
public String getSysTypeName(a)
{
return sysTypeName;
}
public void setSysTypeName(String sysTypeName)
{
this.sysTypeName = sysTypeName;
}
public String getTypeName(a)
{
return typeName;
}
public void setTypeName(String typeName)
{
this.typeName = typeName;
}
public String getTotal(a)
{
return total;
}
public void setTotal(String total)
{
this.total = total;
}
public String getFree(a)
{
return free;
}
public void setFree(String free)
{
this.free = free;
}
public String getUsed(a)
{
return used;
}
public void setUsed(String used)
{
this.used = used;
}
public double getUsage(a)
{
return usage;
}
public void setUsage(double usage)
{
this.usage = usage; }}Copy the code
6. Server related information entity class
/** * Server information **@author csp
*/
public class Server
{
private static final int OSHI_WAIT_SECOND = 1000;
/** * CPU related information */
private Cpu cpu = new Cpu();
/** * Memory related information */
private Mem mem = new Mem();
/** * JVM information */
private Jvm jvm = new Jvm();
/** * Server information */
private Sys sys = new Sys();
/**
* 磁盘相关信息
*/
private List<SysFile> sysFiles = new LinkedList<SysFile>();
public Cpu getCpu(a)
{
return cpu;
}
public void setCpu(Cpu cpu)
{
this.cpu = cpu;
}
public Mem getMem(a)
{
return mem;
}
public void setMem(Mem mem)
{
this.mem = mem;
}
public Jvm getJvm(a)
{
return jvm;
}
public void setJvm(Jvm jvm)
{
this.jvm = jvm;
}
public Sys getSys(a)
{
return sys;
}
public void setSys(Sys sys)
{
this.sys = sys;
}
public List<SysFile> getSysFiles(a)
{
return sysFiles;
}
public void setSysFiles(List<SysFile> sysFiles)
{
this.sysFiles = sysFiles;
}
/** * Obtain information about the server host *@throws Exception
*/
public void copyTo(a) throws Exception
{
// Obtain system information
SystemInfo si = new SystemInfo();
// Obtain the hardware instance according to SystemInfo
HardwareAbstractionLayer hal = si.getHardware();
// Obtain hardware CPU information
setCpuInfo(hal.getProcessor());
// Get hardware memory information
setMemInfo(hal.getMemory());
// Set the server information
setSysInfo();
// Set the Java VIRTUAL machine
setJvmInfo();
// Set the disk information
setSysFiles(si.getOperatingSystem());
}
/** * Set the CPU information */
private void setCpuInfo(CentralProcessor processor)
{
/ / CPU information
long[] prevTicks = processor.getSystemCpuLoadTicks();
Util.sleep(OSHI_WAIT_SECOND);
long[] ticks = processor.getSystemCpuLoadTicks();
long nice = ticks[TickType.NICE.getIndex()] - prevTicks[TickType.NICE.getIndex()];
long irq = ticks[TickType.IRQ.getIndex()] - prevTicks[TickType.IRQ.getIndex()];
long softirq = ticks[TickType.SOFTIRQ.getIndex()] - prevTicks[TickType.SOFTIRQ.getIndex()];
long steal = ticks[TickType.STEAL.getIndex()] - prevTicks[TickType.STEAL.getIndex()];
long cSys = ticks[TickType.SYSTEM.getIndex()] - prevTicks[TickType.SYSTEM.getIndex()];
long user = ticks[TickType.USER.getIndex()] - prevTicks[TickType.USER.getIndex()];
long iowait = ticks[TickType.IOWAIT.getIndex()] - prevTicks[TickType.IOWAIT.getIndex()];
long idle = ticks[TickType.IDLE.getIndex()] - prevTicks[TickType.IDLE.getIndex()];
long totalCpu = user + nice + cSys + idle + iowait + irq + softirq + steal;
cpu.setCpuNum(processor.getLogicalProcessorCount());/ / the number of Cpu cores
cpu.setTotal(totalCpu);// Total CPU usage
cpu.setSys(cSys);// CPU system usage
cpu.setUsed(user);// CPU usage
cpu.setWait(iowait);// Current CPU wait rate
cpu.setFree(idle);// The current CPU idle rate
}
/** * Set the memory information */
private void setMemInfo(GlobalMemory memory)
{
mem.setTotal(memory.getTotal());// Total memory size
mem.setUsed(memory.getTotal() - memory.getAvailable());// Used memory size
mem.setFree(memory.getAvailable());// Free memory size
}
/** * Set the server information */
private void setSysInfo(a)
{
// Get the current system properties
Properties props = System.getProperties();
sys.setComputerName(IpUtils.getHostName());// Get the host name
sys.setComputerIp(IpUtils.getHostIp());// Obtain the host IP address
sys.setOsName(props.getProperty("os.name"));// Obtain the host type Windows 10
sys.setOsArch(props.getProperty("os.arch"));// Obtain the host graphics card type AMD64
sys.setUserDir(props.getProperty("user.dir"));// Get the project path F:\git\ruoyi\ ruoyi-vue
}
/** * Set the Java virtual machine */
private void setJvmInfo(a) throws UnknownHostException
{
Properties props = System.getProperties();
jvm.setTotal(Runtime.getRuntime().totalMemory());// The total memory of the JVM is 625.5m
jvm.setMax(Runtime.getRuntime().maxMemory());// The JVM has used 347.99m of memory
jvm.setFree(Runtime.getRuntime().freeMemory());// The JVM has 277.51 MB free memory
jvm.setVersion(props.getProperty("java.version"));// JDK version 1.8
jvm.setHome(props.getProperty("java.home"));// JDK installation path C:\Program Files\Java\jdk1.8.0_201\jre
}
/** * Set the disk information */
private void setSysFiles(OperatingSystem os)
{
// Obtain FileSystem based on OS
FileSystem fileSystem = os.getFileSystem();
// Obtain the list of host disk information based on FileSystem
List<OSFileStore> fsArray = fileSystem.getFileStores();
for (OSFileStore fs : fsArray)
{
long free = fs.getUsableSpace();// Free disk capacity
long total = fs.getTotalSpace();// Total disk capacity
long used = total - free;// The disk capacity has been used
SysFile sysFile = new SysFile();
sysFile.setDirName(fs.getMount());// Disk symbol C:\
sysFile.setSysTypeName(fs.getType());// Disk type NTFS
sysFile.setTypeName(fs.getName());// disk name local fixed disk (C:)
sysFile.setTotal(convertFileSize(total));// Total disk capacity
sysFile.setFree(convertFileSize(free));// Free disk capacity
sysFile.setUsed(convertFileSize(used));// The disk capacity has been used
sysFile.setUsage(Arith.mul(Arith.div(used, total, 4), 100));// Disk resource usagesysFiles.add(sysFile); }}/** * byte conversion **@paramSize Indicates the byte size *@returnConverted value */
public String convertFileSize(long size)
{
long kb = 1024;
long mb = kb * 1024;
long gb = mb * 1024;
if (size >= gb)
{
return String.format("%.1f GB", (float) size / gb);
}
else if (size >= mb)
{
float f = (float) size / mb;
return String.format(f > 100 ? "%.0f MB" : "%.1f MB", f);
}
else if (size >= kb)
{
float f = (float) size / kb;
return String.format(f > 100 ? "%.0f KB" : "%.1f KB", f);
}
else
{
return String.format("%d B", size); }}}Copy the code
7. The Controller interface connects to the front end
/** * Server monitoring **@author csp
*/
@RestController
@RequestMapping("/monitor/server")
public class ServerController
{
/** * Get server information *@return
* @throws Exception
*/
@PreAuthorize("@ss.hasPermi('monitor:server:list')")// Spring Security defines custom permission control annotations that do not affect the result
// AjaxResult custom return result class, does not affect the result, can be customized
@GetMapping()
public AjaxResult getInfo(a) throws Exception
{
// Get server information
Server server = new Server();
server.copyTo();
returnAjaxResult.success(server); }}Copy the code