Skip to content

Commit

Permalink
[hertzbeat] support sms alert notice (#503)
Browse files Browse the repository at this point in the history
  [hertzbeat] support sms alert notice

  [docs] support sms config docs

  [home] update doc

  [web-app] fix eslint
  • Loading branch information
tomsun28 authored Dec 20, 2022
1 parent 5e2916f commit 655134d
Show file tree
Hide file tree
Showing 12 changed files with 369 additions and 15 deletions.
8 changes: 7 additions & 1 deletion common/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -108,5 +108,11 @@
<groupId>org.apache.httpcomponents</groupId>
<artifactId>httpclient</artifactId>
</dependency>
<!-- sms -->
<dependency>
<groupId>com.tencentcloudapi</groupId>
<artifactId>tencentcloud-sdk-java-sms</artifactId>
<version>3.1.648</version>
</dependency>
</dependencies>
</project>
</project>
93 changes: 93 additions & 0 deletions common/src/main/java/com/usthe/common/config/CommonProperties.java
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,11 @@ public class CommonProperties {
*/
private DataQueueProperties queue;

/**
* sms impl properties
*/
private SmsProperties sms;

public String getSecretKey() {
return secretKey;
}
Expand All @@ -56,6 +61,14 @@ public void setQueue(DataQueueProperties queue) {
this.queue = queue;
}

public SmsProperties getSms() {
return sms;
}

public void setSms(SmsProperties sms) {
this.sms = sms;
}

public static class DataQueueProperties {

private QueueType type = QueueType.Memory;
Expand All @@ -77,4 +90,84 @@ public static enum QueueType {
/** rabbit mq **/
Rabbit_Mq
}

public static class SmsProperties {
private TencentSmsProperties tencent;

public TencentSmsProperties getTencent() {
return tencent;
}

public void setTencent(TencentSmsProperties tencent) {
this.tencent = tencent;
}
}

public static class TencentSmsProperties {

/**
* 腾讯云账户访问密钥id
*/
private String secretId;

/**
* 腾讯云账户访问密钥key
*/
private String secretKey;

/**
* SMS短信应用app id
*/
private String appId;

/**
* 短信签名
*/
private String signName;

/**
* 短信模版ID
*/
private String templateId;

public String getAppId() {
return appId;
}

public void setAppId(String appId) {
this.appId = appId;
}

public String getSecretId() {
return secretId;
}

public void setSecretId(String secretId) {
this.secretId = secretId;
}

public String getSecretKey() {
return secretKey;
}

public void setSecretKey(String secretKey) {
this.secretKey = secretKey;
}

public String getSignName() {
return signName;
}

public void setSignName(String signName) {
this.signName = signName;
}

public String getTemplateId() {
return templateId;
}

public void setTemplateId(String templateId) {
this.templateId = templateId;
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,88 @@
package com.usthe.common.service;

import com.tencentcloudapi.common.Credential;
import com.tencentcloudapi.sms.v20210111.SmsClient;
import com.tencentcloudapi.sms.v20210111.models.SendSmsRequest;
import com.tencentcloudapi.sms.v20210111.models.SendSmsResponse;
import com.tencentcloudapi.sms.v20210111.models.SendStatus;
import com.usthe.common.config.CommonProperties;
import com.usthe.common.support.exception.SendMessageException;
import lombok.extern.slf4j.Slf4j;
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
import org.springframework.context.annotation.Configuration;

/**
* sms service client for tencent cloud
* @author tom
* @date 2022/12/17 17:41
*/
@Configuration
@ConditionalOnProperty("common.sms.tencent.app-id")
@Slf4j
public class TencentSmsClient {

private static final String RESPONSE_OK = "Ok";
private static final String REGION = "ap-guangzhou";

private SmsClient smsClient;
private String appId;
private String signName;
private String templateId;

public TencentSmsClient(CommonProperties properties) {
if (properties == null || properties.getSms() == null || properties.getSms().getTencent() == null) {
log.error("init error, please config TencentSmsClient props in application.yml");
throw new IllegalArgumentException("please config TencentSmsClient props");
}
initSmsClient(properties.getSms().getTencent());
}

private void initSmsClient(CommonProperties.TencentSmsProperties tencent) {
this.appId = tencent.getAppId();
this.signName = tencent.getSignName();
this.templateId = tencent.getTemplateId();
Credential cred = new Credential(tencent.getSecretId(), tencent.getSecretKey());
smsClient = new SmsClient(cred, REGION);
}

/**
* 发送短信
* @param appId appId
* @param signName sign name
* @param templateId template id
* @param templateValues template values
* @param phones phones num
* @return true when send success
*/
public void sendMessage(String appId, String signName, String templateId,
String[] templateValues, String[] phones) {
SendSmsRequest req = new SendSmsRequest();
req.setSmsSdkAppId(appId);
req.setSignName(signName);
req.setTemplateId(templateId);
req.setTemplateParamSet(templateValues);
req.setPhoneNumberSet(phones);
try {
SendSmsResponse smsResponse = this.smsClient.SendSms(req);
SendStatus sendStatus = smsResponse.getSendStatusSet()[0];
if (!RESPONSE_OK.equals(sendStatus.getCode())) {
throw new SendMessageException(sendStatus.getCode() + ":" + sendStatus.getMessage());
}
} catch (Exception e) {
log.warn(e.getMessage());
throw new SendMessageException(e.getMessage());
}
}

/**
* 发送短信
* @param templateValues template values
* @param phones phones num
* @return true when send success
*/
public void sendMessage(String[] templateValues, String[] phones) {
sendMessage(this.appId, this.signName, this.templateId, templateValues, phones);
}


}
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
package com.usthe.common.support.exception;

/**
* send message exception
* @author tom
* @date 2022/5/8 17:59
*/
public class SendMessageException extends RuntimeException {
public SendMessageException(String message) {
super(message);
}
}
1 change: 1 addition & 0 deletions common/src/main/resources/META-INF/spring.factories
Original file line number Diff line number Diff line change
Expand Up @@ -19,4 +19,5 @@ com.usthe.common.config.CommonProperties,\
com.usthe.common.config.CommonConfig,\
com.usthe.common.config.AviatorConfiguration,\
com.usthe.common.queue.impl.InMemoryCommonDataQueue,\
com.usthe.common.service.TencentSmsClient,\
com.usthe.common.support.SpringContextHolder
64 changes: 64 additions & 0 deletions home/docs/start/custom-config.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
---
id: custom-config
title: 常见参数配置
sidebar_label: 常见参数配置
---

这里描述了如果配置短信服务器,内置可用性告警触发次数等。

**`hertzbeat`的配置文件`application.yml`**

### 配置HertzBeat的配置文件
修改位于 `hertzbeat/config/application.yml` 的配置文件
注意⚠️docker容器方式需要将application.yml文件挂载到主机本地
安装包方式解压修改位于 `hertzbeat/config/application.yml` 即可

1. 配置短信发送服务器

> 只有成功配置了您自己的短信服务器,监控系统内触发的告警短信才会正常发送。
`application.yml`新增如下腾讯平台短信服务器配置(参数需替换为您的短信服务器配置)
```yaml
common:
sms:
tencent:
secret-id: AKIDbQ4VhdMr89wDedFrIcgU2PaaMvOuBCzY
secret-key: PaXGl0ziY9UcWFjUyiFlCPMr77rLkJYlyA
app-id: 1435441637
sign-name: 赫兹跳动
template-id: 1343434
```
2. 配置告警自定义参数
> 如果您收到频繁的内置可用性告警,或在您所在网络抖动厉害,建议调整以下参数
```yaml
alerter:
# 自定义控制台地址
console-url: https://console.tancloud.cn
# 告警触发评估间隔基础时间,相同重复告警在2倍此时间内不会被重复连续触发 单位毫秒
alert-eval-interval-base: 600000
# 告警触发评估间隔最大时间,相同重复告警最多在此时间段被抑制 单位毫秒
max-alert-eval-interval: 86400000
# 内置可用性告警连续触发几次才会真正发送告警 默认1次,当网络环境不好,不想频繁收到可用性告警时,可将此值调大(3)
system-alert-trigger-times: 1
```
3. 使用外置redis代替内存存储实时指标数据
> 默认我们的指标实时数据存储在内存中,可以配置如下来使用redis代替内存存储。
注意⚠️ `memory.enabled: false, redis.enabled: true`
```yaml
warehouse:
store:
memory:
enabled: false
init-size: 1024
redis:
enabled: true
host: 127.0.0.1
port: 6379
password: 123456
```
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ We also provide paid remote installation and deployment services.
#### Cluster SAAS version of HertzBeat-TanCloud (paid)

This version is the corresponding version of [TanCloud Official website](https://console.tancloud.cn) environment. Compared with hertzbeat, it has functions of clustering, users, tenants and so on, which is suitable for medium and large-scale team enterprises.
This version is a paid version. For tancloud privatization deployment plan and price, please consult us on WeChat tomsun28, and add WeChat please note: privatization + team name.
This version is a paid version. For tancloud privatization deployment plan and price, please consult us on WeChat tan-cloud, and add WeChat please note: privatization + team name.

#### Privatized custom function development (paid)

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
---
id: custom-config
title: Advanced Config
sidebar_label: Advanced Config
---

This describes how to configure the SMS server, the number of built-in availability alarm triggers, etc.

**Configuration file `application.yml` of `hertzbeat`**

### Configure the configuration file of HertzBeat

Modify the configuration file located at `hertzbeat/config/application.yml`
Note ⚠️The docker container method needs to mount the application.yml file to the local host
The installation package can be decompressed and modified in `hertzbeat/config/application.yml`

1. Configure the SMS sending server

> Only when your own SMS server is successfully configured, the alarm SMS triggered in the monitoring system will be sent normally.
Add the following Tencent platform SMS server configuration in `application.yml` (parameters need to be replaced with your SMS server configuration)
```yaml
common:
sms:
tencent:
secret-id: AKIDbQ4VhdMr89wDedFrIcgU2PaaMvOuBCzY
secret-key: PaXGl0ziY9UcWFjUyiFlCPMr77rLkJYlyA
app-id: 1435441637
sign-name: XX Technology
template-id: 1343434
```
2. Configure alarm custom parameters
> If you receive frequent built-in availability alarms, or the network jitter is severe in your area, it is recommended to adjust the following parameters
```yaml
alerter:
# Custom console address
console-url: https://console.tancloud.cn
# Alarm trigger evaluation interval basic time, the same repeated alarm will not be repeatedly triggered continuously within 2 times this time, unit milliseconds
alert-eval-interval-base: 600000
# The maximum time between alarm trigger evaluation intervals, the same repeated alarms can be suppressed at most during this time period, in milliseconds
max-alert-eval-interval: 86400000
# The built-in availability alarm will be triggered several times in a row before the actual alarm is sent. The default is 1 time. When the network environment is not good and you don't want to receive availability alarms frequently, you can increase this value (3)
system-alert-trigger-times: 1
```
3. Use external redis instead of memory to store real-time indicator data
> By default, the real-time data of our indicators is stored in memory, which can be configured as follows to use redis instead of memory storage.
Note ⚠️ `memory.enabled: false, redis.enabled: true`
```yaml
warehouse:
store:
memory:
enabled: false
init-size: 1024
redis:
enabled: true
host: 127.0.0.1
port: 6379
password: 123456
```
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
---
id: mysql-change
title: Dependent Relational Database H2 Switch to MYSQL
sidebar_label: H2 Database Switch to MYSQL
sidebar_label: H2 Switch to MYSQL
---
MYSQL is a reliable relational database. In addition to default built-in H2 database, HertzBeat allow you to use MYSQL to store structured relational data such as monitoring information, alarm information and configuration information.

Expand Down
3 changes: 2 additions & 1 deletion home/sidebars.json
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,8 @@
"start/package-deploy",
"start/rainbond-deploy",
"start/ssl-cert-practice",
"start/mysql-change"
"start/mysql-change",
"start/custom-config"
]
},
{
Expand Down
Loading

0 comments on commit 655134d

Please sign in to comment.