|
@@ -1,15 +1,21 @@
|
|
|
package api.common.util;
|
|
|
|
|
|
|
|
|
+import api.common.pojo.dto.Gpu;
|
|
|
import org.apache.sshd.client.SshClient;
|
|
|
import org.apache.sshd.client.channel.ChannelExec;
|
|
|
import org.apache.sshd.client.channel.ClientChannelEvent;
|
|
|
import org.apache.sshd.client.future.AuthFuture;
|
|
|
import org.apache.sshd.client.session.ClientSession;
|
|
|
+import org.dom4j.Document;
|
|
|
+import org.dom4j.DocumentException;
|
|
|
+import org.dom4j.DocumentHelper;
|
|
|
+import org.dom4j.Element;
|
|
|
|
|
|
import java.io.ByteArrayOutputStream;
|
|
|
import java.io.IOException;
|
|
|
-import java.util.Collections;
|
|
|
+import java.util.*;
|
|
|
+
|
|
|
|
|
|
/**
|
|
|
* https://github.com/apache/mina-sshd
|
|
@@ -26,13 +32,12 @@ public class SshUtil {
|
|
|
private static final int port = 22;
|
|
|
|
|
|
/**
|
|
|
- *
|
|
|
- * @param hostname 主机名或 IP
|
|
|
- * @param username 用户名
|
|
|
- * @param password 密码
|
|
|
- * @param command 命令
|
|
|
- * @return 执行结果
|
|
|
- * @throws IOException 异常
|
|
|
+ * @param hostname 主机名或 IP
|
|
|
+ * @param username 用户名
|
|
|
+ * @param password 密码
|
|
|
+ * @param command 命令
|
|
|
+ * @return 执行结果
|
|
|
+ * @throws IOException 异常
|
|
|
*/
|
|
|
public static String execute(String hostname, String username, String password, String command) throws IOException {
|
|
|
String result;
|
|
@@ -62,4 +67,223 @@ public class SshUtil {
|
|
|
}
|
|
|
|
|
|
|
|
|
+ /**
|
|
|
+ * 获取 cpu 使用率
|
|
|
+ */
|
|
|
+ public static double cpu(String hostname, String username, String password) throws IOException, InterruptedException {
|
|
|
+
|
|
|
+ Map<?, ?> map1 = cpuInfo(hostname, username, password);
|
|
|
+ Thread.sleep(5 * 1000);
|
|
|
+ Map<?, ?> map2 = cpuInfo(hostname, username, password);
|
|
|
+
|
|
|
+ long user1 = Long.parseLong(map1.get("user").toString());
|
|
|
+ long nice1 = Long.parseLong(map1.get("nice").toString());
|
|
|
+ long system1 = Long.parseLong(map1.get("system").toString());
|
|
|
+ long idle1 = Long.parseLong(map1.get("idle").toString());
|
|
|
+
|
|
|
+ long user2 = Long.parseLong(map2.get("user").toString());
|
|
|
+ long nice2 = Long.parseLong(map2.get("nice").toString());
|
|
|
+ long system2 = Long.parseLong(map2.get("system").toString());
|
|
|
+ long idle2 = Long.parseLong(map2.get("idle").toString());
|
|
|
+
|
|
|
+ long total1 = user1 + system1 + nice1;
|
|
|
+ long total2 = user2 + system2 + nice2;
|
|
|
+ double total = total2 - total1;
|
|
|
+
|
|
|
+ long totalIdle1 = user1 + nice1 + system1 + idle1;
|
|
|
+ long totalIdle2 = user2 + nice2 + system2 + idle2;
|
|
|
+ double totalIdle = totalIdle2 - totalIdle1;
|
|
|
+
|
|
|
+ return total / totalIdle;
|
|
|
+
|
|
|
+ }
|
|
|
+
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 功能:CPU使用信息
|
|
|
+ */
|
|
|
+ private static Map<?, ?> cpuInfo(String hostname, String username, String password) throws IOException {
|
|
|
+
|
|
|
+ String execute = SshUtil.execute(hostname, username, password, "cat /proc/stat");
|
|
|
+ String[] split = execute.split("\n");
|
|
|
+ Map<String, Object> map = new HashMap<>();
|
|
|
+ for (String line : split) {
|
|
|
+ if (line.startsWith("cpu")) {
|
|
|
+ StringTokenizer tokenizer = new StringTokenizer(line);
|
|
|
+ List<String> temp = new ArrayList<>();
|
|
|
+ while (tokenizer.hasMoreElements()) {
|
|
|
+ String value = tokenizer.nextToken();
|
|
|
+ temp.add(value);
|
|
|
+ }
|
|
|
+ map.put("user", temp.get(1));
|
|
|
+ map.put("nice", temp.get(2));
|
|
|
+ map.put("system", temp.get(3));
|
|
|
+ map.put("idle", temp.get(4));
|
|
|
+ map.put("iowait", temp.get(5));
|
|
|
+ map.put("irq", temp.get(6));
|
|
|
+ map.put("softirq", temp.get(7));
|
|
|
+ map.put("stealstolen", temp.get(8));
|
|
|
+ break;
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ return map;
|
|
|
+ }
|
|
|
+
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 功能:CPU使用信息
|
|
|
+ */
|
|
|
+ public static List<Gpu> gpu(String hostname, String username, String password) throws IOException, DocumentException {
|
|
|
+ String execute = SshUtil.execute(hostname, username, password, "nvidia-smi -q -x");
|
|
|
+ String REG = "<!DOCTYPE.*.dtd\">";
|
|
|
+ execute = execute.replaceAll(REG, "");
|
|
|
+ Document document = DocumentHelper.parseText(execute);
|
|
|
+ List<Element> gpu = document.getRootElement().elements("gpu");
|
|
|
+ List<Gpu> gpuList = new ArrayList<>();
|
|
|
+ gpu.forEach(element -> {
|
|
|
+ Gpu gpuInfo = new Gpu();
|
|
|
+ String uuid = element.element("uuid").getText();
|
|
|
+ Element fbMemoryUsage = element.element("fb_memory_usage");
|
|
|
+ String total = fbMemoryUsage.element("total").getText();
|
|
|
+ String used = fbMemoryUsage.element("used").getText();
|
|
|
+ String free = fbMemoryUsage.element("free").getText();
|
|
|
+ gpuInfo.setTotalMemory(total);
|
|
|
+ gpuInfo.setUsedMemory(used);
|
|
|
+ gpuInfo.setFreeMemory(free);
|
|
|
+ gpuInfo.setName(uuid);
|
|
|
+ Element processes = element.element("processes");
|
|
|
+ List<Element> infos = processes.elements("process_info");
|
|
|
+ List<api.common.pojo.dto.Process> processInfos = new ArrayList<>();
|
|
|
+ infos.forEach(info -> {
|
|
|
+ api.common.pojo.dto.Process process = new api.common.pojo.dto.Process();
|
|
|
+ String pid = info.element("pid").getText();
|
|
|
+ String name = info.element("process_name").getText();
|
|
|
+ String usedMemory = info.element("used_memory").getText();
|
|
|
+ process.setPid(pid);
|
|
|
+ process.setName(name);
|
|
|
+ process.setUsedMemory(usedMemory);
|
|
|
+ processInfos.add(process);
|
|
|
+ });
|
|
|
+ gpuInfo.setProcesses(processInfos);
|
|
|
+ int intTotal = Integer.parseInt(total.split(" ")[0]);
|
|
|
+ int intUsed = Integer.parseInt(used.split(" ")[0]);
|
|
|
+ gpuInfo.setUsageRate((int) ((float) intUsed / intTotal * 100));
|
|
|
+ gpuList.add(gpuInfo);
|
|
|
+ });
|
|
|
+ return gpuList;
|
|
|
+ }
|
|
|
+
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 内存使用率
|
|
|
+ *
|
|
|
+ * @param hostname 主机名
|
|
|
+ * @param username 用户名
|
|
|
+ * @param password 密码
|
|
|
+ * @return 剩余可用内存
|
|
|
+ */
|
|
|
+ public static double memory0(String hostname, String username, String password) throws IOException {
|
|
|
+ String execute = SshUtil.execute(hostname, username, password, "cat /proc/meminfo");
|
|
|
+ String[] split = execute.split("\n");
|
|
|
+ Map<String, Object> map = new HashMap<>();
|
|
|
+ for (String line : split) {
|
|
|
+ if (line == null)
|
|
|
+ break;
|
|
|
+ int beginIndex = 0;
|
|
|
+ int endIndex = line.indexOf(":");
|
|
|
+ if (endIndex != -1) {
|
|
|
+ String key = line.substring(beginIndex, endIndex);
|
|
|
+ beginIndex = endIndex + 1;
|
|
|
+ endIndex = line.length();
|
|
|
+ String memory = line.substring(beginIndex, endIndex);
|
|
|
+ String value = memory.replace("kB", "").trim();
|
|
|
+ map.put(key, value);
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ long memTotal = Long.parseLong(map.get("MemTotal").toString());
|
|
|
+ long memFree = Long.parseLong(map.get("MemFree").toString());
|
|
|
+ long memused = memTotal - memFree;
|
|
|
+ long buffers = Long.parseLong(map.get("Buffers").toString());
|
|
|
+ long cached = Long.parseLong(map.get("Cached").toString());
|
|
|
+
|
|
|
+ return (double) (memused - buffers - cached) / memTotal;
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 剩余可用内存
|
|
|
+ *
|
|
|
+ * @param hostname 主机名
|
|
|
+ * @param username 用户名
|
|
|
+ * @param password 密码
|
|
|
+ * @return 剩余可用内存
|
|
|
+ */
|
|
|
+ public static double memory1(String hostname, String username, String password) throws IOException {
|
|
|
+ String execute = SshUtil.execute(hostname, username, password, "cat /proc/meminfo");
|
|
|
+ String[] split = execute.split("\n");
|
|
|
+ Map<String, Object> map = new HashMap<>();
|
|
|
+ for (String line : split) {
|
|
|
+ if (line == null)
|
|
|
+ break;
|
|
|
+ int beginIndex = 0;
|
|
|
+ int endIndex = line.indexOf(":");
|
|
|
+ if (endIndex != -1) {
|
|
|
+ String key = line.substring(beginIndex, endIndex);
|
|
|
+ beginIndex = endIndex + 1;
|
|
|
+ endIndex = line.length();
|
|
|
+ String memory = line.substring(beginIndex, endIndex);
|
|
|
+ String value = memory.replace("kB", "").trim();
|
|
|
+ map.put(key, value);
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ long memTotal = Long.parseLong(map.get("MemTotal").toString());
|
|
|
+ long memFree = Long.parseLong(map.get("MemFree").toString());
|
|
|
+ long memused = memTotal - memFree;
|
|
|
+ long buffers = Long.parseLong(map.get("Buffers").toString());
|
|
|
+ long cached = Long.parseLong(map.get("Cached").toString());
|
|
|
+
|
|
|
+ return memTotal - (memused - buffers - cached);
|
|
|
+ }
|
|
|
+
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 磁盘使用率
|
|
|
+ *
|
|
|
+ * @param hostname 主机名
|
|
|
+ * @param username 用户名
|
|
|
+ * @param password 密码
|
|
|
+ * @return 磁盘使用率
|
|
|
+ */
|
|
|
+ public static double disk0(String hostname, String username, String password) throws IOException {
|
|
|
+ String execute = execute(hostname, username, password, "df --total");
|
|
|
+ String[] split = execute.split("\n");
|
|
|
+ String[] split1 = split[split.length - 1].split("\\s+");
|
|
|
+ long totalSize = Long.parseLong(split1[1]);
|
|
|
+ long totalUsed = Long.parseLong(split1[2]);
|
|
|
+ long totalAvailable = Long.parseLong(split1[3]);
|
|
|
+ String totalUsedPercent = split1[4];
|
|
|
+ return totalUsed * 1.0 / totalSize;
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 剩余可用磁盘
|
|
|
+ *
|
|
|
+ * @param hostname 主机名
|
|
|
+ * @param username 用户名
|
|
|
+ * @param password 密码
|
|
|
+ * @return 剩余可用磁盘
|
|
|
+ */
|
|
|
+ public static double disk1(String hostname, String username, String password) throws IOException {
|
|
|
+ String execute = execute(hostname, username, password, "df --total");
|
|
|
+ String[] split = execute.split("\n");
|
|
|
+ String[] split1 = split[split.length - 1].split("\\s+");
|
|
|
+ long totalSize = Long.parseLong(split1[1]);
|
|
|
+ long totalUsed = Long.parseLong(split1[2]);
|
|
|
+ long totalAvailable = Long.parseLong(split1[3]);
|
|
|
+ String totalUsedPercent = split1[4];
|
|
|
+ return totalAvailable;
|
|
|
+ }
|
|
|
+
|
|
|
}
|