|
|
|
package com.zhonglai.luhui.admin.controller.monitor;
|
|
|
|
|
|
|
|
import com.ruoyi.common.core.domain.AjaxResult;
|
|
|
|
import com.ruoyi.common.core.page.TableDataInfo;
|
|
|
|
import com.ruoyi.common.utils.ServletUtils;
|
|
|
|
import com.ruoyi.common.utils.ip.IpUtils;
|
|
|
|
import com.ruoyi.framework.web.domain.Server;
|
|
|
|
import com.ruoyi.system.domain.sys.SysMonitorServer;
|
|
|
|
import com.ruoyi.system.domain.sys.SysMonitorServerLog;
|
|
|
|
import com.zhonglai.luhui.action.BaseController;
|
|
|
|
import com.zhonglai.luhui.admin.dto.MonitorServerUploadDto;
|
|
|
|
import com.zhonglai.luhui.dao.service.PublicService;
|
|
|
|
import com.zhonglai.luhui.device.domain.IotAlertLog;
|
|
|
|
import io.swagger.annotations.Api;
|
|
|
|
import io.swagger.annotations.ApiOperation;
|
|
|
|
import org.springframework.beans.factory.annotation.Autowired;
|
|
|
|
import org.springframework.security.access.prepost.PreAuthorize;
|
|
|
|
import org.springframework.web.bind.annotation.GetMapping;
|
|
|
|
import org.springframework.web.bind.annotation.RequestMapping;
|
|
|
|
import org.springframework.web.bind.annotation.RestController;
|
|
|
|
import org.springframework.web.bind.annotation.*;
|
|
|
|
|
|
|
|
|
|
|
|
import javax.servlet.http.HttpServletRequest;
|
|
|
|
import java.io.UnsupportedEncodingException;
|
|
|
|
import java.net.URLDecoder;
|
|
|
|
import java.util.HashMap;
|
|
|
|
import java.util.List;
|
|
|
|
import java.util.Map;
|
|
|
|
|
|
|
|
import java.security.MessageDigest;
|
|
|
|
import java.security.NoSuchAlgorithmException;
|
|
|
|
import java.util.Arrays;
|
|
|
|
import java.util.stream.Collectors;
|
|
|
|
|
|
|
|
import javax.crypto.Cipher;
|
|
|
|
import javax.crypto.spec.IvParameterSpec;
|
|
|
|
import javax.crypto.spec.SecretKeySpec;
|
|
|
|
import java.security.MessageDigest;
|
|
|
|
import java.security.NoSuchAlgorithmException;
|
|
|
|
import java.util.Base64;
|
|
|
|
import java.util.Arrays;
|
|
|
|
import java.util.stream.Collectors;
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
* 服务器监控
|
|
...
|
...
|
@@ -17,8 +48,10 @@ import org.springframework.web.bind.annotation.RestController; |
|
|
|
@Api(tags = "服务器监控")
|
|
|
|
@RestController
|
|
|
|
@RequestMapping("/monitor/server")
|
|
|
|
public class ServerController
|
|
|
|
public class ServerController extends BaseController
|
|
|
|
{
|
|
|
|
@Autowired
|
|
|
|
private PublicService publicService;
|
|
|
|
@ApiOperation("获取详情")
|
|
|
|
@PreAuthorize("@ss.hasPermi('monitor:server:list')")
|
|
|
|
@GetMapping()
|
|
...
|
...
|
@@ -28,4 +61,158 @@ public class ServerController |
|
|
|
server.copyTo();
|
|
|
|
return AjaxResult.success(server);
|
|
|
|
}
|
|
|
|
|
|
|
|
@ApiOperation("上传状态")
|
|
|
|
@PostMapping("/upload")
|
|
|
|
public AjaxResult upload(@RequestBody MonitorServerUploadDto monitorServerUploadDto)
|
|
|
|
{
|
|
|
|
String ip = IpUtils.getIpAddr(ServletUtils.getRequest());
|
|
|
|
SysMonitorServer sysMonitorServer = new SysMonitorServer();
|
|
|
|
sysMonitorServer.setIp(ip);
|
|
|
|
Long st = publicService.getObjectListTotle(sysMonitorServer, null);
|
|
|
|
sysMonitorServer.setCpu_usage(new Double(monitorServerUploadDto.getCpu_usage()*100).intValue());
|
|
|
|
sysMonitorServer.setConnection_count(monitorServerUploadDto.getConnection_count());
|
|
|
|
sysMonitorServer.setMemory_usage(new Double(monitorServerUploadDto.getMemory_usage()*100).intValue());
|
|
|
|
sysMonitorServer.setDisk_usage(new Double(monitorServerUploadDto.getDisk_usage()*100).intValue());
|
|
|
|
if(st==0)
|
|
|
|
{
|
|
|
|
sysMonitorServer.setCreate_time(System.currentTimeMillis());
|
|
|
|
publicService.insert(sysMonitorServer);
|
|
|
|
}else {
|
|
|
|
publicService.updateObject(sysMonitorServer,"ip");
|
|
|
|
}
|
|
|
|
|
|
|
|
SysMonitorServerLog sysMonitorServerLog = new SysMonitorServerLog();
|
|
|
|
sysMonitorServerLog.setIp(ip);
|
|
|
|
sysMonitorServerLog.setCpu_usage(sysMonitorServer.getCpu_usage());
|
|
|
|
sysMonitorServerLog.setConnection_count(sysMonitorServer.getConnection_count());
|
|
|
|
sysMonitorServerLog.setMemory_usage(sysMonitorServer.getMemory_usage());
|
|
|
|
sysMonitorServerLog.setDisk_usage(sysMonitorServer.getDisk_usage());
|
|
|
|
sysMonitorServerLog.setCreate_time(System.currentTimeMillis());
|
|
|
|
publicService.insert(sysMonitorServerLog);
|
|
|
|
return AjaxResult.success();
|
|
|
|
}
|
|
|
|
@ApiOperation("企业微信消息")
|
|
|
|
@RequestMapping("/uploadWxMessage/{agentId}")
|
|
|
|
public AjaxResult uploadWxMessage(@PathVariable String agentId)
|
|
|
|
{
|
|
|
|
|
|
|
|
return AjaxResult.success();
|
|
|
|
}
|
|
|
|
|
|
|
|
public static void main(String[] args) throws UnsupportedEncodingException {
|
|
|
|
String TOKEN = "DDbVb3cSiTAbCSgI4UndPlvy"; // 替换为你的token
|
|
|
|
String ENCODING_AES_KEY = "FH1CDpqZhNkcXCWEYJOyAO4HLG2mxYrE1c4j83mXruB";
|
|
|
|
String msgSignature = "6cfbf56d42c00a182a6da650023ef5ba407cb203";
|
|
|
|
String timestamp = "1724921459";
|
|
|
|
String nonce = "1725665565";
|
|
|
|
String echoStr = "FQYmaDL0vzQPBz5rRVYYGVLx62EmD8krbPYO6RzVaAtsIuFz+n8nPZ8fPtnNTbMCiGaRrjZCYgbsaeDcqPqvNQ\u003d\u003d";
|
|
|
|
|
|
|
|
// URL解码
|
|
|
|
String decodedEchoStr = decodeEchoStr(echoStr);
|
|
|
|
|
|
|
|
// 校验msg_signature
|
|
|
|
boolean isValid = validateMsgSignature(TOKEN, timestamp, nonce, decodedEchoStr, msgSignature);
|
|
|
|
if (isValid) {
|
|
|
|
System.out.println("请求合法");
|
|
|
|
} else {
|
|
|
|
System.out.println("请求不合法");
|
|
|
|
}
|
|
|
|
|
|
|
|
// 解密echostr
|
|
|
|
String messageContent = decryptEchoStr(decodedEchoStr,ENCODING_AES_KEY);
|
|
|
|
System.out.println("消息内容: " + messageContent);
|
|
|
|
}
|
|
|
|
|
|
|
|
private static String decodeEchoStr(String echoStr) throws UnsupportedEncodingException {
|
|
|
|
// 假设echoStr已经被Base64编码过,这里直接返回
|
|
|
|
return URLDecoder.decode(echoStr, "UTF-8"); // 实际上需要根据情况解码
|
|
|
|
}
|
|
|
|
|
|
|
|
private static boolean validateMsgSignature(String token, String timestamp, String nonce, String msgEncrypt, String msgSignature) {
|
|
|
|
try {
|
|
|
|
String signature = sha1(Arrays.asList(token, timestamp, nonce, msgEncrypt).stream().sorted().collect(Collectors.joining()));
|
|
|
|
return msgSignature.equals(signature);
|
|
|
|
} catch (NoSuchAlgorithmException e) {
|
|
|
|
e.printStackTrace();
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
private static String sha1(String input) throws NoSuchAlgorithmException {
|
|
|
|
MessageDigest mDigest = MessageDigest.getInstance("SHA1");
|
|
|
|
byte[] result = mDigest.digest(input.getBytes());
|
|
|
|
StringBuilder sb = new StringBuilder();
|
|
|
|
for (byte aResult : result) {
|
|
|
|
sb.append(Integer.toString((aResult & 0xff) + 0x100, 16).substring(1));
|
|
|
|
}
|
|
|
|
return sb.toString();
|
|
|
|
}
|
|
|
|
private static final String CORPID = "wx5823bf96d3bd56c7";
|
|
|
|
private static String decryptEchoStr(String encryptedMsg,String ENCODING_AES_KEY) {
|
|
|
|
// 这里需要实现具体的解密逻辑
|
|
|
|
try {
|
|
|
|
// 1. BASE64解码
|
|
|
|
byte[] base64Decoded = Base64.getDecoder().decode(encryptedMsg);
|
|
|
|
|
|
|
|
// 2. AES解密
|
|
|
|
byte[] keyBytes = Base64.getDecoder().decode(ENCODING_AES_KEY + "=");
|
|
|
|
SecretKeySpec keySpec = new SecretKeySpec(keyBytes, "AES");
|
|
|
|
IvParameterSpec ivSpec = new IvParameterSpec(keyBytes, 0, 16); // 使用前16个字节作为IV
|
|
|
|
|
|
|
|
Cipher cipher = Cipher.getInstance("AES/CBC/PKCS5Padding");
|
|
|
|
cipher.init(Cipher.DECRYPT_MODE, keySpec, ivSpec);
|
|
|
|
|
|
|
|
byte[] decrypted = cipher.doFinal(base64Decoded);
|
|
|
|
|
|
|
|
// 3. 去除随机字节和msg_len
|
|
|
|
byte[] content = Arrays.copyOfRange(decrypted, 16, decrypted.length);
|
|
|
|
int msgLen = ((content[0] & 0xFF) << 24) |
|
|
|
|
((content[1] & 0xFF) << 16) |
|
|
|
|
((content[2] & 0xFF) << 8) |
|
|
|
|
(content[3] & 0xFF);
|
|
|
|
|
|
|
|
byte[] msg = Arrays.copyOfRange(content, 4, 4 + msgLen);
|
|
|
|
byte[] receiveId = Arrays.copyOfRange(content, 4 + msgLen, content.length);
|
|
|
|
|
|
|
|
// 4. 转换为字符串
|
|
|
|
String msgStr = new String(msg, "UTF-8");
|
|
|
|
String receiveIdStr = new String(receiveId, "UTF-8");
|
|
|
|
|
|
|
|
// 5. 验证receiveId
|
|
|
|
if (receiveIdStr.equals(CORPID)) {
|
|
|
|
return msgStr;
|
|
|
|
} else {
|
|
|
|
throw new IllegalArgumentException("ReceiveId 不匹配");
|
|
|
|
}
|
|
|
|
} catch (Exception e) {
|
|
|
|
e.printStackTrace();
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
@ApiOperation("获取状态列表")
|
|
|
|
@GetMapping("/getSysMonitorServerList")
|
|
|
|
public TableDataInfo getSysMonitorServerList(SysMonitorServer sysMonitorServer) throws Exception
|
|
|
|
{
|
|
|
|
startPage();
|
|
|
|
startOrderBy();
|
|
|
|
List<Map<String,Object>> list = publicService.getObjectList(sysMonitorServer,"*",null,null,0,0);
|
|
|
|
return getDataTable(list);
|
|
|
|
}
|
|
|
|
|
|
|
|
@ApiOperation("获取服务器状态曲线")
|
|
|
|
@GetMapping("/getSysMonitorServerLogList")
|
|
|
|
public TableDataInfo getSysMonitorServerLogList(String ip,Long startTime,Long endTime) throws Exception
|
|
|
|
{
|
|
|
|
SysMonitorServerLog sysMonitorServerLog = new SysMonitorServerLog();
|
|
|
|
sysMonitorServerLog.setIp(ip);
|
|
|
|
|
|
|
|
Map<String, Object> whereMap = new HashMap<>();
|
|
|
|
whereMap.put("create_time","time=");
|
|
|
|
sysMonitorServerLog.setCreate_time(startTime);
|
|
|
|
whereMap.put("end_create_time",endTime);
|
|
|
|
List<Map<String,Object>> list = publicService.getObjectList(sysMonitorServerLog,"*",whereMap,"create_time asc",0,0);
|
|
|
|
return getDataTable(list);
|
|
|
|
}
|
|
|
|
} |
...
|
...
|
|