Skip to content

Commit

Permalink
[monitor] notify test throw msg to front, optional spring.email config (
Browse files Browse the repository at this point in the history
#305)

* spring.email选配,未配置spring.email前端配置邮箱告警通知返回错误消息

* spring.email选配,未配置spring.email前端配置邮箱告警通知返回错误消息

* [manager] spring.email config update

* 发送报警通知的消息异常消息展示给前端

* [manager] alerter notify test throw msg to front

* [home] add tdengine limit version desc (#304)

* spring.email选配,未配置spring.email前端配置邮箱告警通知返回错误消息

* [manager] spring.email config update

* 发送报警通知的消息异常消息展示给前端

* [manager] alerter notify test throw msg to front

* [home] add tdengine limit version desc

* [home] add tdengine limit version desc

Co-authored-by: tomsun28 <tomsun28@outlook.com>
Co-authored-by: emrys <1205096966@qq.com>
  • Loading branch information
3 people committed Sep 25, 2022
1 parent e014246 commit 11834ae
Show file tree
Hide file tree
Showing 16 changed files with 110 additions and 35 deletions.
7 changes: 2 additions & 5 deletions home/docs/start/docker-deploy.md
Original file line number Diff line number Diff line change
Expand Up @@ -203,8 +203,5 @@ $ docker run -d -p 1157:1157 \
> 安装初始化此数据库参考 [TDengine安装初始化](tdengine-init)

6.如果无法显示图表,则是TDengine版本不对
![cd4402a1d6fd769a575869ec41b1c8f](https://user-images.githubusercontent.com/5848915/191882873-27ba5955-ca3c-4fa4-910a-e66c2e8a7173.png)
2.2.0.5无法显示图表
![91e30e27455ba38f9dfedc4c402208c](https://user-images.githubusercontent.com/5848915/191882978-c4a59180-d2cd-4e2b-ba48-ec33073c2015.png)
安装2.4即可
6. 监控详情历史图片不展示或无数据,已经配置了TDengine
> 请确认是否安装的TDengine版本为2.4.0.12附近,版本3.0和2.2不支持兼容
3 changes: 3 additions & 0 deletions home/docs/start/tdengine-init.md
Original file line number Diff line number Diff line change
Expand Up @@ -78,3 +78,6 @@ $ docker run -d -p 6030-6049:6030-6049 -p 6030-6049:6030-6049/udp \

1. 监控页面历史图表不显示,弹出 [无法提供历史图表数据,请配置依赖服务TDengine时序数据库]
> 如弹窗所示,历史图表展示的前提是需要安装配置hertzbeat的依赖服务 - TDengine数据库
2. 监控详情历史图片不展示或无数据,已经配置了TDengine
> 请确认是否安装的TDengine版本为2.4.0.12附近,版本3.0和2.2不支持兼容
Original file line number Diff line number Diff line change
Expand Up @@ -200,3 +200,6 @@ Please refer to the following points to troubleshoot issues:
5. If the history chart on the monitoring page is not displayed,popup [please configure dependency service on TDengine time series database]
> As shown in the popup window,the premise of history chart display is that you need install and configure hertzbeat's dependency service - TDengine database.
> Installation and initialization this database refer to [TDengine Installation and Initialization](tdengine-init).
6. The historical picture of monitoring details is not displayed or has no data, and TDengine has been deployed
> Please confirm whether the installed TDengine version is near 2.4.0.12, version 3.0 and 2.2 are not compatible.
Original file line number Diff line number Diff line change
Expand Up @@ -74,4 +74,7 @@ Note⚠️ Need TDengine 2.4.x Version.
### Common issues

1. If the history chart on the monitoring page is not displayed,popup [unable to provide historical chart data, please configure dependency service on TDengine time series database]
> As shown in the popup window,the premise of history chart display is that you need install and configure hertzbeat's dependency service - TDengine database.
> As shown in the popup window,the premise of history chart display is that you need install and configure HertzBeat's dependency service - TDengine database.
2. The historical picture of monitoring details is not displayed or has no data, and TDengine has been deployed
> Please confirm whether the installed TDengine version is near 2.4.0.12, version 3.0 and 2.2 are not compatible.
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@

import com.usthe.common.entity.alerter.Alert;
import com.usthe.common.entity.manager.NoticeReceiver;
import com.usthe.manager.support.exception.AlertNoticeException;

/**
* @author <a href="mailto:Musk.Chen@fanruan.com">Musk.Chen</a>
Expand All @@ -32,7 +33,7 @@ public interface AlertNotifyHandler {
* @param receiver Notification configuration information 通知配置信息
* @param alert Alarm information 告警信息
*/
void send(NoticeReceiver receiver, Alert alert);
void send(NoticeReceiver receiver, Alert alert) throws AlertNoticeException;

/**
* 通知类型
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
import com.usthe.common.entity.alerter.Alert;
import com.usthe.common.entity.manager.NoticeReceiver;
import com.usthe.manager.service.NoticeConfigService;
import com.usthe.manager.support.exception.AlertNoticeException;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.InitializingBean;
import org.springframework.stereotype.Component;
Expand Down Expand Up @@ -76,7 +77,7 @@ public void afterPropertiesSet() throws Exception {
* @param alert alert msg
* @return send success or failed
*/
public boolean sendNoticeMsg(NoticeReceiver receiver, Alert alert){
public boolean sendNoticeMsg(NoticeReceiver receiver, Alert alert) {
if(receiver == null || receiver.getType() == null){
log.warn("DispatcherAlarm-sendNoticeMsg params is empty alert:[{}], receiver:[{}]", alert, receiver);
return false;
Expand Down Expand Up @@ -114,11 +115,15 @@ public void run() {
}

private void sendNotify(Alert alert) {
// todo Forward configured email WeChat webhook 转发配置的邮件 微信 webhook
// Forward configured email WeChat webhook
List<NoticeReceiver> receivers = matchReceiverByNoticeRules(alert);
// todo Send notification here temporarily single thread 发送通知这里暂时单线程
for (NoticeReceiver receiver : receivers) {
sendNoticeMsg(receiver, alert);
try {
sendNoticeMsg(receiver, alert);
} catch (AlertNoticeException e) {
log.warn("DispatchTask sendNoticeMsg error, message: {}", e.getMessage());
}
}
}
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
package com.usthe.manager.component.alerter.impl;

import com.fasterxml.jackson.annotation.JsonProperty;
import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;

/**
* common robot http response entity
* @author tom
* @date 2022/9/23 22:11
*/
@Data
@AllArgsConstructor
@NoArgsConstructor
public class CommonRobotNotifyResp {

@JsonProperty(value = "errcode")
private Integer errCode;

@JsonProperty(value = "errmsg")
private String errMsg;

private Integer code;

private String msg;
}
Original file line number Diff line number Diff line change
Expand Up @@ -21,16 +21,15 @@
import com.usthe.common.entity.alerter.Alert;
import com.usthe.common.entity.manager.NoticeReceiver;
import com.usthe.common.util.CommonConstants;
import com.usthe.common.util.CommonUtil;
import com.usthe.common.util.ResourceBundleUtil;
import com.usthe.manager.component.alerter.AlertNotifyHandler;
import com.usthe.manager.pojo.dto.DingTalkWebHookDto;
import com.usthe.manager.support.exception.AlertNoticeException;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Component;
import org.springframework.web.client.ResourceAccessException;
import org.springframework.web.client.RestTemplate;

import java.text.SimpleDateFormat;
Expand Down Expand Up @@ -89,16 +88,22 @@ public void send(NoticeReceiver receiver, Alert alert) {
dingTalkWebHookDto.setMarkdown(markdownDTO);
String webHookUrl = alerterProperties.getDingTalkWebHookUrl() + receiver.getAccessToken();
try {
ResponseEntity<String> entity = restTemplate.postForEntity(webHookUrl, dingTalkWebHookDto, String.class);
ResponseEntity<CommonRobotNotifyResp> entity = restTemplate.postForEntity(webHookUrl,
dingTalkWebHookDto, CommonRobotNotifyResp.class);
if (entity.getStatusCode() == HttpStatus.OK) {
log.debug("Send dingTalk webHook: {} Success", webHookUrl);
assert entity.getBody() != null;
if (entity.getBody().getErrCode() == 0) {
log.debug("Send dingTalk webHook: {} Success", webHookUrl);
} else {
log.warn("Send dingTalk webHook: {} Failed: {}", webHookUrl, entity.getBody().getErrMsg());
throw new AlertNoticeException(entity.getBody().getErrMsg());
}
} else {
log.warn("Send dingTalk webHook: {} Failed: {}", webHookUrl, entity.getBody());
throw new AlertNoticeException("Http StatusCode " + entity.getStatusCode());
}
} catch (ResourceAccessException e) {
log.warn("Send dingTalk: {} Failed: {}.", webHookUrl, e.getMessage());
} catch (Exception e) {
log.error(e.getMessage(), e);
throw new AlertNoticeException("[DingTalk Notify Error] " + e.getMessage());
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,9 +22,11 @@
import com.usthe.common.util.ResourceBundleUtil;
import com.usthe.manager.component.alerter.AlertNotifyHandler;
import com.usthe.manager.service.MailService;
import com.usthe.manager.support.exception.AlertNoticeException;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
import org.springframework.mail.javamail.JavaMailSender;
import org.springframework.mail.javamail.MimeMessageHelper;
import org.springframework.stereotype.Component;
Expand All @@ -40,6 +42,7 @@
@Component
@RequiredArgsConstructor
@Slf4j
@ConditionalOnProperty("spring.mail.username")
final class EmailAlertNotifyHandlerImpl implements AlertNotifyHandler {
private final JavaMailSender javaMailSender;
private final MailService mailService;
Expand All @@ -50,7 +53,7 @@ final class EmailAlertNotifyHandlerImpl implements AlertNotifyHandler {
private ResourceBundle bundle = ResourceBundleUtil.getBundle("alerter");

@Override
public void send(NoticeReceiver receiver, Alert alert) {
public void send(NoticeReceiver receiver, Alert alert) throws AlertNoticeException {
try {
MimeMessage mimeMessage = javaMailSender.createMimeMessage();
MimeMessageHelper messageHelper = new MimeMessageHelper(mimeMessage, true, "UTF-8");
Expand All @@ -66,7 +69,7 @@ public void send(NoticeReceiver receiver, Alert alert) {
messageHelper.setText(process, true);
javaMailSender.send(mimeMessage);
} catch (Exception e) {
log.error("[Email Alert] Exception,Exception information={}", e.getMessage());
throw new AlertNoticeException("[Email Notify Error] " + e.getMessage());
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@
import com.usthe.common.util.ResourceBundleUtil;
import com.usthe.manager.component.alerter.AlertNotifyHandler;
import com.usthe.manager.pojo.dto.FlyBookWebHookDto;
import com.usthe.manager.support.exception.AlertNoticeException;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.http.HttpStatus;
Expand Down Expand Up @@ -106,16 +107,22 @@ public void send(NoticeReceiver receiver, Alert alert) {
flyBookWebHookDto.setContent(content);
String webHookUrl = alerterProperties.getFlyBookWebHookUrl() + receiver.getWechatId();
try {
ResponseEntity<String> entity = restTemplate.postForEntity(webHookUrl, flyBookWebHookDto, String.class);
ResponseEntity<CommonRobotNotifyResp> entity = restTemplate.postForEntity(webHookUrl,
flyBookWebHookDto, CommonRobotNotifyResp.class);
if (entity.getStatusCode() == HttpStatus.OK) {
log.debug("Send feiShu webHook: {} Success", webHookUrl);
assert entity.getBody() != null;
if (entity.getBody().getCode() == null || entity.getBody().getCode() == 0) {
log.debug("Send feiShu webHook: {} Success", webHookUrl);
} else {
log.warn("Send feiShu webHook: {} Failed: {}", webHookUrl, entity.getBody().getMsg());
throw new AlertNoticeException(entity.getBody().getMsg());
}
} else {
log.warn("Send feiShu webHook: {} Failed: {}", webHookUrl, entity.getBody());
throw new AlertNoticeException("Http StatusCode " + entity.getStatusCode());
}
} catch (ResourceAccessException e) {
log.warn("Send WebHook: {} Failed: {}.", webHookUrl, e.getMessage());
} catch (Exception e) {
log.error(e.getMessage(), e);
throw new AlertNoticeException("[FeiShu Notify Error] " + e.getMessage());
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,12 +24,12 @@
import com.usthe.common.util.ResourceBundleUtil;
import com.usthe.manager.component.alerter.AlertNotifyHandler;
import com.usthe.manager.pojo.dto.WeWorkWebHookDto;
import com.usthe.manager.support.exception.AlertNoticeException;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Component;
import org.springframework.web.client.ResourceAccessException;
import org.springframework.web.client.RestTemplate;

import java.text.SimpleDateFormat;
Expand Down Expand Up @@ -92,16 +92,21 @@ public void send(NoticeReceiver receiver, Alert alert) {
weWorkWebHookDTO.setMarkdown(markdownDTO);
String webHookUrl = alerterProperties.getWeWorkWebHookUrl() + receiver.getWechatId();
try {
ResponseEntity<String> entity = restTemplate.postForEntity(webHookUrl, weWorkWebHookDTO, String.class);
ResponseEntity<CommonRobotNotifyResp> entity = restTemplate.postForEntity(webHookUrl, weWorkWebHookDTO, CommonRobotNotifyResp.class);
if (entity.getStatusCode() == HttpStatus.OK) {
log.debug("Send weWork webHook: {} Success", webHookUrl);
assert entity.getBody() != null;
if (entity.getBody().getErrCode() == 0) {
log.debug("Send WeWork webHook: {} Success", webHookUrl);
} else {
log.warn("Send WeWork webHook: {} Failed: {}", webHookUrl, entity.getBody().getErrMsg());
throw new AlertNoticeException(entity.getBody().getErrMsg());
}
} else {
log.warn("Send weWork webHook: {} Failed: {}", webHookUrl, entity.getBody());
log.warn("Send WeWork webHook: {} Failed: {}", webHookUrl, entity.getBody());
throw new AlertNoticeException("Http StatusCode " + entity.getStatusCode());
}
} catch (ResourceAccessException e) {
log.warn("Send WebHook: {} Failed: {}.", webHookUrl, e.getMessage());
} catch (Exception e) {
log.error(e.getMessage(), e);
throw new AlertNoticeException("[WeWork Notify Error] " + e.getMessage());
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
import com.usthe.common.entity.alerter.Alert;
import com.usthe.common.entity.manager.NoticeReceiver;
import com.usthe.manager.component.alerter.AlertNotifyHandler;
import com.usthe.manager.support.exception.AlertNoticeException;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.http.HttpStatus;
Expand All @@ -44,10 +45,11 @@ public void send(NoticeReceiver receiver, Alert alert) {
if (entity.getStatusCode().value() < HttpStatus.BAD_REQUEST.value()) {
log.debug("Send WebHook: {} Success", receiver.getHookUrl());
} else {
log.warn("Send WebHook: {} Failed", receiver.getHookUrl());
log.warn("Send WebHook: {} Failed: {}", receiver.getHookUrl(), entity.getBody());
throw new AlertNoticeException("Http StatusCode " + entity.getStatusCode());
}
} catch (Exception e) {
log.warn("Send WebHook: {} Failed: {}.", receiver.getHookUrl(), e.getMessage());
throw new AlertNoticeException("[WebHook Notify Error] " + e.getMessage());
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@

import java.util.List;

import static com.usthe.common.util.CommonConstants.FAIL_CODE;
import static org.springframework.http.MediaType.APPLICATION_JSON_VALUE;

/**
Expand Down Expand Up @@ -156,9 +157,16 @@ public ResponseEntity<Message<List<NoticeRule>>> getRules(

@PostMapping(path = "/receiver/send-test-msg")
@ApiOperation(value = "Send test msg to receiver", notes = "给指定接收人发送测试消息")
public ResponseEntity<Message<Boolean>> sendTestMsg(@Valid @RequestBody NoticeReceiver noticeReceiver) {
public ResponseEntity<Message<Void>> sendTestMsg(@Valid @RequestBody NoticeReceiver noticeReceiver) {
boolean sendFlag = noticeConfigService.sendTestMsg(noticeReceiver);
return ResponseEntity.ok(new Message<>(sendFlag));
if (sendFlag) {
return ResponseEntity.ok(new Message<>());
}
Message<Void> message = Message.<Void>builder()
.msg("Notify service not available, please check config!")
.code(FAIL_CODE)
.build();
return ResponseEntity.ok(message);
}

}
2 changes: 2 additions & 0 deletions manager/src/main/resources/application.yml
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,8 @@ spring:
hibernate:
ddl-auto: update

# Not Require, Please config if you need email notify
# 非必填:不使用邮箱作为警告通知可以去掉spring.mail配置
mail:
# Attention: this is mail server address.
# 请注意此为邮件服务器地址:qq邮箱为 smtp.qq.com qq企业邮箱为 smtp.exmail.qq.com
Expand Down
2 changes: 2 additions & 0 deletions script/application.yml
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,8 @@ spring:
hibernate:
ddl-auto: update

# Not Require, Please config if you need email notify
# 非必填:不使用邮箱作为警告通知可以去掉spring.mail配置
mail:
# Attention: this is mail server address.
# 请注意此为邮件服务器地址:qq邮箱为 smtp.qq.com qq企业邮箱为 smtp.exmail.qq.com
Expand Down
2 changes: 2 additions & 0 deletions script/docker-compose/conf/application.yml
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,8 @@ spring:
database: mysql
hibernate:
ddl-auto: update
# Not Require, Please config if you need email notify
# 非必填:不使用邮箱作为警告通知可以去掉spring.mail配置
mail:
# Attention: this is mail server address.
# 请注意此为邮件服务器地址:qq邮箱为 smtp.qq.com qq企业邮箱为 smtp.exmail.qq.com
Expand Down

0 comments on commit 11834ae

Please sign in to comment.