import java.io.BufferedReader;
import java.io.File;
import java.io.IOException;
import java.io.InputStreamReader;
import java.lang.management.ManagementFactory;
import java.lang.management.ThreadMXBean;
import java.math.BigDecimal;
import java.text.DecimalFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Map;
import com.alibaba.druid.stat.DruidStatManagerFacade;
import com.alibaba.druid.support.http.stat.WebAppStatManager;
public class SystemMonitorHandler {
@XxlJob(value = "systemIndexMonitor")
public ReturnT<String> systemIndexMonitor(String params) throws IOException {
OperatingSystemMXBean osmxb = (OperatingSystemMXBean) ManagementFactory.getOperatingSystemMXBean();
long totalPhysicalMemorySize = osmxb.getTotalPhysicalMemorySize();
long usedMemory = osmxb.getTotalPhysicalMemorySize() - osmxb.getFreePhysicalMemorySize();
String memoryUsedRate = new DecimalFormat("#.##").format((double) usedMemory / (double) totalPhysicalMemorySize);
String osJson = JSON.toJSONString(osmxb);
JSONObject jsonObject = JSON.parseObject(osJson);
String systemCpuLoad = new DecimalFormat("#.##").format(jsonObject.getDouble("systemCpuLoad"));
ThreadMXBean thread = ManagementFactory.getThreadMXBean();
int threadLiveSize = thread.getThreadCount();
int requestErrorCount = 0;
String requestAverageTime = "0.00";
int requestCount = 0;
int activeCount = 0;
int httpSessionCount = 0;
int physicalConnectCount = 0;
try {
List<Map<String, Object>> webAppStatData = WebAppStatManager.getInstance().getWebAppStatData();
List<Map<String, Object>> uriStatData = WebAppStatManager.getInstance().getURIStatData();
List<Map<String, Object>> dataSourceStatData = DruidStatManagerFacade.getInstance().getDataSourceStatDataList();
if (!webAppStatData.isEmpty()) {
requestCount = (int) webAppStatData.get(0).get("RequestCount");
httpSessionCount= (int) webAppStatData.get(0).get("SessionCount");
}
if (!uriStatData.isEmpty()) {
int requestTime = 0;
for (Map<String, Object> uriStatDatum : uriStatData) {
requestErrorCount += (Integer) uriStatDatum.get("ErrorCount");
requestTime += (Integer) uriStatDatum.get("RequestTimeMillis");
}
requestAverageTime = new DecimalFormat("#.##").format(requestTime / requestCount);
}
if (!dataSourceStatData.isEmpty()) {
activeCount = dataSourceStatData.get(0).get("ActiveCount") == null ? 0 : (int) dataSourceStatData.get(0).get("ActiveCount");
physicalConnectCount = dataSourceStatData.get(0).get("PhysicalConnectCount") == null ? 0 : (int) dataSourceStatData.get(0).get("PhysicalConnectCount");
}
} catch (Exception e) {
e.printStackTrace();
}
String osName = System.getProperty("os.name");
if (osName.startsWith("Windows")) {
osName = "windows";
DecimalFormat df=new DecimalFormat("0.00");
File[] roots = File.listRoots();
for (File file : roots) {
long totalSpace = file.getTotalSpace();
long freeSpace = file.getFreeSpace();
long usedSpace = totalSpace - freeSpace;
float totalSpaceG = Float.parseFloat(df.format((float) totalSpace / 1024 / 1024 / 1024));
float freeSpaceG = Float.parseFloat(df.format((float) freeSpace / 1024 / 1024 / 1024));
float usedSpaceG = Float.parseFloat(df.format((float) usedSpace / 1024 / 1024 / 1024));
log.info(file + " 容量 : " + totalSpaceG + "G, 可用 : " + freeSpaceG + "G, 已用 : " + usedSpaceG + "G");
}
} else {
osName = "linux";
try {
Runtime rt = Runtime.getRuntime();
Process p = rt.exec("df -hl");
try (BufferedReader in = new BufferedReader(new InputStreamReader(p.getInputStream()))) {
String str;
int line = 0;
while ((str = in.readLine()) != null) {
line++;
if (line == 1) {
continue;
}
str = str.replaceAll("\\s+", " ");
String[] strArray = str.split(" ");
log.info("文件系统 " + strArray[0] + ", 容量 " + strArray[1] + ", 已用 " + strArray[2] + ", 可用 " + strArray[3] + ", 占比 " + strArray[4] + ", 挂载目录 " + strArray[5]);
}
} catch (Exception e) {
e.printStackTrace();
}
} catch (Exception e) {
e.printStackTrace();
}
}
log.info("当前服务运行系统为=> " + osName);
log.info("memoryUsedRate:" + memoryUsedRate);
log.info("requestCount:" + requestCount);
log.info("activeCount:" + activeCount);
log.info("physicalConnectCount:" + physicalConnectCount);
log.info("cpuUsageSize:" + systemCpuLoad);
log.info("threadLiveSize:" + threadLiveSize);
log.info("requestErrorCount:" + requestErrorCount);
log.info("httpSessionCount:" + httpSessionCount);
log.info("requestAverageTime:" + requestAverageTime);
return ReturnT.SUCCESS;
}
}
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 无需6万激活码!GitHub神秘组织3小时极速复刻Manus,手把手教你使用OpenManus搭建本
· C#/.NET/.NET Core优秀项目和框架2025年2月简报
· 什么是nginx的强缓存和协商缓存
· 一文读懂知识蒸馏
· Manus爆火,是硬核还是营销?