|
|
@@ -1,6 +1,7 @@
|
|
|
package com.xunmei.system.service.impl;
|
|
|
|
|
|
import cn.hutool.core.util.ObjectUtil;
|
|
|
+import cn.hutool.crypto.digest.DigestUtil;
|
|
|
import cn.hutool.http.HttpException;
|
|
|
import cn.hutool.http.HttpRequest;
|
|
|
import cn.hutool.http.HttpResponse;
|
|
|
@@ -17,8 +18,10 @@ import com.xunmei.common.core.thread.ThreadPoolConfig;
|
|
|
import com.xunmei.common.core.util.BeanHelper;
|
|
|
import com.xunmei.common.core.utils.DateUtils;
|
|
|
import com.xunmei.common.core.web.page.TableDataInfo;
|
|
|
+import com.xunmei.common.redis.utils.RedisUtils;
|
|
|
import com.xunmei.common.security.utils.SecurityUtils;
|
|
|
import com.xunmei.system.api.RemoteFileService;
|
|
|
+import com.xunmei.system.config.MonitorCatConfig;
|
|
|
import com.xunmei.system.domain.SysServer;
|
|
|
import com.xunmei.system.dto.server.SysServerEditDto;
|
|
|
import com.xunmei.system.dto.server.SysServerPageDto;
|
|
|
@@ -29,6 +32,7 @@ import com.xunmei.system.vo.server.CheckListVo;
|
|
|
import com.xunmei.system.vo.server.FileInfoVo;
|
|
|
import com.xunmei.system.vo.server.SysServerPageVo;
|
|
|
import feign.Response;
|
|
|
+import lombok.extern.slf4j.Slf4j;
|
|
|
import org.springframework.beans.BeanUtils;
|
|
|
import org.springframework.beans.factory.annotation.Autowired;
|
|
|
import org.springframework.beans.factory.annotation.Qualifier;
|
|
|
@@ -36,12 +40,10 @@ import org.springframework.scheduling.concurrent.ThreadPoolTaskExecutor;
|
|
|
import org.springframework.stereotype.Service;
|
|
|
import org.springframework.transaction.annotation.Transactional;
|
|
|
|
|
|
+import javax.annotation.Resource;
|
|
|
import javax.servlet.http.HttpServletResponse;
|
|
|
import java.io.*;
|
|
|
-import java.util.ArrayList;
|
|
|
-import java.util.Arrays;
|
|
|
-import java.util.List;
|
|
|
-import java.util.UUID;
|
|
|
+import java.util.*;
|
|
|
import java.util.concurrent.CompletableFuture;
|
|
|
import java.util.concurrent.ExecutionException;
|
|
|
import java.util.concurrent.TimeUnit;
|
|
|
@@ -54,6 +56,7 @@ import java.util.stream.Collectors;
|
|
|
* @author xunmei
|
|
|
* @date 2024-09-29
|
|
|
*/
|
|
|
+@Slf4j
|
|
|
@Service
|
|
|
public class SysServerServiceImpl extends ServiceImpl<SysServerMapper, SysServer> implements ISysServerService {
|
|
|
|
|
|
@@ -64,6 +67,8 @@ public class SysServerServiceImpl extends ServiceImpl<SysServerMapper, SysServer
|
|
|
@Autowired
|
|
|
@Qualifier(ThreadPoolConfig.SOC_EXECUTOR)
|
|
|
private ThreadPoolTaskExecutor threadPoolTaskExecutor;
|
|
|
+ @Resource
|
|
|
+ private MonitorCatConfig monitorCatConfig;
|
|
|
|
|
|
@Override
|
|
|
public TableDataInfo<SysServerPageVo> selectPage(SysServerPageDto req) {
|
|
|
@@ -107,17 +112,43 @@ public class SysServerServiceImpl extends ServiceImpl<SysServerMapper, SysServer
|
|
|
String baseUrl = String.format("http://%s:%d", server.getServerIp(), server.getServerPort());
|
|
|
String getServicesApi = "/result/list?size=10000¤t=1";
|
|
|
HttpResponse response = HttpUtil.createPost(baseUrl + getServicesApi)
|
|
|
- .header("token", server.getServerToken())
|
|
|
+ .header("token", getToken(server))
|
|
|
.execute();
|
|
|
String serverJson = response.body();
|
|
|
JSONObject parse = JSONObject.parseObject(serverJson);
|
|
|
return parse.getObject("rows", new TypeReference<List<CheckListVo>>() {
|
|
|
});
|
|
|
+
|
|
|
} catch (HttpException e) {
|
|
|
return new ArrayList<>();
|
|
|
}
|
|
|
}
|
|
|
|
|
|
+ private String getToken(SysServer server) {
|
|
|
+ final String tokenCache = RedisUtils.getCacheObject("MonitorCatToken:" + server.getServerIp());
|
|
|
+ if (ObjectUtil.isNotEmpty(tokenCache)) {
|
|
|
+ return tokenCache;
|
|
|
+ }
|
|
|
+ String baseUrl = String.format("http://%s:%d", server.getServerIp(), server.getServerPort());
|
|
|
+ String loginUrl = "/login/login";
|
|
|
+ final HashMap<String, Object> hashMap = new HashMap<>();
|
|
|
+ hashMap.put("userName", monitorCatConfig.getUserName());
|
|
|
+ hashMap.put("userPwd", DigestUtil.sha1Hex(monitorCatConfig.getPassword()));
|
|
|
+ Object post = new Object();
|
|
|
+ try {
|
|
|
+ post = HttpUtil.get(baseUrl + loginUrl, hashMap, 3000);
|
|
|
+ final Map map = JSONObject.parseObject(post.toString(), Map.class);
|
|
|
+ final String token = (String) map.get("token");
|
|
|
+ if (ObjectUtil.isNotEmpty(token)) {
|
|
|
+ RedisUtils.setCacheObject("MonitorCatToken:" + server.getServerIp(), token, 1000 * 60 * 60 * 24);
|
|
|
+ }
|
|
|
+ return token;
|
|
|
+ } catch (Exception e) {
|
|
|
+ log.error("获取看门猫token失败, ip:{},{}", server.getServerIp(), post.toString());
|
|
|
+ throw new RuntimeException(e);
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
@Override
|
|
|
public Object getLogList(Long serverId, Long checkId) {
|
|
|
SysServer server = this.selectSysServerById(serverId);
|
|
|
@@ -126,7 +157,7 @@ public class SysServerServiceImpl extends ServiceImpl<SysServerMapper, SysServer
|
|
|
String getLogs = "/result/fileList?checkId=" + checkId;
|
|
|
//获取服务器列表
|
|
|
HttpResponse response = HttpUtil.createPost(baseUrl + getLogs)
|
|
|
- .header("token", server.getServerToken())
|
|
|
+ .header("token", getToken(server))
|
|
|
.execute();
|
|
|
String serverJson = response.body();
|
|
|
List<FileInfoVo> checks = JSONArray.parseArray(serverJson, FileInfoVo.class);
|
|
|
@@ -210,7 +241,7 @@ public class SysServerServiceImpl extends ServiceImpl<SysServerMapper, SysServer
|
|
|
public void downloadLogData(TransferLogDto req, HttpServletResponse response) {
|
|
|
final SysServer sysServer = getById(req.getServerId());
|
|
|
final String serverIp = sysServer.getServerIp();
|
|
|
- FileDownloadDto dto=new FileDownloadDto();
|
|
|
+ FileDownloadDto dto = new FileDownloadDto();
|
|
|
dto.setServerName(serverIp);
|
|
|
dto.setFilePath(req.getParentPath().split("logs")[1]);
|
|
|
dto.setFileName(req.getPath());
|
|
|
@@ -241,6 +272,7 @@ public class SysServerServiceImpl extends ServiceImpl<SysServerMapper, SysServer
|
|
|
throw new RuntimeException(e);
|
|
|
}
|
|
|
}
|
|
|
+
|
|
|
/**
|
|
|
* 查询服务器维护
|
|
|
*
|