Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[feature] add sms config #2399

Merged
merged 14 commits into from
Jul 30, 2024
Merged
Show file tree
Hide file tree
Changes from 6 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
/*
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright ownership.
* The ASF licenses this file to You under the Apache License, Version 2.0
* (the "License"); you may not use this file except in compliance with
* the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

package org.apache.hertzbeat.common.constants;

/**
* Sms type Enum
*/
public enum SmsTypeEnum {

/**
* tencent sms
*/
tencent,
Aias00 marked this conversation as resolved.
Show resolved Hide resolved

/**
* alibaba sms
*/
alibaba;

/**
* get type
*/
public static SmsTypeEnum getTypeByName(String type) {
for (SmsTypeEnum smsTypeEnum : values()) {
if (smsTypeEnum.name().equals(type)) {
return smsTypeEnum;
}

}
return null;
Aias00 marked this conversation as resolved.
Show resolved Hide resolved
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -20,14 +20,19 @@
import java.util.ResourceBundle;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.apache.hertzbeat.common.config.CommonProperties;
import org.apache.hertzbeat.common.constants.CommonConstants;
import org.apache.hertzbeat.common.constants.SmsTypeEnum;
import org.apache.hertzbeat.common.entity.alerter.Alert;
import org.apache.hertzbeat.common.entity.manager.GeneralConfig;
import org.apache.hertzbeat.common.entity.manager.NoticeReceiver;
import org.apache.hertzbeat.common.entity.manager.NoticeTemplate;
import org.apache.hertzbeat.common.util.JsonUtil;
import org.apache.hertzbeat.common.util.ResourceBundleUtil;
import org.apache.hertzbeat.manager.dao.GeneralConfigDao;
import org.apache.hertzbeat.manager.pojo.dto.SmsNoticeSender;
import org.apache.hertzbeat.manager.service.TencentSmsClient;
import org.apache.hertzbeat.manager.support.exception.AlertNoticeException;
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
import org.springframework.stereotype.Component;

/**
Expand All @@ -36,31 +41,61 @@
@Component
@RequiredArgsConstructor
@Slf4j
@ConditionalOnProperty("common.sms.tencent.app-id")
final class SmsAlertNotifyHandlerImpl extends AbstractAlertNotifyHandlerImpl {

private final TencentSmsClient tencentSmsClient;


private static final String TYPE = "sms";

private final GeneralConfigDao generalConfigDao;

private final CommonProperties commonProperties;

private final ResourceBundle bundle = ResourceBundleUtil.getBundle("alerter");


private TencentSmsClient tencentSmsClient;

@Override
public void send(NoticeReceiver receiver, NoticeTemplate noticeTemplate, Alert alert) {
// SMS notification
try {
String monitorName = null;
if (alert.getTags() != null) {
monitorName = alert.getTags().get(CommonConstants.TAG_MONITOR_NAME);
}
String[] params = new String[3];
params[0] = monitorName == null ? alert.getTarget() : monitorName;
params[1] = bundle.getString("alerter.priority." + alert.getPriority());
params[2] = alert.getContent();
tencentSmsClient.sendMessage(params, new String[]{receiver.getPhone()});
getSmsSender();
doSendSms(receiver, alert);
} catch (Exception e) {
throw new AlertNoticeException("[Sms Notify Error] " + e.getMessage());
}
}


private void doSendSms(final NoticeReceiver receiver, final Alert alert) {
String monitorName = null;
if (alert.getTags() != null) {
monitorName = alert.getTags().get(CommonConstants.TAG_MONITOR_NAME);
}
String[] params = new String[3];
params[0] = monitorName == null ? alert.getTarget() : monitorName;
params[1] = bundle.getString("alerter.priority." + alert.getPriority());
params[2] = alert.getContent();
tencentSmsClient.sendMessage(params, new String[]{receiver.getPhone()});
}

private void getSmsSender() {
boolean useDatabase = false;
GeneralConfig smsConfig = generalConfigDao.findByType(TYPE);
if (smsConfig != null && smsConfig.getContent() != null) {
// enable database configuration
String content = smsConfig.getContent();
SmsNoticeSender smsNoticeSenderConfig = JsonUtil.fromJson(content, SmsNoticeSender.class);
if (null != smsNoticeSenderConfig && smsNoticeSenderConfig.isEnable()) {
// tencent sms
if (SmsTypeEnum.tencent.name().equalsIgnoreCase(smsNoticeSenderConfig.getType())) {
tencentSmsClient = new TencentSmsClient(smsNoticeSenderConfig.getTencent());
useDatabase = true;
}
}
}
if (!useDatabase) {
tencentSmsClient = new TencentSmsClient(commonProperties);
}
}

@Override
public byte type() {
return 0;
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
/*
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright ownership.
* The ASF licenses this file to You under the Apache License, Version 2.0
* (the "License"); you may not use this file except in compliance with
* the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

package org.apache.hertzbeat.manager.pojo.dto;

import lombok.Data;

/**
* Alibaba Sms Sender configuration dto
*/
@Data
public class SmsAlibabaConfig {

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
/*
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright ownership.
* The ASF licenses this file to You under the Apache License, Version 2.0
* (the "License"); you may not use this file except in compliance with
* the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

package org.apache.hertzbeat.manager.pojo.dto;

import jakarta.validation.constraints.NotBlank;
import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;

/**
* Sms Sender configuration dto
*/
@Data
@NoArgsConstructor
@AllArgsConstructor
public class SmsNoticeSender {

@NotBlank(message = "Type cannot be empty")
private String type;

private SmsTencentConfig tencent;

private SmsAlibabaConfig alibaba;

private boolean enable = true;
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
/*
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright ownership.
* The ASF licenses this file to You under the Apache License, Version 2.0
* (the "License"); you may not use this file except in compliance with
* the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

package org.apache.hertzbeat.manager.pojo.dto;

import jakarta.validation.constraints.NotBlank;
import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;

/**
* Tencent Sms Sender configuration dto
*/
@Data
@NoArgsConstructor
@AllArgsConstructor
public class SmsTencentConfig {

@NotBlank(message = "SecretId cannot be empty")
private String secretId;

@NotBlank(message = "SecretKey cannot be empty")
private String secretKey;

@NotBlank(message = "SignName cannot be empty")
private String signName;

@NotBlank(message = "AppId cannot be null")
private String appId;

@NotBlank(message = "templateId cannot be null")
private String templateId;

}
Original file line number Diff line number Diff line change
Expand Up @@ -25,14 +25,11 @@
import lombok.extern.slf4j.Slf4j;
import org.apache.hertzbeat.common.config.CommonProperties;
import org.apache.hertzbeat.common.support.exception.SendMessageException;
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
import org.springframework.stereotype.Component;
import org.apache.hertzbeat.manager.pojo.dto.SmsTencentConfig;

/**
* sms service client for tencent cloud
*/
@Component
@ConditionalOnProperty("common.sms.tencent.app-id")
@Slf4j
public class TencentSmsClient {

Expand All @@ -51,6 +48,14 @@ public TencentSmsClient(CommonProperties properties) {
}
initSmsClient(properties.getSms().getTencent());
}

public TencentSmsClient(SmsTencentConfig 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);
}

private void initSmsClient(CommonProperties.TencentSmsProperties tencent) {
this.appId = tencent.getAppId();
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
/*
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright ownership.
* The ASF licenses this file to You under the Apache License, Version 2.0
* (the "License"); you may not use this file except in compliance with
* the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

package org.apache.hertzbeat.manager.service.impl;

import com.fasterxml.jackson.core.type.TypeReference;
import com.fasterxml.jackson.databind.ObjectMapper;
import java.lang.reflect.Type;
import org.apache.hertzbeat.manager.dao.GeneralConfigDao;
import org.apache.hertzbeat.manager.pojo.dto.SmsNoticeSender;
import org.springframework.stereotype.Service;

/**
* SmsGeneralConfigServiceImpl class is the implementation of general sms configuration service,
* which inherits the AbstractGeneralConfigServiceImpl class.
*/

@Service
public class SmsGeneralConfigServiceImpl extends AbstractGeneralConfigServiceImpl<SmsNoticeSender> {

/**
* SmsGeneralConfigServiceImpl's constructor creates an instance of this class
* through the default constructor or deserialization construction (setBeanProps).
* The parameter generalConfigDao is used for dao layer operation data,
* and objectMapper is used for object mapping.
* @param generalConfigDao dao layer operation data, needed to create an instance of this class
* @param objectMapper object mapping , needed to create an instance of this class
*/
public SmsGeneralConfigServiceImpl(GeneralConfigDao generalConfigDao, ObjectMapper objectMapper) {
super(generalConfigDao, objectMapper);
}

@Override
public String type() {
return "sms";
}

/**
* This method is used to get the TypeReference of NoticeSender type for subsequent processing.
* a TypeReference of NoticeSender type
*/
@Override
protected TypeReference<SmsNoticeSender> getTypeReference() {
return new TypeReference<>() {
@Override
public Type getType() {
return SmsNoticeSender.class;
}
};
}
}
26 changes: 26 additions & 0 deletions web-app/src/app/pojo/AlibabaSmsConfig.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
/*
* Licensed to the Apache Software Foundation (ASF) under one
* or more contributor license agreements. See the NOTICE file
* distributed with this work for additional information
* regarding copyright ownership. The ASF licenses this file
* to you under the Apache License, Version 2.0 (the
* "License"); you may not use this file except in compliance
* with the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing,
* software distributed under the License is distributed on an
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
* KIND, either express or implied. See the License for the
* specific language governing permissions and limitations
* under the License.
*/

export class AlibabaSmsConfig {
secretId!: string;
secretKey!: string;
signName!: string;
appId!: string;
templateId!: string;
}
Loading