diff --git a/common/src/main/java/com/usthe/common/entity/manager/NoticeReceiver.java b/common/src/main/java/com/usthe/common/entity/manager/NoticeReceiver.java index d5353d4..7f91223 100644 --- a/common/src/main/java/com/usthe/common/entity/manager/NoticeReceiver.java +++ b/common/src/main/java/com/usthe/common/entity/manager/NoticeReceiver.java @@ -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; 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 b8446d3..e0713be 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 @@ -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[TanCloud探云告警]\n" + - "告警目标对象 : " + alert.getTarget() + "\n" + - "所属监控ID : " + alert.getMonitorId() + "\n" + - "所属监控名称 : " + alert.getMonitorName() + "\n"); - if (alert.getPriority() < PriorityLevelEnum.WARNING.getLevel()){ - content.append("告警级别 : " + CommonUtil.transferAlertPriority(alert.getPriority()) + "\n"); + content.append("[TanCloud探云告警通知]\n告警目标对象 : ") + .append(alert.getTarget()).append("\n") + .append("所属监控ID : ").append(alert.getMonitorId()).append("\n") + .append("所属监控名称 : ").append(alert.getMonitorName()).append("\n"); + if (alert.getPriority() < CommonConstants.ALERT_PRIORITY_CODE_WARNING) { + content.append("告警级别 : ") + .append(CommonUtil.transferAlertPriority(alert.getPriority())).append("\n"); }else { - content.append("告警级别 : " + CommonUtil.transferAlertPriority(alert.getPriority()) + "\n"); + content.append("告警级别 : ") + .append(CommonUtil.transferAlertPriority(alert.getPriority())).append("\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 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) { diff --git a/manager/src/main/java/com/usthe/manager/pojo/dto/WeWorkWebHookDTO.java b/manager/src/main/java/com/usthe/manager/pojo/dto/WeWorkWebHookDTO.java new file mode 100644 index 0000000..4cd6738 --- /dev/null +++ b/manager/src/main/java/com/usthe/manager/pojo/dto/WeWorkWebHookDTO.java @@ -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; + } + +} diff --git a/script/sql/schema.sql b/script/sql/schema.sql index ccf5e4a..7b85496 100644 --- a/script/sql/schema.sql +++ b/script/sql/schema.sql @@ -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', diff --git a/web-app/src/app/pojo/NoticeReceiver.ts b/web-app/src/app/pojo/NoticeReceiver.ts index 0742ff1..67d2bc1 100644 --- a/web-app/src/app/pojo/NoticeReceiver.ts +++ b/web-app/src/app/pojo/NoticeReceiver.ts @@ -7,6 +7,7 @@ export class NoticeReceiver { email!: string; hookUrl!: string; wechatId!: string; + accessToken!: string; creator!: string; modifier!: string; gmtCreate!: number; 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 0561ba5..d0f0fab 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 @@ -60,12 +60,17 @@ 微信公众号 + + + 企业微信机器人 + {{ data.phone }} {{ data.email }} {{ data.hookUrl }} {{ data.wechatId }} + {{ data.wechatId }} {{ data.gmtUpdate ? data.gmtUpdate : data.gmtCreate }} @@ -158,24 +163,25 @@
接收人名称 - + 通知方式 - + + 手机号 - + 邮箱 - + URL地址 - + 微信OPENID - - + + + + + + 企业微信机器人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 1523ed1..6ecca0a 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 @@ -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,