Преглед на файлове

Merge remote-tracking branch 'origin/master'

wangzhiqiang преди 3 години
родител
ревизия
658c9fc7e1

+ 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;
+
+}

+ 19 - 0
api-common/src/main/java/api/common/pojo/po/home/SystemAccessPO.java

@@ -0,0 +1,19 @@
+package api.common.pojo.po.home;
+
+
+import lombok.AllArgsConstructor;
+import lombok.Builder;
+import lombok.Data;
+import lombok.NoArgsConstructor;
+
+import java.sql.Timestamp;
+
+
+@Data
+@Builder
+@NoArgsConstructor
+@AllArgsConstructor
+public class SystemAccessPO {
+    String id;
+    Timestamp accessTime;
+}

+ 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


+ 0 - 64
simulation-resource-server/src/main/java/com/css/simulation/resource/aspect/WebLogAspect.java

@@ -1,64 +0,0 @@
-//package com.css.simulation.resource.aspect;
-//
-//import api.common.pojo.po.WebLog;
-//import lombok.extern.slf4j.Slf4j;
-//import org.aspectj.lang.JoinPoint;
-//import org.aspectj.lang.ProceedingJoinPoint;
-//import org.aspectj.lang.Signature;
-//import org.aspectj.lang.annotation.*;
-//import org.aspectj.lang.reflect.MethodSignature;
-//import org.springframework.stereotype.Component;
-//import org.springframework.web.context.request.RequestContextHolder;
-//import org.springframework.web.context.request.ServletRequestAttributes;
-//import sun.net.util.URLUtil;
-//
-//import javax.servlet.http.HttpServletRequest;
-//import java.lang.reflect.Method;
-//import java.util.HashMap;
-//import java.util.Map;
-//
-///**
-// * 统一日志处理切面 * Created by 石磊
-// */
-//@Aspect
-//@Component
-//@Slf4j
-//public class WebLogAspect {
-//
-//    @Pointcut("execution(public * com.*.controller.*.*(..))")
-//    public void webLog() {
-//    }
-//
-//    @Before("webLog()")
-//    public void doBefore(JoinPoint joinPoint) throws Throwable {
-//    }
-//
-//    @AfterReturning(value = "webLog()", returning = "ret")
-//    public void doAfterReturning(Object ret) throws Throwable {
-//    }
-//
-//    @Around("webLog()")
-//    public Object doAround(ProceedingJoinPoint joinPoint) throws Throwable {
-//        long startTime = System.currentTimeMillis();        //获取当前请求对象
-//        ServletRequestAttributes attributes = (ServletRequestAttributes) RequestContextHolder.getRequestAttributes();
-//        HttpServletRequest request = attributes.getRequest();        //记录请求信息(通过Logstash传入Elasticsearch)
-//        WebLog webLog = new WebLog();
-//        Object result = joinPoint.proceed();
-//        Signature signature = joinPoint.getSignature();
-//        MethodSignature methodSignature = (MethodSignature) signature;
-//        Method method = methodSignature.getMethod();
-//        if (method.isAnnotationPresent(ApiOperation.class)) {
-//            ApiOperation log = method.getAnnotation(ApiOperation.class);
-//            webLog.setDescription(log.value());
-//        }
-//        long endTime = System.currentTimeMillis();
-//        String urlStr = request.getRequestURL().toString();
-//        webLog.setBasePath(StrUtil.removeSuffix(urlStr, URLUtil.url(urlStr).getPath()));
-//        webLog.setIp(request.getRemoteUser());
-//        Map<String, Object> logMap = new HashMap<>();
-//        logMap.put("spendTime", webLog.getSpendTime());
-//        logMap.put("description", webLog.getDescription());
-//        LOGGER.info("{}", JSONUtil.parse(webLog));
-//        return result;
-//    }
-//}

+ 5 - 0
simulation-resource-server/src/main/java/com/css/simulation/resource/scene/mapper/SystemAccessMapper.java

@@ -1,6 +1,8 @@
 package com.css.simulation.resource.scene.mapper;
 
+import api.common.pojo.po.home.SystemAccessPO;
 import org.apache.ibatis.annotations.Mapper;
+import org.apache.ibatis.annotations.Param;
 import org.apache.ibatis.annotations.Select;
 
 import java.util.Map;
@@ -21,4 +23,7 @@ public interface SystemAccessMapper {
             "        </if>" +
             "</script>")
     Integer selectPastAccess(Map<String, Object> params);
+
+    @Select("insert into system_access(id, access_time) values (#{po.id}, #{po.accessTime})")
+    Integer insert(@Param("po") SystemAccessPO systemAccessPO);
 }

+ 9 - 0
simulation-resource-server/src/main/java/com/css/simulation/resource/scene/service/HomePageService.java

@@ -2,11 +2,13 @@ package com.css.simulation.resource.scene.service;
 
 import api.common.pojo.common.PageVO;
 import api.common.pojo.common.ResponseBodyVO;
+import api.common.pojo.po.home.SystemAccessPO;
 import api.common.pojo.po.home.SystemServerPO;
 import api.common.pojo.vo.home.AccessVO;
 import api.common.pojo.vo.home.HardwareVO;
 import api.common.pojo.vo.home.ServiceVO;
 import api.common.pojo.vo.home.SystemServerVO;
+import api.common.util.StringUtil;
 import api.common.util.TimeUtil;
 import com.css.simulation.resource.feign.OauthClientService;
 import com.css.simulation.resource.scene.mapper.SystemAccessMapper;
@@ -75,6 +77,13 @@ public class HomePageService {
 
 
     public ResponseBodyVO<List<AccessVO>> selectAccess() {
+
+        SystemAccessPO systemAccessPO = SystemAccessPO.builder()
+                .id(StringUtil.getRandomUUID())
+                .accessTime(TimeUtil.getNowForMysql())
+                .build();
+        systemAccessMapper.insert(systemAccessPO);
+
         List<AccessVO> list = new ArrayList<>();
         for (int i = 6; i > -1; i--) {
             AccessVO vo = new AccessVO();