fix:完善服务器图表

This commit is contained in:
nieziyan 2024-01-03 19:22:35 +08:00
parent a5412c684e
commit b5211003a5
14 changed files with 56 additions and 96 deletions

View File

@ -9,15 +9,9 @@ public class TokenContext implements AutoCloseable{
private final ThreadLocal<String> userToken = new ThreadLocal<>();
public void setToken() {
userToken.set(TokenUtils.threadToken());
}
public void refreshToken(){
userToken.set(TokenUtils.refreshThreadToken());
userToken.set(TokenUtils.getTempToken());
}
@Override
public void close() throws Exception {
userToken.remove();
}
public void close() throws Exception { userToken.remove(); }
}

View File

@ -23,6 +23,7 @@ public interface MonitorConstant {
String ITEM_OSNAME = "osName";
String ITEM_STARTTIME = "startTime";
String ITEM_CPUTYPE = "cpuType";
String RESPONSE_SUCCESS_RATE = "responseSuccessRate";
String PRIFIX_DISKUSED = "diskUsed";

View File

@ -33,11 +33,9 @@ public interface RedisConstant {
String SERVER_STATUS = "Status:Server_Status";
String EMAIL_SENDER = "Email_Sender";
String EMAIL_SENDER = "EmailSender";
String MANAGE_TOKEN = "Token:Manage"; // 运管系统Token
String QIYE_EMAIL_TOKEN = "Token:QiyeEmail"; // 网易企业邮箱Token
String THREAD_TOKEN = "Token:Thread"; // 线程中设置Token 定时任务中使用Feign调用接口使用 否则提示401
}

View File

@ -164,34 +164,8 @@ public class TokenUtils {
String secret = CommonConstant.TEMP_TOKEN_SECRET;
// 模拟登录生成Token
String token = JwtUtil.sign(username, secret);
// 设置Token缓存有效时间为 5 分钟
redisUtil.set(CommonConstant.PREFIX_USER_TOKEN + token, token, 5 * 60);
return token;
}
public static String threadToken() {
RedisUtil redisUtil = SpringContextUtils.getBean(RedisUtil.class);
if (redisUtil.hasKey(RedisConstant.THREAD_TOKEN))
return (String) redisUtil.get(RedisConstant.THREAD_TOKEN);
String username = CommonConstant.TEMP_TOKEN_USERNAME;
String secret = CommonConstant.TEMP_TOKEN_SECRET;
// 模拟登录生成Token
String token = JwtUtil.sign(username, secret);
// 设置Token缓存有效时间为 6h
redisUtil.set(RedisConstant.THREAD_TOKEN, token, 6 * 60 * 60);
return token;
}
public static String refreshThreadToken(){
RedisUtil redisUtil = SpringContextUtils.getBean(RedisUtil.class);
String username = CommonConstant.TEMP_TOKEN_USERNAME;
String secret = CommonConstant.TEMP_TOKEN_SECRET;
// 模拟登录生成Token
String token = JwtUtil.sign(username, secret);
// 设置Token缓存有效时间为 6h
redisUtil.set(RedisConstant.THREAD_TOKEN, token, 6 * 60 * 60);
// 设置Token缓存有效时间为 3 分钟
redisUtil.set(CommonConstant.PREFIX_USER_TOKEN + token, token, 3 * 60);
return token;
}
}

View File

@ -47,5 +47,7 @@ public class BasicInfo {
private double memoryUsed;
private double responseSuccessRate;
private Map<String, Object> diskUsed;
}

View File

