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 52d80d2..3383b73 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 @@ -5,6 +5,7 @@ import com.usthe.alert.AlerterWorkerPool; import com.usthe.common.util.CommonUtil; import com.usthe.common.entity.alerter.Alert; import com.usthe.alert.service.AlertService; +import com.usthe.manager.pojo.dto.DingTalkWebHookDto; import com.usthe.manager.pojo.dto.FlyBookWebHookDto; import com.usthe.manager.pojo.dto.WeWorkWebHookDto; import com.usthe.common.util.CommonConstants; @@ -126,6 +127,7 @@ public class DispatchAlarm { case 2: sendWebHookAlert(receiver, alert); break; case 3: sendWeChatAlert(receiver, alert); break; case 4: sendWeWorkRobotAlert(receiver, alert); break; + case 5: sendDingTalkRobotAlert(receiver, alert); break; case 6: sendFlyBookAlert(receiver,alert); break; default: break; } @@ -180,6 +182,39 @@ public class DispatchAlarm { } } + /** + * 通过钉钉机器人发送告警信息 + * @param receiver 通知配置信息 + * @param alert 告警信息 + */ + private void sendDingTalkRobotAlert(NoticeReceiver receiver, Alert alert) { + DingTalkWebHookDto dingTalkWebHookDto = new DingTalkWebHookDto(); + DingTalkWebHookDto.MarkdownDTO markdownDTO = new DingTalkWebHookDto.MarkdownDTO(); + String content = "#### [TanCloud探云告警通知]\n##### **告警目标对象** : " + + alert.getTarget() + "\n " + + "##### **所属监控ID** : " + alert.getMonitorId() + "\n " + + "##### **所属监控名称** : " + alert.getMonitorName() + "\n " + + "##### **告警级别** : " + + CommonUtil.transferAlertPriority(alert.getPriority()) + "\n " + + "##### **内容详情** : " + alert.getContent(); + markdownDTO.setText(content); + markdownDTO.setTitle("TanCloud探云告警通知"); + dingTalkWebHookDto.setMarkdown(markdownDTO); + String webHookUrl = DingTalkWebHookDto.WEBHOOK_URL + receiver.getAccessToken(); + try { + ResponseEntity entity = restTemplate.postForEntity(webHookUrl, dingTalkWebHookDto, String.class); + if (entity.getStatusCode() == HttpStatus.OK) { + log.debug("Send dingTalk webHook: {} Success", webHookUrl); + } else { + log.warn("Send dingTalk webHook: {} Failed: {}", webHookUrl, entity.getBody()); + } + } catch (ResourceAccessException e) { + log.warn("Send dingTalk: {} Failed: {}.", webHookUrl, e.getMessage()); + } catch (Exception e) { + log.error(e.getMessage(), e); + } + } + /** * 通过企业微信发送告警信息 * @param receiver 通知配置信息 diff --git a/manager/src/main/java/com/usthe/manager/pojo/dto/DingTalkWebHookDto.java b/manager/src/main/java/com/usthe/manager/pojo/dto/DingTalkWebHookDto.java new file mode 100644 index 0000000..f146b6b --- /dev/null +++ b/manager/src/main/java/com/usthe/manager/pojo/dto/DingTalkWebHookDto.java @@ -0,0 +1,45 @@ +package com.usthe.manager.pojo.dto; + +import lombok.AllArgsConstructor; +import lombok.Builder; +import lombok.Data; +import lombok.NoArgsConstructor; + +/** + * 钉钉机器人请求消息体 + * @author 花城 + * @version 1.0 + * @date 2022/2/21 6:55 下午 + */ +@Data +@Builder +@AllArgsConstructor +@NoArgsConstructor +public class DingTalkWebHookDto { + + public static final String WEBHOOK_URL = "https://oapi.dingtalk.com/robot/send?access_token="; + private static final String MARKDOWN = "markdown"; + + /** + * 消息类型 + */ + private String msgtype = MARKDOWN; + + /** + * markdown消息 + */ + private MarkdownDTO markdown; + + @Data + public static class MarkdownDTO { + /** + * 消息内容 + */ + private String text; + /** + * 消息标题 + */ + private String title; + } + +} diff --git a/web-app/src/app/routes/alert/alert-notice/alert-notice.component.html b/web-app/src/app/routes/alert/alert-notice/alert-notice.component.html index fe11175..7313a7e 100644 --- a/web-app/src/app/routes/alert/alert-notice/alert-notice.component.html +++ b/web-app/src/app/routes/alert/alert-notice/alert-notice.component.html @@ -64,6 +64,10 @@ 企业微信机器人 + + + 钉钉机器人 + 飞书机器人 @@ -75,6 +79,7 @@ {{ data.hookUrl }} {{ data.wechatId }} {{ data.wechatId }} + {{ data.accessToken }} {{ data.wechatId }} {{ data.gmtUpdate ? data.gmtUpdate : data.gmtCreate }} @@ -181,6 +186,7 @@ + @@ -223,6 +229,12 @@ + + 机器人ACCESS_TOKEN + + + + 飞书机器人KEY diff --git a/web-app/src/app/routes/alert/alert-notice/alert-notice.component.ts b/web-app/src/app/routes/alert/alert-notice/alert-notice.component.ts index 862bfea..0268457 100644 --- a/web-app/src/app/routes/alert/alert-notice/alert-notice.component.ts +++ b/web-app/src/app/routes/alert/alert-notice/alert-notice.component.ts @@ -262,6 +262,9 @@ export class AlertNoticeComponent implements OnInit { case 4: label = `${label}WeWork`; break; + case 5: + label = `${label}DingDing`; + break; case 6: label = `${label}FeiShu`; break;