martin 3 лет назад
Родитель
Сommit
51e7169f10

+ 14 - 0
api-common/src/main/java/api/common/pojo/dto/Host.java

@@ -0,0 +1,14 @@
+package api.common.pojo.dto;
+
+import lombok.Data;
+
+@Data
+public class Host {
+
+    private String hostname;
+    private Integer port;
+    private String username;
+    private String password;
+    private String type;
+
+}

+ 70 - 52
api-common/src/main/java/api/common/util/SshUtil.java

@@ -2,6 +2,7 @@ package api.common.util;
 
 
 import api.common.pojo.dto.Gpu;
+import api.common.pojo.dto.Process;
 import org.apache.sshd.client.SshClient;
 import org.apache.sshd.client.channel.ChannelExec;
 import org.apache.sshd.client.channel.ClientChannelEvent;
@@ -16,7 +17,6 @@ import java.io.ByteArrayOutputStream;
 import java.io.IOException;
 import java.util.*;
 
-
 /**
  * https://github.com/apache/mina-sshd
  * https://github.com/apache/mina-sshd/blob/master/docs/client-setup.md
@@ -30,39 +30,61 @@ import java.util.*;
 public class SshUtil {
 
     private static final int port = 22;
+    private static final SshClient client = SshClient.setUpDefaultClient();
+
 
     /**
      * @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;
+    public static ClientSession getSession(String hostname, String username, String password) throws IOException {
 
-        SshClient client = SshClient.setUpDefaultClient();
         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();
+    }
+
+
+
+
+    /**
+     * @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;
     }
 
@@ -70,11 +92,19 @@ public class SshUtil {
     /**
      * 获取 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);
-        Map<?, ?> map2 = cpuInfo(hostname, username, password);
+        Map<?, ?> map2 = cpuInfo(session);
 
         long user1 = Long.parseLong(map1.get("user").toString());
         long nice1 = Long.parseLong(map1.get("nice").toString());
@@ -102,9 +132,9 @@ public class SshUtil {
     /**
      * 功能: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");
         Map<String, Object> map = new HashMap<>();
         for (String line : split) {
@@ -134,8 +164,8 @@ public class SshUtil {
     /**
      * 功能: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\">";
         execute = execute.replaceAll(REG, "");
         Document document = DocumentHelper.parseText(execute);
@@ -154,9 +184,9 @@ public class SshUtil {
             gpuInfo.setName(uuid);
             Element processes = element.element("processes");
             List<Element> infos = processes.elements("process_info");
-            List<api.common.pojo.dto.Process> processInfos = new ArrayList<>();
+            List<Process> processInfos = new ArrayList<>();
             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 name = info.element("process_name").getText();
                 String usedMemory = info.element("used_memory").getText();
@@ -178,13 +208,10 @@ public class SshUtil {
     /**
      * 内存使用率
      *
-     * @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");
+    public static double memory0(ClientSession session) throws IOException {
+        String execute = SshUtil.execute(session, "cat /proc/meminfo");
         String[] split = execute.split("\n");
         Map<String, Object> map = new HashMap<>();
         for (String line : split) {
@@ -214,13 +241,10 @@ public class SshUtil {
     /**
      * 剩余可用内存
      *
-     * @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");
+    public static double memory1(ClientSession session) throws IOException {
+        String execute = SshUtil.execute(session, "cat /proc/meminfo");
         String[] split = execute.split("\n");
         Map<String, Object> map = new HashMap<>();
         for (String line : split) {
@@ -251,13 +275,11 @@ public class SshUtil {
     /**
      * 磁盘使用率
      *
-     * @param hostname 主机名
-     * @param username 用户名
-     * @param password 密码
+     * @param session 会话
      * @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[] split1 = split[split.length - 1].split("\\s+");
         long totalSize = Long.parseLong(split1[1]);
@@ -269,14 +291,10 @@ public class SshUtil {
 
     /**
      * 剩余可用磁盘
-     *
-     * @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");
+    public static double disk1(ClientSession session) throws IOException {
+        String execute = execute(session, "df --total");
         String[] split = execute.split("\n");
         String[] split1 = split[split.length - 1].split("\\s+");
         long totalSize = Long.parseLong(split1[1]);

+ 46 - 4
simulation-resource-monitor/src/main/java/com/css/simulation/resource/monitor/scheduler/MyScheduler.java

@@ -1,15 +1,57 @@
 package com.css.simulation.resource.monitor.scheduler;
 
+import api.common.pojo.dto.Host;
+import lombok.Data;
+import org.springframework.boot.context.properties.ConfigurationProperties;
+import org.springframework.scheduling.annotation.Scheduled;
 import org.springframework.stereotype.Component;
 
+import java.util.List;
+
 @Component
+@ConfigurationProperties(prefix = "ssh")
+@Data
 public class MyScheduler {
 
+    List<Host> hostList;
+
+    @Scheduled(fixedDelay = 60 * 60 * 1000)
+    public void server() {
+
+
+        hostList.forEach(host ->{
+
+            String ip = host.getHostname();
+            Integer port = host.getPort();
+            String username = host.getUsername();
+            String password = host.getPassword();
+            String type = host.getType();
+
+//            try {
+//                ClientSession session = SshUtil.getSession(ip, username, password);
+//                List<Gpu> gpuList = ;
+//
+//                SystemServerPO systemServerPO = SystemServerPO.builder()
+//                        .id(StringUtil.getRandomUUID())
+//                        .serverId(SshUtil.hostname(session))
+//                        .serverAddress(ip)
+//                        .serverType(type)
+//                        .cpuUsage((int) SshUtil.cpu(session))
+//                        .memoryUsage((int) SshUtil.memory0(session))
+//                        .memoryAvailable(SshUtil.memory1(session))
+//                        .diskUsage((int) SshUtil.disk0(session))
+//                        .diskAvailable(SshUtil.disk1(session))
+//                        .taskNumber()
+//                        .weight()
+//                        .gpuUsage("gpu".equals(type)?SshUtil.gpu(session).stream().mapto :0)
+//                        .build();
+//            } catch (IOException | InterruptedException | DocumentException e) {
+//                e.printStackTrace();
+//            }
+        });
+
 
-//    @Scheduled(fixedDelay = 60 * 60 * 1000)
-//    public void server() {
-//        SshUtil
-//    }
+    }
 
 
 }

+ 5 - 0
simulation-resource-monitor/src/main/resources/bootstrap.yaml

@@ -0,0 +1,5 @@
+spring:
+  application:
+    name: simulation-resource-monitor
+  profiles:
+    active: aliyun

+ 0 - 16
simulation-resource-monitor/src/main/resources/bootstrap.yml

@@ -1,16 +0,0 @@
-spring:
-  application:
-    name: simulation-resource-scheduler
-  #* -------------------------------- 环境配置 --------------------------------
-  profiles:
-    active: dev
-  #* -------------------------------- 微服务配置 --------------------------------
-  cloud:
-    nacos:
-      discovery:
-        server-addr: 10.15.12.70:8848
-        namespace: 3698bfc2-a612-487a-b2a2-aaad16cd9d9d
-      config:
-        server-addr: 10.15.12.70:8848
-        namespace: 3698bfc2-a612-487a-b2a2-aaad16cd9d9d
-        file-extension: yaml

+ 0 - 2
simulation-resource-scheduler/src/main/resources/bootstrap-aliyun.yaml

@@ -4,8 +4,6 @@ spring:
       discovery:
         server-addr: 47.94.105.148:8848
         namespace: 3698bfc2-a612-487a-b2a2-aaad16cd9d9d
-        ip: 182.92.203.182
-        port: 8002
       config:
         server-addr: 47.94.105.148:8848
         namespace: 3698bfc2-a612-487a-b2a2-aaad16cd9d9d

+ 0 - 0
simulation-resource-scheduler/src/main/resources/bootstrap.yml → simulation-resource-scheduler/src/main/resources/bootstrap.yaml