Modify monitoring and add transaction support. (#88)
* feat: MonitorsController Chinese and English support #huacheng * feat: [manager]feature:Delete notification operation compatibility query is empty #huacheng * feat: [manager,alert,collector,common]feature:Modify monitoring and add transaction support. Monitoring compatible with Chinese and English #wqh Co-authored-by: tomsun28 <tomsun28@outlook.com>
This commit is contained in:
@@ -1,6 +1,7 @@
|
||||
package com.usthe.manager.controller;
|
||||
|
||||
import com.usthe.common.entity.dto.Message;
|
||||
import com.usthe.common.entity.manager.Monitor;
|
||||
import com.usthe.manager.pojo.dto.MonitorDto;
|
||||
import com.usthe.manager.service.MonitorService;
|
||||
import io.swagger.annotations.Api;
|
||||
@@ -23,11 +24,13 @@ import static com.usthe.common.util.CommonConstants.MONITOR_NOT_EXIST_CODE;
|
||||
import static org.springframework.http.MediaType.APPLICATION_JSON_VALUE;
|
||||
|
||||
/**
|
||||
* Monitoring management API
|
||||
* 监控管理API
|
||||
*
|
||||
* @author tomsun28
|
||||
* @date 2021/11/14 10:57
|
||||
*/
|
||||
@Api(tags = "监控管理API")
|
||||
@Api(tags = "en: Monitoring management API,zh: 监控管理API")
|
||||
@RestController
|
||||
@RequestMapping(path = "/monitor", produces = {APPLICATION_JSON_VALUE})
|
||||
public class MonitorController {
|
||||
@@ -36,12 +39,12 @@ public class MonitorController {
|
||||
private MonitorService monitorService;
|
||||
|
||||
@PostMapping
|
||||
@ApiOperation(value = "新增监控", notes = "新增一个监控应用")
|
||||
@ApiOperation(value = "Add a monitoring application", notes = "新增一个监控应用")
|
||||
public ResponseEntity<Message<Void>> addNewMonitor(@Valid @RequestBody MonitorDto monitorDto) {
|
||||
// 校验请求数据
|
||||
// Verify request data 校验请求数据
|
||||
monitorService.validate(monitorDto, false);
|
||||
if (monitorDto.isDetected()) {
|
||||
// 进行探测
|
||||
// Probe 进行探测
|
||||
monitorService.detectMonitor(monitorDto.getMonitor(), monitorDto.getParams());
|
||||
}
|
||||
monitorService.addMonitor(monitorDto.getMonitor(), monitorDto.getParams());
|
||||
@@ -49,12 +52,12 @@ public class MonitorController {
|
||||
}
|
||||
|
||||
@PutMapping
|
||||
@ApiOperation(value = "修改监控", notes = "修改一个已存在监控应用")
|
||||
@ApiOperation(value = "Modify an existing monitoring application", notes = "修改一个已存在监控应用")
|
||||
public ResponseEntity<Message<Void>> modifyMonitor(@Valid @RequestBody MonitorDto monitorDto) {
|
||||
// 校验请求数据
|
||||
// Verify request data 校验请求数据
|
||||
monitorService.validate(monitorDto, true);
|
||||
if (monitorDto.isDetected()) {
|
||||
// 进行探测
|
||||
// Probe 进行探测
|
||||
monitorService.detectMonitor(monitorDto.getMonitor(), monitorDto.getParams());
|
||||
}
|
||||
monitorService.modifyMonitor(monitorDto.getMonitor(), monitorDto.getParams());
|
||||
@@ -62,9 +65,10 @@ public class MonitorController {
|
||||
}
|
||||
|
||||
@GetMapping(path = "/{id}")
|
||||
@ApiOperation(value = "查询监控", notes = "根据监控ID获取监控信息")
|
||||
@ApiOperation(value = "Obtain monitoring information based on monitoring ID", notes = "根据监控ID获取监控信息")
|
||||
public ResponseEntity<Message<MonitorDto>> getMonitor(
|
||||
@ApiParam(value = "监控ID", example = "6565463543") @PathVariable("id") final long id) {
|
||||
// Get monitoring information
|
||||
// 获取监控信息
|
||||
MonitorDto monitorDto = monitorService.getMonitorDto(id);
|
||||
Message.MessageBuilder<MonitorDto> messageBuilder = Message.builder();
|
||||
@@ -77,16 +81,20 @@ public class MonitorController {
|
||||
}
|
||||
|
||||
@DeleteMapping(path = "/{id}")
|
||||
@ApiOperation(value = "删除监控", notes = "根据监控ID删除监控应用,监控不存在也是删除成功")
|
||||
@ApiOperation(value = "Delete monitoring application based on monitoring ID", notes = "根据监控ID删除监控应用")
|
||||
public ResponseEntity<Message<Void>> deleteMonitor(
|
||||
@ApiParam(value = "监控ID", example = "6565463543") @PathVariable("id") final long id) {
|
||||
// 删除监控,监控不存在或删除成功都返回成功
|
||||
@ApiParam(value = "en: Monitor ID,zh: 监控ID", example = "6565463543") @PathVariable("id") final long id) {
|
||||
// delete monitor 删除监控
|
||||
Monitor monitor = monitorService.getMonitor(id);
|
||||
if (monitor == null) {
|
||||
return ResponseEntity.ok(new Message<>("The specified monitoring was not queried, please check whether the parameters are correct"));
|
||||
}
|
||||
monitorService.deleteMonitor(id);
|
||||
return ResponseEntity.ok(new Message<>("Delete success"));
|
||||
}
|
||||
|
||||
@PostMapping(path = "/detect")
|
||||
@ApiOperation(value = "探测监控", notes = "根据监控信息去对此监控进行可用性探测")
|
||||
@ApiOperation(value = "Perform availability detection on this monitoring based on monitoring information", notes = "根据监控信息去对此监控进行可用性探测")
|
||||
public ResponseEntity<Message<Void>> detectMonitor(@Valid @RequestBody MonitorDto monitorDto) {
|
||||
monitorService.validate(monitorDto, null);
|
||||
monitorService.detectMonitor(monitorDto.getMonitor(), monitorDto.getParams());
|
||||
|
||||
@@ -8,27 +8,34 @@ import java.util.Set;
|
||||
|
||||
/**
|
||||
* ParamDao 数据库操作
|
||||
*
|
||||
* @author tomsun28
|
||||
* @date 2021/11/14 11:26
|
||||
*/
|
||||
public interface ParamDao extends JpaRepository<Param, Long> {
|
||||
|
||||
/**
|
||||
* Query the list of parameters associated with the monitoring ID'
|
||||
* 根据监控ID查询与之关联的参数列表
|
||||
* @param monitorId 监控ID
|
||||
* @return 参数值列表
|
||||
*
|
||||
* @param monitorId Monitor ID 监控ID
|
||||
* @return list of parameter values 参数值列表
|
||||
*/
|
||||
List<Param> findParamsByMonitorId(long monitorId);
|
||||
|
||||
/**
|
||||
* Remove the parameter list associated with the monitoring ID based on it
|
||||
* 根据监控ID删除与之关联的参数列表
|
||||
* @param monitorId 监控ID
|
||||
*
|
||||
* @param monitorId Monitor Id 监控ID
|
||||
*/
|
||||
void deleteParamsByMonitorId(long monitorId);
|
||||
|
||||
/**
|
||||
* Remove the parameter list associated with the monitoring ID list based on it
|
||||
* 根据监控ID列表删除与之关联的参数列表
|
||||
* @param monitorIds 监控ID列表
|
||||
*
|
||||
* @param monitorIds Monitoring ID List 监控ID列表
|
||||
*/
|
||||
void deleteParamsByMonitorIdIn(Set<Long> monitorIds);
|
||||
}
|
||||
|
||||
@@ -14,15 +14,18 @@ import static io.swagger.annotations.ApiModelProperty.AccessMode.READ_ONLY;
|
||||
import static io.swagger.annotations.ApiModelProperty.AccessMode.READ_WRITE;
|
||||
|
||||
/**
|
||||
* Monitoring Information External Interaction Entities
|
||||
* 监控信息对外交互实体
|
||||
*
|
||||
* @author tomsun28
|
||||
* @date 2021/11/14 10:13
|
||||
*/
|
||||
@Data
|
||||
@ApiModel(description = "监控信息实体")
|
||||
@ApiModel(description = "en: Monitoring information entities,zh: 监控信息实体")
|
||||
public class MonitorDto {
|
||||
|
||||
/**
|
||||
* Monitoring entity
|
||||
* 监控实体
|
||||
*/
|
||||
@ApiModelProperty(value = "监控实体", accessMode = READ_WRITE, position = 0)
|
||||
@@ -31,17 +34,22 @@ public class MonitorDto {
|
||||
private Monitor monitor;
|
||||
|
||||
/**
|
||||
* 参数
|
||||
* Params 参数
|
||||
*/
|
||||
@ApiModelProperty(value = "监控参数", accessMode = READ_WRITE, position = 1)
|
||||
@NotNull
|
||||
@Valid
|
||||
private List<Param> params;
|
||||
|
||||
/**
|
||||
* List of indicator groups
|
||||
* 指标组列表
|
||||
*/
|
||||
@ApiModelProperty(value = "指标组列表", accessMode = READ_ONLY, position = 2)
|
||||
private List<String> metrics;
|
||||
|
||||
/**
|
||||
* Whether to detect
|
||||
* 是否探测
|
||||
*/
|
||||
@ApiModelProperty(value = "是否进行探测", accessMode = READ_WRITE, position = 3)
|
||||
|
||||
@@ -9,6 +9,7 @@ import java.util.Map;
|
||||
|
||||
/**
|
||||
* 监控类型管理接口
|
||||
*
|
||||
* @author tomsun28
|
||||
* @date 2021/11/14 17:12
|
||||
*/
|
||||
@@ -16,21 +17,26 @@ public interface AppService {
|
||||
|
||||
/**
|
||||
* 根据监控类型查询定义的参数结构
|
||||
*
|
||||
* @param app 监控类型
|
||||
* @return 参数结构列表
|
||||
*/
|
||||
List<ParamDefine> getAppParamDefines(String app);
|
||||
|
||||
/**
|
||||
* Get monitor structure definition based on monitor type name
|
||||
* 根据监控类型名称获取监控结构定义
|
||||
* @param app 监控类型名称
|
||||
* @return 监控结构定义
|
||||
* @throws IllegalArgumentException 当不存在即不支持对应名称的监控类型时抛出
|
||||
*
|
||||
* @param app Monitoring type name 监控类型名称
|
||||
* @return Monitoring Structure Definition 监控结构定义
|
||||
* @throws IllegalArgumentException Thrown when there is no monitoring type with the corresponding name that is not supported
|
||||
* 当不存在即不支持对应名称的监控类型时抛出
|
||||
*/
|
||||
Job getAppDefine(String app) throws IllegalArgumentException;
|
||||
|
||||
/**
|
||||
* 获取定义的监控I18N资源
|
||||
*
|
||||
* @param lang 语言类型
|
||||
* @return I18N资源
|
||||
*/
|
||||
@@ -38,6 +44,7 @@ public interface AppService {
|
||||
|
||||
/**
|
||||
* 查询所有监控的类型-指标组-指标层级
|
||||
*
|
||||
* @param lang 语言
|
||||
* @return 层级信息
|
||||
*/
|
||||
|
||||
@@ -23,46 +23,50 @@ public interface MonitorService {
|
||||
|
||||
|
||||
/**
|
||||
* Monitoring Availability Probes
|
||||
* 监控可用性探测
|
||||
*
|
||||
* @param monitor 监控实体信息
|
||||
* @param params 参数信息
|
||||
* @throws MonitorDetectException 探测失败抛出
|
||||
* @param monitor Monitoring entity information 监控实体信息
|
||||
* @param params Parameter information 参数信息
|
||||
* @throws MonitorDetectException Probe failure throws 探测失败抛出
|
||||
*/
|
||||
void detectMonitor(Monitor monitor, List<Param> params) throws MonitorDetectException;
|
||||
|
||||
/**
|
||||
* 新增监控
|
||||
* Add monitoring 新增监控
|
||||
*
|
||||
* @param monitor 监控实体
|
||||
* @param params 参数信息
|
||||
* @throws RuntimeException 新增过程异常抛出
|
||||
* @param monitor Monitoring Entity 监控实体
|
||||
* @param params Parameter information 参数信息
|
||||
* @throws RuntimeException Add process exception throw 新增过程异常抛出
|
||||
*/
|
||||
void addMonitor(Monitor monitor, List<Param> params) throws RuntimeException;
|
||||
|
||||
/**
|
||||
* Verify the correctness of request data parameters
|
||||
* 校验请求数据参数正确性
|
||||
*
|
||||
* @param monitorDto monitorDto
|
||||
* @param isModify 是否是修改监控
|
||||
* @throws IllegalArgumentException 校验参数错误抛出
|
||||
* @param isModify Whether it is a modification monitoring 是否是修改监控
|
||||
* @throws IllegalArgumentException Validation parameter error thrown 校验参数错误抛出
|
||||
*/
|
||||
void validate(MonitorDto monitorDto, Boolean isModify) throws IllegalArgumentException;
|
||||
|
||||
/**
|
||||
* Modify update monitoring
|
||||
* 修改更新监控
|
||||
*
|
||||
* @param monitor 监控实体
|
||||
* @param params 参数信息
|
||||
* @throws RuntimeException 修改过程中异常抛出
|
||||
* @param monitor Monitor Entity 监控实体
|
||||
* @param params Parameter information 参数信息
|
||||
* @throws RuntimeException Exception thrown during modification 修改过程中异常抛出
|
||||
*/
|
||||
void modifyMonitor(Monitor monitor, List<Param> params) throws RuntimeException;
|
||||
|
||||
/**
|
||||
* Delete Monitor
|
||||
* 删除监控
|
||||
*
|
||||
* @param id 监控ID
|
||||
* @throws RuntimeException 删除过程中异常抛出
|
||||
* @param id Monitor ID 监控ID
|
||||
* @throws RuntimeException Exception thrown during deletion 删除过程中异常抛出
|
||||
*/
|
||||
void deleteMonitor(long id) throws RuntimeException;
|
||||
|
||||
|
||||
@@ -80,11 +80,13 @@ public class MonitorServiceImpl implements MonitorService {
|
||||
List<Configmap> configmaps = params.stream().map(param ->
|
||||
new Configmap(param.getField(), param.getValue(), param.getType())).collect(Collectors.toList());
|
||||
appDefine.setConfigmap(configmaps);
|
||||
// To detect availability, you only need to collect the set of availability indicators with a priority of 0.
|
||||
// 探测可用性只需要采集优先级为0的可用性指标集合
|
||||
List<Metrics> availableMetrics = appDefine.getMetrics().stream()
|
||||
.filter(item -> item.getPriority() == 0).collect(Collectors.toList());
|
||||
appDefine.setMetrics(availableMetrics);
|
||||
List<CollectRep.MetricsData> collectRep = collectJobService.collectSyncJobData(appDefine);
|
||||
// If the detection result fails, a detection exception is thrown
|
||||
// 判断探测结果 失败则抛出探测异常
|
||||
if (collectRep == null || collectRep.isEmpty()) {
|
||||
throw new MonitorDetectException("No collector response");
|
||||
@@ -97,9 +99,9 @@ public class MonitorServiceImpl implements MonitorService {
|
||||
@Override
|
||||
@Transactional(rollbackFor = Exception.class)
|
||||
public void addMonitor(Monitor monitor, List<Param> params) throws RuntimeException {
|
||||
// 申请 monitor id
|
||||
// Apply for monitor id 申请 monitor id
|
||||
long monitorId = SnowFlakeIdGenerator.generateId();
|
||||
// 构造采集任务Job实体
|
||||
// Construct the collection task Job entity 构造采集任务Job实体
|
||||
Job appDefine = appService.getAppDefine(monitor.getApp());
|
||||
appDefine.setMonitorId(monitorId);
|
||||
appDefine.setInterval(monitor.getIntervals());
|
||||
@@ -110,8 +112,10 @@ public class MonitorServiceImpl implements MonitorService {
|
||||
return new Configmap(param.getField(), param.getValue(), param.getType());
|
||||
}).collect(Collectors.toList());
|
||||
appDefine.setConfigmap(configmaps);
|
||||
// Send the collection task to get the job ID
|
||||
// 下发采集任务得到jobId
|
||||
long jobId = collectJobService.addAsyncCollectJob(appDefine);
|
||||
// Brush the library after the download is successful
|
||||
// 下发成功后刷库
|
||||
try {
|
||||
monitor.setId(monitorId);
|
||||
@@ -121,6 +125,7 @@ public class MonitorServiceImpl implements MonitorService {
|
||||
paramDao.saveAll(params);
|
||||
} catch (Exception e) {
|
||||
log.error(e.getMessage(), e);
|
||||
// Repository brushing abnormally cancels the previously delivered task
|
||||
// 刷库异常取消之前的下发任务
|
||||
collectJobService.cancelAsyncCollectJob(jobId);
|
||||
throw new MonitorDatabaseException(e.getMessage());
|
||||
@@ -130,6 +135,7 @@ public class MonitorServiceImpl implements MonitorService {
|
||||
@Override
|
||||
@Transactional(readOnly = true)
|
||||
public void validate(MonitorDto monitorDto, Boolean isModify) throws IllegalArgumentException {
|
||||
// The request monitoring parameter matches the monitoring parameter definition mapping check
|
||||
// 请求监控参数与监控参数定义映射校验匹配
|
||||
Monitor monitor = monitorDto.getMonitor();
|
||||
monitor.setHost(monitor.getHost().trim());
|
||||
@@ -142,7 +148,7 @@ public class MonitorServiceImpl implements MonitorService {
|
||||
param.setValue(value);
|
||||
})
|
||||
.collect(Collectors.toMap(Param::getField, param -> param));
|
||||
// 校验名称唯一性
|
||||
// Check name uniqueness 校验名称唯一性
|
||||
if (isModify != null) {
|
||||
Optional<Monitor> monitorOptional = monitorDao.findMonitorByNameEquals(monitor.getName());
|
||||
if (monitorOptional.isPresent()) {
|
||||
@@ -157,7 +163,7 @@ public class MonitorServiceImpl implements MonitorService {
|
||||
}
|
||||
}
|
||||
|
||||
// 参数定义结构校验
|
||||
// Parameter definition structure verification 参数定义结构校验
|
||||
List<ParamDefine> paramDefines = appService.getAppParamDefines(monitorDto.getMonitor().getApp());
|
||||
if (paramDefines != null) {
|
||||
for (ParamDefine paramDefine : paramDefines) {
|
||||
@@ -203,6 +209,7 @@ public class MonitorServiceImpl implements MonitorService {
|
||||
}
|
||||
break;
|
||||
case "password":
|
||||
// The plaintext password needs to be encrypted for transmission and storage
|
||||
// 明文密码需加密传输存储
|
||||
String passwordValue = param.getValue();
|
||||
if (!AesUtil.isCiphertext(passwordValue)) {
|
||||
@@ -212,7 +219,7 @@ public class MonitorServiceImpl implements MonitorService {
|
||||
param.setType(CommonConstants.PARAM_TYPE_PASSWORD);
|
||||
break;
|
||||
case "boolean":
|
||||
// boolean校验
|
||||
// boolean check
|
||||
String booleanValue = param.getValue();
|
||||
try {
|
||||
Boolean.parseBoolean(booleanValue);
|
||||
@@ -222,7 +229,7 @@ public class MonitorServiceImpl implements MonitorService {
|
||||
}
|
||||
break;
|
||||
case "radio":
|
||||
// radio单选值校验
|
||||
// radio single value check radio单选值校验
|
||||
List<ParamDefine.Option> options = paramDefine.getOptions();
|
||||
boolean invalid = true;
|
||||
if (options != null) {
|
||||
@@ -244,7 +251,8 @@ public class MonitorServiceImpl implements MonitorService {
|
||||
case "key-value":
|
||||
// todo key-value校验
|
||||
break;
|
||||
// todo 更多参数定义与实际值格式校验
|
||||
// todo More parameter definitions and actual value format verification
|
||||
// 更多参数定义与实际值格式校验
|
||||
default:
|
||||
throw new IllegalArgumentException("ParamDefine type " + paramDefine.getType() + " is invalid.");
|
||||
}
|
||||
@@ -254,8 +262,10 @@ public class MonitorServiceImpl implements MonitorService {
|
||||
}
|
||||
|
||||
@Override
|
||||
@Transactional(rollbackFor = Exception.class)
|
||||
public void modifyMonitor(Monitor monitor, List<Param> params) throws RuntimeException {
|
||||
long monitorId = monitor.getId();
|
||||
// Check to determine whether the monitor corresponding to the monitor Id exists
|
||||
// 查判断monitorId对应的此监控是否存在
|
||||
Optional<Monitor> queryOption = monitorDao.findById(monitorId);
|
||||
if (!queryOption.isPresent()) {
|
||||
@@ -263,9 +273,11 @@ public class MonitorServiceImpl implements MonitorService {
|
||||
}
|
||||
Monitor preMonitor = queryOption.get();
|
||||
if (!preMonitor.getApp().equals(monitor.getApp())) {
|
||||
// The type of monitoring cannot be modified
|
||||
// 监控的类型不能修改
|
||||
throw new IllegalArgumentException("Can not modify monitor's app type");
|
||||
}
|
||||
// Construct the collection task Job entity
|
||||
// 构造采集任务Job实体
|
||||
Job appDefine = appService.getAppDefine(monitor.getApp());
|
||||
appDefine.setId(preMonitor.getJobId());
|
||||
@@ -276,14 +288,16 @@ public class MonitorServiceImpl implements MonitorService {
|
||||
List<Configmap> configmaps = params.stream().map(param ->
|
||||
new Configmap(param.getField(), param.getValue(), param.getType())).collect(Collectors.toList());
|
||||
appDefine.setConfigmap(configmaps);
|
||||
// 更新采集任务
|
||||
collectJobService.updateAsyncCollectJob(appDefine);
|
||||
// After the update is successfully released, refresh the library
|
||||
// 下发更新成功后刷库
|
||||
try {
|
||||
monitor.setJobId(preMonitor.getJobId());
|
||||
monitor.setStatus(preMonitor.getStatus());
|
||||
monitorDao.save(monitor);
|
||||
paramDao.saveAll(params);
|
||||
// Update the collection task after the storage is completed
|
||||
// 入库完成后更新采集任务
|
||||
collectJobService.updateAsyncCollectJob(appDefine);
|
||||
} catch (Exception e) {
|
||||
log.error(e.getMessage(), e);
|
||||
throw new MonitorDatabaseException(e.getMessage());
|
||||
|
||||
Reference in New Issue
Block a user