Skip to content

Commit

Permalink
support sending alert to ServerChan(#1092)
Browse files Browse the repository at this point in the history
  • Loading branch information
zqr10159 authored Jul 12, 2023
1 parent f1b3d87 commit 2869242
Show file tree
Hide file tree
Showing 10 changed files with 130 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
Original file line number Diff line number Diff line change
@@ -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;
/**
* @author zqr10159
* @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<ServerChanAlertNotifyHandlerImpl.ServerChanWebHookDto> httpEntity = new HttpEntity<>(serverChanWebHookDto, headers);
String webHookUrl = String.format(alerterProperties.getServerChanNotifyUrl(),receiver.getServerChanToken());
ResponseEntity<CommonRobotNotifyResp> 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;

}


}
Original file line number Diff line number Diff line change
@@ -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})]
3 changes: 2 additions & 1 deletion web-app/src/app/pojo/NoticeReceiver.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -22,6 +22,7 @@ export class NoticeReceiver {
smnProjectId!: string;
smnRegion!: string;
smnTopicUrn!: string;
serverChanToken!: string;
creator!: string;
modifier!: string;
gmtCreate!: number;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -103,6 +103,10 @@
<i nz-icon nzType="notification" nzTheme="outline"></i>
<span>{{ 'alert.notice.type.smn' | i18n }}</span>
</nz-tag>
<nz-tag *ngIf="data.type == 12" nzColor="orange">
<i nz-icon nzType="notification" nzTheme="outline"></i>
<span>{{ 'alert.notice.type.serverchan' | i18n }}</span>
</nz-tag>
</td>
<td nzAlign="center">
<span *ngIf="data.type == 0">{{ data.phone }}</span>
Expand All @@ -117,6 +121,7 @@
<span *ngIf="data.type == 9">{{ data.discordChannelId }}<br />{{ data.discordBotToken }}</span>
<span *ngIf="data.type == 10">{{ data.corpId }}<br />{{ data.agentId }}<br />{{ data.appSecret }}</span>
<span *ngIf="data.type == 11">{{ data.smnAk }}</span>
<span *ngIf="data.type == 12">{{ data.serverChanToken }}</span>
</td>
<td nzAlign="center">{{ (data.gmtUpdate ? data.gmtUpdate : data.gmtCreate) | date : 'YYYY-MM-dd HH:mm:ss' }}</td>
<td nzAlign="center">
Expand Down Expand Up @@ -249,6 +254,7 @@
<nz-option [nzValue]="7" [nzLabel]="'alert.notice.type.telegram-bot' | i18n"></nz-option>
<nz-option [nzValue]="10" [nzLabel]="'alert.notice.type.weChatApp' | i18n"></nz-option>
<nz-option [nzValue]="11" [nzLabel]="'alert.notice.type.smn' | i18n"></nz-option>
<nz-option [nzValue]="12" [nzLabel]="'alert.notice.type.serverchan' | i18n"></nz-option>
</nz-select>
</nz-form-control>
</nz-form-item>
Expand Down Expand Up @@ -406,6 +412,14 @@
<input [(ngModel)]="receiver.smnTopicUrn" nz-input [required]="receiver.type === 11" name="smnTopicUrn" type="text" />
</nz-form-control>
</nz-form-item>
<nz-form-item *ngIf="receiver.type === 12">
<nz-form-label [nzSpan]="7" nzFor="serverChanToken" [nzRequired]="receiver.type === 12">{{
'alert.notice.type.serverchan-token' | i18n
}}</nz-form-label>
<nz-form-control [nzSpan]="12" [nzErrorTip]="'validation.required' | i18n">
<input [(ngModel)]="receiver.serverChanToken" nz-input [required]="receiver.type === 12" name="serverChanToken" type="text" />
</nz-form-control>
</nz-form-item>
<nz-form-item>
<nz-form-control [nzOffset]="7" [nzSpan]="12">
<button nz-button nzDanger nzType="primary" [nzLoading]="isSendTestButtonLoading" (click)="onSendAlertTestMsg()">
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -377,6 +377,9 @@ export class AlertNoticeComponent implements OnInit {
case 11:
label = `${label}smn`;
break;
case 12:
label = `${label}ServerChan`;
break;
}
this.receiversOption.push({
value: item.id,
Expand Down
2 changes: 2 additions & 0 deletions web-app/src/assets/i18n/en-US.json
Original file line number Diff line number Diff line change
Expand Up @@ -235,6 +235,8 @@
"alert.notice.type.smn-projectId": "Project ID",
"alert.notice.type.smn-region": "Region",
"alert.notice.type.smn-topicUrn": "TopicUrn",
"alert.notice.type.serverchan": "ServerChan",
"alert.notice.type.serverchan-token": "ServerChanToken",
"alert.notice.rule": "Alert Notice Policy",
"alert.notice.rule.new": "New Notice Policy",
"alert.notice.rule.edit": "Edit Notice Policy",
Expand Down
2 changes: 2 additions & 0 deletions web-app/src/assets/i18n/zh-CN.json
Original file line number Diff line number Diff line change
Expand Up @@ -235,6 +235,8 @@
"alert.notice.type.smn-projectId": "项目ID",
"alert.notice.type.smn-region": "地域",
"alert.notice.type.smn-topicUrn": "TopicUrn",
"alert.notice.type.serverchan": "Server酱(ServerChan)",
"alert.notice.type.serverchan-token": "Server酱Token",
"alert.notice.rule": "告警通知策略",
"alert.notice.rule.new": "新增通知策略",
"alert.notice.rule.edit": "编辑通知策略",
Expand Down
2 changes: 2 additions & 0 deletions web-app/src/assets/i18n/zh-TW.json
Original file line number Diff line number Diff line change
Expand Up @@ -235,6 +235,8 @@
"alert.notice.type.smn-projectId": "項目ID",
"alert.notice.type.smn-region": "地域",
"alert.notice.type.smn-topicUrn": "TopicUrn",
"alert.notice.type.serverchan": "ServerChan",
"alert.notice.type.serverchan-token": "ServerChanToken",
"alert.notice.rule": "告警通知策略",
"alert.notice.rule.new": "新增通知策略",
"alert.notice.rule.edit": "編輯通知策略",
Expand Down

0 comments on commit 2869242

Please sign in to comment.