From 22fc18e2dee0404feebb95869fad671bd40b272e Mon Sep 17 00:00:00 2001 From: SurryChen <1974023122@qq.com> Date: Wed, 8 Nov 2023 18:46:54 +0800 Subject: [PATCH 1/2] =?UTF-8?q?[alerter]=20=E4=BC=98=E5=8C=96=E7=AC=AC?= =?UTF-8?q?=E4=B8=89=E6=96=B9=E4=BA=91=E6=9C=8D=E5=8A=A1=E5=91=8A=E8=AD=A6?= =?UTF-8?q?=E6=8E=A5=E5=85=A5=E7=9A=84=E4=BB=A3=E7=A0=81?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../controller/AlertReportController.java | 66 ++++++++++++---- .../alert/dto/CloudAlertReportAbstract.java | 61 +++++++++++++++ .../alert/dto/TenCloudAlertReport.java | 75 +++++++++++++++++-- .../CloudServiceAlarmInformationEnum.java | 36 +++++++++ .../alert/service/AlertConvertService.java | 16 ---- .../impl/AlertConvertTenCloudServiceImpl.java | 64 ---------------- .../controller/AlertReportControllerTest.java | 32 +++++--- 7 files changed, 241 insertions(+), 109 deletions(-) create mode 100644 alerter/src/main/java/org/dromara/hertzbeat/alert/dto/CloudAlertReportAbstract.java create mode 100644 alerter/src/main/java/org/dromara/hertzbeat/alert/enums/CloudServiceAlarmInformationEnum.java delete mode 100644 alerter/src/main/java/org/dromara/hertzbeat/alert/service/AlertConvertService.java delete mode 100644 alerter/src/main/java/org/dromara/hertzbeat/alert/service/impl/AlertConvertTenCloudServiceImpl.java diff --git a/alerter/src/main/java/org/dromara/hertzbeat/alert/controller/AlertReportController.java b/alerter/src/main/java/org/dromara/hertzbeat/alert/controller/AlertReportController.java index 548eccd1d3b..365091b6543 100644 --- a/alerter/src/main/java/org/dromara/hertzbeat/alert/controller/AlertReportController.java +++ b/alerter/src/main/java/org/dromara/hertzbeat/alert/controller/AlertReportController.java @@ -2,18 +2,20 @@ import io.swagger.v3.oas.annotations.Operation; import io.swagger.v3.oas.annotations.tags.Tag; +import lombok.extern.slf4j.Slf4j; +import org.dromara.hertzbeat.alert.dto.CloudAlertReportAbstract; +import org.dromara.hertzbeat.alert.enums.CloudServiceAlarmInformationEnum; import org.dromara.hertzbeat.alert.service.AlertService; -import org.dromara.hertzbeat.alert.service.impl.AlertConvertTenCloudServiceImpl; import org.dromara.hertzbeat.common.entity.dto.AlertReport; import org.dromara.hertzbeat.common.entity.dto.Message; +import org.dromara.hertzbeat.common.util.JsonUtil; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.http.ResponseEntity; -import org.springframework.web.bind.annotation.PostMapping; -import org.springframework.web.bind.annotation.RequestBody; -import org.springframework.web.bind.annotation.RequestMapping; -import org.springframework.web.bind.annotation.RestController; +import org.springframework.web.bind.annotation.*; import javax.validation.Valid; +import java.util.Date; +import java.util.Optional; import static org.springframework.http.MediaType.APPLICATION_JSON_VALUE; @@ -24,20 +26,56 @@ @Tag(name = "Extern Alarm Manage API | 第三方告警管理API") @RestController @RequestMapping(path = "/api/alerts/report", produces = {APPLICATION_JSON_VALUE}) +@Slf4j public class AlertReportController { - @Autowired - AlertConvertTenCloudServiceImpl alertConvertTenCloudService; - @Autowired private AlertService alertService; - @PostMapping("/tencloud") - @Operation(summary = "Interface for reporting external alarm information of tencloud | 对外上报告警信息 接口", - description = "对外 新增一个腾讯云告警") - public ResponseEntity> addNewAlertReportTencent(@Valid @RequestBody String alertReport) { - AlertReport convert = alertConvertTenCloudService.convert(alertReport); - alertService.addNewAlertReport(convert); + @PostMapping("/{cloud}") + @Operation(summary = "Interface for reporting external alarm information of cloud service | 对外上报告警信息 接口", + description = "对外 新增一个云服务告警") + public ResponseEntity> addNewAlertReportFromCloud(@PathVariable("cloud") String cloudServiceName, + @RequestBody String alertReport) { + // 根据枚举获取到对应的枚举对象 + CloudServiceAlarmInformationEnum cloudService = CloudServiceAlarmInformationEnum + .getEnumFromCloudServiceName(cloudServiceName); + + AlertReport alert = null; + // 校验是否存在对应的对象 + if (cloudService != null) { + try { + // 实例化对应的Class + CloudAlertReportAbstract cloudAlertReport = (CloudAlertReportAbstract) JsonUtil + .fromJson(alertReport, cloudService.getCloudServiceAlarmInformationEntity()); + // 模板填充 + alert = AlertReport.builder() + .content(cloudAlertReport.getContent()) + .alertName(cloudAlertReport.getAlertName()) + .alertTime(cloudAlertReport.getAlertTime()) + .alertDuration(cloudAlertReport.getAlertDuration()) + .priority(cloudAlertReport.getPriority()) + .reportType(cloudAlertReport.getReportType()) + .labels(cloudAlertReport.getLabels()) + .annotations(cloudAlertReport.getAnnotations()) + .build(); + } catch (Exception e) { + log.error("[AlertReportController]:解析云服务告警内容失败!云服务商:" + + cloudService.name() + ";传入JSON字符串:" + alertReport); + } + } else { + // 用户异常使用第三方接入API告警 + alert = AlertReport.builder() + .content("第三方告警API接入异常:不存在该API,详情请看文档") + .alertName("/api/alerts/report/" + cloudServiceName) + .alertTime(new Date().getTime()) + .priority(1) + .reportType(1) + .build(); + } + // 异常判断是否为空 + Optional.ofNullable(alert).ifPresent(alertReportPresent -> + alertService.addNewAlertReport(alertReportPresent)); return ResponseEntity.ok(Message.success("Add report success")); } diff --git a/alerter/src/main/java/org/dromara/hertzbeat/alert/dto/CloudAlertReportAbstract.java b/alerter/src/main/java/org/dromara/hertzbeat/alert/dto/CloudAlertReportAbstract.java new file mode 100644 index 00000000000..42a3ce10268 --- /dev/null +++ b/alerter/src/main/java/org/dromara/hertzbeat/alert/dto/CloudAlertReportAbstract.java @@ -0,0 +1,61 @@ +package org.dromara.hertzbeat.alert.dto; + +import com.fasterxml.jackson.annotation.JsonIgnore; + +import java.util.Map; + +/** + * 云服务告警信息的抽象父类 + * {@link org.dromara.hertzbeat.common.entity.dto.AlertReport} - 方法与该类参数一一对应,方法含义详情请看该类 + */ +public abstract class CloudAlertReportAbstract { + + /** + * 云告警转为内部告警时的告警名称 + */ + @JsonIgnore + public abstract String getAlertName() throws Exception; + + /** + * 云告警转为内部告警时的告警间隔 + */ + @JsonIgnore + public abstract Integer getAlertDuration() throws Exception; + + /** + * 云告警转为内部告警时的告警时间 + */ + @JsonIgnore + public abstract long getAlertTime() throws Exception; + + /** + * 云告警转为内部告警时的告警严重程度 + */ + @JsonIgnore + public abstract Integer getPriority() throws Exception; + + /** + * 云告警转为内部告警时的告警类型 + */ + @JsonIgnore + public abstract Integer getReportType() throws Exception; + + /** + * 云告警转为内部告警时的告警标签信息 + */ + @JsonIgnore + public abstract Map getLabels() throws Exception; + + /** + * 云告警转为内部告警时的告警标注 + */ + @JsonIgnore + public abstract Map getAnnotations() throws Exception; + + /** + * 云告警转为内部告警时的告警内容 + */ + @JsonIgnore + public abstract String getContent() throws Exception; + +} diff --git a/alerter/src/main/java/org/dromara/hertzbeat/alert/dto/TenCloudAlertReport.java b/alerter/src/main/java/org/dromara/hertzbeat/alert/dto/TenCloudAlertReport.java index c36bc90f2e5..fe7885ee0cd 100644 --- a/alerter/src/main/java/org/dromara/hertzbeat/alert/dto/TenCloudAlertReport.java +++ b/alerter/src/main/java/org/dromara/hertzbeat/alert/dto/TenCloudAlertReport.java @@ -2,12 +2,12 @@ import com.fasterxml.jackson.annotation.JsonProperty; -import lombok.AllArgsConstructor; -import lombok.Builder; -import lombok.Data; -import lombok.NoArgsConstructor; +import lombok.*; import java.io.Serializable; +import java.text.ParseException; +import java.text.SimpleDateFormat; +import java.util.Map; /** * @author zqr10159 @@ -16,8 +16,9 @@ @Data @AllArgsConstructor @NoArgsConstructor +@EqualsAndHashCode(callSuper = true) @Builder -public class TenCloudAlertReport implements Serializable { +public class TenCloudAlertReport extends CloudAlertReportAbstract implements Serializable { @JsonProperty("sessionID") private String sessionId; private String alarmStatus; @@ -79,6 +80,70 @@ public static class Conditions { private long alarmNotifyPeriod; } + @Override + public String getAlertName() { + return "TenCloud|腾讯云"; + } + + @Override + public Integer getAlertDuration() { + return this.durationTime; + } + + @Override + public long getAlertTime() throws ParseException { + SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"); + long occurTime; + occurTime = sdf.parse(getFirstOccurTime()).getTime(); + return occurTime; + } + + @Override + public Integer getPriority() { + return 1; + } + + @Override + public Integer getReportType() { + return 1; + } + + @Override + public Map getLabels() { + return Map.of("app", "TenCloud"); + } + + @Override + public Map getAnnotations() { + return Map.of("app", "TenCloud"); + } + + @Override + public String getContent() { + StringBuilder contentBuilder = new StringBuilder(); + return contentBuilder + .append("[") + .append("告警对象:地区") + .append(getAlarmObjInfo().getRegion()).append("|") + .append(getAlarmObjInfo().getNamespace()) + .append("]") + .append("[") + .append("告警内容:") + .append(getAlarmPolicyInfo().getPolicyTypeCname()).append("|") + .append(getAlarmPolicyInfo().getConditions().getMetricShowName()).append("|") + .append(getAlarmPolicyInfo().getConditions().getMetricName()) + .append(getAlarmPolicyInfo().getConditions().getCalcType()) + .append(getAlarmPolicyInfo().getConditions().getCalcValue()) + .append(getAlarmPolicyInfo().getConditions().getCalcUnit()) + .append("]") + .append("[") + .append("当前数据") + .append(getAlarmPolicyInfo().getConditions().getCurrentValue()) + .append(getAlarmPolicyInfo().getConditions().getCalcUnit()) + .append("]") + .toString(); + } + } diff --git a/alerter/src/main/java/org/dromara/hertzbeat/alert/enums/CloudServiceAlarmInformationEnum.java b/alerter/src/main/java/org/dromara/hertzbeat/alert/enums/CloudServiceAlarmInformationEnum.java new file mode 100644 index 00000000000..aee631b095c --- /dev/null +++ b/alerter/src/main/java/org/dromara/hertzbeat/alert/enums/CloudServiceAlarmInformationEnum.java @@ -0,0 +1,36 @@ +package org.dromara.hertzbeat.alert.enums; + +import lombok.AllArgsConstructor; +import lombok.Getter; +import org.dromara.hertzbeat.alert.dto.CloudAlertReportAbstract; +import org.dromara.hertzbeat.alert.dto.TenCloudAlertReport; + +import java.util.Arrays; + +/** + * 云服务告警枚举 + */ +@AllArgsConstructor +@Getter +public enum CloudServiceAlarmInformationEnum { + + TencentCloud("tencloud", TenCloudAlertReport.class); + + /** + * 云服务名称 + */ + private final String cloudServiceName; + + /** + * 云服务对应的请求实体 + */ + private final Class cloudServiceAlarmInformationEntity; + + public static CloudServiceAlarmInformationEnum getEnumFromCloudServiceName(String name) { + return Arrays.stream(CloudServiceAlarmInformationEnum.values()) + .filter(cloudService -> cloudService.cloudServiceName.equals(name)) + .findFirst() + .orElse(null); + } + +} diff --git a/alerter/src/main/java/org/dromara/hertzbeat/alert/service/AlertConvertService.java b/alerter/src/main/java/org/dromara/hertzbeat/alert/service/AlertConvertService.java deleted file mode 100644 index 63f578d003d..00000000000 --- a/alerter/src/main/java/org/dromara/hertzbeat/alert/service/AlertConvertService.java +++ /dev/null @@ -1,16 +0,0 @@ -package org.dromara.hertzbeat.alert.service; - -/** - * Alert Convert Interface. - * @author zqr10159 - * @param the type of object to convert the JSON into - */ -public interface AlertConvertService { - /** - * Alert Convert Method. - * - * @param json the JSON string to convert - * @return the converted object of type T - */ - T convert(String json); -} diff --git a/alerter/src/main/java/org/dromara/hertzbeat/alert/service/impl/AlertConvertTenCloudServiceImpl.java b/alerter/src/main/java/org/dromara/hertzbeat/alert/service/impl/AlertConvertTenCloudServiceImpl.java deleted file mode 100644 index e1213bf9f7d..00000000000 --- a/alerter/src/main/java/org/dromara/hertzbeat/alert/service/impl/AlertConvertTenCloudServiceImpl.java +++ /dev/null @@ -1,64 +0,0 @@ -package org.dromara.hertzbeat.alert.service.impl; - -import com.fasterxml.jackson.core.JsonProcessingException; -import com.fasterxml.jackson.databind.ObjectMapper; -import lombok.extern.slf4j.Slf4j; -import org.dromara.hertzbeat.alert.dto.TenCloudAlertReport; -import org.dromara.hertzbeat.alert.service.AlertConvertService; -import org.dromara.hertzbeat.common.entity.dto.AlertReport; -import org.springframework.beans.factory.annotation.Autowired; -import org.springframework.stereotype.Component; - -import java.text.ParseException; -import java.text.SimpleDateFormat; -import java.util.HashMap; - -/** - * @author zqr10159 - * 腾讯云告警转化类 - */ -@Component -@Slf4j -public class AlertConvertTenCloudServiceImpl implements AlertConvertService { - @Autowired - private ObjectMapper objectMapper; - @Override - public AlertReport convert(String json) { - TenCloudAlertReport tenCloudAlertReport; - AlertReport alert = null; - try { - tenCloudAlertReport = objectMapper.readValue(json, TenCloudAlertReport.class); - StringBuilder contentBuilder = new StringBuilder(); - String content = contentBuilder.append("[").append("告警对象:地区") - .append(tenCloudAlertReport.getAlarmObjInfo().getRegion()).append("|") - .append(tenCloudAlertReport.getAlarmObjInfo().getNamespace()).append("]") - .append("[").append("告警内容:") - .append(tenCloudAlertReport.getAlarmPolicyInfo().getPolicyTypeCname()).append("|") - .append(tenCloudAlertReport.getAlarmPolicyInfo().getConditions().getMetricShowName()).append("|") - .append(tenCloudAlertReport.getAlarmPolicyInfo().getConditions().getMetricName()) - .append(tenCloudAlertReport.getAlarmPolicyInfo().getConditions().getCalcType()) - .append(tenCloudAlertReport.getAlarmPolicyInfo().getConditions().getCalcValue()) - .append(tenCloudAlertReport.getAlarmPolicyInfo().getConditions().getCalcUnit()).append("]") - .append("[").append("当前数据") - .append(tenCloudAlertReport.getAlarmPolicyInfo().getConditions().getCurrentValue()) - .append(tenCloudAlertReport.getAlarmPolicyInfo().getConditions().getCalcUnit()).append("]").toString(); - - HashMap tagMap = new HashMap<>(1); - tagMap.put("app", "TenCloud"); - SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"); - long occurTime = sdf.parse(tenCloudAlertReport.getFirstOccurTime()).getTime(); - alert = AlertReport.builder().content(content) - .alertName("TenCloud|腾讯云") - .alertTime(occurTime) - .alertDuration(tenCloudAlertReport.getDurationTime()) - .priority(1) - .reportType(1) - .labels(tagMap) - .annotations(tagMap).build(); - - } catch (JsonProcessingException|ParseException e) { - log.error("解析腾讯云告警内容失败!"); - } - return alert; - } -} diff --git a/alerter/src/test/java/org/dromara/hertzbeat/alert/controller/AlertReportControllerTest.java b/alerter/src/test/java/org/dromara/hertzbeat/alert/controller/AlertReportControllerTest.java index b04faffd4d0..3a58fe42b7f 100644 --- a/alerter/src/test/java/org/dromara/hertzbeat/alert/controller/AlertReportControllerTest.java +++ b/alerter/src/test/java/org/dromara/hertzbeat/alert/controller/AlertReportControllerTest.java @@ -1,8 +1,6 @@ package org.dromara.hertzbeat.alert.controller; import org.dromara.hertzbeat.alert.dto.TenCloudAlertReport; -import org.dromara.hertzbeat.alert.service.AlertService; -import org.dromara.hertzbeat.alert.service.impl.AlertConvertTenCloudServiceImpl; import org.dromara.hertzbeat.common.constants.CommonConstants; import org.dromara.hertzbeat.common.entity.dto.AlertReport; import org.dromara.hertzbeat.common.util.JsonUtil; @@ -10,7 +8,6 @@ import org.junit.jupiter.api.Test; import org.junit.jupiter.api.extension.ExtendWith; import org.mockito.InjectMocks; -import org.mockito.Mock; import org.mockito.junit.jupiter.MockitoExtension; import org.springframework.http.MediaType; import org.springframework.test.web.servlet.MockMvc; @@ -33,12 +30,6 @@ class AlertReportControllerTest { @InjectMocks private AlertReportController alertReportController; - @Mock - private AlertService alertService; - - @Mock - AlertConvertTenCloudServiceImpl alertConvertTenCloudService; - @BeforeEach void setUp() { this.mockMvc = MockMvcBuilders.standaloneSetup(alertReportController).build(); @@ -46,10 +37,31 @@ void setUp() { @Test void addNewAlertReportTencent() throws Exception { + TenCloudAlertReport.AlarmObjInfo alarmObjInfo = new TenCloudAlertReport.AlarmObjInfo(); + alarmObjInfo.setRegion("广东"); + alarmObjInfo.setNamespace("广州节点1"); + + TenCloudAlertReport.Conditions conditions = new TenCloudAlertReport.Conditions(); + conditions.setMetricName("xx"); + conditions.setMetricShowName("xxx"); + conditions.setCalcType("a"); + conditions.setCalcValue("aa"); + conditions.setCalcUnit("aaa"); + conditions.setCurrentValue("b"); + conditions.setCalcUnit("bb"); + + TenCloudAlertReport.AlarmPolicyInfo alarmPolicyInfo = new TenCloudAlertReport.AlarmPolicyInfo(); + alarmPolicyInfo.setPolicyTypeCname("x"); + alarmPolicyInfo.setConditions(conditions); + TenCloudAlertReport report = TenCloudAlertReport.builder() - .sessionId("xxxxxxxx") + .sessionId("123") .alarmStatus("1") .alarmType("metric") + .durationTime(2) + .firstOccurTime("2023-08-14 11:11:11") + .alarmObjInfo(alarmObjInfo) + .alarmPolicyInfo(alarmPolicyInfo) .build(); mockMvc.perform( MockMvcRequestBuilders From 2d32eec0ba75009c283dc94a0c08b81574e5f58d Mon Sep 17 00:00:00 2001 From: SurryChen <1974023122@qq.com> Date: Wed, 8 Nov 2023 19:36:18 +0800 Subject: [PATCH 2/2] =?UTF-8?q?[alerter]=20=E6=B5=8B=E8=AF=95=E4=BB=A3?= =?UTF-8?q?=E7=A0=81=E4=B8=AD=E6=B3=A8=E5=85=A5=E6=BC=8F=E4=BA=86=E4=B8=80?= =?UTF-8?q?=E4=B8=AA=E5=AF=B9=E8=B1=A1?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../alert/controller/AlertReportControllerTest.java | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/alerter/src/test/java/org/dromara/hertzbeat/alert/controller/AlertReportControllerTest.java b/alerter/src/test/java/org/dromara/hertzbeat/alert/controller/AlertReportControllerTest.java index 3a58fe42b7f..0877f390010 100644 --- a/alerter/src/test/java/org/dromara/hertzbeat/alert/controller/AlertReportControllerTest.java +++ b/alerter/src/test/java/org/dromara/hertzbeat/alert/controller/AlertReportControllerTest.java @@ -1,6 +1,7 @@ package org.dromara.hertzbeat.alert.controller; import org.dromara.hertzbeat.alert.dto.TenCloudAlertReport; +import org.dromara.hertzbeat.alert.service.AlertService; import org.dromara.hertzbeat.common.constants.CommonConstants; import org.dromara.hertzbeat.common.entity.dto.AlertReport; import org.dromara.hertzbeat.common.util.JsonUtil; @@ -8,6 +9,7 @@ import org.junit.jupiter.api.Test; import org.junit.jupiter.api.extension.ExtendWith; import org.mockito.InjectMocks; +import org.mockito.Mock; import org.mockito.junit.jupiter.MockitoExtension; import org.springframework.http.MediaType; import org.springframework.test.web.servlet.MockMvc; @@ -26,6 +28,9 @@ class AlertReportControllerTest { private MockMvc mockMvc; + + @Mock + private AlertService alertService; @InjectMocks private AlertReportController alertReportController;