Merge remote-tracking branch 'origin/master'

# Conflicts:
#	manager/src/main/resources/templates/mailAlarm.html
This commit is contained in:
chenghua
2022-02-21 18:41:27 +08:00
9 changed files with 966 additions and 327 deletions

View File

@@ -21,7 +21,7 @@ import static java.lang.annotation.RetentionPolicy.RUNTIME;
@Constraint(validatedBy = HostParamValidator.class)
public @interface HostValid {
String message() default "Host value is invalid,must ipv4, ipv6 or domain";
String message() default "监控Host必须是ipv4,ipv6或域名";
Class<?>[] groups() default {};

View File

@@ -62,4 +62,19 @@ public class CommonUtil {
return m.find();
}
/**
* 告警级别文字转换
* @param priority 告警级别
* @return 告警级别文字
*/
public static String transferAlertPriority(byte priority) {
String priorityMsg = "警告告警";
switch (priority) {
case 0: priorityMsg = "紧急告警"; break;
case 1: priorityMsg = "严重告警"; break;
case 2: priorityMsg = "警告告警"; break;
default: break;
}
return priorityMsg;
}
}

View File

@@ -15,10 +15,13 @@ public class IpDomainUtil {
* 域名校验正则
*/
private static final Pattern DOMAIN_PATTERN =
Pattern.compile("^([hH][tT]{2}[pP]://|[hH][tT]{2}[pP][sS]://)?(([A-Za-z0-9-~]+).)+([A-Za-z0-9-~\\/])+$");
Pattern.compile("^(?=^.{3,255}$)[a-zA-Z0-9][-a-zA-Z0-9]{0,62}(\\.[a-zA-Z0-9][-a-zA-Z0-9]{0,62})+$");
private static final String LOCALHOST = "localhost";
/**
* HTTP协议头校验规则
*/
private static final Pattern DOMAIN_SCHEMA = Pattern.compile("^([hH][tT]{2}[pP]://|[hH][tT]{2}[pP][sS]://){1}[^\\s]*");
/**

View File

@@ -0,0 +1,39 @@
package com.usthe.common.util;
import org.junit.jupiter.api.Test;
import static org.junit.jupiter.api.Assertions.assertFalse;
import static org.junit.jupiter.api.Assertions.assertTrue;
/**
* @author tom
* @date 2022/2/19 20:32
*/
class IpDomainUtilTest {
@Test
void validateIpDomain() {
assertTrue(IpDomainUtil.validateIpDomain("127.7.5.3"));
assertTrue(IpDomainUtil.validateIpDomain("255.255.4.3"));
assertTrue(IpDomainUtil.validateIpDomain("255.255.255.255"));
assertTrue(IpDomainUtil.validateIpDomain("tancloud.cn"));
assertTrue(IpDomainUtil.validateIpDomain("tancloud.com.cn"));
assertTrue(IpDomainUtil.validateIpDomain("student.dev.com.cn"));
assertTrue(IpDomainUtil.validateIpDomain("www.student.dev.com.cn"));
assertTrue(IpDomainUtil.validateIpDomain("www.baidu.com"));
assertTrue(IpDomainUtil.validateIpDomain("good.didi"));
assertFalse(IpDomainUtil.validateIpDomain("tmp"));
assertFalse(IpDomainUtil.validateIpDomain("good"));
assertFalse(IpDomainUtil.validateIpDomain("www.baidu.com."));
assertFalse(IpDomainUtil.validateIpDomain("good."));
assertFalse(IpDomainUtil.validateIpDomain(".good."));
}
@Test
void isHasSchema() {
assertTrue(IpDomainUtil.isHasSchema("http://www.baidu.com"));
assertTrue(IpDomainUtil.isHasSchema("https://www.baidu.com"));
assertFalse(IpDomainUtil.isHasSchema("www.baidu.com"));
assertFalse(IpDomainUtil.isHasSchema("https_www.baidu.com"));
}
}

View File

@@ -11,6 +11,7 @@ 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.beans.factory.annotation.Value;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.mail.javamail.JavaMailSender;
@@ -41,6 +42,9 @@ public class DispatchAlarm {
private RestTemplate restTemplate;
private MailService mailService;
@Value("${spring.mail.username}")
private String emailFromUser;
public DispatchAlarm(AlerterWorkerPool workerPool, AlerterDataQueue dataQueue,
JavaMailSender javaMailSender, NoticeConfigService noticeConfigService,
AlertService alertService, MonitorService monitorService, RestTemplate restTemplate, MailService mailService) {
@@ -148,12 +152,12 @@ public class DispatchAlarm {
MimeMessageHelper messageHelper = new MimeMessageHelper(mimeMessage,true,"UTF-8");
messageHelper.setSubject("TanCloud探云-监控告警");
//设置发件人Email
messageHelper.setFrom("gongchao@tancloud.cn");
messageHelper.setFrom(emailFromUser);
//设定收件人Email
messageHelper.setTo(receiver.getEmail());
messageHelper.setSentDate(new Date());
//构建邮件模版
String process = mailService.buildHTMLTemplate(alert);
String process = mailService.buildAlertHtmlTemplate(alert);
//设置邮件内容模版
messageHelper.setText(process,true);
javaMailSender.send(mimeMessage);

View File

@@ -11,7 +11,6 @@ import org.springframework.stereotype.Service;
* @author 花城
* @version 1.0
* @date 2022/2/19 6:11 下午
* @Description
*/
public interface MailService {
@@ -20,5 +19,5 @@ public interface MailService {
* @param alert 告警信息
* @return 邮件内容
*/
String buildHTMLTemplate(Alert alert);
String buildAlertHtmlTemplate(Alert alert);
}

View File

@@ -1,9 +1,9 @@
package com.usthe.manager.service.impl;
import com.usthe.common.entity.alerter.Alert;
import com.usthe.common.util.CommonUtil;
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;
@@ -16,7 +16,6 @@ import javax.annotation.Resource;
* @author 花城
* @version 1.0
* @date 2022/2/19 6:13 下午
* @Description
*/
@Slf4j
@Service
@@ -26,13 +25,13 @@ public class MailServiceImpl implements MailService {
private TemplateEngine templateEngine;
@Override
public String buildHTMLTemplate(final Alert alert) {
public String buildAlertHtmlTemplate(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("monitorId",alert.getMonitorId());
context.setVariable("monitorName",alert.getMonitorName());
context.setVariable("priority", CommonUtil.transferAlertPriority(alert.getPriority()));
context.setVariable("content",alert.getContent());
return templateEngine.process("mailAlarm", context);
}

File diff suppressed because it is too large Load Diff

View File

@@ -4,6 +4,22 @@
**面向开发者,易用友好的高性能监控云服务**
## 本地启动
### npm 方式
1. 需要nodejs npm环境
下载地址https://nodejs.org/en/download
2. 在前端工程目录web-app下执行 `npm install --registry=https://registry.npm.taobao.org`
3. 全局安装angular-cli `npm install -g @angular/cli@12 --registry=https://registry.npm.taobao.org`
4. 待本地后端启动后在web-app目录下启动本地前端 `ng serve --open`
### yarn 方式
1. 需要nodejs npm环境
下载地址https://nodejs.org/en/download
2. 安装yarn `npm install -g yarn`
3. 在前端工程目录web-app下执行 `yarn install`
4. 全局安装angular-cli `npm install -g @angular/cli@12 --registry=https://registry.npm.taobao.org`
5. 待本地后端启动后在web-app目录下启动本地前端 `ng serve --open`
## 编译打包
@@ -15,4 +31,4 @@ manager目录下执行
```mvn package```
生成的安装包在 manager/target/hertz-beta-1.0.tar.gz
生成的安装包在 manager/target/hertz-beta.tar.gz