@[TOC]
前言
- 今日访问mybatis-plus 官网偶然看到一个爱组搭广告,出于好奇点进去看了一下
1.构建maven
<dependency>
<groupId>com.aizuda</groupId>
<artifactId>aizuda-monitor</artifactId>
<version>1.0.0</version>
</dependency>
2.代码示例
package cn.itcast.user.web;
import com.aizuda.monitor.DiskInfo;
import com.aizuda.monitor.OshiMonitor;
import com.alibaba.nacos.common.utils.CollectionUtils;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import oshi.software.os.OSProcess;
import oshi.software.os.OperatingSystem;
import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
/**
* @auther:Wangxl
* @Emile:18335844494@163.com
* @Time:2022/4/2 15:16
*/
@RestController
@RequestMapping("/v1/monitor")
public class MonitorController {
// 注入监控模块 Oshi 调用类
@Resource
private OshiMonitor oshiMonitor;
@GetMapping("/server")
public Map<String, Object> monitor() {
Map<String, Object> server = new HashMap<>(5);
// 系统信息
server.put("sysInfo", oshiMonitor.getSysInfo());
// CPU 信息
server.put("cupInfo", oshiMonitor.getCpuInfo());
// 内存信息
server.put("memoryInfo", oshiMonitor.getMemoryInfo());
// Jvm 虚拟机信息
server.put("jvmInfo", oshiMonitor.getJvmInfo());
// 磁盘信息
List<DiskInfo> diskInfos = oshiMonitor.getDiskInfos();
server.put("diskInfos", diskInfos);
if (CollectionUtils.isNotEmpty(diskInfos)) {
long usableSpace = 0;
long totalSpace = 0;
for (DiskInfo diskInfo : diskInfos) {
usableSpace += diskInfo.getUsableSpace();
totalSpace += diskInfo.getTotalSpace();
}
double usedSize = (totalSpace - usableSpace);
// 统计所有磁盘的使用率
server.put("diskUsePercent", oshiMonitor.formatDouble(usedSize / totalSpace * 100));
}
// 系统前 10 个进程
List<OSProcess> processList = oshiMonitor.getOperatingSystem().getProcesses(null,
OperatingSystem.ProcessSorting.CPU_DESC, 10);
List<Map<String, Object>> processMapList = new ArrayList<>();
for (OSProcess process : processList) {
Map<String, Object> processMap = new HashMap<>(5);
processMap.put("name", process.getName());
processMap.put("pid", process.getProcessID());
processMap.put("cpu", oshiMonitor.formatDouble(process.getProcessCpuLoadCumulative()));
processMapList.add(processMap);
}
server.put("processList", processMapList);
return server;
}
}
3.结果返回(前端展示需要自己开发,oshi只提供数据)
{
"cupInfo": {
"physicalProcessorCount": 4,
"logicalProcessorCount": 8,
"systemPercent": 0.09,
"userPercent": 0.08,
"waitPercent": 0.0,
"usePercent": 0.18
},
"memoryInfo": {
"total": "15.81GB",
"used": "11.79GB",
"free": "4.02GB",
"usePercent": 0.75
},
"processList": [
{
"name": "Idle",
"cpu": 7.41,
"pid": 0
},
{
"name": "javaw",
"cpu": 0.4,
"pid": 9540
},
{
"name": "idea64",
"cpu": 0.07,
"pid": 12840
},
{
"name": "msedge",
"cpu": 0.06,
"pid": 15956
},
{
"name": "java",
"cpu": 0.05,
"pid": 2400
},
{
"name": "java",
"cpu": 0.05,
"pid": 9760
},
{
"name": "msedge",
"cpu": 0.04,
"pid": 16768
},
{
"name": "msedge",
"cpu": 0.04,
"pid": 15444
},
{
"name": "msedge",
"cpu": 0.03,
"pid": 7860
},
{
"name": "QQPCTray",
"cpu": 0.02,
"pid": 9568
}
],
"sysInfo": {
"name": "DESKTOP-4BGLRMJ",
"ip": "172.23.39.68",
"osName": "Windows 10",
"osArch": "amd64",
"userDir": "F:\\2022Projects\\cloud-demo-dubbo"
},
"diskUsePercent": 47.1,
"diskInfos": [
{
"name": "本地固定磁盘 (C:)",
"volume": "\\\\?\\Volume{0ee2db33-fc0a-464f-aa79-f110edd1be4b}\\",
"label": "OS",
"logicalVolume": "",
"mount": "C:\\",
"description": "Fixed drive",
"options": "rw,reparse,sparse,trans,journaled,quota,casess,oids,casepn,efs,streams,unicode,acls,fcomp",
"type": "NTFS",
"size": "103.73GB",
"totalSpace": 111376592896,
"used": "89.17GB",
"usableSpace": 15626911744,
"avail": "14.55GB",
"usePercent": 85.97,
"uuid": "0ee2db33-fc0a-464f-aa79-f110edd1be4b"
},
{
"name": "本地固定磁盘 (D:)",
"volume": "\\\\?\\Volume{b6dd9496-40b0-416d-8fca-fb27692d3883}\\",
"label": "DATA",
"logicalVolume": "",
"mount": "D:\\",
"description": "Fixed drive",
"options": "rw,reparse,sparse,trans,journaled,quota,casess,oids,casepn,efs,streams,unicode,acls,fcomp",
"type": "NTFS",
"size": "531.39GB",
"totalSpace": 570572140544,
"used": "244.99GB",
"usableSpace": 307517067264,
"avail": "286.4GB",
"usePercent": 46.1,
"uuid": "b6dd9496-40b0-416d-8fca-fb27692d3883"
},
{
"name": "本地固定磁盘 (E:)",
"volume": "\\\\?\\Volume{3fdb949e-bba5-4052-bad9-275232a18e7e}\\",
"label": "新加卷",
"logicalVolume": "",
"mount": "E:\\",
"description": "Fixed drive",
"options": "rw,reparse,sparse,trans,journaled,quota,casess,oids,casepn,efs,streams,unicode,acls,fcomp",
"type": "NTFS",
"size": "200GB",
"totalSpace": 214747312128,
"used": "24.41GB",
"usableSpace": 188541489152,
"avail": "175.59GB",
"usePercent": 12.2,
"uuid": "3fdb949e-bba5-4052-bad9-275232a18e7e"
},
{
"name": "本地固定磁盘 (F:)",
"volume": "\\\\?\\Volume{72ed311d-d25b-4368-b5d3-e3f2e40d6aa8}\\",
"label": "新加卷",
"logicalVolume": "",
"mount": "F:\\",
"description": "Fixed drive",
"options": "rw,reparse,sparse,trans,journaled,quota,casess,oids,casepn,efs,streams,unicode,acls,fcomp",
"type": "NTFS",
"size": "200GB",
"totalSpace": 214747312128,
"used": "128.99GB",
"usableSpace": 76241727488,
"avail": "71.01GB",
"usePercent": 64.5,
"uuid": "72ed311d-d25b-4368-b5d3-e3f2e40d6aa8"
}
],
"jvmInfo": {
"jdkVersion": "1.8.0_131",
"jdkHome": "D:\\Java\\jdk1.8.0_131\\jre",
"jdkName": "Java HotSpot(TM) 64-Bit Server VM",
"jvmTotalMemory": "540MB",
"maxMemory": "3.51GB",
"freeMemory": "446.6MB",
"usedMemory": "93.4MB",
"usePercent": 0.17,
"startTime": 1648884476737,
"uptime": 310174
}
}