-
Notifications
You must be signed in to change notification settings - Fork 994
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[Feature]Add third report of TenCloud (#1113)
Signed-off-by: Logic <zqr10159@126.com> Signed-off-by: tomsun28 <tomsun28@outlook.com> Co-authored-by: tomsun28 <tomsun28@outlook.com>
- Loading branch information
Showing
9 changed files
with
300 additions
and
26 deletions.
There are no files selected for viewing
52 changes: 52 additions & 0 deletions
52
alerter/src/main/java/org/dromara/hertzbeat/alert/controller/AlertReportController.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,52 @@ | ||
package org.dromara.hertzbeat.alert.controller; | ||
|
||
import io.swagger.v3.oas.annotations.Operation; | ||
import io.swagger.v3.oas.annotations.tags.Tag; | ||
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.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 javax.validation.Valid; | ||
|
||
import static org.springframework.http.MediaType.APPLICATION_JSON_VALUE; | ||
|
||
/** | ||
* @author zqr10159 | ||
* 第三方告警上报接口 | ||
*/ | ||
@Tag(name = "Extern Alarm Manage API | 第三方告警管理API") | ||
@RestController | ||
@RequestMapping(path = "/api/alerts/report", produces = {APPLICATION_JSON_VALUE}) | ||
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<Message<Void>> addNewAlertReportTencent(@Valid @RequestBody String alertReport) { | ||
AlertReport convert = alertConvertTenCloudService.convert(alertReport); | ||
alertService.addNewAlertReport(convert); | ||
return ResponseEntity.ok(new Message<>("Add report success")); | ||
} | ||
|
||
@PostMapping | ||
@Operation(summary = "Interface for reporting external alarm information | 对外上报告警信息 接口", | ||
description = "对外 新增一个告警") | ||
public ResponseEntity<Message<Void>> addNewAlertReport(@Valid @RequestBody AlertReport alertReport) { | ||
// 校验请求数据 TODO | ||
alertService.addNewAlertReport(alertReport); | ||
return ResponseEntity.ok(new Message<>("Add report success")); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
84 changes: 84 additions & 0 deletions
84
alerter/src/main/java/org/dromara/hertzbeat/alert/dto/TenCloudAlertReport.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,84 @@ | ||
package org.dromara.hertzbeat.alert.dto; | ||
|
||
|
||
import com.fasterxml.jackson.annotation.JsonProperty; | ||
import lombok.AllArgsConstructor; | ||
import lombok.Builder; | ||
import lombok.Data; | ||
import lombok.NoArgsConstructor; | ||
|
||
import java.io.Serializable; | ||
|
||
/** | ||
* @author zqr10159 | ||
* 腾讯云告警实体类 | ||
*/ | ||
@Data | ||
@AllArgsConstructor | ||
@NoArgsConstructor | ||
@Builder | ||
public class TenCloudAlertReport implements Serializable { | ||
@JsonProperty("sessionID") | ||
private String sessionId; | ||
private String alarmStatus; | ||
private String alarmType; | ||
private AlarmObjInfo alarmObjInfo; | ||
private AlarmPolicyInfo alarmPolicyInfo; | ||
private String firstOccurTime; | ||
private int durationTime; | ||
private String recoverTime; | ||
|
||
@Data | ||
@AllArgsConstructor | ||
@NoArgsConstructor | ||
public static class AlarmObjInfo { | ||
private String region; | ||
private String namespace; | ||
@JsonProperty("appID") | ||
private String appId; | ||
private String uin; | ||
private Dimensions dimensions; | ||
} | ||
|
||
@Data | ||
@AllArgsConstructor | ||
@NoArgsConstructor | ||
public static class Dimensions { | ||
@JsonProperty("unInstanceID") | ||
private String unInstanceId; | ||
@JsonProperty("objID") | ||
private String objId; | ||
} | ||
@Data | ||
@AllArgsConstructor | ||
@NoArgsConstructor | ||
public static class AlarmPolicyInfo { | ||
@JsonProperty("policyID") | ||
private String policyId; | ||
private String policyType; | ||
private String policyName; | ||
@JsonProperty("policyTypeCName") | ||
private String policyTypeCname; | ||
private Conditions conditions; | ||
} | ||
@Data | ||
@AllArgsConstructor | ||
@NoArgsConstructor | ||
public static class Conditions { | ||
private String metricName; | ||
private String metricShowName; | ||
private String calcType; | ||
private String calcValue; | ||
private String calcUnit; | ||
private String currentValue; | ||
private String historyValue; | ||
private String unit; | ||
private String period; | ||
private String periodNum; | ||
private String alarmNotifyType; | ||
private long alarmNotifyPeriod; | ||
} | ||
|
||
} | ||
|
||
|
16 changes: 16 additions & 0 deletions
16
alerter/src/main/java/org/dromara/hertzbeat/alert/service/AlertConvertService.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
package org.dromara.hertzbeat.alert.service; | ||
|
||
/** | ||
* Alert Convert Interface. | ||
* @author zqr10159 | ||
* @param <T> the type of object to convert the JSON into | ||
*/ | ||
public interface AlertConvertService<T> { | ||
/** | ||
* Alert Convert Method. | ||
* | ||
* @param json the JSON string to convert | ||
* @return the converted object of type T | ||
*/ | ||
T convert(String json); | ||
} |
64 changes: 64 additions & 0 deletions
64
...c/main/java/org/dromara/hertzbeat/alert/service/impl/AlertConvertTenCloudServiceImpl.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,64 @@ | ||
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<AlertReport> { | ||
@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<String, String> 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; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
79 changes: 79 additions & 0 deletions
79
alerter/src/test/java/org/dromara/hertzbeat/alert/controller/AlertReportControllerTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,79 @@ | ||
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; | ||
import org.junit.jupiter.api.BeforeEach; | ||
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; | ||
import org.springframework.test.web.servlet.request.MockMvcRequestBuilders; | ||
import org.springframework.test.web.servlet.setup.MockMvcBuilders; | ||
|
||
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.content; | ||
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.jsonPath; | ||
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.status; | ||
|
||
/** | ||
* unit test for {@link AlertReportController } | ||
* @author tom | ||
*/ | ||
@ExtendWith(MockitoExtension.class) | ||
class AlertReportControllerTest { | ||
|
||
private MockMvc mockMvc; | ||
|
||
@InjectMocks | ||
private AlertReportController alertReportController; | ||
|
||
@Mock | ||
private AlertService alertService; | ||
|
||
@Mock | ||
AlertConvertTenCloudServiceImpl alertConvertTenCloudService; | ||
|
||
@BeforeEach | ||
void setUp() { | ||
this.mockMvc = MockMvcBuilders.standaloneSetup(alertReportController).build(); | ||
} | ||
|
||
@Test | ||
void addNewAlertReportTencent() throws Exception { | ||
TenCloudAlertReport report = TenCloudAlertReport.builder() | ||
.sessionId("xxxxxxxx") | ||
.alarmStatus("1") | ||
.alarmType("metric") | ||
.build(); | ||
mockMvc.perform( | ||
MockMvcRequestBuilders | ||
.post("/api/alerts/report/tencloud") | ||
.contentType(MediaType.APPLICATION_JSON) | ||
.content(JsonUtil.toJson(report)) | ||
) | ||
.andExpect(status().isOk()) | ||
.andExpect(jsonPath("$.code").value((int) CommonConstants.SUCCESS_CODE)) | ||
.andExpect(content().json("{\"data\":null,\"msg\":\"Add report success\",\"code\":0}")) | ||
.andReturn(); | ||
} | ||
|
||
@Test | ||
void addNewAlertReport() throws Exception { | ||
mockMvc.perform( | ||
MockMvcRequestBuilders | ||
.post("/api/alerts/report") | ||
.contentType(MediaType.APPLICATION_JSON) | ||
.content(JsonUtil.toJson(AlertReport.builder().build())) | ||
) | ||
.andExpect(status().isOk()) | ||
.andExpect(jsonPath("$.code").value((int) CommonConstants.SUCCESS_CODE)) | ||
.andExpect(content().json("{\"data\":null,\"msg\":\"Add report success\",\"code\":0}")) | ||
.andReturn(); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters