Jelajahi Sumber

[manager,webapp]支持钉钉机器人通知告警信息

tomsun28 4 tahun lalu
induk
melakukan
e1d34c925b

+ 35 - 0
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;
             }
@@ -181,6 +183,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<String> 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  通知配置信息
      * @param alert     告警信息

+ 45 - 0
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;
+    }
+
+}

+ 12 - 0
web-app/src/app/routes/alert/alert-notice/alert-notice.component.html

@@ -64,6 +64,10 @@
               <i nz-icon nzType="notification" nzTheme="outline"></i>
               <span>企业微信机器人</span>
             </nz-tag>
+            <nz-tag *ngIf="data.type == 5" nzColor="orange">
+              <i nz-icon nzType="notification" nzTheme="outline"></i>
+              <span>钉钉机器人</span>
+            </nz-tag>
             <nz-tag *ngIf="data.type == 6" nzColor="orange">
               <i nz-icon nzType="notification" nzTheme="outline"></i>
               <span>飞书机器人</span>
@@ -75,6 +79,7 @@
             <span *ngIf="data.type == 2">{{ data.hookUrl }}</span>
             <span *ngIf="data.type == 3">{{ data.wechatId }}</span>
             <span *ngIf="data.type == 4">{{ data.wechatId }}</span>
+            <span *ngIf="data.type == 5">{{ data.accessToken }}</span>
             <span *ngIf="data.type == 6">{{ data.wechatId }}</span>
           </td>
           <td nzAlign="center">{{ data.gmtUpdate ? data.gmtUpdate : data.gmtCreate }}</td>
@@ -181,6 +186,7 @@
             <nz-option [nzValue]="2" nzLabel="WebHook"></nz-option>
             <nz-option [nzValue]="3" nzDisabled nzLabel="微信公众号"></nz-option>
             <nz-option [nzValue]="4" nzLabel="企业微信机器人"></nz-option>
+            <nz-option [nzValue]="5" nzLabel="钉钉机器人"></nz-option>
             <nz-option [nzValue]="6" nzLabel="飞书机器人"></nz-option>
           </nz-select>
         </nz-form-control>
@@ -223,6 +229,12 @@
           <input [(ngModel)]="receiver.wechatId" nz-input [required]="receiver.type === 4" name="wechatId" type="text" />
         </nz-form-control>
       </nz-form-item>
+      <nz-form-item *ngIf="receiver.type === 5">
+        <nz-form-label [nzSpan]="7" nzFor="accessToken" [nzRequired]="receiver.type === 5">机器人ACCESS_TOKEN</nz-form-label>
+        <nz-form-control [nzSpan]="12" [nzErrorTip]="'validation.required' | i18n">
+          <input [(ngModel)]="receiver.accessToken" nz-input [required]="receiver.type === 5" name="accessToken" type="text" />
+        </nz-form-control>
+      </nz-form-item>
       <nz-form-item *ngIf="receiver.type === 6">
         <nz-form-label [nzSpan]="7" nzFor="wechatId" [nzRequired]="receiver.type === 6">飞书机器人KEY</nz-form-label>
         <nz-form-control [nzSpan]="12" [nzErrorTip]="'validation.required' | i18n">

+ 3 - 0
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;