[manager] 告警信息邮件转发
This commit is contained in:
@@ -83,6 +83,11 @@
|
||||
<version>${mysql.version}</version>
|
||||
<!--<scope>runtime</scope>-->
|
||||
</dependency>
|
||||
<!-- email -->
|
||||
<dependency>
|
||||
<groupId>org.springframework.boot</groupId>
|
||||
<artifactId>spring-boot-starter-mail</artifactId>
|
||||
</dependency>
|
||||
<!-- swagger -->
|
||||
<dependency>
|
||||
<groupId>io.springfox</groupId>
|
||||
|
||||
@@ -6,10 +6,17 @@ import com.usthe.alert.pojo.entity.Alert;
|
||||
import com.usthe.alert.service.AlertService;
|
||||
import com.usthe.common.util.CommonConstants;
|
||||
import com.usthe.manager.pojo.entity.Monitor;
|
||||
import com.usthe.manager.pojo.entity.NoticeReceiver;
|
||||
import com.usthe.manager.service.MonitorService;
|
||||
import com.usthe.manager.service.NoticeConfigService;
|
||||
import lombok.extern.slf4j.Slf4j;
|
||||
import org.springframework.mail.SimpleMailMessage;
|
||||
import org.springframework.mail.javamail.JavaMailSender;
|
||||
import org.springframework.stereotype.Component;
|
||||
|
||||
import java.util.Date;
|
||||
import java.util.List;
|
||||
|
||||
/**
|
||||
* 告警信息入库分发
|
||||
* @author tom
|
||||
@@ -23,13 +30,18 @@ public class DispatchAlarm {
|
||||
private AlerterDataQueue dataQueue;
|
||||
private AlertService alertService;
|
||||
private MonitorService monitorService;
|
||||
private NoticeConfigService noticeConfigService;
|
||||
private JavaMailSender javaMailSender;
|
||||
|
||||
public DispatchAlarm(AlerterWorkerPool workerPool, AlerterDataQueue dataQueue,
|
||||
JavaMailSender javaMailSender,NoticeConfigService noticeConfigService,
|
||||
AlertService alertService, MonitorService monitorService) {
|
||||
this.workerPool = workerPool;
|
||||
this.dataQueue = dataQueue;
|
||||
this.alertService = alertService;
|
||||
this.monitorService = monitorService;
|
||||
this.noticeConfigService = noticeConfigService;
|
||||
this.javaMailSender = javaMailSender;
|
||||
startDispatch();
|
||||
}
|
||||
|
||||
@@ -86,8 +98,46 @@ public class DispatchAlarm {
|
||||
|
||||
private void sendAlertDataListener(Alert alert) {
|
||||
// todo 转发配置的邮件 微信 webhook
|
||||
|
||||
List<NoticeReceiver> receivers = matchReceiverByNoticeRules(alert);
|
||||
// todo 发送通知这里暂时单线程
|
||||
for (NoticeReceiver receiver : receivers) {
|
||||
switch (receiver.getType()) {
|
||||
// todo 短信通知
|
||||
case 0: break;
|
||||
case 1: sendEmailAlert(receiver, alert); break;
|
||||
case 2: sendWebHookAlert(receiver, alert); break;
|
||||
case 3: sendWeChatAlert(receiver, alert); break;
|
||||
default: break;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
private void sendWeChatAlert(NoticeReceiver receiver, Alert alert) {
|
||||
|
||||
}
|
||||
|
||||
private void sendWebHookAlert(NoticeReceiver receiver, Alert alert) {
|
||||
|
||||
}
|
||||
|
||||
private void sendEmailAlert(NoticeReceiver receiver, Alert alert) {
|
||||
SimpleMailMessage message = new SimpleMailMessage();
|
||||
message.setSubject("TanCloud探云-监控告警");
|
||||
message.setFrom("gongchao@tancloud.cn");
|
||||
message.setTo(receiver.getEmail());
|
||||
message.setSentDate(new Date());
|
||||
message.setText("探云TanCloud-监控告警\n" +
|
||||
"告警目标对象: " + alert.getTarget() + "\n" +
|
||||
"所属监控ID: " + alert.getMonitorId() + "\n" +
|
||||
"所属监控名称: " + alert.getMonitorName() + "\n" +
|
||||
"告警级别: " + alert.getPriority() + "\n" +
|
||||
"告警详情: \n" + alert.getContent());
|
||||
javaMailSender.send(message);
|
||||
}
|
||||
|
||||
private List<NoticeReceiver> matchReceiverByNoticeRules(Alert alert) {
|
||||
// todo 使用缓存
|
||||
return noticeConfigService.getReceiverFilterRule(alert);
|
||||
}
|
||||
|
||||
|
||||
|
||||
@@ -1,5 +1,6 @@
|
||||
package com.usthe.manager.service;
|
||||
|
||||
import com.usthe.alert.pojo.entity.Alert;
|
||||
import com.usthe.manager.pojo.entity.NoticeReceiver;
|
||||
import com.usthe.manager.pojo.entity.NoticeRule;
|
||||
import org.springframework.data.jpa.domain.Specification;
|
||||
@@ -62,4 +63,11 @@ public interface NoticeConfigService {
|
||||
* @param ruleId 通知策略ID
|
||||
*/
|
||||
void deleteNoticeRule(Long ruleId);
|
||||
|
||||
/**
|
||||
* 根据告警信息与所有通知策略匹配,过滤出需要通知的接收人
|
||||
* @param alert 告警信息
|
||||
* @return 接收人
|
||||
*/
|
||||
List<NoticeReceiver> getReceiverFilterRule(Alert alert);
|
||||
}
|
||||
|
||||
@@ -1,5 +1,6 @@
|
||||
package com.usthe.manager.service.impl;
|
||||
|
||||
import com.usthe.alert.pojo.entity.Alert;
|
||||
import com.usthe.manager.dao.NoticeReceiverDao;
|
||||
import com.usthe.manager.dao.NoticeRuleDao;
|
||||
import com.usthe.manager.pojo.entity.NoticeReceiver;
|
||||
@@ -11,7 +12,10 @@ import org.springframework.data.jpa.domain.Specification;
|
||||
import org.springframework.stereotype.Service;
|
||||
import org.springframework.transaction.annotation.Transactional;
|
||||
|
||||
import java.util.HashSet;
|
||||
import java.util.List;
|
||||
import java.util.Set;
|
||||
import java.util.stream.Collectors;
|
||||
|
||||
/**
|
||||
* 消息通知配置实现
|
||||
@@ -68,4 +72,16 @@ public class NoticeConfigServiceImpl implements NoticeConfigService {
|
||||
public void deleteNoticeRule(Long ruleId) {
|
||||
noticeRuleDao.deleteById(ruleId);
|
||||
}
|
||||
|
||||
@Override
|
||||
public List<NoticeReceiver> getReceiverFilterRule(Alert alert) {
|
||||
// todo 使用缓存
|
||||
List<NoticeRule> rules = noticeRuleDao.findAll();
|
||||
// todo 暂时规则是全部转发 后面实现更多匹配规则:告警状态选择 监控类型选择等
|
||||
Set<Long> receiverIds = rules.stream()
|
||||
.filter(item -> item.isFilterAll() && item.isEnable())
|
||||
.map(NoticeRule::getReceiverId)
|
||||
.collect(Collectors.toSet());
|
||||
return noticeReceiverDao.findAllById(receiverIds);
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user