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 08a191654b2..0c64797af8b 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 @@ -68,8 +68,8 @@ public class NoticeReceiver { @NotNull private String name; - @Schema(title = "Notification information method: 0-SMS 1-Email 2-webhook 3-WeChat Official Account 4-Enterprise WeChat Robot 5-DingTalk Robot 6-FeiShu Robot 7-Telegram Bot", - description = "通知信息方式: 0-手机短信 1-邮箱 2-webhook 3-微信公众号 4-企业微信机器人 5-钉钉机器人 6-飞书机器人 7-Telegram机器人", + @Schema(title = "Notification information method: 0-SMS 1-Email 2-webhook 3-WeChat Official Account 4-Enterprise WeChat Robot 5-DingTalk Robot 6-FeiShu Robot 7-Telegram Bot 8-SlackWebHook", + description = "通知信息方式: 0-手机短信 1-邮箱 2-webhook 3-微信公众号 4-企业微信机器人 5-钉钉机器人 6-飞书机器人 7-Telegram机器人 8-SlackWebHook", accessMode = READ_WRITE) @Min(0) @Max(8) @@ -116,6 +116,12 @@ public class NoticeReceiver { example = "779294123", accessMode = READ_WRITE) private String tgUserId; + @Schema(title = "URL address: The notification method is valid for Slack", + description = "URL地址 : 通知方式为Slack有效", + example = "https://hooks.slack.com/services/XXXX/XXXX/XXXX", accessMode = READ_WRITE) + @Length(max = 300) + private String slackWebHookUrl; + @Schema(title = "The creator of this record", description = "此条记录创建者", example = "tom", accessMode = READ_ONLY) @CreatedBy diff --git a/common/src/main/java/com/usthe/common/util/GsonUtil.java b/common/src/main/java/com/usthe/common/util/GsonUtil.java index 66a5131fbb0..a6b3071ab37 100644 --- a/common/src/main/java/com/usthe/common/util/GsonUtil.java +++ b/common/src/main/java/com/usthe/common/util/GsonUtil.java @@ -50,8 +50,8 @@ public static T fromJson(String jsonStr, Class clazz) { return gson.fromJson(jsonStr, clazz); } - public static T fromJson(String jsonStr, Type typeOfT) { - return gson.fromJson(jsonStr, typeOfT); + public static T fromJson(String jsonStr, Type type) { + return gson.fromJson(jsonStr, type); } public static T fromJson(JsonElement element, Class clazz) { diff --git a/home/versioned_docs/version-v1.0.0/start/package-deploy.md b/home/versioned_docs/version-v1.0.0/start/package-deploy.md index 9804096a773..efdb9963a4f 100644 --- a/home/versioned_docs/version-v1.0.0/start/package-deploy.md +++ b/home/versioned_docs/version-v1.0.0/start/package-deploy.md @@ -13,9 +13,9 @@ sidebar_label: 安装包方式部署 安装后命令行检查是否成功安装 ``` $ java -version - openjdk version "1.8.0_312" - OpenJDK Runtime Environment (Zulu 8.58.0.13-CA-macos-aarch64) (build 1.8.0_312-b07) - OpenJDK 64-Bit Server VM (Zulu 8.58.0.13-CA-macos-aarch64) (build 25.312-b07, mixed mode) + openjdk version "11.0.11" 2021-04-20 + OpenJDK Runtime Environment AdoptOpenJDK-11.0.11+9 (build 11.0.11+9) + OpenJDK 64-Bit Server VM AdoptOpenJDK-11.0.11+9 (build 11.0.11+9, mixed mode) ``` 2. 下载HertzBeat安装包 下载您系统环境对应的安装包 diff --git a/manager/src/main/java/com/usthe/manager/component/alerter/impl/AbstractAlertNotifyHandlerImpl.java b/manager/src/main/java/com/usthe/manager/component/alerter/impl/AbstractAlertNotifyHandlerImpl.java index d7384566cc7..21e5b5eaec9 100644 --- a/manager/src/main/java/com/usthe/manager/component/alerter/impl/AbstractAlertNotifyHandlerImpl.java +++ b/manager/src/main/java/com/usthe/manager/component/alerter/impl/AbstractAlertNotifyHandlerImpl.java @@ -34,7 +34,7 @@ abstract class AbstractAlertNotifyHandlerImpl implements AlertNotifyHandler { @Resource protected AlerterProperties alerterProperties; - protected String renderContext(Alert alert) { + protected String renderContent(Alert alert) { Context context = new Context(); context.setVariable("title", "[" + bundle.getString("alerter.notify.title") + "]"); if (alert.getTags() != null) { diff --git a/manager/src/main/java/com/usthe/manager/component/alerter/impl/DingTalkRobotAlertNotifyHandlerImpl.java b/manager/src/main/java/com/usthe/manager/component/alerter/impl/DingTalkRobotAlertNotifyHandlerImpl.java index ff085f4d7e4..07e7ca4fadb 100644 --- a/manager/src/main/java/com/usthe/manager/component/alerter/impl/DingTalkRobotAlertNotifyHandlerImpl.java +++ b/manager/src/main/java/com/usthe/manager/component/alerter/impl/DingTalkRobotAlertNotifyHandlerImpl.java @@ -44,7 +44,7 @@ public void send(NoticeReceiver receiver, Alert alert) { try { DingTalkWebHookDto dingTalkWebHookDto = new DingTalkWebHookDto(); MarkdownDTO markdownDTO = new MarkdownDTO(); - markdownDTO.setText(renderContext(alert)); + markdownDTO.setText(renderContent(alert)); markdownDTO.setTitle(bundle.getString("alerter.notify.title")); dingTalkWebHookDto.setMarkdown(markdownDTO); String webHookUrl = alerterProperties.getDingTalkWebHookUrl() + receiver.getAccessToken(); diff --git a/manager/src/main/java/com/usthe/manager/component/alerter/impl/FlyBookAlertNotifyHandlerImpl.java b/manager/src/main/java/com/usthe/manager/component/alerter/impl/FlyBookAlertNotifyHandlerImpl.java index 7f6eb30dccf..d83ba523e08 100644 --- a/manager/src/main/java/com/usthe/manager/component/alerter/impl/FlyBookAlertNotifyHandlerImpl.java +++ b/manager/src/main/java/com/usthe/manager/component/alerter/impl/FlyBookAlertNotifyHandlerImpl.java @@ -57,7 +57,7 @@ public void send(NoticeReceiver receiver, Alert alert) { List contents1 = new ArrayList<>(); FlyBookContent flyBookContent = new FlyBookContent(); flyBookContent.setTag("text"); - flyBookContent.setText(renderContext(alert)); + flyBookContent.setText(renderContent(alert)); contents1.add(flyBookContent); FlyBookContent bookContent = new FlyBookContent(); bookContent.setTag("a"); diff --git a/manager/src/main/java/com/usthe/manager/component/alerter/impl/SlackAlertNotifyHandlerImpl.java b/manager/src/main/java/com/usthe/manager/component/alerter/impl/SlackAlertNotifyHandlerImpl.java new file mode 100644 index 00000000000..727a5030314 --- /dev/null +++ b/manager/src/main/java/com/usthe/manager/component/alerter/impl/SlackAlertNotifyHandlerImpl.java @@ -0,0 +1,72 @@ +package com.usthe.manager.component.alerter.impl; + +import com.usthe.common.entity.alerter.Alert; +import com.usthe.common.entity.manager.NoticeReceiver; +import com.usthe.manager.support.exception.AlertNoticeException; +import lombok.Builder; +import lombok.Data; +import lombok.RequiredArgsConstructor; +import lombok.extern.slf4j.Slf4j; +import org.springframework.http.HttpStatus; +import org.springframework.stereotype.Component; +import org.springframework.web.client.RestTemplate; + +import java.util.Objects; + +/** + * Send alarm information by Slack Webhook + * 通过Slack Webhook发送告警信息 + * + * @author gcdd1993 + * @version 2.1 + * Created by Musk.Chen on 2023/1/17 + */ +@Component +@RequiredArgsConstructor +@Slf4j +final class SlackAlertNotifyHandlerImpl extends AbstractAlertNotifyHandlerImpl { + private static final String SUCCESS = "ok"; + private final RestTemplate restTemplate; + + @Override + public void send(NoticeReceiver receiver, Alert alert) throws AlertNoticeException { + try { + var slackNotify = SlackNotifyDTO.builder() + .text(renderContent(alert)) + .build(); + + var entity = restTemplate.postForEntity(receiver.getSlackWebHookUrl(), slackNotify, String.class); + if (entity.getStatusCode() == HttpStatus.OK && entity.getBody() != null) { + var body = entity.getBody(); + if (Objects.equals(SUCCESS, body)) { + log.debug("Send Slack Success"); + } else { + log.warn("Send Slack Failed: {}", body); + throw new AlertNoticeException(body); + } + } else { + log.warn("Send Slack Failed {}", entity.getBody()); + throw new AlertNoticeException("Http StatusCode " + entity.getStatusCode()); + } + } catch (Exception e) { + throw new AlertNoticeException("[Slack Notify Error] " + e.getMessage()); + } + } + + @Override + public byte type() { + return 8; + } + + @Override + protected String templateName() { + return "alertNotifySlack"; + } + + @Data + @Builder + private static class SlackNotifyDTO { + private String text; + } + +} diff --git a/manager/src/main/java/com/usthe/manager/component/alerter/impl/TelegramBotAlertNotifyHandlerImpl.java b/manager/src/main/java/com/usthe/manager/component/alerter/impl/TelegramBotAlertNotifyHandlerImpl.java index 2c2d90b6ff6..156a5e85f75 100644 --- a/manager/src/main/java/com/usthe/manager/component/alerter/impl/TelegramBotAlertNotifyHandlerImpl.java +++ b/manager/src/main/java/com/usthe/manager/component/alerter/impl/TelegramBotAlertNotifyHandlerImpl.java @@ -35,7 +35,7 @@ public void send(NoticeReceiver receiver, Alert alert) throws AlertNoticeExcepti String url = String.format(alerterProperties.getTelegramBotApiUrl(), receiver.getTgBotToken()); TelegramBotNotifyDTO notifyBody = TelegramBotNotifyDTO.builder() .chatId(receiver.getTgUserId()) - .text(renderContext(alert)) + .text(renderContent(alert)) .disableWebPagePreview(true) .build(); ResponseEntity entity = restTemplate.postForEntity(url, notifyBody, TelegramBotNotifyResponse.class); diff --git a/manager/src/main/java/com/usthe/manager/component/alerter/impl/WeWorkRobotAlertNotifyHandlerImpl.java b/manager/src/main/java/com/usthe/manager/component/alerter/impl/WeWorkRobotAlertNotifyHandlerImpl.java index 3a33716f6e3..ba7338fb6f1 100644 --- a/manager/src/main/java/com/usthe/manager/component/alerter/impl/WeWorkRobotAlertNotifyHandlerImpl.java +++ b/manager/src/main/java/com/usthe/manager/component/alerter/impl/WeWorkRobotAlertNotifyHandlerImpl.java @@ -44,7 +44,7 @@ public void send(NoticeReceiver receiver, Alert alert) { try { WeWorkWebHookDto weWorkWebHookDTO = new WeWorkWebHookDto(); WeWorkWebHookDto.MarkdownDTO markdownDTO = new WeWorkWebHookDto.MarkdownDTO(); - markdownDTO.setContent(renderContext(alert)); + markdownDTO.setContent(renderContent(alert)); weWorkWebHookDTO.setMarkdown(markdownDTO); String webHookUrl = alerterProperties.getWeWorkWebHookUrl() + receiver.getWechatId(); ResponseEntity entity = restTemplate.postForEntity(webHookUrl, weWorkWebHookDTO, CommonRobotNotifyResp.class); diff --git a/manager/src/main/resources/templates/alertNotifySlack.txt b/manager/src/main/resources/templates/alertNotifySlack.txt new file mode 100644 index 00000000000..5633717fa58 --- /dev/null +++ b/manager/src/main/resources/templates/alertNotifySlack.txt @@ -0,0 +1,7 @@ +*[(${title})]* +[(${targetLabel})] : [(${target})] +[# th:if="${monitorId != null}"][(${monitorIdLabel})] : `[(${monitorId})]`[/] +[# th:if="${monitorName != null}"][(${monitorNameLabel})] : [(${monitorName})][/] +[(${priorityLabel})] : [(${priority})] +[(${triggerTimeLabel})] : [(${triggerTime})] +[(${contentLabel})] : [(${content})] \ No newline at end of file diff --git a/manager/src/test/java/com/usthe/manager/component/alerter/impl/SlackAlertNotifyHandlerImplTest.java b/manager/src/test/java/com/usthe/manager/component/alerter/impl/SlackAlertNotifyHandlerImplTest.java new file mode 100644 index 00000000000..a9ff6181fc0 --- /dev/null +++ b/manager/src/test/java/com/usthe/manager/component/alerter/impl/SlackAlertNotifyHandlerImplTest.java @@ -0,0 +1,52 @@ +package com.usthe.manager.component.alerter.impl; + +import com.usthe.common.entity.alerter.Alert; +import com.usthe.common.entity.manager.NoticeReceiver; +import com.usthe.common.util.CommonConstants; +import com.usthe.manager.AbstractSpringIntegrationTest; +import lombok.extern.slf4j.Slf4j; +import org.junit.jupiter.api.Test; +import org.springframework.util.StringUtils; + +import javax.annotation.Resource; +import java.util.Map; + +/** + * Test case for {@link SlackAlertNotifyHandlerImpl} + * + * @author gcdd1993 + * @version 2.1 + * Created by Musk.Chen on 2023/1/17 + */ +@Slf4j +class SlackAlertNotifyHandlerImplTest extends AbstractSpringIntegrationTest { + + @Resource + private SlackAlertNotifyHandlerImpl slackAlertNotifyHandler; + + @Test + void send() { + var slackWebHook = System.getenv("SLACK_WEB_HOOK"); + if (!StringUtils.hasText(slackWebHook)) { + log.warn("Please provide environment variables SLACK_WEB_HOOK"); + return; + } + var receiver = new NoticeReceiver(); + receiver.setId(1L); + receiver.setName("Mock 告警"); + receiver.setSlackWebHookUrl(slackWebHook); + var alert = new Alert(); + alert.setId(1L); + alert.setTarget("Mock Target"); + var map = Map.of( + CommonConstants.TAG_MONITOR_ID, "Mock monitor id", + CommonConstants.TAG_MONITOR_NAME, "Mock monitor name" + ); + alert.setTags(map); + alert.setContent("mock content"); + alert.setPriority((byte) 0); + alert.setLastTriggerTime(System.currentTimeMillis()); + + slackAlertNotifyHandler.send(receiver, alert); + } +} \ No newline at end of file diff --git a/pom.xml b/pom.xml index 67451a0a3e9..8f23fe453f0 100644 --- a/pom.xml +++ b/pom.xml @@ -17,9 +17,9 @@ - 1.8 - 1.8 - 1.8 + 11 + 11 + 11 UTF-8 1.7.21 2.3.0 @@ -94,7 +94,7 @@ org.apache.maven.plugins maven-pmd-plugin - 3.8 + 3.12.0 false @@ -124,7 +124,7 @@ com.alibaba.p3c p3c-pmd - 1.3.0 + 2.0.0 diff --git a/web-app/src/app/pojo/NoticeReceiver.ts b/web-app/src/app/pojo/NoticeReceiver.ts index fbac1216d8b..730d914ca88 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; tgBotToken!: string; tgUserId!: string; + slackWebHookUrl!: string; hookUrl!: string; wechatId!: string; accessToken!: string; 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 90129fe7f4f..ac80db81f0e 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 @@ -86,6 +86,10 @@ {{ 'alert.notice.type.telegram-bot' | i18n }} + + + {{ 'alert.notice.type.slack' | i18n }} + {{ data.phone }} @@ -96,6 +100,7 @@ {{ data.accessToken }} {{ data.wechatId }} {{ data.tgBotToken }}
{{ data.tgUserId }}
+ {{ data.slackWebHookUrl }} {{ (data.gmtUpdate ? data.gmtUpdate : data.gmtCreate) | date : 'YYYY-MM-dd HH:mm:ss' }} @@ -225,6 +230,7 @@ + @@ -291,7 +297,15 @@ 'alert.notice.type.telegram-bot-user-id' | i18n }} - + + + + + {{ + 'alert.notice.type.slack-webHook-url' | i18n + }} + + diff --git a/web-app/src/assets/i18n/en-US.json b/web-app/src/assets/i18n/en-US.json index 9c84aea6fbe..47868c29322 100644 --- a/web-app/src/assets/i18n/en-US.json +++ b/web-app/src/assets/i18n/en-US.json @@ -174,6 +174,8 @@ "alert.notice.type.telegram-bot": "Telegram Bot", "alert.notice.type.telegram-bot-token": "Telegram Bot Token", "alert.notice.type.telegram-bot-user-id": "Telegram User Id", + "alert.notice.type.slack": "Slack WebHook", + "alert.notice.type.slack-webHook-url": "Slack WebHook Url", "alert.notice.rule": "Alert Notice Policy", "alert.notice.rule.new": "New Notice Policy", "alert.notice.rule.edit": "Edit Notice Policy", diff --git a/web-app/src/assets/i18n/zh-CN.json b/web-app/src/assets/i18n/zh-CN.json index 5c7953a9457..b8ba8d1d94d 100644 --- a/web-app/src/assets/i18n/zh-CN.json +++ b/web-app/src/assets/i18n/zh-CN.json @@ -174,6 +174,8 @@ "alert.notice.type.telegram-bot": "Telegram机器人", "alert.notice.type.telegram-bot-token": "Telegram Bot Token", "alert.notice.type.telegram-bot-user-id": "Telegram User Id", + "alert.notice.type.slack": "Slack WebHook", + "alert.notice.type.slack-webHook-url": "Slack WebHook地址", "alert.notice.rule": "告警通知策略", "alert.notice.rule.new": "新增通知策略", "alert.notice.rule.edit": "编辑通知策略", diff --git a/web-app/src/assets/i18n/zh-TW.json b/web-app/src/assets/i18n/zh-TW.json index d15d02bd1de..287e11b2f94 100644 --- a/web-app/src/assets/i18n/zh-TW.json +++ b/web-app/src/assets/i18n/zh-TW.json @@ -174,6 +174,8 @@ "alert.notice.type.telegram-bot": "Telegram機器人", "alert.notice.type.telegram-bot-token": "Telegram Bot Token", "alert.notice.type.telegram-bot-user-id": "Telegram User Id", + "alert.notice.type.slack": "Slack WebHook", + "alert.notice.type.slack-webHook-url": "Slack WebHook地址", "alert.notice.rule": "告警通知策略", "alert.notice.rule.new": "新增通知策略", "alert.notice.rule.edit": "編輯通知策略",