feat: 集成飞书机器人推送信息功能 #I4U9BT
This commit is contained in:
@@ -1,10 +1,13 @@
|
|||||||
package com.usthe.manager.component.alerter;
|
package com.usthe.manager.component.alerter;
|
||||||
|
|
||||||
|
import com.alibaba.fastjson.JSON;
|
||||||
import com.usthe.alert.AlerterDataQueue;
|
import com.usthe.alert.AlerterDataQueue;
|
||||||
import com.usthe.alert.AlerterWorkerPool;
|
import com.usthe.alert.AlerterWorkerPool;
|
||||||
|
import com.usthe.collector.collect.common.http.HttpUtils;
|
||||||
import com.usthe.common.util.CommonUtil;
|
import com.usthe.common.util.CommonUtil;
|
||||||
import com.usthe.common.entity.alerter.Alert;
|
import com.usthe.common.entity.alerter.Alert;
|
||||||
import com.usthe.alert.service.AlertService;
|
import com.usthe.alert.service.AlertService;
|
||||||
|
import com.usthe.manager.pojo.dto.FlyBookWebHookDto;
|
||||||
import com.usthe.manager.pojo.dto.WeWorkWebHookDTO;
|
import com.usthe.manager.pojo.dto.WeWorkWebHookDTO;
|
||||||
import com.usthe.common.util.CommonConstants;
|
import com.usthe.common.util.CommonConstants;
|
||||||
import com.usthe.common.entity.manager.Monitor;
|
import com.usthe.common.entity.manager.Monitor;
|
||||||
@@ -23,6 +26,7 @@ import org.springframework.web.client.ResourceAccessException;
|
|||||||
import org.springframework.web.client.RestTemplate;
|
import org.springframework.web.client.RestTemplate;
|
||||||
|
|
||||||
import javax.mail.internet.MimeMessage;
|
import javax.mail.internet.MimeMessage;
|
||||||
|
import java.util.ArrayList;
|
||||||
import java.util.Date;
|
import java.util.Date;
|
||||||
import java.util.List;
|
import java.util.List;
|
||||||
|
|
||||||
@@ -120,15 +124,70 @@ public class DispatchAlarm {
|
|||||||
switch (receiver.getType()) {
|
switch (receiver.getType()) {
|
||||||
// todo 短信通知
|
// todo 短信通知
|
||||||
case 0: break;
|
case 0: break;
|
||||||
case 1: sendEmailAlert(receiver, alert); break;
|
// case 1: sendEmailAlert(receiver, alert); break;
|
||||||
case 2: sendWebHookAlert(receiver, alert); break;
|
case 2: sendWebHookAlert(receiver, alert); break;
|
||||||
case 3: sendWeChatAlert(receiver, alert); break;
|
case 3: sendWeChatAlert(receiver, alert); break;
|
||||||
case 4: sendWeWorkRobotAlert(receiver, alert);break;
|
case 4: sendWeWorkRobotAlert(receiver, alert);break;
|
||||||
|
case 1: sendFlyBookAlert(receiver,alert); break;
|
||||||
default: break;
|
default: break;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 通过飞书发送告警信息
|
||||||
|
* @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();
|
||||||
|
content.setPost(post);
|
||||||
|
post.setZh_cn(zh_cn);
|
||||||
|
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());
|
||||||
|
contents1.add(flyBookContent);
|
||||||
|
FlyBookWebHookDto.FlyBookContent bookContent = new FlyBookWebHookDto.FlyBookContent();
|
||||||
|
bookContent.setTag("a");
|
||||||
|
bookContent.setText("点击查看");
|
||||||
|
bookContent.setHref("https://www.baidu.com");
|
||||||
|
contents1.add(bookContent);
|
||||||
|
contents.add(contents1);
|
||||||
|
zh_cn.setTitle("[TanCloud探云告警]");
|
||||||
|
zh_cn.setContent(contents);
|
||||||
|
flyBookWebHookDto.setContent(content);
|
||||||
|
String webHookUrl = FlyBookWebHookDto.WEBHOOK_URL;
|
||||||
|
String s = JSON.toJSONString(flyBookWebHookDto);
|
||||||
|
HttpUtils.sendPostJsonBody(webHookUrl,s);
|
||||||
|
try {
|
||||||
|
ResponseEntity<String> entity = restTemplate.postForEntity(webHookUrl, flyBookWebHookDto, String.class);
|
||||||
|
if (entity.getStatusCode() == HttpStatus.OK) {
|
||||||
|
log.debug("Send weWork webHook: {} Success", webHookUrl);
|
||||||
|
} else {
|
||||||
|
log.warn("Send weWork webHook: {} Failed: {}", webHookUrl, entity.getBody());
|
||||||
|
}
|
||||||
|
} catch (ResourceAccessException e) {
|
||||||
|
log.warn("Send WebHook: {} Failed: {}.", webHookUrl, e.getMessage());
|
||||||
|
} catch (Exception e) {
|
||||||
|
log.error(e.getMessage(), e);
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 通过企业微信发送告警信息
|
* 通过企业微信发送告警信息
|
||||||
* @param receiver 通知配置信息
|
* @param receiver 通知配置信息
|
||||||
|
|||||||
@@ -0,0 +1,66 @@
|
|||||||
|
package com.usthe.manager.pojo.dto;
|
||||||
|
|
||||||
|
import lombok.AllArgsConstructor;
|
||||||
|
import lombok.Builder;
|
||||||
|
import lombok.Data;
|
||||||
|
import lombok.NoArgsConstructor;
|
||||||
|
|
||||||
|
import java.util.List;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 飞书机器人消息实体
|
||||||
|
*
|
||||||
|
* @author 花城
|
||||||
|
* @version 1.0
|
||||||
|
* @date 2022/2/22 6:41 下午
|
||||||
|
* @Description
|
||||||
|
*/
|
||||||
|
@Data
|
||||||
|
@Builder
|
||||||
|
@AllArgsConstructor
|
||||||
|
@NoArgsConstructor
|
||||||
|
public class FlyBookWebHookDto {
|
||||||
|
public static final String WEBHOOK_URL = "https://open.feishu.cn/open-apis/bot/v2/hook/873f1a93-0517-4590-a335-da64e48d6d48";
|
||||||
|
|
||||||
|
private static final String MARKDOWN = "post";
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 消息类型
|
||||||
|
*/
|
||||||
|
private String msg_type = MARKDOWN;
|
||||||
|
|
||||||
|
private Content content;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 消息内容
|
||||||
|
*/
|
||||||
|
@Data
|
||||||
|
public static class Content {
|
||||||
|
public Post post;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Data
|
||||||
|
public static class FlyBookContent{
|
||||||
|
//格式 目前支持文本、超链接、@人的功能 text a at
|
||||||
|
public String tag;
|
||||||
|
//文本
|
||||||
|
public String text;
|
||||||
|
//超链接地址
|
||||||
|
public String href;
|
||||||
|
|
||||||
|
public String user_id;
|
||||||
|
public String user_name;
|
||||||
|
}
|
||||||
|
@Data
|
||||||
|
public static class Post {
|
||||||
|
public zh_cn zh_cn;
|
||||||
|
}
|
||||||
|
@Data
|
||||||
|
public static class zh_cn{
|
||||||
|
//标题
|
||||||
|
public String title;
|
||||||
|
//内容
|
||||||
|
public List<List<FlyBookContent>> content;
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
Reference in New Issue
Block a user