fix:邮件服务器管理同步
This commit is contained in:
parent
668a5d50ad
commit
0be1f3e7fd
|
@ -1,17 +0,0 @@
|
|||
package org.jeecg.common;
|
||||
|
||||
import org.jeecg.common.util.RedisUtil;
|
||||
import org.jeecg.common.util.SpringContextUtils;
|
||||
import org.jeecg.common.util.TokenUtils;
|
||||
|
||||
public class TokenContext implements AutoCloseable{
|
||||
|
||||
private final ThreadLocal<String> userToken = new ThreadLocal<>();
|
||||
|
||||
public void setToken() {
|
||||
userToken.set(TokenUtils.getTempToken());
|
||||
}
|
||||
|
||||
@Override
|
||||
public void close() throws Exception { userToken.remove(); }
|
||||
}
|
|
@ -76,6 +76,9 @@ public class AlarmRule implements Serializable {
|
|||
@TableField(value = "item_id")
|
||||
private String itemId;
|
||||
|
||||
@TableField("item_type")
|
||||
private String itemType;
|
||||
|
||||
/**
|
||||
* 创建时间
|
||||
*/
|
||||
|
|
|
@ -4,8 +4,11 @@ import cn.hutool.core.util.ObjectUtil;
|
|||
import lombok.extern.slf4j.Slf4j;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.jdbc.core.JdbcTemplate;
|
||||
import org.springframework.scheduling.annotation.Scheduled;
|
||||
import org.springframework.stereotype.Component;
|
||||
|
||||
import java.util.concurrent.TimeUnit;
|
||||
|
||||
|
||||
@Slf4j
|
||||
@Component
|
||||
|
@ -15,7 +18,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();
|
||||
|
|
|
@ -1,12 +1,11 @@
|
|||
package org.jeecg.modules.idc;
|
||||
|
||||
import com.alibaba.nacos.api.config.annotation.NacosValue;
|
||||
import feign.FeignException;
|
||||
import lombok.extern.slf4j.Slf4j;
|
||||
import org.jeecg.common.TokenContext;
|
||||
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.TokenUtils;
|
||||
import org.jeecg.modules.base.dto.ConnR;
|
||||
import org.jeecg.modules.feignclient.SystemClient;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
|
@ -14,6 +13,7 @@ import org.springframework.beans.factory.annotation.Value;
|
|||
import org.springframework.cloud.context.config.annotation.RefreshScope;
|
||||
import org.springframework.jdbc.core.JdbcTemplate;
|
||||
import org.springframework.scheduling.annotation.Scheduled;
|
||||
import org.springframework.scheduling.annotation.Schedules;
|
||||
import org.springframework.stereotype.Component;
|
||||
|
||||
import java.util.HashMap;
|
||||
|
@ -67,7 +67,7 @@ public class TemplateManager {
|
|||
* */
|
||||
// @Scheduled(fixedDelay = 10, timeUnit = TimeUnit.SECONDS)
|
||||
private void checkTemplate(){
|
||||
try (TokenContext tokenContext = new TokenContext()){
|
||||
try {
|
||||
MessageDTO messageDTO = new MessageDTO(null, IDC_DATASOURCE_STATUS.getCode(), "admin");
|
||||
messageDTO.setType(MessageTypeEnum.XT.getType());
|
||||
ConnR connR = JDBCUtil.isConnection(urlM, usernameM, passwordM);
|
||||
|
@ -78,8 +78,7 @@ public class TemplateManager {
|
|||
this.setTemplate(template);
|
||||
} else {
|
||||
// 为当前线程设置临时Token 避免OpenFeign调用其它服务接口时401
|
||||
tokenContext.setToken();
|
||||
|
||||
UserTokenContext.setToken(TokenUtils.getTempToken());
|
||||
// 给admin发送数据源异常消息
|
||||
Map<String, Object> data = new HashMap<>();
|
||||
data.put("param1", "inland");
|
||||
|
@ -102,6 +101,8 @@ public class TemplateManager {
|
|||
}
|
||||
}catch (Exception e){
|
||||
log.error("定时检查inland/oversea数据源状态异常: {}", e.getMessage());
|
||||
}finally {
|
||||
UserTokenContext.remove();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
@ -3,6 +3,7 @@ package org.jeecg.modules.service.impl;
|
|||
import cn.hutool.core.bean.BeanUtil;
|
||||
import cn.hutool.core.collection.ListUtil;
|
||||
import cn.hutool.core.util.ArrayUtil;
|
||||
import cn.hutool.core.util.NumberUtil;
|
||||
import cn.hutool.core.util.ObjectUtil;
|
||||
import cn.hutool.core.util.StrUtil;
|
||||
import com.alibaba.fastjson.JSON;
|
||||
|
@ -14,6 +15,7 @@ import org.jeecg.common.constant.CommonConstant;
|
|||
import org.jeecg.common.constant.Prompt;
|
||||
import org.jeecg.common.constant.RedisConstant;
|
||||
import org.jeecg.common.constant.SymbolConstant;
|
||||
import org.jeecg.common.util.NumUtil;
|
||||
import org.jeecg.common.util.RedisStreamUtil;
|
||||
import org.jeecg.common.util.RedisUtil;
|
||||
import org.jeecg.modules.base.dto.AlarmRuleDto;
|
||||
|
@ -105,7 +107,10 @@ public class AlarmRuleServiceImpl extends ServiceImpl<AlarmRuleMapper, AlarmRule
|
|||
}
|
||||
boolean success = updateById(alarmRule);
|
||||
if (success){
|
||||
updateRule(ruleId, sourceTypeOld);
|
||||
// 如果当前规则是生效状态 则将修改后的规则同步到Redis
|
||||
Integer enabled = alarmRuleOld.getEnabled();
|
||||
if (Enabled.valueOf(enabled) == ENABLED)
|
||||
updateRule(ruleId, sourceTypeOld);
|
||||
return Result.OK(Prompt.UPDATE_SUCC);
|
||||
}
|
||||
return Result.error(Prompt.UPDATE_ERR);
|
||||
|
|
|
@ -16,7 +16,7 @@ public interface MonitorSystem {
|
|||
@PostMapping("/sys/login")
|
||||
Result<LoginResult> login(@RequestBody LoginVo loginVo);
|
||||
|
||||
@GetMapping("queryItemHistory")
|
||||
@GetMapping("/omms/device/monitor/queryItemHistory")
|
||||
Result<ItemHistory> itemBack(@RequestParam String itemId,
|
||||
@RequestParam Integer itemType,
|
||||
@RequestParam String start,
|
||||
|
|
|
@ -4,6 +4,7 @@ import cn.hutool.core.util.ObjectUtil;
|
|||
import cn.hutool.core.util.StrUtil;
|
||||
import com.sun.mail.smtp.SMTPAddressFailedException;
|
||||
import lombok.extern.slf4j.Slf4j;
|
||||
import org.jdom2.output.support.WalkerNORMALIZE;
|
||||
import org.jeecg.common.api.dto.message.MessageDTO;
|
||||
import org.jeecg.common.email.EmailServiceManager;
|
||||
import org.jeecg.modules.base.entity.postgre.SysEmail;
|
||||
|
@ -41,8 +42,15 @@ public class EmailPushMsgHandle implements ISendMsgHandle {
|
|||
public void sendMessage(MessageDTO messageDTO) {
|
||||
// 获取邮件发送服务器信息
|
||||
SysEmail sender = abnormalAlarmClient.getSender().getResult();
|
||||
if(ObjectUtil.isNull(sender))
|
||||
log.error("没有可用的发件邮箱服务");
|
||||
if(ObjectUtil.isNull(sender)){
|
||||
String title = "配置邮箱发件服务器提醒";
|
||||
String warnInfo = "系统当前没有配置邮箱发件服务器, 发送邮件功能暂不可用!";
|
||||
MessageDTO warn = new MessageDTO(title, warnInfo).setToUser("admin");
|
||||
systemSendMsg.sendMessage(warn);
|
||||
log.error(warnInfo);
|
||||
return;
|
||||
}
|
||||
|
||||
// 初始化邮件服务
|
||||
EmailServiceManager emailService = getInstance();
|
||||
emailService.init(sender);
|
||||
|
@ -52,8 +60,9 @@ public class EmailPushMsgHandle implements ISendMsgHandle {
|
|||
String title = messageDTO.getTitle();
|
||||
String context = messageDTO.getContent();
|
||||
String alarmTitle = "邮件发送失败提醒";
|
||||
String alarmContext = "系统给您发送的邮件[标题:"+ title +
|
||||
", 内容:" + context + "]发送失败,失败原因:";
|
||||
StringBuilder alarmContext = new StringBuilder();
|
||||
alarmContext.append("系统给您发送的邮件 [标题: ").append(title)
|
||||
.append(", 内容: ").append(context).append("]发送失败,失败原因: ");
|
||||
// 循环给每一个用户发送邮件
|
||||
for (Map.Entry<String, String> userEmails : messageDTO.getUserEmail().entrySet()) {
|
||||
String username = userEmails.getKey();
|
||||
|
@ -63,19 +72,18 @@ public class EmailPushMsgHandle implements ISendMsgHandle {
|
|||
emailService.sendMailToOne(messageDTO);
|
||||
} catch (MessagingException e) {
|
||||
if (e instanceof SMTPAddressFailedException) { // 无效的电子邮箱异常
|
||||
alarmContext += "无效的电子邮箱 " + email;
|
||||
alarmContext.append("无效的电子邮箱: ").append(email);
|
||||
} else if (e instanceof SendFailedException) { // 邮件发送失败异常
|
||||
alarmContext += e.getMessage();
|
||||
alarmContext.append(e.getMessage());
|
||||
}
|
||||
// 邮件发送失败 给用户发送站内提示消息
|
||||
MessageDTO sysMsg = new MessageDTO();
|
||||
sysMsg.setToUser(username);
|
||||
sysMsg.setTitle(alarmTitle);
|
||||
sysMsg.setContent(alarmContext);
|
||||
sysMsg.setContent(alarmContext.toString());
|
||||
systemSendMsg.sendMessage(sysMsg);
|
||||
}
|
||||
}
|
||||
|
||||
sysMessageService.add(messageDTO);
|
||||
}
|
||||
|
||||
|
|
|
@ -27,7 +27,7 @@ import java.util.Date;
|
|||
@Component("systemSendMsgHandle")
|
||||
public class SystemSendMsgHandle implements ISendMsgHandle {
|
||||
|
||||
public static final String FROM_USER = "system";
|
||||
public static final String FROM_USER = "admin";
|
||||
|
||||
@Resource
|
||||
private WebSocket webSocket;
|
||||
|
|
|
@ -39,6 +39,7 @@ public abstract class Monitor {
|
|||
sendMessage = SpringContextUtils.getBean(SendMessage.class);
|
||||
redisStreamUtil = SpringContextUtils.getBean(RedisStreamUtil.class);
|
||||
alarmClient = SpringContextUtils.getBean(AbnormalAlarmClient.class);
|
||||
monitorSystem = SpringContextUtils.getBean(MonitorSystem.class);
|
||||
}
|
||||
|
||||
protected void destroy() {
|
||||
|
|
|
@ -1,9 +1,9 @@
|
|||
package org.jeecg.modules.quartz.job;
|
||||
|
||||
import lombok.extern.slf4j.Slf4j;
|
||||
import org.jeecg.common.TokenContext;
|
||||
import org.jeecg.common.config.mqtoken.UserTokenContext;
|
||||
import org.jeecg.common.util.SpringContextUtils;
|
||||
import org.jeecg.common.util.TokenUtils;
|
||||
import org.jeecg.modules.feignclient.AbnormalAlarmClient;
|
||||
import org.quartz.*;
|
||||
|
||||
|
@ -23,12 +23,14 @@ public class NuclideParamJob implements Job {
|
|||
|
||||
@Override
|
||||
public void execute(JobExecutionContext context) throws JobExecutionException {
|
||||
try (TokenContext tokenContext = new TokenContext()){
|
||||
tokenContext.setToken();
|
||||
try {
|
||||
UserTokenContext.setToken(TokenUtils.getTempToken());
|
||||
AbnormalAlarmClient alarmClient = SpringContextUtils.getBean(AbnormalAlarmClient.class);
|
||||
alarmClient.refreshParam();
|
||||
}catch (Exception e){
|
||||
log.error("定时任务[NuclideParamJob]执行异常: {}", e.getMessage());
|
||||
}finally {
|
||||
UserTokenContext.remove();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
@ -1,18 +1,16 @@
|
|||
package org.jeecg.modules.quartz.job;
|
||||
|
||||
import lombok.extern.slf4j.Slf4j;
|
||||
import org.jeecg.common.TokenContext;
|
||||
import org.jeecg.common.config.mqtoken.UserTokenContext;
|
||||
import org.jeecg.common.constant.DateConstant;
|
||||
import org.jeecg.common.util.SpringContextUtils;
|
||||
import org.jeecg.common.util.TokenUtils;
|
||||
import org.jeecg.modules.feignclient.AbnormalAlarmClient;
|
||||
import org.quartz.*;
|
||||
|
||||
import java.time.LocalDateTime;
|
||||
import java.time.format.DateTimeFormatter;
|
||||
|
||||
import static org.jeecg.common.util.TokenUtils.getTempToken;
|
||||
|
||||
/**
|
||||
* 此处的同步是指:当定时任务的执行时间大于任务的时间
|
||||
* 间隔时会等待第一个任务执行完成才会走第二个任务
|
||||
|
@ -28,13 +26,15 @@ public class NucliedAvgJob implements Job {
|
|||
|
||||
@Override
|
||||
public void execute(JobExecutionContext context) throws JobExecutionException {
|
||||
try (TokenContext tokenContext = new TokenContext()){
|
||||
tokenContext.setToken();
|
||||
try {
|
||||
UserTokenContext.setToken(TokenUtils.getTempToken());
|
||||
AbnormalAlarmClient alarmClient = SpringContextUtils.getBean(AbnormalAlarmClient.class);
|
||||
alarmClient.calculateConc();
|
||||
log.info(log());
|
||||
}catch (Exception e){
|
||||
log.error("定时任务[NucliedAvgJob]执行异常: {}", e.getMessage());
|
||||
}finally {
|
||||
UserTokenContext.remove();
|
||||
}
|
||||
}
|
||||
|
||||
|
|
|
@ -72,6 +72,8 @@ public class ServerJob extends Monitor implements Job {
|
|||
operator = alarmRule.getOperator();
|
||||
String ruleId = alarmRule.getId();
|
||||
String itemId = alarmRule.getItemId();
|
||||
String type = alarmRule.getItemType();
|
||||
Integer itemType = StrUtil.isBlank(type) ? 0 : Integer.parseInt(type);
|
||||
String silenceKey = prefixSilence + ruleId;
|
||||
boolean hasKey = getRedisStreamUtil().hasKey(silenceKey);
|
||||
boolean blank1 = StrUtil.isBlank(operator);
|
||||
|
@ -85,8 +87,13 @@ public class ServerJob extends Monitor implements Job {
|
|||
|
||||
// 向运管查询监控项数据
|
||||
String token = ManageUtil.getToken();
|
||||
Result<ItemHistory> result = getMonitorSystem().itemBack(itemId, 0, start, end, token);
|
||||
Double current = result.getResult().getNow();
|
||||
Result<ItemHistory> result = getMonitorSystem().itemBack(itemId, itemType, start, end, token);
|
||||
ItemHistory itemHistory = result.getResult();
|
||||
if (ObjectUtil.isNull(itemHistory)){
|
||||
log.warn("Server监控异常: [{}]查询监控项历史数据为空", serverName);
|
||||
continue;
|
||||
}
|
||||
Double current = itemHistory.getNow();
|
||||
|
||||
// 解析预警规则,判断是否需要报警
|
||||
ObjectMapper mapper = new ObjectMapper();
|
||||
|
|
Loading…
Reference in New Issue
Block a user