首页接口重构
This commit is contained in:
@@ -0,0 +1,13 @@
|
||||
package com.jeesite.modules.apps.Module;
|
||||
|
||||
import lombok.Data;
|
||||
|
||||
import java.io.Serializable;
|
||||
|
||||
@Data
|
||||
public class SystemInfo implements Serializable {
|
||||
private String cpu; // CPU使用率
|
||||
private String memory; // 内存
|
||||
private String disk; // 磁盘
|
||||
private String usage; // 使用率
|
||||
}
|
||||
@@ -2,6 +2,7 @@ package com.jeesite.modules.apps.web.docker;
|
||||
|
||||
import com.jeesite.modules.apps.Module.ContainerInfo;
|
||||
import com.jeesite.modules.apps.Module.DockerResult;
|
||||
import com.jeesite.modules.apps.Module.SystemInfo;
|
||||
import com.jeesite.modules.biz.dao.MySftpAccountsDao;
|
||||
import com.jeesite.modules.biz.entity.MySftpAccounts;
|
||||
import com.jeesite.modules.utils.DockerUtil;
|
||||
@@ -90,4 +91,16 @@ public class myContainerController {
|
||||
MySftpAccounts accounts = mySftpAccountsDao.get(mySftpAccounts);
|
||||
return DockerUtil.listContainers(accounts, true);
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取CPU列表
|
||||
*/
|
||||
@RequestMapping(value = "systemInfo")
|
||||
@ResponseBody
|
||||
public SystemInfo systemInfo(ContainerInfo containerInfo){
|
||||
MySftpAccounts mySftpAccounts = new MySftpAccounts();
|
||||
mySftpAccounts.setAccountId(containerInfo.getAccountId());
|
||||
MySftpAccounts accounts = mySftpAccountsDao.get(mySftpAccounts);
|
||||
return DockerUtil.systemInfo(accounts);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,8 +1,10 @@
|
||||
package com.jeesite.modules.apps.web.docker;
|
||||
|
||||
import com.jeesite.modules.apps.Module.ServerInfo;
|
||||
import com.jeesite.modules.apps.Module.SystemInfo;
|
||||
import com.jeesite.modules.biz.dao.MySftpAccountsDao;
|
||||
import com.jeesite.modules.biz.entity.MySftpAccounts;
|
||||
import com.jeesite.modules.utils.DockerUtil;
|
||||
import jakarta.annotation.Resource;
|
||||
import org.springframework.stereotype.Controller;
|
||||
import org.springframework.web.bind.annotation.RequestMapping;
|
||||
|
||||
@@ -3,6 +3,7 @@ package com.jeesite.modules.utils;
|
||||
import com.jcraft.jsch.*;
|
||||
import com.jeesite.modules.apps.Module.ContainerInfo;
|
||||
import com.jeesite.modules.apps.Module.DockerResult;
|
||||
import com.jeesite.modules.apps.Module.SystemInfo;
|
||||
import com.jeesite.modules.biz.entity.MySftpAccounts;
|
||||
|
||||
import java.io.ByteArrayOutputStream;
|
||||
@@ -14,7 +15,7 @@ import java.util.List;
|
||||
|
||||
public class DockerUtil {
|
||||
|
||||
private static final int SSH_TIMEOUT = 30000;
|
||||
private static final int SSH_TIMEOUT = 3000;
|
||||
|
||||
private static String runCommand(MySftpAccounts account, String cmd) {
|
||||
JSch jsch = new JSch();
|
||||
@@ -89,6 +90,7 @@ public class DockerUtil {
|
||||
}
|
||||
return list;
|
||||
}
|
||||
|
||||
public static DockerResult start(MySftpAccounts accounts, String containerId) {
|
||||
String res = runCommand(accounts, "docker start " + containerId);
|
||||
return res != null ? DockerResult.ok(res) : DockerResult.fail("执行失败");
|
||||
@@ -120,36 +122,30 @@ public class DockerUtil {
|
||||
return res != null ? DockerResult.ok(res) : DockerResult.fail("查询详情失败");
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取CPU使用率
|
||||
*/
|
||||
// 获取 CPU 使用率
|
||||
public static String getCpuUsage(MySftpAccounts accounts) {
|
||||
try {
|
||||
return runCommand(accounts, "top -bn1 | grep Cpu | awk '{print 100 - $8}'");
|
||||
} catch (Exception e) {
|
||||
return null;
|
||||
}
|
||||
// 1秒采样,输出纯数字百分比
|
||||
return runCommand(accounts,
|
||||
"top -bn1 | grep 'Cpu(s)' | sed -n '1p' | awk '{printf \"%.1f\", 100 - $8}'");
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取内存使用情况
|
||||
*/
|
||||
// 获取内存使用率
|
||||
public static String getMemoryUsage(MySftpAccounts accounts) {
|
||||
try {
|
||||
return runCommand(accounts, "free -m | grep Mem | awk '{print $3\"MB / \"$2\"MB\"}'");
|
||||
} catch (Exception e) {
|
||||
return null;
|
||||
}
|
||||
return runCommand(accounts,
|
||||
"free | grep Mem | awk '{printf \"%.1f\", $3/$2*100}'");
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取磁盘使用情况
|
||||
*/
|
||||
// 获取磁盘使用率
|
||||
public static String getDiskUsage(MySftpAccounts accounts) {
|
||||
try {
|
||||
return runCommand(accounts, "df -h / | grep / | awk '{print $3\" / \"$2}\" \"$5}'");
|
||||
} catch (Exception e) {
|
||||
return null;
|
||||
}
|
||||
return runCommand(accounts,
|
||||
"df -h / | grep / | awk '{gsub(/%/,\"\"); print $5}'");
|
||||
}
|
||||
|
||||
public static SystemInfo systemInfo(MySftpAccounts accounts) {
|
||||
SystemInfo systemInfo = new SystemInfo();
|
||||
systemInfo.setCpu(getCpuUsage(accounts));
|
||||
systemInfo.setMemory(getMemoryUsage(accounts));
|
||||
systemInfo.setDisk(getDiskUsage(accounts));
|
||||
return systemInfo;
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user