@ -22,6 +22,7 @@ public class ItemDto implements Serializable{
this.itemId = alarmItem.getId();
this.name = alarmItem.getName();
this.units = alarmItem.getUnits();
this.valueType = alarmItem.getValueType();
}
public ItemDto(AlarmItemDe alarmItemDe) {

View File

@ -20,4 +20,6 @@ public class Item {
private String status;
private String lastValue; // 监控项最新值
private String valueType;
}

View File

@ -20,4 +20,7 @@ public class AlarmItem extends JeecgEntity {
@TableField("host_id")
private String hostId;
@TableField("value_type")
private String valueType;
}

View File

@ -76,21 +76,12 @@ public class StatusAspect {
@Autowired
private IAlarmItemService alarmItemService;
// 新增邮箱服务器信息后 异步更新其状态信息
// 新增|修改邮箱服务器信息后 异步更新其状态信息
// 并更新一个可用的发件服务器缓存到Redis
@Async
@AfterReturning("execution(* org.jeecg.modules.service.impl.SysEmailServiceImpl.create(..))")
@AfterReturning("execution(* org.jeecg.modules.service.impl.SysEmailServiceImpl.create(..)) ||" +
"execution(* org.jeecg.modules.service.impl.SysEmailServiceImpl.update(..))")
public void updateEamilStatus(JoinPoint point){
Object[] args = point.getArgs();
if (ArrayUtil.length(args) == 0) return;
SysEmail email = (SysEmail) args[0];
emailService.status2Redis(email);
}
// 修改邮箱服务器信息后 异步更新其状态信息
// 并更新一个可用的发件服务器缓存到Reids
@Async
@AfterReturning("execution(* org.jeecg.modules.service.impl.SysEmailServiceImpl.update(..))")
public void updateEamil(JoinPoint point){
emailService.sender2Redis();
Object[] args = point.getArgs();
if (ArrayUtil.length(args) == 0) return;
@ -195,11 +186,11 @@ public class StatusAspect {
}
/*
* Server时 同步删除数据库中相关联的监控项&报警规则
* 删除Server时 同步删除关联的监控项&报警规则
* */
@Async
@AfterReturning(value = "execution(* org.jeecg.modules.service.impl.SysServerServiceImpl.deleteById(..))", returning = "result")
public void deleteItems(JoinPoint point, Object result){
public void serverDelete(JoinPoint point, Object result){
if (ObjectUtil.isNotNull(result)){
String hostId = ((Result<String>) result).getResult();
// 删除服务器相关联的监控项
@ -212,15 +203,34 @@ public class StatusAspect {
}
/*
* 删除Email|Database时 同步删除Redis和数据库中相关联的预警规则
* 删除Database时 同步删除关联的报警规则
* */
@Async
@AfterReturning("execution(* org.jeecg.modules.service.impl.SysEmailServiceImpl.deleteById(..)) || " +
"execution(* org.jeecg.modules.service.impl.SysDatabaseServiceImpl.deleteById(..))")
public void deleteRules(JoinPoint point){
// 删除数据库和邮箱相关联的报警规则
@AfterReturning("execution(* org.jeecg.modules.service.impl.SysDatabaseServiceImpl.deleteById(..))")
public void databaseDelete(JoinPoint point){
// 删除数据库相关联的报警规则
Object[] args = point.getArgs();
if (ArrayUtil.length(args) > 0)
alarmRuleService.deleteBySourceId((String) args[0]);
}
/*
* 删除Email时 同步删除关联的报警规则
* 同时 如果是Redis中缓存的发件服务器 清空Redis缓存
* */
@Async
@AfterReturning("execution(* org.jeecg.modules.service.impl.SysEmailServiceImpl.deleteById(..))")
public void emailDelete(JoinPoint point){
Object[] args = point.getArgs();
if (ArrayUtil.length(args) > 0){
String emailId = (String) args[0];
// 删除邮箱关联的报警规则
alarmRuleService.deleteBySourceId(emailId);
// 如果是发件服务器 清除Redis缓存
SysEmail sender = (SysEmail) redisUtil.get(RedisConstant.EMAIL_SENDER);
if (ObjectUtil.isNull(sender)) return;
if (StrUtil.equals(emailId, sender.getId()))
redisUtil.del(RedisConstant.EMAIL_SENDER);
}
}
}

View File

@ -1,31 +1,11 @@
package org.jeecg.modules.idc;
import cn.hutool.core.map.MapUtil;
import cn.hutool.core.util.ObjectUtil;
import cn.hutool.core.util.StrUtil;
import lombok.Data;
import lombok.extern.slf4j.Slf4j;
import org.jeecg.common.api.dto.message.MessageDTO;
import org.jeecg.common.config.mqtoken.UserTokenContext;
import org.jeecg.common.constant.enums.MessageTypeEnum;
import org.jeecg.common.util.JDBCUtil;
import org.jeecg.common.util.ParamUtil;
import org.jeecg.modules.base.dto.ConnR;
import org.jeecg.modules.feignclient.SystemClient;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;
import java.sql.Connection;
import java.sql.SQLException;
import java.util.HashMap;
import java.util.Map;
import java.util.concurrent.TimeUnit;
import static org.jeecg.common.util.TokenUtils.getTempToken;
import static org.jeecg.modules.base.enums.Template.IDC_DATASOURCE_STATUS;
@Slf4j
@Component
@ -35,7 +15,7 @@ public class IDCDataFetch {
private TemplateManager templateManager;
// 定时拾取IDC数据
@Scheduled(initialDelay = 5, fixedDelay = 10, timeUnit = TimeUnit.SECONDS)
//@Scheduled(initialDelay = 5, fixedDelay = 10, timeUnit = TimeUnit.SECONDS)
public void fetch() {
try {
JdbcTemplate template = templateManager.getTemplate();

View File

@ -54,8 +54,6 @@ public class TemplateManager {
private final ConcurrentMap<String, JdbcTemplate> templateMap = new ConcurrentHashMap<>();
private TemplateManager(){}
private void setTemplate(JdbcTemplate template){
templateMap.put(templateKey, template);
}
@ -67,7 +65,7 @@ public class TemplateManager {
/*
* 定时检查inland/oversea数据源的的连接状态
* */
@Scheduled(fixedDelay = 10, timeUnit = TimeUnit.SECONDS)
// @Scheduled(fixedDelay = 10, timeUnit = TimeUnit.SECONDS)
private void checkTemplate(){
try (TokenContext tokenContext = new TokenContext()){
MessageDTO messageDTO = new MessageDTO(null, IDC_DATASOURCE_STATUS.getCode(), "admin");
@ -79,7 +77,7 @@ public class TemplateManager {
template = JDBCUtil.template(urlM, usernameM, passwordM);
this.setTemplate(template);
} else {
// 为当前线程设置临时Token 避免OpenFeign调用其它服务接口时403
// 为当前线程设置临时Token 避免OpenFeign调用其它服务接口时401
tokenContext.setToken();
// 给admin发送数据源异常消息
@ -88,11 +86,6 @@ public class TemplateManager {
data.put("param2", connR.getInfo());
messageDTO.setData(data);
try { systemClient.sendTo(messageDTO); }
catch (FeignException.Unauthorized e) {
// 刷新临时Token 重新发送信息
tokenContext.refreshToken();
systemClient.sendTo(messageDTO);
}
catch(Exception e) {log.error("发送inland数据源异常信息失败: {}" , e.getMessage());}
connR = JDBCUtil.isConnection(urlS, usernameS, passwordS);
if (connR.isConn()){
@ -105,11 +98,6 @@ public class TemplateManager {
data.put("param2", connR.getInfo());
messageDTO.setData(data);
try { systemClient.sendTo(messageDTO); }
catch (FeignException.Unauthorized e) {
// 刷新临时Token 重新发送信息
tokenContext.refreshToken();
systemClient.sendTo(messageDTO);
}
catch(Exception e) {log.error("发送oversea数据源异常信息失败: {}" , e.getMessage());}
}
}catch (Exception e){

View File

@ -323,6 +323,12 @@ public class SysServerServiceImpl extends ServiceImpl<SysServerMapper, SysServer
Double memoryUsedValue = NumUtil.keep(memoryUsed, 1);
memoryUsedValue = ObjectUtil.isNull(memoryUsedValue) ? 0 : memoryUsedValue;
// 服务器响应成功率
empty = items.get(MonitorConstant.RESPONSE_SUCCESS_RATE);
String responseSuccessRate = ObjectUtil.isNull(empty) ? null : empty.getLastValue(); // 100
Double responseSuccessRateValue = NumUtil.keep(responseSuccessRate, 1);
responseSuccessRateValue = ObjectUtil.isNull(responseSuccessRateValue) ? 0 : responseSuccessRateValue;
// 磁盘使用情况
Map<String, String> diskUsedMap = items.entrySet().stream() // 6.540206
.filter(entry -> entry.getKey().contains(MonitorConstant.PRIFIX_DISKUSED))
@ -342,7 +348,8 @@ public class SysServerServiceImpl extends ServiceImpl<SysServerMapper, SysServer
.setCpuCores(cpuCores).setTotalDiskPar(totalDiskPar).setHostName(hostName)
.setOsVersion(osVersion).setNetwork(netWork).setLocation(location).setIp(ip)
.setZone(zone).setOsName(osName).setStartTime(startTime).setCpuType(cpuType)
.setCpuUsed(cpuUsedValue).setMemoryUsed(memoryUsedValue).setDiskUsed(diskUsedValue);
.setCpuUsed(cpuUsedValue).setMemoryUsed(memoryUsedValue)
.setResponseSuccessRate(responseSuccessRateValue).setDiskUsed(diskUsedValue);
return Result.OK(basicInfo);
}

View File

@ -7,7 +7,6 @@ import org.jeecg.common.util.SpringContextUtils;
import org.jeecg.modules.feignclient.AbnormalAlarmClient;
import org.quartz.*;
import static org.jeecg.common.util.TokenUtils.getTempToken;
/**
* 此处的同步是指:当定时任务的执行时间大于任务的时间

View File

@ -119,6 +119,7 @@ public class GardsSampleDataServiceImpl extends ServiceImpl<GardsSampleDataMappe
List<String> suitable = ListUtil.toList(ORIGINAL, RNAUTO, RNMAN);
List<OwnerDto> ownerDtos = baseMapper.containSampleId("SAMPLE_ID").stream()
.filter(owner -> CollUtil.contains(suitable, owner.getOwner()))
.filter(owner -> StrUtil.contains(owner.getTableName(), "GARDS"))
.collect(Collectors.toList());
String DOT = StrUtil.DOT;