[manager,webapp]前端开放飞书告警通知

This commit is contained in:
tomsun28
2022-02-22 23:39:18 +08:00
parent 8a3b304738
commit 3a948ffb28
9 changed files with 53 additions and 588 deletions

View File

@@ -1,9 +1,7 @@
package com.usthe.manager.component.alerter;
import com.alibaba.fastjson.JSON;
import com.usthe.alert.AlerterDataQueue;
import com.usthe.alert.AlerterWorkerPool;
import com.usthe.collector.collect.common.http.HttpUtils;
import com.usthe.common.util.CommonUtil;
import com.usthe.common.entity.alerter.Alert;
import com.usthe.alert.service.AlertService;
@@ -124,11 +122,11 @@ public class DispatchAlarm {
switch (receiver.getType()) {
// todo 短信通知
case 0: break;
// case 1: sendEmailAlert(receiver, alert); break;
case 1: sendEmailAlert(receiver, alert); break;
case 2: sendWebHookAlert(receiver, alert); break;
case 3: sendWeChatAlert(receiver, alert); break;
case 4: sendWeWorkRobotAlert(receiver, alert);break;
case 1: sendFlyBookAlert(receiver,alert); break;
case 4: sendWeWorkRobotAlert(receiver, alert); break;
case 6: sendFlyBookAlert(receiver,alert); break;
default: break;
}
}
@@ -136,46 +134,44 @@ public class DispatchAlarm {
/**
* 通过飞书发送告警信息
* @param receiver
* @param alert
* @param receiver 接收人
* @param alert 告警信息
*/
private void sendFlyBookAlert(NoticeReceiver receiver, Alert alert) {
FlyBookWebHookDto flyBookWebHookDto = new FlyBookWebHookDto();
FlyBookWebHookDto.Content content = new FlyBookWebHookDto.Content();
FlyBookWebHookDto.Post post = new FlyBookWebHookDto.Post();
FlyBookWebHookDto.zh_cn zh_cn = new FlyBookWebHookDto.zh_cn();
FlyBookWebHookDto.zh_cn zhCn = new FlyBookWebHookDto.zh_cn();
content.setPost(post);
post.setZh_cn(zh_cn);
post.setZh_cn(zhCn);
flyBookWebHookDto.setMsg_type("post");
List<List<FlyBookWebHookDto.FlyBookContent>> contents = new ArrayList<>();
List<FlyBookWebHookDto.FlyBookContent> contents1 = new ArrayList<>();
FlyBookWebHookDto.FlyBookContent flyBookContent = new FlyBookWebHookDto.FlyBookContent();
flyBookContent.setTag("text");
StringBuilder text = new StringBuilder();
text.append("告警目标对象 :" + alert.getTarget())
.append("\n所属监控ID :" + alert.getMonitorId())
.append("\n所属监控名称 :" + alert.getMonitorName())
.append("\n告警级别 :" + CommonUtil.transferAlertPriority(alert.getPriority()))
.append("\n内容详情 : " + alert.getContent());
flyBookContent.setText(text.toString());
String text = "告警目标对象 :" + alert.getTarget() +
"\n所属监控ID :" + alert.getMonitorId() +
"\n所属监控名称 :" + alert.getMonitorName() +
"\n告警级别 :" + CommonUtil.transferAlertPriority(alert.getPriority()) +
"\n内容详情 : " + alert.getContent();
flyBookContent.setText(text);
contents1.add(flyBookContent);
FlyBookWebHookDto.FlyBookContent bookContent = new FlyBookWebHookDto.FlyBookContent();
bookContent.setTag("a");
bookContent.setText("点击查看");
bookContent.setHref("https://www.baidu.com");
bookContent.setText("登入控制台");
bookContent.setHref("https://www.tancloud.cn");
contents1.add(bookContent);
contents.add(contents1);
zh_cn.setTitle("[TanCloud探云告警]");
zh_cn.setContent(contents);
zhCn.setTitle("[TanCloud探云告警通知]");
zhCn.setContent(contents);
flyBookWebHookDto.setContent(content);
//TODO 这个地方要加新的字段
String webHookUrl = FlyBookWebHookDto.WEBHOOK_URL + receiver.getWechatId();
try {
ResponseEntity<String> entity = restTemplate.postForEntity(webHookUrl, flyBookWebHookDto, String.class);
if (entity.getStatusCode() == HttpStatus.OK) {
log.debug("Send weWork webHook: {} Success", webHookUrl);
log.debug("Send feiShu webHook: {} Success", webHookUrl);
} else {
log.warn("Send weWork webHook: {} Failed: {}", webHookUrl, entity.getBody());
log.warn("Send feiShu webHook: {} Failed: {}", webHookUrl, entity.getBody());
}
} catch (ResourceAccessException e) {
log.warn("Send WebHook: {} Failed: {}.", webHookUrl, e.getMessage());

View File

@@ -13,14 +13,13 @@ import java.util.List;
* @author 花城
* @version 1.0
* @date 2022/2/22 6:41 下午
* @Description
*/
@Data
@Builder
@AllArgsConstructor
@NoArgsConstructor
public class FlyBookWebHookDto {
//TODO hook后面是特有的地址
public static final String WEBHOOK_URL = "https://open.feishu.cn/open-apis/bot/v2/hook/";
private static final String MARKDOWN = "post";
@@ -42,11 +41,17 @@ public class FlyBookWebHookDto {
@Data
public static class FlyBookContent{
//格式 目前支持文本、超链接、@人的功能 text a at
/**
* 格式 目前支持文本、超链接、@人的功能 text a at
*/
public String tag;
//文本
/**
* 文本
*/
public String text;
//超链接地址
/**
* 超链接地址
*/
public String href;
public String user_id;
@@ -58,9 +63,13 @@ public class FlyBookWebHookDto {
}
@Data
public static class zh_cn{
//标题
/**
* 标题
*/
public String title;
//内容
/**
* 内容
*/
public List<List<FlyBookContent>> content;
}