martin há 3 anos atrás
pai
commit
41f61f06f9

+ 1 - 1
api-common/src/main/java/api/common/pojo/po/home/SystemServerPO.java

@@ -24,7 +24,7 @@ public class SystemServerPO extends CommonPO {
     Double cpuUsage;
     Long cpuTotal;   // cpu 线程数
     Double gpuUsage;
-    Integer gpuTotal;   // gpu 内存数
+    Long gpuTotal;   // gpu 内存数
     Integer taskNumber;
 
 

+ 0 - 1
api-common/src/main/java/api/common/pojo/vo/home/SystemServerVO.java

@@ -21,5 +21,4 @@ public class SystemServerVO {
     Integer diskUsage;
     Double diskAvailable;
     Integer taskNumber;
-    Integer weight;
 }

+ 5 - 5
api-common/src/main/java/api/common/util/SshUtil.java

@@ -199,7 +199,7 @@ public class SshUtil {
      * 显存使用率
      *
      * @param session 会话
-     * @return 显存总量,单位 MB
+     * @return 显存使用率
      */
     public static double gpuUsage(ClientSession session) throws DocumentException, IOException {
         List<GpuDTO> gpuDTOList = SshUtil.gpuInfo(session);
@@ -209,14 +209,14 @@ public class SshUtil {
     }
 
     /**
-     * 显存总量
+     * 显存总量 kb
      *
      * @param session 会话
-     * @return 显存总量,单位 MB
+     * @return 显存总量,单位 kb
      */
-    public static int gpuTotal(ClientSession session) throws DocumentException, IOException {
+    public static long gpuTotal(ClientSession session) throws DocumentException, IOException {
         List<GpuDTO> gpuDTOList = SshUtil.gpuInfo(session);
-        return gpuDTOList.stream().mapToInt(gpu -> Integer.parseInt(gpu.getTotalMemory().split(" ")[0])).sum();
+        return gpuDTOList.stream().mapToLong(gpu -> Integer.parseInt(gpu.getTotalMemory().split(" ")[0])).sum() * 1024;
     }
 
 

+ 19 - 0
api-common/src/main/java/api/common/util/StringUtil.java

@@ -23,6 +23,25 @@ public class StringUtil {
     }
 
 
+    /**
+     * @param source 长字符串
+     * @param target 子字符串
+     * @return 包含个数
+     */
+    public static int countSubString(String source, String target) {
+        if (isEmpty(source)|| isEmpty(target)) {
+            throw new RuntimeException("StringUtil-------countSubString 传入字符串不能为空!");
+        }
+        int count = 0;
+        int index = 0;
+        while ((index = source.indexOf(target, index)) != -1) {
+            index = index + target.length();
+            count++;
+        }
+        return count;
+    }
+
+
     public static String replace(String string, String target, String replacement) {
         return string.replace(target, replacement);
     }

+ 27 - 0
simulation-resource-monitor/src/main/java/com/css/simulation/resource/monitor/manager/SystemServerManager.java

@@ -0,0 +1,27 @@
+package com.css.simulation.resource.monitor.manager;
+
+import api.common.pojo.po.home.SystemServerPO;
+import com.css.simulation.resource.monitor.mappper.SystemServerMapper;
+import org.apache.ibatis.session.ExecutorType;
+import org.apache.ibatis.session.SqlSession;
+import org.apache.ibatis.session.SqlSessionFactory;
+import org.springframework.beans.factory.annotation.Autowired;
+import org.springframework.stereotype.Component;
+
+@Component
+public class SystemServerManager {
+
+
+    @Autowired
+    private SqlSessionFactory sqlSessionFactory;
+
+    public void batchUpdateByScoreResult(SystemServerPO systemServerPO) {
+
+        try (SqlSession sqlSession = sqlSessionFactory.openSession(ExecutorType.BATCH, false)) {
+            SystemServerMapper systemServerMapper = sqlSession.getMapper(SystemServerMapper.class);
+            systemServerMapper.delete();
+            systemServerMapper.insert(systemServerPO);
+            sqlSession.commit();
+        }
+    }
+}

+ 10 - 34
simulation-resource-monitor/src/main/java/com/css/simulation/resource/monitor/mappper/SystemServerMapper.java

@@ -4,43 +4,19 @@ import api.common.pojo.po.home.SystemServerPO;
 import org.apache.ibatis.annotations.Mapper;
 import org.apache.ibatis.annotations.Select;
 
-import java.util.List;
-
-
-/**
- * create table if not exists simulation.system_server
- * (
- * 	id varchar(32) null,
- * 	server_id varchar(64) null,
- * 	server_address varchar(32) null,
- * 	server_type varchar(32) null,
- * 	cpu_useage varchar(32) null,
- * 	memory_useage varchar(32) null,
- * 	memory_available varchar(32) null comment '剩余内存,单位GB',
- * 	disk_usage varchar(32) null,
- * 	disk_abailable varchar(32) null comment '剩余磁盘,单位GB',
- * 	task_number varchar(32) null comment '执行任务数量'
- * )
- * comment '服务器表';
- */
+
+
 @Mapper
 public interface SystemServerMapper {
 
 
-    @Select("select id,\n" +
-            "       server_id,\n" +
-            "       server_address,\n" +
-            "       server_type,\n" +
-            "       cpu_usage,\n" +
-            "       memory_usage,\n" +
-            "       memory_available,\n" +
-            "       disk_usage,\n" +
-            "       disk_available,\n" +
-            "       task_number,\n" +
-            "       weight,\n" +
-            "       gpu_usage\n" +
-            "from system_server\n" +
-            "where is_deleted = '0'")
-    List<SystemServerPO> insert();
+    @Select("delete from system_server where 1=1")
+    void delete();
+
+    @Select("insert into system_server(id, server_id, server_address, server_type, memory_usage, memory_available, memory_total,\n" +
+            "                          disk_usage, disk_available, disk_total, cpu_usage, cpu_total, gpu_usage, task_number,\n" +
+            "                          create_time, modify_time, create_user_id, modify_user_id, is_deleted, gpu_total)\n" +
+            "values (#{id})")
+    void insert(SystemServerPO systemServerPO);
 
 }

+ 16 - 9
simulation-resource-monitor/src/main/java/com/css/simulation/resource/monitor/scheduler/MyScheduler.java

@@ -4,9 +4,11 @@ import api.common.pojo.dto.Host;
 import api.common.pojo.po.home.SystemServerPO;
 import api.common.util.SshUtil;
 import api.common.util.StringUtil;
+import com.css.simulation.resource.monitor.mappper.SystemServerMapper;
 import lombok.Data;
 import org.apache.sshd.client.SshClient;
 import org.apache.sshd.client.session.ClientSession;
+import org.springframework.beans.factory.annotation.Autowired;
 import org.springframework.boot.context.properties.ConfigurationProperties;
 import org.springframework.scheduling.annotation.Scheduled;
 import org.springframework.stereotype.Component;
@@ -20,6 +22,10 @@ public class MyScheduler {
 
     List<Host> hostList;
 
+    @Autowired
+    SystemServerMapper systemServerManager;
+
+
     @Scheduled(fixedDelay = 60 * 60 * 1000)
     public void server() {
 
@@ -35,12 +41,11 @@ public class MyScheduler {
             try {
                 SshClient sshClient = SshUtil.getClient();
                 ClientSession session = SshUtil.getSession(sshClient, ip, username, password);
-//                if ("gpu".equals(type)){
-//                    List<GpuDTO> gpuList = SshUtil.gpuInfo(session);
-//                    gpuList.forEach(gpu -> {
-//                        gpu.get
-//                    });
-//                }
+                int taskNumber = 0;
+                if ("gpu".equals(type)) {
+                    String podList = SshUtil.execute(session, "kubectl get pod | grep project");
+                    taskNumber = StringUtil.countSubString(podList, "project");
+                }
 
                 SystemServerPO systemServerPO = SystemServerPO.builder()
                         .id(StringUtil.getRandomUUID())
@@ -49,16 +54,18 @@ public class MyScheduler {
                         .serverType(type)
                         .memoryUsage(SshUtil.memoryUsage(session))
                         .memoryAvailable(SshUtil.memoryAvailable(session))
-                        .memoryTotal( SshUtil.memoryTotal(session))
-                        .diskUsage( SshUtil.diskUsage(session))
+                        .memoryTotal(SshUtil.memoryTotal(session))
+                        .diskUsage(SshUtil.diskUsage(session))
                         .diskAvailable(SshUtil.diskAvailable(session))
                         .diskTotal(SshUtil.diskTotal(session))
                         .cpuUsage(SshUtil.cpuUsage(session))
                         .cpuTotal(SshUtil.cpuTotal(session))
                         .gpuUsage(SshUtil.gpuUsage(session))
                         .gpuTotal(SshUtil.gpuTotal(session))
-//                        .taskNumber()
+                        .taskNumber(taskNumber)
                         .build();
+
+                systemServerManager.insert(systemServerPO);
             } catch (Exception e) {
                 e.printStackTrace();
             }