feat:增加自动处理邮箱邮件监控

This commit is contained in:
nieziyan 2024-04-29 17:12:07 +08:00
parent 99f74539d2
commit fc978d459e
9 changed files with 145 additions and 98 deletions

View File

@ -7,12 +7,13 @@ import lombok.Getter;
@Getter
@AllArgsConstructor
public enum Item {
EMAIL_CONN("1", "Connection Status"),
TABLESPACE_USAGE("2", "TableSpace Usage");
EMAIL_CONN("1", "Email Connection Status"),
TABLESPACE_USAGE("2", "TableSpace Usage"),
EMAIL_PROCESS("3", "Email Process");
private String value;
private final String value;
private String name;
private final String name;
public static Item of(String value){
for (Item item : Item.values()) {

View File

@ -15,6 +15,7 @@ import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
@RestController
@ -63,8 +64,14 @@ public class SysEmailLogController {
@GetMapping("analysis")
@ApiOperation("根据日期统计-折线图")
public Result<?> analysis(@RequestParam("emailId") String emailId,
@RequestParam("startDate") String startDate,
@RequestParam("endDate") String endDate){
@RequestParam("startDate") String startDate,
@RequestParam("endDate") String endDate){
return sysEmailLogService.analysis(emailId, startDate, endDate);
}
@GetMapping("hoursAgo")
public Boolean hoursAgo(@RequestParam("emailId") String emailId,
@RequestParam("hoursAgo") String hoursAgo){
return sysEmailLogService.hoursAgo(emailId, hoursAgo);
}
}

View File

@ -4,6 +4,7 @@ import com.baomidou.mybatisplus.extension.service.IService;
import org.jeecg.common.api.vo.Result;
import org.jeecg.modules.base.entity.postgre.SysEmailLog;
import java.util.List;
import java.util.Map;
public interface ISysEmailLogService extends IService<SysEmailLog> {
@ -16,4 +17,6 @@ public interface ISysEmailLogService extends IService<SysEmailLog> {
Result<?> todayMin(String emailId);
Result<?> analysis(String emailId, String startDate, String endDate);
Boolean hoursAgo(String emailId, String hoursAgo);
}

View File

@ -227,4 +227,12 @@ public class SysEmailLogServiceImpl extends ServiceImpl<SysEmailLogMapper, SysEm
result.put("yData",yData);
return Result.OK(result);
}
@Override
public Boolean hoursAgo(String emailId, String hoursAgo) {
LambdaQueryWrapper<SysEmailLog> wrapper = new LambdaQueryWrapper<>();
wrapper.eq(SysEmailLog::getEmailId, emailId);
wrapper.gt(SysEmailLog::getCreateTime, hoursAgo);
return CollUtil.isEmpty(this.list(wrapper));
}
}

View File

@ -43,4 +43,9 @@ public interface AbnormalAlarmClient {
/* SysServerController下相关接口 */
@GetMapping("/sysServer/getNameById")
String getServerName(@RequestParam String id);
/* SysEmailLogController下相关接口 */
@GetMapping("/sysEmailLog/hoursAgo")
Boolean hoursAgo(@RequestParam String emailId,
@RequestParam String hoursAgo);
}

View File

@ -105,31 +105,30 @@ public class DatabaseJob extends Monitor {
String op = rule.getOperator();
Double threshold = rule.getThreshold();
boolean needWarn = NumUtil.compare(current, threshold, op);
if (needWarn){
// 记录报警日志
AlarmLog alarmLog = new AlarmLog();
alarmLog.setRuleId(ruleId);
alarmLog.setOperator(operator);
alarmLog.setAlarmValue(StrUtil.toString(current));
if (!needWarn) continue;
// 记录报警日志
AlarmLog alarmLog = new AlarmLog();
alarmLog.setRuleId(ruleId);
alarmLog.setOperator(operator);
alarmLog.setAlarmValue(StrUtil.toString(current));
String ruleName = alarmRule.getName();
Map<String, Object> data = DataTool.getInstance().
put(databaseName).put(ruleName).put(rule.joint()).put(current).get();
MessageDTO messageDTO = TemplateUtil.parse(MONITOR_DATABASE.getCode(), data);
String ruleName = alarmRule.getName();
Map<String, Object> data = DataTool.getInstance().
put(databaseName).put(ruleName).put(rule.joint()).put(current).get();
MessageDTO messageDTO = TemplateUtil.parse(MONITOR_DATABASE.getCode(), data);
alarmLog.setAlarmInfo(messageDTO.getContent());
getAlarmClient().create(alarmLog);
// 规则触发报警后,设置该规则的沉默周期(如果有)
// 沉默周期失效之前,该规则不会再次被触发
Long silenceCycle = alarmRule.getSilenceCycle();
ruleSilence(silenceKey, silenceCycle);
alarmLog.setAlarmInfo(messageDTO.getContent());
getAlarmClient().create(alarmLog);
// 规则触发报警后,设置该规则的沉默周期(如果有)
// 沉默周期失效之前,该规则不会再次被触发
Long silenceCycle = alarmRule.getSilenceCycle();
ruleSilence(silenceKey, silenceCycle);
// 发送报警信息
String groupId = alarmRule.getContactId();
String notific = alarmRule.getNotification();
getSendMessage().send(messageDTO, groupId, notific);
getPushAppUtil().pushToSingle(messageDTO, groupId);
}
// 发送报警信息
String groupId = alarmRule.getContactId();
String notific = alarmRule.getNotification();
getSendMessage().send(messageDTO, groupId, notific);
getPushAppUtil().pushToSingle(messageDTO, groupId);
}catch (FeignException.Unauthorized e){
ManageUtil.refreshToken();
log.warn("向运管系统查询ItemHistory信息异常: Token失效,已刷新Token");

View File

@ -1,6 +1,7 @@
package org.jeecg.modules.quartz.jobs;
import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.date.DateUtil;
import cn.hutool.core.util.ObjectUtil;
import cn.hutool.core.util.StrUtil;
import com.fasterxml.jackson.core.JsonProcessingException;
@ -8,6 +9,8 @@ import com.fasterxml.jackson.databind.ObjectMapper;
import lombok.Data;
import lombok.extern.slf4j.Slf4j;
import org.jeecg.common.api.dto.message.MessageDTO;
import org.jeecg.common.constant.DateConstant;
import org.jeecg.common.constant.FillRuleConstant;
import org.jeecg.common.constant.RedisConstant;
import org.jeecg.common.util.DataTool;
import org.jeecg.common.util.NumUtil;
@ -19,9 +22,12 @@ import org.jeecg.modules.base.entity.postgre.AlarmRule;
import org.jeecg.modules.base.enums.Item;
import org.jeecg.modules.quartz.entity.Monitor;
import org.quartz.*;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;
import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.util.Map;
import java.util.Set;
import java.util.concurrent.TimeUnit;
@ -32,6 +38,10 @@ import static org.jeecg.modules.base.enums.Template.MONITOR_EMAIL;
@Slf4j
@Component
public class EmailJob extends Monitor{
@Value("${task.undealHours:2}")
private Integer undealHours; // 邮箱邮件未处理时长 单位: h 默认: 2h
/**
* 解析Email预警规则
**/
@ -69,8 +79,11 @@ public class EmailJob extends Monitor{
if (ObjectUtil.isNull(item)) continue;
Number current = null;
switch (item){
case EMAIL_CONN: // 监控项-1: 测试邮箱服务是否可以连接成功
current = isConnection(sourceId);
case EMAIL_CONN: // 监控项id 1: 测试邮箱服务是否可以连接成功
current = connectionStatus(sourceId);
break;
case EMAIL_PROCESS: // 监控项id 3: 邮箱邮件是否被自动处理程序处理
current = process(sourceId);
break;
// 追加的监控项...
default:
@ -82,31 +95,30 @@ public class EmailJob extends Monitor{
String op = rule.getOperator();
Double threshold = rule.getThreshold();
boolean needWarn = NumUtil.compare(current, threshold, op);
if (needWarn){
// 记录报警日志
AlarmLog alarmLog = new AlarmLog();
alarmLog.setRuleId(ruleId);
alarmLog.setOperator(operator);
alarmLog.setAlarmValue(StrUtil.toString(current));
if (!needWarn) continue;
// 记录报警日志
AlarmLog alarmLog = new AlarmLog();
alarmLog.setRuleId(ruleId);
alarmLog.setOperator(operator);
alarmLog.setAlarmValue(StrUtil.toString(current));
String ruleName = alarmRule.getName();
Map<String, Object> data = DataTool.getInstance().
put(emailName).put(ruleName).put(rule.joint()).put(current).get();
MessageDTO messageDTO = TemplateUtil.parse(MONITOR_EMAIL.getCode(), data);
String ruleName = alarmRule.getName();
Map<String, Object> data = DataTool.getInstance().
put(emailName).put(ruleName).put(rule.joint()).put(current).get();
MessageDTO messageDTO = TemplateUtil.parse(MONITOR_EMAIL.getCode(), data);
alarmLog.setAlarmInfo(messageDTO.getContent());
getAlarmClient().create(alarmLog);
// 规则触发报警后,设置该规则的沉默周期(如果有)
// 沉默周期失效之前,该规则不会再次被触发
Long silenceCycle = alarmRule.getSilenceCycle();
ruleSilence(silenceKey, silenceCycle);
alarmLog.setAlarmInfo(messageDTO.getContent());
getAlarmClient().create(alarmLog);
// 规则触发报警后,设置该规则的沉默周期(如果有)
// 沉默周期失效之前,该规则不会再次被触发
Long silenceCycle = alarmRule.getSilenceCycle();
ruleSilence(silenceKey, silenceCycle);
// 发送报警信息
String groupId = alarmRule.getContactId();
String notific = alarmRule.getNotification();
getSendMessage().send(messageDTO, groupId, notific);
getPushAppUtil().pushToSingle(messageDTO, groupId);
}
// 发送报警信息
String groupId = alarmRule.getContactId();
String notific = alarmRule.getNotification();
getSendMessage().send(messageDTO, groupId, notific);
getPushAppUtil().pushToSingle(messageDTO, groupId);
} catch (JsonProcessingException e) {
log.error("Email预警规则: {}解析失败,失败原因: {}", operator, e.getMessage());
}catch (Exception e){
@ -117,9 +129,9 @@ public class EmailJob extends Monitor{
}
/*
* 监控项-1: 测试邮箱服务是否可以连接成功 (0:失败 1:成功)
* 监控项id: 1 测试邮箱服务是否可以连接成功 (预警值: 0 非0值则不需要报警)
* */
private Integer isConnection(String emailId){
private Integer connectionStatus(String emailId){
int res = 1;
String statusKey = RedisConstant.EMAIL_STATUS;
NameValue nameValue = (NameValue)getRedisUtil().hget(statusKey, emailId);
@ -127,4 +139,18 @@ public class EmailJob extends Monitor{
res = 0;
return res;
}
/*
* 监控项id: 3 邮箱邮件在一定时间内是否被处理 (预警值: 0 非0值则不需要报警)
* */
private Integer process(String emailId){
int res = 1;
DateTimeFormatter formatter = DateTimeFormatter.ofPattern(DateConstant.DATE_TIME);
LocalDateTime minusHours = LocalDateTime.now().minusHours(undealHours);
String hoursAgo = minusHours.format(formatter);
Boolean result = getAlarmClient().hoursAgo(emailId, hoursAgo);
if (ObjectUtil.isNotNull(result) && result)
res = 0;
return res;
}
}

View File

@ -102,31 +102,30 @@ public class ServerJob extends Monitor{
String op = rule.getOperator();
Double threshold = rule.getThreshold();
boolean needWarn = NumUtil.compare(current, threshold, op);
if (needWarn){
// 记录报警日志
AlarmLog alarmLog = new AlarmLog();
alarmLog.setRuleId(ruleId);
alarmLog.setOperator(operator);
alarmLog.setAlarmValue(StrUtil.toString(current));
if (!needWarn) continue;
// 记录报警日志
AlarmLog alarmLog = new AlarmLog();
alarmLog.setRuleId(ruleId);
alarmLog.setOperator(operator);
alarmLog.setAlarmValue(StrUtil.toString(current));
String ruleName = alarmRule.getName();
Map<String, Object> data = DataTool.getInstance().
put(serverName).put(ruleName).put(rule.joint()).put(current).get();
MessageDTO messageDTO = TemplateUtil.parse(MONITOR_SERVER.getCode(), data);
String ruleName = alarmRule.getName();
Map<String, Object> data = DataTool.getInstance().
put(serverName).put(ruleName).put(rule.joint()).put(current).get();
MessageDTO messageDTO = TemplateUtil.parse(MONITOR_SERVER.getCode(), data);
alarmLog.setAlarmInfo(messageDTO.getContent());
getAlarmClient().create(alarmLog);
// 规则触发报警后,设置该规则的沉默周期(如果有)
// 沉默周期失效之前,该规则不会再次被触发
Long silenceCycle = alarmRule.getSilenceCycle();
ruleSilence(silenceKey, silenceCycle);
alarmLog.setAlarmInfo(messageDTO.getContent());
getAlarmClient().create(alarmLog);
// 规则触发报警后,设置该规则的沉默周期(如果有)
// 沉默周期失效之前,该规则不会再次被触发
Long silenceCycle = alarmRule.getSilenceCycle();
ruleSilence(silenceKey, silenceCycle);
// 发送报警信息
String groupId = alarmRule.getContactId();
String notific = alarmRule.getNotification();
getSendMessage().send(messageDTO, groupId, notific);
getPushAppUtil().pushToSingle(messageDTO, groupId);
}
// 发送报警信息
String groupId = alarmRule.getContactId();
String notific = alarmRule.getNotification();
getSendMessage().send(messageDTO, groupId, notific);
getPushAppUtil().pushToSingle(messageDTO, groupId);
} catch (FeignException.Unauthorized e){
ManageUtil.refreshToken();
log.warn("向运管系统查询ItemHistory信息异常: Token失效,已刷新Token");

View File

@ -95,31 +95,30 @@ public class TableSpaceJob extends Monitor {
}
// 如果当前值超过阈值 则需要发送报警信息
if (needWarn){
// 记录报警日志
AlarmLog alarmLog = new AlarmLog();
alarmLog.setRuleId(ruleId);
alarmLog.setOperator(operator);
alarmLog.setAlarmValue(value(spaceMap, result));
if (!needWarn) continue;
// 记录报警日志
AlarmLog alarmLog = new AlarmLog();
alarmLog.setRuleId(ruleId);
alarmLog.setOperator(operator);
alarmLog.setAlarmValue(value(spaceMap, result));
String ruleName = alarmRule.getName();
Map<String, Object> data = DataTool.getInstance().
put(databaseName).put(ruleName).put(rule.joint()).put(info(spaceMap, result)).get();
MessageDTO messageDTO = TemplateUtil.parse(MONITOR_DATABASE.getCode(), data);
String ruleName = alarmRule.getName();
Map<String, Object> data = DataTool.getInstance().
put(databaseName).put(ruleName).put(rule.joint()).put(info(spaceMap, result)).get();
MessageDTO messageDTO = TemplateUtil.parse(MONITOR_DATABASE.getCode(), data);
alarmLog.setAlarmInfo(messageDTO.getContent());
getAlarmClient().create(alarmLog);
// 规则触发报警后,设置该规则的沉默周期(如果有)
// 沉默周期失效之前,该规则不会再次被触发
Long silenceCycle = alarmRule.getSilenceCycle();
ruleSilence(silenceKey, silenceCycle);
alarmLog.setAlarmInfo(messageDTO.getContent());
getAlarmClient().create(alarmLog);
// 规则触发报警后,设置该规则的沉默周期(如果有)
// 沉默周期失效之前,该规则不会再次被触发
Long silenceCycle = alarmRule.getSilenceCycle();
ruleSilence(silenceKey, silenceCycle);
// 发送报警信息
String groupId = alarmRule.getContactId();
String notific = alarmRule.getNotification();
getSendMessage().send(messageDTO, groupId, notific);
getPushAppUtil().pushToSingle(messageDTO, groupId);
}
// 发送报警信息
String groupId = alarmRule.getContactId();
String notific = alarmRule.getNotification();
getSendMessage().send(messageDTO, groupId, notific);
getPushAppUtil().pushToSingle(messageDTO, groupId);
} catch (JsonProcessingException e) {
log.error("Database-TableSpace预警规则: {}解析失败,失败原因: {}", operator, e.getMessage());
}catch (Exception e){