feat: update monitor, terminal, and SFTP interaction flow

This commit is contained in:
liumangmang
2026-04-01 15:22:51 +08:00
parent 832d55c722
commit 9f133bd337
6 changed files with 644 additions and 564 deletions

View File

@@ -46,27 +46,68 @@ public class MonitorController {
Map<String, Object> metrics = new HashMap<>();
// 获取CPU使用率
// 获取CPU使用率(兼容多种系统)
try {
String cpuOutput = sshService.executeCommand(conn, password, privateKey, passphrase,
"top -bn1 | grep 'Cpu(s)' | sed 's/.*, *\\([0-9.]*\\)%* id.*/\\1/' | awk '{print 100 - $1}'");
double cpuUsage = Double.parseDouble(cpuOutput.trim());
double cpuUsage = 0;
try {
// 优先使用top
String cpuOutput = sshService.executeCommand(conn, password, privateKey, passphrase,
"top -bn1 2>/dev/null | grep 'Cpu(s)' | sed 's/.*, *\\([0-9.]*\\)%* id.*/\\1/' | awk '{print 100 - $1}'");
if (!cpuOutput.trim().isEmpty()) {
cpuUsage = Double.parseDouble(cpuOutput.trim());
} else {
throw new Exception("top command failed");
}
} catch (Exception e) {
// 备用方案:使用/proc/stat计算需要两次采样这里简化处理用vmstat
try {
String vmstatOutput = sshService.executeCommand(conn, password, privateKey, passphrase,
"vmstat 1 2 | tail -1 | awk '{print 100 - $15}'");
cpuUsage = Double.parseDouble(vmstatOutput.trim());
} catch (Exception e2) {
throw new Exception("Both top and vmstat failed");
}
}
metrics.put("cpuUsage", Math.round(cpuUsage * 10.0) / 10.0);
} catch (Exception e) {
metrics.put("cpuUsage", null);
}
// 获取内存信息
// 获取内存信息(兼容多种系统)
try {
String memOutput = sshService.executeCommand(conn, password, privateKey, passphrase,
"free -b | grep Mem");
String[] memParts = memOutput.trim().split("\\s+");
long totalMem = Long.parseLong(memParts[1]);
long usedMem = Long.parseLong(memParts[2]);
double memUsage = (double) usedMem / totalMem * 100;
metrics.put("memTotal", totalMem);
metrics.put("memUsed", usedMem);
metrics.put("memUsage", Math.round(memUsage * 10.0) / 10.0);
long totalMem = 0;
long usedMem = 0;
try {
// 优先使用free -b
String memOutput = sshService.executeCommand(conn, password, privateKey, passphrase,
"free -b 2>/dev/null | grep Mem");
if (!memOutput.trim().isEmpty()) {
String[] memParts = memOutput.trim().split("\\s+");
if (memParts.length >= 3) {
totalMem = Long.parseLong(memParts[1]);
usedMem = Long.parseLong(memParts[2]);
}
}
} catch (Exception e) {
// 备用方案:从/proc/meminfo读取
String meminfoOutput = sshService.executeCommand(conn, password, privateKey, passphrase,
"cat /proc/meminfo | grep -E 'MemTotal|MemAvailable'");
String[] lines = meminfoOutput.trim().split("\n");
if (lines.length >= 2) {
totalMem = Long.parseLong(lines[0].replaceAll("\\D+", "")) * 1024;
long availableMem = Long.parseLong(lines[1].replaceAll("\\D+", "")) * 1024;
usedMem = totalMem - availableMem;
}
}
if (totalMem > 0 && usedMem >= 0) {
double memUsage = (double) usedMem / totalMem * 100;
metrics.put("memTotal", totalMem);
metrics.put("memUsed", usedMem);
metrics.put("memUsage", Math.round(memUsage * 10.0) / 10.0);
} else {
throw new Exception("Failed to parse memory info");
}
} catch (Exception e) {
metrics.put("memTotal", null);
metrics.put("memUsed", null);