|
@@ -2,6 +2,7 @@ package api.common.util;
|
|
|
|
|
|
|
|
|
|
import api.common.pojo.dto.Gpu;
|
|
import api.common.pojo.dto.Gpu;
|
|
|
|
+import api.common.pojo.dto.Process;
|
|
import org.apache.sshd.client.SshClient;
|
|
import org.apache.sshd.client.SshClient;
|
|
import org.apache.sshd.client.channel.ChannelExec;
|
|
import org.apache.sshd.client.channel.ChannelExec;
|
|
import org.apache.sshd.client.channel.ClientChannelEvent;
|
|
import org.apache.sshd.client.channel.ClientChannelEvent;
|
|
@@ -16,7 +17,6 @@ import java.io.ByteArrayOutputStream;
|
|
import java.io.IOException;
|
|
import java.io.IOException;
|
|
import java.util.*;
|
|
import java.util.*;
|
|
|
|
|
|
-
|
|
|
|
/**
|
|
/**
|
|
* https://github.com/apache/mina-sshd
|
|
* https://github.com/apache/mina-sshd
|
|
* https://github.com/apache/mina-sshd/blob/master/docs/client-setup.md
|
|
* https://github.com/apache/mina-sshd/blob/master/docs/client-setup.md
|
|
@@ -30,39 +30,61 @@ import java.util.*;
|
|
public class SshUtil {
|
|
public class SshUtil {
|
|
|
|
|
|
private static final int port = 22;
|
|
private static final int port = 22;
|
|
|
|
+ private static final SshClient client = SshClient.setUpDefaultClient();
|
|
|
|
+
|
|
|
|
|
|
/**
|
|
/**
|
|
* @param hostname 主机名或 IP
|
|
* @param hostname 主机名或 IP
|
|
* @param username 用户名
|
|
* @param username 用户名
|
|
* @param password 密码
|
|
* @param password 密码
|
|
- * @param command 命令
|
|
|
|
* @return 执行结果
|
|
* @return 执行结果
|
|
* @throws IOException 异常
|
|
* @throws IOException 异常
|
|
*/
|
|
*/
|
|
- public static String execute(String hostname, String username, String password, String command) throws IOException {
|
|
|
|
- String result;
|
|
|
|
|
|
+ public static ClientSession getSession(String hostname, String username, String password) throws IOException {
|
|
|
|
|
|
- SshClient client = SshClient.setUpDefaultClient();
|
|
|
|
client.start();
|
|
client.start();
|
|
- // using the client for multiple sessions...
|
|
|
|
- try (ClientSession session = client.connect(username, hostname, port).verify(10000).getSession()) {
|
|
|
|
- session.addPasswordIdentity(password); // for password-based authentication
|
|
|
|
- AuthFuture verify = session.auth().verify(10000);
|
|
|
|
- if (verify.isFailure()) {
|
|
|
|
- throw new RuntimeException("------- SSH 用户名密码验证失败!");
|
|
|
|
- }
|
|
|
|
- ChannelExec execChannel = session.createExecChannel(command);
|
|
|
|
- // 创建输出流
|
|
|
|
- ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream(2048);
|
|
|
|
- execChannel.setOut(byteArrayOutputStream);
|
|
|
|
- execChannel.open();
|
|
|
|
- execChannel.waitFor(Collections.singleton(ClientChannelEvent.CLOSED), 0);
|
|
|
|
- execChannel.close();
|
|
|
|
- // 结果写入
|
|
|
|
- result = byteArrayOutputStream.toString();
|
|
|
|
- byteArrayOutputStream.close();
|
|
|
|
|
|
+ ClientSession session = client.connect(username, hostname, port).verify(10000).getSession();
|
|
|
|
+ session.addPasswordIdentity(password);
|
|
|
|
+ AuthFuture verify = session.auth().verify(10000);
|
|
|
|
+ if (verify.isFailure()) {
|
|
|
|
+ throw new RuntimeException("------- ssh 用户名密码验证失败!");
|
|
}
|
|
}
|
|
|
|
+ return session;
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ /**
|
|
|
|
+ * @param clientSession 主机名或 IP
|
|
|
|
+ * @throws IOException 异常
|
|
|
|
+ */
|
|
|
|
+ public static void stop(ClientSession clientSession) throws IOException {
|
|
|
|
+ clientSession.close();
|
|
client.stop();
|
|
client.stop();
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+
|
|
|
|
+
|
|
|
|
+
|
|
|
|
+ /**
|
|
|
|
+ * @param session 会话
|
|
|
|
+ * @param command 命令
|
|
|
|
+ * @return 执行结果
|
|
|
|
+ * @throws IOException 异常
|
|
|
|
+ */
|
|
|
|
+ public static String execute(ClientSession session, String command) throws IOException {
|
|
|
|
+ String result;
|
|
|
|
+
|
|
|
|
+ ChannelExec execChannel = session.createExecChannel(command);
|
|
|
|
+ // 创建输出流
|
|
|
|
+ ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream(2048);
|
|
|
|
+ execChannel.setOut(byteArrayOutputStream);
|
|
|
|
+ execChannel.open();
|
|
|
|
+ execChannel.waitFor(Collections.singleton(ClientChannelEvent.CLOSED), 0);
|
|
|
|
+ execChannel.close();
|
|
|
|
+ // 结果写入
|
|
|
|
+ result = byteArrayOutputStream.toString();
|
|
|
|
+ byteArrayOutputStream.close();
|
|
|
|
+
|
|
|
|
+
|
|
return result;
|
|
return result;
|
|
}
|
|
}
|
|
|
|
|
|
@@ -70,11 +92,19 @@ public class SshUtil {
|
|
/**
|
|
/**
|
|
* 获取 cpu 使用率
|
|
* 获取 cpu 使用率
|
|
*/
|
|
*/
|
|
- public static double cpu(String hostname, String username, String password) throws IOException, InterruptedException {
|
|
|
|
|
|
+ public static String hostname(ClientSession session) throws IOException {
|
|
|
|
+ return SshUtil.execute(session, "hostname");
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+
|
|
|
|
+ /**
|
|
|
|
+ * 获取 cpu 使用率
|
|
|
|
+ */
|
|
|
|
+ public static double cpu(ClientSession session) throws IOException, InterruptedException {
|
|
|
|
|
|
- Map<?, ?> map1 = cpuInfo(hostname, username, password);
|
|
|
|
|
|
+ Map<?, ?> map1 = cpuInfo(session);
|
|
Thread.sleep(5 * 1000);
|
|
Thread.sleep(5 * 1000);
|
|
- Map<?, ?> map2 = cpuInfo(hostname, username, password);
|
|
|
|
|
|
+ Map<?, ?> map2 = cpuInfo(session);
|
|
|
|
|
|
long user1 = Long.parseLong(map1.get("user").toString());
|
|
long user1 = Long.parseLong(map1.get("user").toString());
|
|
long nice1 = Long.parseLong(map1.get("nice").toString());
|
|
long nice1 = Long.parseLong(map1.get("nice").toString());
|
|
@@ -102,9 +132,9 @@ public class SshUtil {
|
|
/**
|
|
/**
|
|
* 功能:CPU使用信息
|
|
* 功能:CPU使用信息
|
|
*/
|
|
*/
|
|
- private static Map<?, ?> cpuInfo(String hostname, String username, String password) throws IOException {
|
|
|
|
|
|
+ private static Map<?, ?> cpuInfo(ClientSession session) throws IOException {
|
|
|
|
|
|
- String execute = SshUtil.execute(hostname, username, password, "cat /proc/stat");
|
|
|
|
|
|
+ String execute = SshUtil.execute(session, "cat /proc/stat");
|
|
String[] split = execute.split("\n");
|
|
String[] split = execute.split("\n");
|
|
Map<String, Object> map = new HashMap<>();
|
|
Map<String, Object> map = new HashMap<>();
|
|
for (String line : split) {
|
|
for (String line : split) {
|
|
@@ -134,8 +164,8 @@ public class SshUtil {
|
|
/**
|
|
/**
|
|
* 功能:CPU使用信息
|
|
* 功能: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");
|
|
|
|
|
|
+ public static List<Gpu> gpu(ClientSession session) throws IOException, DocumentException {
|
|
|
|
+ String execute = SshUtil.execute(session, "nvidia-smi -q -x");
|
|
String REG = "<!DOCTYPE.*.dtd\">";
|
|
String REG = "<!DOCTYPE.*.dtd\">";
|
|
execute = execute.replaceAll(REG, "");
|
|
execute = execute.replaceAll(REG, "");
|
|
Document document = DocumentHelper.parseText(execute);
|
|
Document document = DocumentHelper.parseText(execute);
|
|
@@ -154,9 +184,9 @@ public class SshUtil {
|
|
gpuInfo.setName(uuid);
|
|
gpuInfo.setName(uuid);
|
|
Element processes = element.element("processes");
|
|
Element processes = element.element("processes");
|
|
List<Element> infos = processes.elements("process_info");
|
|
List<Element> infos = processes.elements("process_info");
|
|
- List<api.common.pojo.dto.Process> processInfos = new ArrayList<>();
|
|
|
|
|
|
+ List<Process> processInfos = new ArrayList<>();
|
|
infos.forEach(info -> {
|
|
infos.forEach(info -> {
|
|
- api.common.pojo.dto.Process process = new api.common.pojo.dto.Process();
|
|
|
|
|
|
+ Process process = new Process();
|
|
String pid = info.element("pid").getText();
|
|
String pid = info.element("pid").getText();
|
|
String name = info.element("process_name").getText();
|
|
String name = info.element("process_name").getText();
|
|
String usedMemory = info.element("used_memory").getText();
|
|
String usedMemory = info.element("used_memory").getText();
|
|
@@ -178,13 +208,10 @@ public class SshUtil {
|
|
/**
|
|
/**
|
|
* 内存使用率
|
|
* 内存使用率
|
|
*
|
|
*
|
|
- * @param hostname 主机名
|
|
|
|
- * @param username 用户名
|
|
|
|
- * @param password 密码
|
|
|
|
* @return 剩余可用内存
|
|
* @return 剩余可用内存
|
|
*/
|
|
*/
|
|
- public static double memory0(String hostname, String username, String password) throws IOException {
|
|
|
|
- String execute = SshUtil.execute(hostname, username, password, "cat /proc/meminfo");
|
|
|
|
|
|
+ public static double memory0(ClientSession session) throws IOException {
|
|
|
|
+ String execute = SshUtil.execute(session, "cat /proc/meminfo");
|
|
String[] split = execute.split("\n");
|
|
String[] split = execute.split("\n");
|
|
Map<String, Object> map = new HashMap<>();
|
|
Map<String, Object> map = new HashMap<>();
|
|
for (String line : split) {
|
|
for (String line : split) {
|
|
@@ -214,13 +241,10 @@ public class SshUtil {
|
|
/**
|
|
/**
|
|
* 剩余可用内存
|
|
* 剩余可用内存
|
|
*
|
|
*
|
|
- * @param hostname 主机名
|
|
|
|
- * @param username 用户名
|
|
|
|
- * @param password 密码
|
|
|
|
* @return 剩余可用内存
|
|
* @return 剩余可用内存
|
|
*/
|
|
*/
|
|
- public static double memory1(String hostname, String username, String password) throws IOException {
|
|
|
|
- String execute = SshUtil.execute(hostname, username, password, "cat /proc/meminfo");
|
|
|
|
|
|
+ public static double memory1(ClientSession session) throws IOException {
|
|
|
|
+ String execute = SshUtil.execute(session, "cat /proc/meminfo");
|
|
String[] split = execute.split("\n");
|
|
String[] split = execute.split("\n");
|
|
Map<String, Object> map = new HashMap<>();
|
|
Map<String, Object> map = new HashMap<>();
|
|
for (String line : split) {
|
|
for (String line : split) {
|
|
@@ -251,13 +275,11 @@ public class SshUtil {
|
|
/**
|
|
/**
|
|
* 磁盘使用率
|
|
* 磁盘使用率
|
|
*
|
|
*
|
|
- * @param hostname 主机名
|
|
|
|
- * @param username 用户名
|
|
|
|
- * @param password 密码
|
|
|
|
|
|
+ * @param session 会话
|
|
* @return 磁盘使用率
|
|
* @return 磁盘使用率
|
|
*/
|
|
*/
|
|
- public static double disk0(String hostname, String username, String password) throws IOException {
|
|
|
|
- String execute = execute(hostname, username, password, "df --total");
|
|
|
|
|
|
+ public static double disk0(ClientSession session) throws IOException {
|
|
|
|
+ String execute = execute(session, "df --total");
|
|
String[] split = execute.split("\n");
|
|
String[] split = execute.split("\n");
|
|
String[] split1 = split[split.length - 1].split("\\s+");
|
|
String[] split1 = split[split.length - 1].split("\\s+");
|
|
long totalSize = Long.parseLong(split1[1]);
|
|
long totalSize = Long.parseLong(split1[1]);
|
|
@@ -269,14 +291,10 @@ public class SshUtil {
|
|
|
|
|
|
/**
|
|
/**
|
|
* 剩余可用磁盘
|
|
* 剩余可用磁盘
|
|
- *
|
|
|
|
- * @param hostname 主机名
|
|
|
|
- * @param username 用户名
|
|
|
|
- * @param password 密码
|
|
|
|
* @return 剩余可用磁盘
|
|
* @return 剩余可用磁盘
|
|
*/
|
|
*/
|
|
- public static double disk1(String hostname, String username, String password) throws IOException {
|
|
|
|
- String execute = execute(hostname, username, password, "df --total");
|
|
|
|
|
|
+ public static double disk1(ClientSession session) throws IOException {
|
|
|
|
+ String execute = execute(session, "df --total");
|
|
String[] split = execute.split("\n");
|
|
String[] split = execute.split("\n");
|
|
String[] split1 = split[split.length - 1].split("\\s+");
|
|
String[] split1 = split[split.length - 1].split("\\s+");
|
|
long totalSize = Long.parseLong(split1[1]);
|
|
long totalSize = Long.parseLong(split1[1]);
|