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 third report of TenCloud #1113

Merged
merged 18 commits into from
Jul 20, 2023
Merged
Show file tree
Hide file tree
Changes from 13 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,40 @@
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 = "Alarm Manage Batch API | 第三方告警管理API")
@RestController
@RequestMapping(path = "/api/alerts", produces = {APPLICATION_JSON_VALUE})
public class AlertConvertController {
@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"));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
package org.dromara.hertzbeat.alert.controller;

import org.dromara.hertzbeat.alert.dto.AlertSummary;
import org.dromara.hertzbeat.alert.service.impl.AlertConvertTenCloudServiceImpl;
import org.dromara.hertzbeat.common.entity.alerter.Alert;
import org.dromara.hertzbeat.alert.service.AlertService;
import org.dromara.hertzbeat.common.entity.dto.AlertReport;
Expand Down Expand Up @@ -153,4 +154,6 @@ public ResponseEntity<Message<Void>> addNewAlertReport(@Valid @RequestBody Alert
alertService.addNewAlertReport(alertReport);
return ResponseEntity.ok(new Message<>("Add report success"));
}


}
Original file line number Diff line number Diff line change
@@ -0,0 +1,81 @@
package org.dromara.hertzbeat.alert.dto;


import com.fasterxml.jackson.annotation.JsonProperty;
import lombok.AllArgsConstructor;
import lombok.NoArgsConstructor;

import java.io.Serializable;

/**
* @author zqr10159
* 腾讯云告警实体类
*/
@lombok.Data
@AllArgsConstructor
@NoArgsConstructor
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;

@lombok.Data
@AllArgsConstructor
@NoArgsConstructor
public static class AlarmObjInfo {
private String region;
private String namespace;
@JsonProperty("appID")
private String appId;
private String uin;
private Dimensions dimensions;
}

@lombok.Data
@AllArgsConstructor
@NoArgsConstructor
public static class Dimensions {
@JsonProperty("unInstanceID")
private String unInstanceId;
@JsonProperty("objID")
private String objId;
}
@lombok.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;
}
@lombok.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;
}

}


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);
}
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;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@

package org.dromara.hertzbeat.alert.service.impl;

import com.fasterxml.jackson.databind.ObjectMapper;
import org.dromara.hertzbeat.alert.reduce.AlarmCommonReduce;
import org.dromara.hertzbeat.alert.dao.AlertDao;
import org.dromara.hertzbeat.alert.dto.AlertPriorityNum;
Expand Down Expand Up @@ -167,4 +168,6 @@ private Alert buildAlertData(AlertReport alertReport){
.build();
}



}
4 changes: 1 addition & 3 deletions manager/src/main/resources/application.yml
Original file line number Diff line number Diff line change
Expand Up @@ -73,9 +73,7 @@ spring:
driver-class-name: org.h2.Driver
username: sa
password: 123456
url: jdbc:h2:./data/hertzbeat;MODE=MYSQL
hikari:
max-lifetime: 120000
url: jdbc:h2:./data/hertzbeat-test;MODE=MYSQL
zqr10159 marked this conversation as resolved.
Show resolved Hide resolved

jpa:
hibernate:
Expand Down
1 change: 1 addition & 0 deletions manager/src/main/resources/sureness.yml
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,7 @@ resourceRole:
# 需要被过滤保护的资源,不认证鉴权直接访问
# /api/v1/source3===get 表示 /api/v1/source3===get 可以被任何人访问 无需登录认证鉴权
excludedResource:
- /api/alerts/**===*
- /api/account/auth/**===*
- /api/i18n/**===get
- /api/metrics===get
Expand Down
Loading