|
@@ -4,80 +4,99 @@ import api.common.pojo.dto.Host;
|
|
import api.common.pojo.po.home.SystemServerPO;
|
|
import api.common.pojo.po.home.SystemServerPO;
|
|
import api.common.util.SshUtil;
|
|
import api.common.util.SshUtil;
|
|
import api.common.util.StringUtil;
|
|
import api.common.util.StringUtil;
|
|
-import com.css.simulation.resource.monitor.mappper.SystemServerMapper;
|
|
|
|
|
|
+import api.common.util.TimeUtil;
|
|
|
|
+import com.css.simulation.resource.monitor.manager.SystemServerManager;
|
|
import lombok.Data;
|
|
import lombok.Data;
|
|
import lombok.extern.slf4j.Slf4j;
|
|
import lombok.extern.slf4j.Slf4j;
|
|
import org.apache.sshd.client.SshClient;
|
|
import org.apache.sshd.client.SshClient;
|
|
import org.apache.sshd.client.session.ClientSession;
|
|
import org.apache.sshd.client.session.ClientSession;
|
|
|
|
+import org.dom4j.DocumentException;
|
|
import org.springframework.beans.factory.annotation.Autowired;
|
|
import org.springframework.beans.factory.annotation.Autowired;
|
|
import org.springframework.boot.context.properties.ConfigurationProperties;
|
|
import org.springframework.boot.context.properties.ConfigurationProperties;
|
|
import org.springframework.scheduling.annotation.Scheduled;
|
|
import org.springframework.scheduling.annotation.Scheduled;
|
|
import org.springframework.stereotype.Component;
|
|
import org.springframework.stereotype.Component;
|
|
|
|
|
|
|
|
+import java.io.IOException;
|
|
|
|
+import java.util.ArrayList;
|
|
import java.util.List;
|
|
import java.util.List;
|
|
|
|
|
|
@Component
|
|
@Component
|
|
-@ConfigurationProperties(prefix = "ssh")
|
|
|
|
|
|
+
|
|
@Data
|
|
@Data
|
|
@Slf4j
|
|
@Slf4j
|
|
|
|
+@ConfigurationProperties(prefix = "ssh")
|
|
public class MyScheduler {
|
|
public class MyScheduler {
|
|
|
|
|
|
List<Host> hostList;
|
|
List<Host> hostList;
|
|
|
|
|
|
|
|
+
|
|
@Autowired
|
|
@Autowired
|
|
- SystemServerMapper systemServerManager;
|
|
|
|
|
|
+ SystemServerManager systemServerManager;
|
|
|
|
|
|
|
|
|
|
-// @Scheduled(fixedDelay = 60 * 60 * 1000)
|
|
|
|
|
|
+ // @Scheduled(fixedDelay = 60 * 60 * 1000)
|
|
@Scheduled(fixedDelay = 2 * 1000)
|
|
@Scheduled(fixedDelay = 2 * 1000)
|
|
- public void server() {
|
|
|
|
-
|
|
|
|
- log.info("MyScheduler-------server 获取服务器信息!");
|
|
|
|
-
|
|
|
|
-
|
|
|
|
- hostList.forEach(host -> {
|
|
|
|
-
|
|
|
|
|
|
+ public void server() throws IOException, InterruptedException, DocumentException {
|
|
|
|
+ List<SystemServerPO> systemServerPOList = new ArrayList<>();
|
|
|
|
+ for (Host host : hostList) {
|
|
|
|
+ int taskNumber = 0;
|
|
|
|
+ SshClient sshClient = SshClient.setUpDefaultClient();
|
|
String ip = host.getHostname();
|
|
String ip = host.getHostname();
|
|
Integer port = host.getPort();
|
|
Integer port = host.getPort();
|
|
String username = host.getUsername();
|
|
String username = host.getUsername();
|
|
String password = host.getPassword();
|
|
String password = host.getPassword();
|
|
String type = host.getType();
|
|
String type = host.getType();
|
|
-
|
|
|
|
- try {
|
|
|
|
- SshClient sshClient = SshUtil.getClient();
|
|
|
|
- ClientSession session = SshUtil.getSession(sshClient, ip, username, password);
|
|
|
|
- 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())
|
|
|
|
- .serverId(SshUtil.hostname(session))
|
|
|
|
- .serverAddress(ip)
|
|
|
|
- .serverType(type)
|
|
|
|
- .memoryUsage(SshUtil.memoryUsage(session))
|
|
|
|
- .memoryAvailable(SshUtil.memoryAvailable(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)
|
|
|
|
- .build();
|
|
|
|
-
|
|
|
|
- systemServerManager.insert(systemServerPO);
|
|
|
|
- } catch (Exception e) {
|
|
|
|
- e.printStackTrace();
|
|
|
|
|
|
+ ClientSession session = SshUtil.getSession(sshClient, ip, username, password);
|
|
|
|
+ double gpuUsage = 0.0;
|
|
|
|
+ long gpuTotal = 0L;
|
|
|
|
+ if ("gpu".equals(type)) {
|
|
|
|
+ gpuUsage = SshUtil.gpuUsage(session);
|
|
|
|
+ gpuTotal = SshUtil.gpuTotal(session);
|
|
|
|
+ String podList = SshUtil.execute(session, "kubectl get pod | grep project");
|
|
|
|
+ taskNumber = StringUtil.countSubString(podList, "project");
|
|
}
|
|
}
|
|
- });
|
|
|
|
|
|
|
|
|
|
+ SystemServerPO systemServerPO = SystemServerPO.builder()
|
|
|
|
+ .id(StringUtil.getRandomUUID())
|
|
|
|
+ .serverId(SshUtil.hostname(session))
|
|
|
|
+ .serverAddress(ip)
|
|
|
|
+ .serverType(type)
|
|
|
|
+ .memoryUsage(SshUtil.memoryUsage(session))
|
|
|
|
+ .memoryAvailable(SshUtil.memoryAvailable(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(gpuUsage)
|
|
|
|
+ .gpuTotal(gpuTotal)
|
|
|
|
+ .taskNumber(taskNumber)
|
|
|
|
+ .build();
|
|
|
|
+ systemServerPO.setCreateTime(TimeUtil.getNowForMysql());
|
|
|
|
+ systemServerPO.setCreateUserId("simulation-resource-monitor");
|
|
|
|
+ systemServerPO.setModifyTime(TimeUtil.getNowForMysql());
|
|
|
|
+ systemServerPO.setModifyUserId("simulation-resource-monitor");
|
|
|
|
+ systemServerPO.setModifyTime(TimeUtil.getNowForMysql());
|
|
|
|
+ systemServerPO.setIsDeleted("0");
|
|
|
|
+ systemServerPOList.add(systemServerPO);
|
|
|
|
+ session.close();
|
|
|
|
+ sshClient.stop();
|
|
|
|
+ }
|
|
|
|
+ log.info("MyScheduler-------server 获取服务器信息!" + systemServerPOList);
|
|
|
|
+ systemServerManager.replace(systemServerPOList);
|
|
|
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
+// @Scheduled(fixedDelay = 2 * 1000)
|
|
|
|
+// public void test() throws IOException, DocumentException {
|
|
|
|
+// SshClient client = SshUtil.getClient();
|
|
|
|
+// ClientSession session = SshUtil.getSession(client, "182.92.203.182", "root", "CICV2022test");
|
|
|
|
+// double gpuUsage = SshUtil.gpuUsage(session);
|
|
|
|
+// long gpuTotal = SshUtil.gpuTotal(session);
|
|
|
|
+// System.out.println(gpuUsage);
|
|
|
|
+// System.out.println(gpuTotal);
|
|
|
|
+// }
|
|
|
|
+
|
|
|
|
|
|
}
|
|
}
|