Merge remote-tracking branch 'origin/master'

# Conflicts:
#	manager/src/main/java/com/usthe/manager/component/alerter/DispatchAlarm.java
This commit is contained in:
chenghua
2022-02-22 11:29:57 +08:00
7 changed files with 105 additions and 36 deletions

View File

@@ -46,9 +46,9 @@ public class NoticeReceiver {
@NotNull
private String name;
@ApiModelProperty(value = "通知信息方式: 0-手机短信 1-邮箱 2-webhook 3-微信公众号", accessMode = READ_WRITE, position = 2)
@ApiModelProperty(value = "通知信息方式: 0-手机短信 1-邮箱 2-webhook 3-微信公众号 4-企业微信机器人 5-钉钉机器人", accessMode = READ_WRITE, position = 2)
@Min(0)
@Max(3)
@Max(8)
@NotNull
private Byte type;
@@ -61,13 +61,17 @@ public class NoticeReceiver {
private String email;
@ApiModelProperty(value = "URL地址, 通知方式为webhook有效", example = "https://www.tancloud.cn", accessMode = READ_WRITE, position = 5)
@Length(max = 100)
@Length(max = 300)
private String hookUrl;
@ApiModelProperty(value = "wechat用户openId, 通知方式为微信公众号有效", example = "343432", accessMode = READ_WRITE, position = 6)
@Length(max = 100)
@ApiModelProperty(value = "openId, 通知方式为微信公众号或企业微信机器人有效", example = "343432", accessMode = READ_WRITE, position = 6)
@Length(max = 300)
private String wechatId;
@ApiModelProperty(value = "访问token, 通知方式为钉钉机器人有效", example = "34823984635647", accessMode = READ_WRITE, position = 7)
@Length(max = 300)
private String accessToken;
@ApiModelProperty(value = "此条记录创建者", example = "tom", accessMode = READ_ONLY, position = 7)
private String creator;

View File

@@ -1,14 +1,11 @@
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.util.PriorityLevelEnum;
import com.usthe.common.entity.alerter.Alert;
import com.usthe.alert.service.AlertService;
import com.usthe.common.entity.dto.WeChatWebHookDTO;
import com.usthe.manager.pojo.dto.WeWorkWebHookDTO;
import com.usthe.common.util.CommonConstants;
import com.usthe.common.entity.manager.Monitor;
import com.usthe.common.entity.manager.NoticeReceiver;
@@ -126,7 +123,7 @@ public class DispatchAlarm {
case 1: sendEmailAlert(receiver, alert); break;
case 2: sendWebHookAlert(receiver, alert); break;
case 3: sendWeChatAlert(receiver, alert); break;
case 4: sendWeChatWebHookAlert(receiver, alert);break;
case 4: sendWeWorkRobotAlert(receiver, alert);break;
default: break;
}
}
@@ -137,26 +134,37 @@ public class DispatchAlarm {
* @param receiver 通知配置信息
* @param alert 告警信息
*/
private void sendWeChatWebHookAlert(NoticeReceiver receiver, Alert alert) {
WeChatWebHookDTO weChatWebHookDTO = new WeChatWebHookDTO();
weChatWebHookDTO.setMsgtype("markdown");
WeChatWebHookDTO.MarkdownDTO markdownDTO = new WeChatWebHookDTO.MarkdownDTO();
private void sendWeWorkRobotAlert(NoticeReceiver receiver, Alert alert) {
WeWorkWebHookDTO weWorkWebHookDTO = new WeWorkWebHookDTO();
WeWorkWebHookDTO.MarkdownDTO markdownDTO = new WeWorkWebHookDTO.MarkdownDTO();
StringBuilder content = new StringBuilder();
content.append("\t\t\t\t<font color=\"info\">[TanCloud探云告警]</font>\n" +
"告警目标对象 : <font color=\"info\">" + alert.getTarget() + "</font>\n" +
"所属监控ID : " + alert.getMonitorId() + "\n" +
"所属监控名称 : " + alert.getMonitorName() + "\n");
if (alert.getPriority() < PriorityLevelEnum.WARNING.getLevel()){
content.append("告警级别 <font color=\"warning\">: " + CommonUtil.transferAlertPriority(alert.getPriority()) + "</font>\n");
content.append("<font color=\"info\">[TanCloud探云告警通知]</font>\n告警目标对象 : <font color=\"info\">")
.append(alert.getTarget()).append("</font>\n")
.append("所属监控ID : ").append(alert.getMonitorId()).append("\n")
.append("所属监控名称 : ").append(alert.getMonitorName()).append("\n");
if (alert.getPriority() < CommonConstants.ALERT_PRIORITY_CODE_WARNING) {
content.append("告警级别 : <font color=\"warning\">")
.append(CommonUtil.transferAlertPriority(alert.getPriority())).append("</font>\n");
}else {
content.append("告警级别 <font color=\"comment\">: " + CommonUtil.transferAlertPriority(alert.getPriority()) + "</font>\n");
content.append("告警级别 : <font color=\"comment\">")
.append(CommonUtil.transferAlertPriority(alert.getPriority())).append("</font>\n");
}
content.append("内容详情 : " + alert.getContent());
content.append("内容详情 : ").append(alert.getContent());
markdownDTO.setContent(content.toString());
weChatWebHookDTO.setMarkdown(markdownDTO);
//TODO 以后转移到实体类中
String webHookUrl = "https://qyapi.weixin.qq.com/cgi-bin/webhook/send?key=fcf9ddxxx-ebaf-48a2-810c-404xxxxxxd3bf";
HttpUtils.sendPostJsonBody(webHookUrl, JSON.toJSONString(weChatWebHookDTO));
weWorkWebHookDTO.setMarkdown(markdownDTO);
String webHookUrl = WeWorkWebHookDTO.WEBHOOK_URL + receiver.getWechatId();
try {
ResponseEntity<String> entity = restTemplate.postForEntity(webHookUrl, weWorkWebHookDTO, 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);
}
}
private void sendWeChatAlert(NoticeReceiver receiver, Alert alert) {

View File

@@ -0,0 +1,41 @@
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 WeWorkWebHookDTO {
public static final String WEBHOOK_URL = "https://qyapi.weixin.qq.com/cgi-bin/webhook/send?key=";
private static final String MARKDOWN = "markdown";
/**
* 消息类型
*/
private String msgtype = MARKDOWN;
/**
* markdown消息
*/
private MarkdownDTO markdown;
@Data
public static class MarkdownDTO {
/**
* 消息内容
*/
private String content;
}
}

View File

@@ -148,11 +148,12 @@ CREATE TABLE notice_receiver
(
id bigint not null auto_increment comment '消息接收人ID',
name varchar(100) not null comment '消息接收人姓名',
type tinyint not null comment '通知信息方式: 0-手机短信 1-邮箱 2-webhook 3-微信公众号',
type tinyint not null comment '通知信息方式: 0-手机短信 1-邮箱 2-webhook 3-微信公众号 4-企业微信机器人 5-钉钉机器人',
phone varchar(100) comment '手机号, 通知方式为手机短信时有效',
email varchar(100) comment '邮箱账号, 通知方式为邮箱时有效',
hook_url varchar(100) comment 'URL地址, 通知方式为webhook有效',
wechat_id varchar(100) comment 'wechat用户openId, 通知方式为微信公众号有效',
hook_url varchar(255) comment 'URL地址, 通知方式为webhook有效',
wechat_id varchar(255) comment 'openId, 通知方式为微信公众号或企业微信机器人有效',
access_token varchar(255) comment '访问token, 通知方式为钉钉机器人有效',
creator varchar(100) comment '创建者',
modifier varchar(100) comment '最新修改者',
gmt_create timestamp default current_timestamp comment 'create time',

View File

@@ -7,6 +7,7 @@ export class NoticeReceiver {
email!: string;
hookUrl!: string;
wechatId!: string;
accessToken!: string;
creator!: string;
modifier!: string;
gmtCreate!: number;

View File

@@ -60,12 +60,17 @@
<i nz-icon nzType="notification" nzTheme="outline"></i>
<span>微信公众号</span>
</nz-tag>
<nz-tag *ngIf="data.type == 4" nzColor="orange">
<i nz-icon nzType="notification" nzTheme="outline"></i>
<span>企业微信机器人</span>
</nz-tag>
</td>
<td nzAlign="center">
<span *ngIf="data.type == 0">{{ data.phone }}</span>
<span *ngIf="data.type == 1">{{ data.email }}</span>
<span *ngIf="data.type == 2">{{ data.hookUrl }}</span>
<span *ngIf="data.type == 3">{{ data.wechatId }}</span>
<span *ngIf="data.type == 4">{{ data.wechatId }}</span>
</td>
<td nzAlign="center">{{ data.gmtUpdate ? data.gmtUpdate : data.gmtCreate }}</td>
<td nzAlign="center">
@@ -158,24 +163,25 @@
<form nz-form #receiverForm="ngForm">
<nz-form-item>
<nz-form-label [nzSpan]="7" nzFor="name" nzRequired="true">接收人名称</nz-form-label>
<nz-form-control [nzSpan]="8" [nzErrorTip]="'validation.required' | i18n">
<nz-form-control [nzSpan]="12" [nzErrorTip]="'validation.required' | i18n">
<input [(ngModel)]="receiver.name" nz-input required name="name" type="text" id="name" />
</nz-form-control>
</nz-form-item>
<nz-form-item>
<nz-form-label nzSpan="7" nzRequired="true" nzFor="type">通知方式 </nz-form-label>
<nz-form-control nzSpan="8" [nzErrorTip]="'validation.required' | i18n">
<nz-form-control nzSpan="12" [nzErrorTip]="'validation.required' | i18n">
<nz-select [(ngModel)]="receiver.type" nzPlaceHolder="Choose" required name="type" id="type">
<nz-option [nzValue]="0" nzDisabled nzLabel="短信"></nz-option>
<nz-option [nzValue]="1" nzLabel="邮箱"></nz-option>
<nz-option [nzValue]="2" nzLabel="WebHook"></nz-option>
<nz-option [nzValue]="3" nzDisabled nzLabel="微信公众号"></nz-option>
<nz-option [nzValue]="4" nzLabel="企业微信机器人"></nz-option>
</nz-select>
</nz-form-control>
</nz-form-item>
<nz-form-item *ngIf="receiver.type === 0">
<nz-form-label [nzSpan]="7" nzFor="phone" [nzRequired]="receiver.type === 0">手机号</nz-form-label>
<nz-form-control [nzSpan]="8" [nzErrorTip]="'validation.phone.invalid' | i18n">
<nz-form-control [nzSpan]="12" [nzErrorTip]="'validation.phone.invalid' | i18n">
<input
[(ngModel)]="receiver.phone"
nz-input
@@ -189,20 +195,26 @@
</nz-form-item>
<nz-form-item *ngIf="receiver.type === 1">
<nz-form-label [nzSpan]="7" nzFor="email" [nzRequired]="receiver.type === 1">邮箱</nz-form-label>
<nz-form-control [nzSpan]="8" [nzErrorTip]="'validation.email.invalid' | i18n">
<nz-form-control [nzSpan]="12" [nzErrorTip]="'validation.email.invalid' | i18n">
<input [(ngModel)]="receiver.email" nz-input [required]="receiver.type === 1" email name="email" type="email" id="email" />
</nz-form-control>
</nz-form-item>
<nz-form-item *ngIf="receiver.type === 2">
<nz-form-label [nzSpan]="7" nzFor="hookUrl" [nzRequired]="receiver.type === 2">URL地址</nz-form-label>
<nz-form-control [nzSpan]="8" [nzErrorTip]="'validation.required' | i18n">
<nz-form-control [nzSpan]="12" [nzErrorTip]="'validation.required' | i18n">
<input [(ngModel)]="receiver.hookUrl" nz-input [required]="receiver.type === 2" name="hookUrl" type="url" id="hookUrl" />
</nz-form-control>
</nz-form-item>
<nz-form-item *ngIf="receiver.type === 3">
<nz-form-label [nzSpan]="7" nzFor="wechatId" [nzRequired]="receiver.type === 3">微信OPENID</nz-form-label>
<nz-form-control [nzSpan]="8" [nzErrorTip]="'validation.required' | i18n">
<input [(ngModel)]="receiver.wechatId" nz-input [required]="receiver.type === 3" name="wechatId" type="text" id="wechatId" />
<nz-form-control [nzSpan]="12" [nzErrorTip]="'validation.required' | i18n">
<input [(ngModel)]="receiver.wechatId" nz-input [required]="receiver.type === 3" name="wechatId" type="text" />
</nz-form-control>
</nz-form-item>
<nz-form-item *ngIf="receiver.type === 4">
<nz-form-label [nzSpan]="7" nzFor="wechatId" [nzRequired]="receiver.type === 4">企业微信机器人KEY</nz-form-label>
<nz-form-control [nzSpan]="12" [nzErrorTip]="'validation.required' | i18n">
<input [(ngModel)]="receiver.wechatId" nz-input [required]="receiver.type === 4" name="wechatId" type="text" />
</nz-form-control>
</nz-form-item>
</form>

View File

@@ -259,6 +259,8 @@ export class AlertNoticeComponent implements OnInit {
case 3:
label = `${label}WeChat`;
break;
case 4:
label = `${label}WeWorkRobot`;
}
this.receiversOption.push({
value: item.id,