diff --git a/manager/pom.xml b/manager/pom.xml index 3e644b5..036f300 100644 --- a/manager/pom.xml +++ b/manager/pom.xml @@ -52,6 +52,16 @@ spring-boot-configuration-processor true + + + org.springframework.boot + spring-boot-starter-thymeleaf + + + net.sourceforge.nekohtml + nekohtml + 1.9.22 + org.springframework.boot @@ -108,6 +118,7 @@ sureness.yml banner.txt define/** + **/*.html diff --git a/manager/src/main/java/com/usthe/manager/component/alerter/DispatchAlarm.java b/manager/src/main/java/com/usthe/manager/component/alerter/DispatchAlarm.java index 8ae7f77..4551d8a 100644 --- a/manager/src/main/java/com/usthe/manager/component/alerter/DispatchAlarm.java +++ b/manager/src/main/java/com/usthe/manager/component/alerter/DispatchAlarm.java @@ -7,17 +7,19 @@ import com.usthe.alert.service.AlertService; import com.usthe.common.util.CommonConstants; import com.usthe.common.entity.manager.Monitor; import com.usthe.common.entity.manager.NoticeReceiver; +import com.usthe.manager.service.MailService; import com.usthe.manager.service.MonitorService; import com.usthe.manager.service.NoticeConfigService; import lombok.extern.slf4j.Slf4j; import org.springframework.http.HttpStatus; import org.springframework.http.ResponseEntity; -import org.springframework.mail.SimpleMailMessage; import org.springframework.mail.javamail.JavaMailSender; +import org.springframework.mail.javamail.MimeMessageHelper; import org.springframework.stereotype.Component; import org.springframework.web.client.ResourceAccessException; import org.springframework.web.client.RestTemplate; +import javax.mail.internet.MimeMessage; import java.util.Date; import java.util.List; @@ -37,10 +39,11 @@ public class DispatchAlarm { private NoticeConfigService noticeConfigService; private JavaMailSender javaMailSender; private RestTemplate restTemplate; + private MailService mailService; public DispatchAlarm(AlerterWorkerPool workerPool, AlerterDataQueue dataQueue, - JavaMailSender javaMailSender,NoticeConfigService noticeConfigService, - AlertService alertService, MonitorService monitorService, RestTemplate restTemplate) { + JavaMailSender javaMailSender, NoticeConfigService noticeConfigService, + AlertService alertService, MonitorService monitorService, RestTemplate restTemplate, MailService mailService) { this.workerPool = workerPool; this.dataQueue = dataQueue; this.alertService = alertService; @@ -48,6 +51,7 @@ public class DispatchAlarm { this.noticeConfigService = noticeConfigService; this.javaMailSender = javaMailSender; this.restTemplate = restTemplate; + this.mailService = mailService; startDispatch(); } @@ -137,19 +141,21 @@ public class DispatchAlarm { } } - 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 void sendEmailAlert(final NoticeReceiver receiver,final Alert alert){ + try{ + MimeMessage mimeMessage = javaMailSender.createMimeMessage(); + MimeMessageHelper messageHelper = new MimeMessageHelper(mimeMessage,true,"UTF-8"); + messageHelper.setSubject("TanCloud探云-监控告警"); //设置邮件主题 + messageHelper.setFrom("gongchao@tancloud.cn"); //设置发件人Email + messageHelper.setTo(receiver.getEmail()); //设定收件人Email + messageHelper.setSentDate(new Date()); + String process = mailService.buildHTMLTemplate(alert); + messageHelper.setText(process,true); //设置邮件内容模版 + javaMailSender.send(mimeMessage); + }catch (Exception e){ + log.error("[邮箱告警] error,Exception information={}",e); + } } private List matchReceiverByNoticeRules(Alert alert) { diff --git a/manager/src/main/java/com/usthe/manager/service/MailService.java b/manager/src/main/java/com/usthe/manager/service/MailService.java new file mode 100644 index 0000000..3fb835b --- /dev/null +++ b/manager/src/main/java/com/usthe/manager/service/MailService.java @@ -0,0 +1,24 @@ +package com.usthe.manager.service; + +import com.usthe.common.entity.alerter.Alert; +import lombok.extern.slf4j.Slf4j; +import org.springframework.core.annotation.Order; +import org.springframework.stereotype.Service; + +/** + * 邮箱发送服务 + * + * @author 花城 + * @version 1.0 + * @date 2022/2/19 6:11 下午 + * @Description + */ +public interface MailService { + + /** + * 构建告警邮件模版 + * @param alert 告警信息 + * @return 邮件内容 + */ + String buildHTMLTemplate(Alert alert); +} diff --git a/manager/src/main/java/com/usthe/manager/service/impl/MailServiceImpl.java b/manager/src/main/java/com/usthe/manager/service/impl/MailServiceImpl.java new file mode 100644 index 0000000..8f37032 --- /dev/null +++ b/manager/src/main/java/com/usthe/manager/service/impl/MailServiceImpl.java @@ -0,0 +1,39 @@ +package com.usthe.manager.service.impl; + +import com.usthe.common.entity.alerter.Alert; +import com.usthe.manager.service.MailService; +import lombok.extern.slf4j.Slf4j; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.stereotype.Service; +import org.thymeleaf.TemplateEngine; +import org.thymeleaf.context.Context; + +import javax.annotation.Resource; + +/** + * 邮箱发送服务接口实现类 + * + * @author 花城 + * @version 1.0 + * @date 2022/2/19 6:13 下午 + * @Description + */ +@Slf4j +@Service +public class MailServiceImpl implements MailService { + + @Resource + private TemplateEngine templateEngine; + + @Override + public String buildHTMLTemplate(final Alert alert) { + //引入thymeleaf上下文参数渲染页面 + Context context = new Context(); + context.setVariable("target",alert.getTarget()); + context.setVariable("ID",alert.getMonitorId()); + context.setVariable("name",alert.getMonitorName()); + context.setVariable("priority",alert.getPriority()); + context.setVariable("content",alert.getContent()); + return templateEngine.process("mailAlarm", context); + } +} diff --git a/manager/src/main/resources/application.yml b/manager/src/main/resources/application.yml index f199836..00d7058 100644 --- a/manager/src/main/resources/application.yml +++ b/manager/src/main/resources/application.yml @@ -27,9 +27,9 @@ spring: on-profile: prod datasource: driver-class-name: com.mysql.cj.jdbc.Driver - username: admin - password: admin - url: jdbc:mysql://localhost:3306/hertzbeat?useUnicode=true&characterEncoding=utf-8&useSSL=false + username: root + password: wang1027 + url: jdbc:mysql://121.40.113.44:3306/hertzbeat?useUnicode=true&characterEncoding=utf-8&useSSL=false platform: mysql hikari: max-lifetime: 120000 @@ -38,8 +38,8 @@ spring: mail: host: smtp.exmail.qq.com - username: example@tancloud.cn - password: example + username: gongchao@tancloud.cn + password: C7Roz9Qe3eGkiVM9 port: 465 default-encoding: UTF-8 properties: @@ -48,6 +48,16 @@ spring: socketFactoryClass: javax.net.ssl.SSLSocketFactory ssl: enable: true + debug: false + + thymeleaf: + prefix: classpath:/templates/ #prefix:指定模板所在的目录 + check-template-location: true #check-tempate-location: 检查模板路径是否存在 + cache: false #cache: 是否缓存,开发模式下设置为false,避免改了模板还要重启服务器,线上设置为true,可以提高性能。 + suffix: .html + #encoding: UTF-8 + #content-type: text/html + mode: LEGACYHTML5 warehouse: store: diff --git a/manager/src/main/resources/templates/mailAlarm.html b/manager/src/main/resources/templates/mailAlarm.html new file mode 100644 index 0000000..3589c4f --- /dev/null +++ b/manager/src/main/resources/templates/mailAlarm.html @@ -0,0 +1,323 @@ + + + + + + + + + + + + + + +
+
+


+
+
+
+
+ +
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+

+ TanCloud探云-监控告警 +

+
+
+

+ 告警通知 +

+
+
+

+ Alarm notification +

+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+

+ TanCloud探云 +

+
+
+
+ +
+
+
+
+


+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+

+ 告警目标对象:
+     所属监控ID:
+     所属监控名称:
+     告警级别:
+     告警详情:
+
+

+

+
+

+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+


+


+ +
+
+ + + + + + + \ No newline at end of file diff --git a/web-app/package.json b/web-app/package.json index a18c9d8..19ad2ce 100644 --- a/web-app/package.json +++ b/web-app/package.json @@ -41,6 +41,7 @@ "@delon/util": "^12.4.2", "ajv": "^8.6.2", "ajv-formats": "^2.1.1", + "ajv-keywords": "^5.1.0", "echarts": "^5.2.2", "ng-alain": "^12.4.2", "ng-zorro-antd": "^12.0.2", @@ -84,12 +85,12 @@ "ng-alain-plugin-theme": "^12.0.0", "prettier": "^2.2.1", "source-map-explorer": "^2.5.2", - "stylelint": "^13.13.1", + "stylelint": "^14.5.1", "stylelint-config-prettier": "^8.0.2", - "stylelint-config-rational-order": "^0.1.2", - "stylelint-config-standard": "^22.0.0", + "stylelint-config-rational-order": "^0.0.4", + "stylelint-config-standard": "^25.0.0", "stylelint-declaration-block-no-ignored-properties": "^2.4.0", - "stylelint-order": "^4.1.0", + "stylelint-order": "^5.0.0", "typescript": "~4.3.5" }, "lint-staged": {