martin 3 年之前
父节点
当前提交
1befde2904

+ 10 - 6
api-common/src/main/java/api/common/util/SshUtil.java

@@ -100,7 +100,7 @@ public class SshUtil {
     /**
      * 获取 cpu 使用率
      */
-    public static double cpu(ClientSession session) throws IOException, InterruptedException {
+    public static double cpuUsage(ClientSession session) throws IOException, InterruptedException {
 
         Map<?, ?> map1 = cpuInfo(session);
         Thread.sleep(5 * 1000);
@@ -164,7 +164,7 @@ public class SshUtil {
     /**
      * 功能:CPU使用信息
      */
-    public static List<Gpu> gpu(ClientSession session) throws IOException, DocumentException {
+    public static List<Gpu> gpuInfo(ClientSession session) throws IOException, DocumentException {
         String execute = SshUtil.execute(session, "nvidia-smi -q -x");
         String REG = "<!DOCTYPE.*.dtd\">";
         execute = execute.replaceAll(REG, "");
@@ -208,9 +208,10 @@ public class SshUtil {
     /**
      * 内存使用率
      *
+     * @param session 会话
      * @return 剩余可用内存
      */
-    public static double memory0(ClientSession session) throws IOException {
+    public static double memoryUsage(ClientSession session) throws IOException {
         String execute = SshUtil.execute(session, "cat /proc/meminfo");
         String[] split = execute.split("\n");
         Map<String, Object> map = new HashMap<>();
@@ -241,9 +242,10 @@ public class SshUtil {
     /**
      * 剩余可用内存
      *
+     * @param session 会话
      * @return 剩余可用内存
      */
-    public static double memory1(ClientSession session) throws IOException {
+    public static double memoryAvailable(ClientSession session) throws IOException {
         String execute = SshUtil.execute(session, "cat /proc/meminfo");
         String[] split = execute.split("\n");
         Map<String, Object> map = new HashMap<>();
@@ -278,7 +280,7 @@ public class SshUtil {
      * @param session 会话
      * @return 磁盘使用率
      */
-    public static double disk0(ClientSession session) throws IOException {
+    public static double diskUsage(ClientSession session) throws IOException {
         String execute = execute(session, "df --total");
         String[] split = execute.split("\n");
         String[] split1 = split[split.length - 1].split("\\s+");
@@ -291,9 +293,11 @@ public class SshUtil {
 
     /**
      * 剩余可用磁盘
+     *
+     * @param session 会话
      * @return 剩余可用磁盘
      */
-    public static double disk1(ClientSession session) throws IOException {
+    public static double diskAvailable(ClientSession session) throws IOException {
         String execute = execute(session, "df --total");
         String[] split = execute.split("\n");
         String[] split1 = split[split.length - 1].split("\\s+");

+ 30 - 18
simulation-resource-monitor/src/main/java/com/css/simulation/resource/monitor/scheduler/MyScheduler.java

@@ -1,11 +1,18 @@
 package com.css.simulation.resource.monitor.scheduler;
 
+import api.common.pojo.dto.Gpu;
 import api.common.pojo.dto.Host;
+import api.common.pojo.po.home.SystemServerPO;
+import api.common.util.SshUtil;
+import api.common.util.StringUtil;
 import lombok.Data;
+import org.apache.sshd.client.session.ClientSession;
+import org.dom4j.DocumentException;
 import org.springframework.boot.context.properties.ConfigurationProperties;
 import org.springframework.scheduling.annotation.Scheduled;
 import org.springframework.stereotype.Component;
 
+import java.io.IOException;
 import java.util.List;
 
 @Component
@@ -27,27 +34,32 @@ public class MyScheduler {
             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))
+            try {
+                ClientSession session = SshUtil.getSession(ip, username, password);
+//                if ("gpu".equals(type)){
+//                    List<Gpu> gpuList = SshUtil.gpuInfo(session);
+//                    gpuList.forEach(gpu -> {
+//                        gpu.get
+//                    });
+//                }
+
+                SystemServerPO systemServerPO = SystemServerPO.builder()
+                        .id(StringUtil.getRandomUUID())
+                        .serverId(SshUtil.hostname(session))
+                        .serverAddress(ip)
+                        .serverType(type)
+                        .cpuUsage((int) SshUtil.cpuUsage(session))
+                        .memoryUsage((int) SshUtil.memoryUsage(session))
+                        .memoryAvailable(SshUtil.memoryAvailable(session))
+                        .diskUsage((int) SshUtil.diskUsage(session))
+                        .diskAvailable(SshUtil.diskAvailable(session))
 //                        .taskNumber()
 //                        .weight()
 //                        .gpuUsage("gpu".equals(type)?SshUtil.gpu(session).stream().mapto :0)
-//                        .build();
-//            } catch (IOException | InterruptedException | DocumentException e) {
-//                e.printStackTrace();
-//            }
+                        .build();
+            } catch (Exception e) {
+                e.printStackTrace();
+            }
         });
 
 

+ 2 - 1
simulation-resource-scheduler/src/main/java/com/css/simulation/resource/scheduler/consumer/ManualProjectConsumer.java

@@ -94,7 +94,8 @@ public class ManualProjectConsumer {
         String projectJson = projectRecord.value();
         ProjectMessageDTO projectMessageDTO = JsonUtil.jsonToBean(projectJson, ProjectMessageDTO.class);
         String projectId = projectMessageDTO.getProjectId();    // 项目 id
-        projectMapper.updateProjectState(projectId, "20");   // 修改该 project 的状态为执行中,同时将已完成任务重置为0方便测试
+        projectMapper.updateProjectState(projectId, DictConstants.PROJECT_RUNNING);   // 修改该 project 的状态为执行中,同时将已完成任务重置为 0 方便测试。
+        taskMapper.updateStateByProjectId(projectId,DictConstants.TASK_PENDING);    // 将该 project 下所有任务重置为待执行方便测试。
 
 
         // -------------------------------- 1 场景 --------------------------------

+ 5 - 0
simulation-resource-scheduler/src/main/java/com/css/simulation/resource/scheduler/mapper/TaskMapper.java

@@ -75,4 +75,9 @@ public interface TaskMapper {
             "from simulation_manual_project_task\n" +
             "where p_id = #{projectId} and scene_id = #{sceneId}")
     String selectIdByProjectIdAndSceneId(@Param("projectId")String projectId,@Param("sceneId") String sceneId);
+
+    @Update("update simulation_manual_project_task\n" +
+            "set run_state = #{runState}\n" +
+            "where p_id = #{projectId}")
+    void updateStateByProjectId(@Param("projectId") String projectId, @Param("runState") String runState);
 }