diff --git a/alerter/src/main/java/org/dromara/hertzbeat/alert/AlerterProperties.java b/alerter/src/main/java/org/dromara/hertzbeat/alert/AlerterProperties.java index 03b85d7bfba..8018f0118ed 100644 --- a/alerter/src/main/java/org/dromara/hertzbeat/alert/AlerterProperties.java +++ b/alerter/src/main/java/org/dromara/hertzbeat/alert/AlerterProperties.java @@ -65,6 +65,11 @@ public class AlerterProperties { */ private String discordNotifyUrl = "https://discord.com/api/v9/channels/%s/messages"; + /** + * ServerChan Notify url + */ + private String serverChanNotifyUrl = "https://sctapi.ftqq.com/%s.send"; + /** * 告警评估时间间隔起始基数 每下一次乘2 单位毫秒 * base of alert eval interval time, unit:ms. The next time is 2 times the previous time. diff --git a/common/src/main/java/org/dromara/hertzbeat/common/entity/manager/NoticeReceiver.java b/common/src/main/java/org/dromara/hertzbeat/common/entity/manager/NoticeReceiver.java index e8e518d2964..228aa803cd9 100644 --- a/common/src/main/java/org/dromara/hertzbeat/common/entity/manager/NoticeReceiver.java +++ b/common/src/main/java/org/dromara/hertzbeat/common/entity/manager/NoticeReceiver.java @@ -188,6 +188,13 @@ public class NoticeReceiver { @Column(length = 300) private String smnTopicUrn; + @Schema(title = "serverChanToken : The notification method is valid for ServerChan", + description = "访问token : 通知方式为Server酱有效", + example = "SCT193569TSNm6xIabdjqeZPtOGOWcvU1e", accessMode = READ_WRITE) + @Length(max = 300) + @Column(length = 300) + private String serverChanToken; + @Schema(title = "The creator of this record", description = "此条记录创建者", example = "tom", accessMode = READ_ONLY) @CreatedBy diff --git a/manager/src/main/java/org/dromara/hertzbeat/manager/component/alerter/impl/ServerChanAlertNotifyHandlerImpl.java b/manager/src/main/java/org/dromara/hertzbeat/manager/component/alerter/impl/ServerChanAlertNotifyHandlerImpl.java new file mode 100644 index 00000000000..285dee06175 --- /dev/null +++ b/manager/src/main/java/org/dromara/hertzbeat/manager/component/alerter/impl/ServerChanAlertNotifyHandlerImpl.java @@ -0,0 +1,86 @@ +package org.dromara.hertzbeat.manager.component.alerter.impl; + +import lombok.Data; +import lombok.RequiredArgsConstructor; +import lombok.extern.slf4j.Slf4j; +import org.dromara.hertzbeat.common.entity.alerter.Alert; +import org.dromara.hertzbeat.common.entity.manager.NoticeReceiver; +import org.dromara.hertzbeat.manager.support.exception.AlertNoticeException; +import org.springframework.http.*; +import org.springframework.stereotype.Component; +/** + * + * @description Server酱发送 + */ +@Component +@RequiredArgsConstructor +@Slf4j +public class ServerChanAlertNotifyHandlerImpl extends AbstractAlertNotifyHandlerImpl{ + /** + * 发送报警通知 + * + * @param receiver Notification configuration information 通知配置信息 + * @param alert Alarm information 告警信息 + * @throws AlertNoticeException when send receiver error + */ + @Override + public void send(NoticeReceiver receiver, Alert alert) throws AlertNoticeException { + try { + ServerChanAlertNotifyHandlerImpl.ServerChanWebHookDto serverChanWebHookDto = new ServerChanAlertNotifyHandlerImpl.ServerChanWebHookDto(); + serverChanWebHookDto.setTitle(bundle.getString("alerter.notify.title")); + serverChanWebHookDto.setDesp(renderContent(alert)); + HttpHeaders headers = new HttpHeaders(); + headers.setContentType(MediaType.APPLICATION_JSON); + HttpEntity httpEntity = new HttpEntity<>(serverChanWebHookDto, headers); + String webHookUrl = String.format(alerterProperties.getServerChanNotifyUrl(),receiver.getServerChanToken()); + ResponseEntity responseEntity = restTemplate.postForEntity(webHookUrl, + httpEntity, CommonRobotNotifyResp.class); + System.out.println(responseEntity); + if (responseEntity.getStatusCode() == HttpStatus.OK) { + log.debug("Send ServerChan webHook: {} Success", webHookUrl); + } else { + log.warn("Send ServerChan webHook: {} Failed: {}", webHookUrl, responseEntity.getBody()); + throw new AlertNoticeException("Http StatusCode " + responseEntity.getStatusCode()); + } + } catch (Exception e) { + throw new AlertNoticeException("[ServerChan Notify Error] " + e.getMessage()); + } + } + + /** + * 通知类型 + * + * @return 通知类型 + */ + @Override + public byte type() { + return 12; + } + + /** + * Get the Thymeleaf template name + * 获取Thymeleaf模板名称 + * + * @return Thymeleaf模板名称 + */ + @Override + protected String templateName() { + return "alertNotifyServerChan"; + } + + @Data + private static class ServerChanWebHookDto { + private static final String MARKDOWN = "markdown"; + /** + * 标题 + */ + private String title; + /** + * markdown消息内容 + */ + private String desp; + + } + + +} diff --git a/manager/src/main/resources/templates/alertNotifyServerChan.txt b/manager/src/main/resources/templates/alertNotifyServerChan.txt new file mode 100644 index 00000000000..0e3fd4ef92c --- /dev/null +++ b/manager/src/main/resources/templates/alertNotifyServerChan.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/web-app/src/app/pojo/NoticeReceiver.ts b/web-app/src/app/pojo/NoticeReceiver.ts index 7cf1082b8d5..507c38a0629 100644 --- a/web-app/src/app/pojo/NoticeReceiver.ts +++ b/web-app/src/app/pojo/NoticeReceiver.ts @@ -2,7 +2,7 @@ export class NoticeReceiver { id!: number; name!: string; // 通知信息方式: 0-手机短信 1-邮箱 2-webhook 3-微信公众号 4-企业微信机器人 5-钉钉机器人 6-飞书机器人 - // 7-Telegram机器人 8-SlackWebHook 9-Discord机器人 10-企业微信应用消息 11-华为云SMN + // 7-Telegram机器人 8-SlackWebHook 9-Discord机器人 10-企业微信应用消息 11-华为云SMN 12-Server酱 type: number = 1; phone!: string; email!: string; @@ -22,6 +22,7 @@ export class NoticeReceiver { smnProjectId!: string; smnRegion!: string; smnTopicUrn!: string; + serverChanToken!: 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 f31b200a297..68af4f4d354 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 @@ -103,6 +103,10 @@ {{ 'alert.notice.type.smn' | i18n }} + + + {{ 'alert.notice.type.serverchan' | i18n }} + {{ data.phone }} @@ -117,6 +121,7 @@ {{ data.discordChannelId }}
{{ data.discordBotToken }}
{{ data.corpId }}
{{ data.agentId }}
{{ data.appSecret }}
{{ data.smnAk }} + {{ data.serverChanToken }} {{ (data.gmtUpdate ? data.gmtUpdate : data.gmtCreate) | date : 'YYYY-MM-dd HH:mm:ss' }} @@ -249,6 +254,7 @@ + @@ -406,6 +412,14 @@ + + {{ + 'alert.notice.type.serverchan-token' | i18n + }} + + + +