Skip to content

Commit

Permalink
[alerter] optimize the encoding of how to add Extern Alarm Manage API(#…
Browse files Browse the repository at this point in the history
  • Loading branch information
SurryChen authored Nov 8, 2023
1 parent 0ddf188 commit 4429068
Show file tree
Hide file tree
Showing 7 changed files with 244 additions and 107 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand All @@ -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<Message<Void>> 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<Message<Void>> 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"));
}

Expand Down
Original file line number Diff line number Diff line change
@@ -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<String, String> getLabels() throws Exception;

/**
* 云告警转为内部告警时的告警标注
*/
@JsonIgnore
public abstract Map<String, String> getAnnotations() throws Exception;

/**
* 云告警转为内部告警时的告警内容
*/
@JsonIgnore
public abstract String getContent() throws Exception;

}
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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;
Expand Down Expand Up @@ -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<String, String> getLabels() {
return Map.of("app", "TenCloud");
}

@Override
public Map<String, String> 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();
}

}


Original file line number Diff line number Diff line change
@@ -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<? extends CloudAlertReportAbstract> cloudServiceAlarmInformationEntity;

public static CloudServiceAlarmInformationEnum getEnumFromCloudServiceName(String name) {
return Arrays.stream(CloudServiceAlarmInformationEnum.values())
.filter(cloudService -> cloudService.cloudServiceName.equals(name))
.findFirst()
.orElse(null);
}

}

This file was deleted.

This file was deleted.

Loading

0 comments on commit 4429068

Please sign in to comment.