[monitor] 完成查询监控类型的参数结构,新增和查询监控接口编码

This commit is contained in:
tomsun28
2021-11-15 15:18:54 +08:00
parent 90e14bb76b
commit d60d7506b4
23 changed files with 475 additions and 46 deletions

View File

@@ -14,6 +14,8 @@ import org.springframework.web.bind.annotation.RestController;
import java.util.List;
import static org.springframework.http.MediaType.APPLICATION_JSON_VALUE;
/**
* 监控类型管理API
* @author tomsun28
@@ -21,7 +23,7 @@ import java.util.List;
*/
@Api(tags = "监控类型管理API")
@RestController
@RequestMapping(path = "/apps")
@RequestMapping(path = "/apps", produces = {APPLICATION_JSON_VALUE})
public class AppController {
@Autowired

View File

@@ -7,6 +7,7 @@ import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
@@ -26,7 +27,7 @@ import static org.springframework.http.MediaType.APPLICATION_JSON_VALUE;
*/
@Api(tags = "监控管理API")
@RestController
@RequestMapping(path = "/monitor", consumes = {APPLICATION_JSON_VALUE}, produces = {APPLICATION_JSON_VALUE})
@RequestMapping(path = "/monitor", produces = {APPLICATION_JSON_VALUE})
public class MonitorController {
@Autowired
@@ -34,23 +35,23 @@ public class MonitorController {
@PostMapping
@ApiOperation(value = "新增监控", notes = "新增一个监控应用")
public ResponseEntity<Message<Void>> addNewMonitor(@RequestBody MonitorDto monitorDto) {
public ResponseEntity<Message<Void>> addNewMonitor(@Validated @RequestBody MonitorDto monitorDto) {
// 校验请求数据
monitorService.validate(monitorDto, false);
if (monitorDto.isDetected()) {
if (monitorDto.getDetected()) {
// 进行探测
monitorService.detectMonitor(monitorDto.getMonitor(), monitorDto.getParams());
}
monitorService.addMonitor(monitorDto.getMonitor(), monitorDto.getParams());
return ResponseEntity.ok().build();
return ResponseEntity.ok(new Message<>("Add success"));
}
@PutMapping
@ApiOperation(value = "修改监控", notes = "修改一个已存在监控应用")
public ResponseEntity<Message<Void>> modifyMonitor(@RequestBody MonitorDto monitorDto) {
public ResponseEntity<Message<Void>> modifyMonitor(@Validated @RequestBody MonitorDto monitorDto) {
// 校验请求数据
monitorService.validate(monitorDto, true);
if (monitorDto.isDetected()) {
if (monitorDto.getDetected()) {
// 进行探测
monitorService.detectMonitor(monitorDto.getMonitor(), monitorDto.getParams());
}
@@ -82,7 +83,7 @@ public class MonitorController {
@PostMapping(path = "/detect")
@ApiOperation(value = "探测监控", notes = "根据监控信息去对此监控进行可用性探测")
public ResponseEntity<Message<Void>> detectMonitor(@RequestBody MonitorDto monitorDto) {
public ResponseEntity<Message<Void>> detectMonitor(@Validated @RequestBody MonitorDto monitorDto) {
monitorService.validate(monitorDto, false);
monitorService.detectMonitor(monitorDto.getMonitor(), monitorDto.getParams());
return ResponseEntity.ok(new Message<>("Detect success."));

View File

@@ -3,10 +3,19 @@ package com.usthe.manager.dao;
import com.usthe.manager.pojo.entity.Param;
import org.springframework.data.jpa.repository.JpaRepository;
import java.util.List;
/**
* ParamDao 数据库操作
* @author tomsun28
* @date 2021/11/14 11:26
*/
public interface ParamDao extends JpaRepository<Param, Long> {
/**
* 根据监控ID查询与之关联的参数列表
* @param monitorId 监控ID
* @return 参数值列表
*/
List<Param> findParamsByMonitorId(long monitorId);
}

View File

@@ -3,6 +3,9 @@ package com.usthe.manager.dao;
import com.usthe.manager.pojo.entity.ParamDefine;
import org.springframework.data.jpa.repository.JpaRepository;
import java.util.List;
import java.util.Optional;
/**
* ParamDefine数据库操作
* @author tomsun28
@@ -10,4 +13,10 @@ import org.springframework.data.jpa.repository.JpaRepository;
*/
public interface ParamDefineDao extends JpaRepository<ParamDefine, Long> {
/**
* 根据监控类型查询其下的参数定义
* @param app 监控类型
* @return 参数定义列表
*/
List<ParamDefine> findParamDefinesByApp(String app);
}

View File

@@ -6,6 +6,7 @@ import io.swagger.annotations.ApiModel;
import io.swagger.annotations.ApiModelProperty;
import lombok.Data;
import javax.validation.constraints.NotNull;
import java.util.List;
import static io.swagger.annotations.ApiModelProperty.AccessMode.READ_WRITE;
@@ -23,17 +24,19 @@ public class MonitorDto {
* 监控实体
*/
@ApiModelProperty(value = "监控实体", accessMode = READ_WRITE, position = 0)
@NotNull
private Monitor monitor;
/**
* 参数
*/
@ApiModelProperty(value = "监控参数", accessMode = READ_WRITE, position = 1)
@NotNull
private List<Param> params;
/**
* 是否探测
*/
@ApiModelProperty(value = "是否进行探测", accessMode = READ_WRITE, position = 2)
private boolean detected;
private Boolean detected;
}

View File

@@ -6,12 +6,13 @@ import lombok.AllArgsConstructor;
import lombok.Builder;
import lombok.Data;
import lombok.NoArgsConstructor;
import org.hibernate.validator.constraints.Length;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.Table;
import javax.validation.constraints.Max;
import javax.validation.constraints.Min;
import java.time.LocalDateTime;
import static io.swagger.annotations.ApiModelProperty.AccessMode.READ_ONLY;
@@ -32,7 +33,6 @@ import static io.swagger.annotations.ApiModelProperty.AccessMode.READ_WRITE;
public class Monitor {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
@ApiModelProperty(value = "监控ID", example = "87584674384", accessMode = READ_ONLY, position = 0)
private Long id;
@@ -46,36 +46,43 @@ public class Monitor {
* 监控的名称
*/
@ApiModelProperty(value = "监控名称", example = "Api-bing.com", accessMode = READ_WRITE, position = 2)
@Length(max = 100)
private String name;
/**
* 监控的类型:linux,mysql,jvm...
*/
@ApiModelProperty(value = "监控类型", example = "api", accessMode = READ_WRITE, position = 3)
@Length(max = 100)
private String app;
/**
* 监控的对端host:ipv4,ipv6,域名
*/
@ApiModelProperty(value = "监控的对端host", example = "192.167.25.11", accessMode = READ_WRITE, position = 4)
@Length(max = 100)
private String host;
/**
* 监控的采集间隔时间,单位秒
*/
@ApiModelProperty(value = "监控的采集间隔时间,单位秒", example = "600", accessMode = READ_WRITE, position = 5)
@Min(10)
private Integer intervals;
/**
* 监控状态 0:未监控,1:可用,2:不可用,3:不可达,4:挂起
*/
@ApiModelProperty(value = "监控状态 0:未监控,1:可用,2:不可用,3:不可达,4:挂起", example = "1", accessMode = READ_WRITE, position = 6)
@Min(0)
@Max(4)
private byte status;
/**
* 监控备注描述
*/
@ApiModelProperty(value = "监控备注描述", example = "对搜索网站bing的可用性监控", accessMode = READ_WRITE, position = 7)
@Length(max = 255)
private String description;
/**

View File

@@ -6,12 +6,15 @@ import lombok.AllArgsConstructor;
import lombok.Builder;
import lombok.Data;
import lombok.NoArgsConstructor;
import org.hibernate.validator.constraints.Length;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.Table;
import javax.validation.constraints.Max;
import javax.validation.constraints.Min;
import java.time.LocalDateTime;
import static io.swagger.annotations.ApiModelProperty.AccessMode.READ_ONLY;
@@ -33,7 +36,7 @@ public class Param {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
@ApiModelProperty(value = "参数ID", example = "87584674384", accessMode = READ_ONLY, position = 0)
@ApiModelProperty(value = "参数主键索引ID", example = "87584674384", accessMode = READ_ONLY, position = 0)
private Long id;
/**
@@ -46,18 +49,22 @@ public class Param {
* 参数字段标识符
*/
@ApiModelProperty(value = "参数标识符字段", example = "port", accessMode = READ_WRITE, position = 2)
@Length(max = 100)
private String field;
/**
* 参数值
*/
@ApiModelProperty(value = "参数值", example = "8080", accessMode = READ_WRITE, position = 3)
@Length(max = 255)
private String value;
/**
* 参数类型 0:数字 1:字符串 2:加密串
*/
@ApiModelProperty(value = "参数类型 0:数字 1:字符串 2:加密串", example = "0", accessMode = READ_WRITE, position = 4)
@Min(0)
@Max(2)
private byte type;
/**

View File

@@ -1,5 +1,6 @@
package com.usthe.manager.service;
import com.usthe.common.entity.job.Job;
import com.usthe.manager.pojo.entity.ParamDefine;
import java.util.List;
@@ -17,4 +18,12 @@ public interface AppService {
* @return 参数结构列表
*/
List<ParamDefine> getAppParamDefines(String app);
/**
* 根据监控类型名称获取监控结构定义
* @param app 监控类型名称
* @return 监控结构定义
* @throws IllegalArgumentException 当不存在即不支持对应名称的监控类型时抛出
*/
Job getAppDefine(String app) throws IllegalArgumentException;
}

View File

@@ -1,10 +1,26 @@
package com.usthe.manager.service.impl;
import com.usthe.common.entity.job.Job;
import com.usthe.manager.dao.ParamDefineDao;
import com.usthe.manager.pojo.entity.ParamDefine;
import com.usthe.manager.service.AppService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.CommandLineRunner;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.yaml.snakeyaml.Yaml;
import javax.persistence.criteria.Join;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.net.URL;
import java.util.Collections;
import java.util.List;
import java.util.Map;
import java.util.Objects;
import java.util.concurrent.ConcurrentHashMap;
/**
* 监控类型管理实现
@@ -12,11 +28,54 @@ import java.util.List;
* @date 2021/11/14 17:17
*/
@Service
public class AppServiceImpl implements AppService {
@Transactional(rollbackFor = Exception.class)
@Slf4j
public class AppServiceImpl implements AppService, CommandLineRunner {
private final Map<String, Job> appDefines = new ConcurrentHashMap<>();
@Autowired
private ParamDefineDao paramDefineDao;
@Override
public List<ParamDefine> getAppParamDefines(String app) {
List<ParamDefine> paramDefines = paramDefineDao.findParamDefinesByApp(app);
if (paramDefines == null) {
paramDefines = Collections.emptyList();
}
return paramDefines;
}
return null;
@Override
public Job getAppDefine(String app) throws IllegalArgumentException {
Job appDefine = appDefines.get(app);
if (appDefine == null) {
throw new IllegalArgumentException("The app " + app + " not support.");
}
return appDefine;
}
@Override
public void run(String... args) throws Exception {
// 读取app定义配置加载到内存中 define/app/*.yml
Yaml yaml = new Yaml();
String defineAppPath = "define" + File.separator + "app";
URL url = Thread.currentThread().getContextClassLoader().getResource(defineAppPath);
assert url != null;
File directory = new File(url.toURI());
if (!directory.exists() || directory.listFiles() == null) {
throw new IllegalArgumentException("define app directory not exist");
}
for (File appFile : Objects.requireNonNull(directory.listFiles())) {
if (appFile.exists()) {
try (FileInputStream fileInputStream = new FileInputStream(appFile)) {
Job app = yaml.loadAs(fileInputStream, Job.class);
appDefines.put(app.getApp(), app);
} catch (IOException e) {
log.error(e.getMessage(), e);
throw new IOException(e);
}
}
}
}
}

View File

@@ -1,13 +1,27 @@
package com.usthe.manager.service.impl;
import com.usthe.common.entity.job.Configmap;
import com.usthe.common.entity.job.Job;
import com.usthe.common.util.CommonConstants;
import com.usthe.common.util.SnowFlakeIdGenerator;
import com.usthe.manager.dao.MonitorDao;
import com.usthe.manager.dao.ParamDao;
import com.usthe.manager.pojo.dto.MonitorDto;
import com.usthe.manager.pojo.entity.Monitor;
import com.usthe.manager.pojo.entity.Param;
import com.usthe.manager.service.AppService;
import com.usthe.manager.service.MonitorService;
import com.usthe.manager.support.exception.MonitorDatabaseException;
import com.usthe.manager.support.exception.MonitorDetectException;
import com.usthe.scheduler.JobScheduling;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import java.util.List;
import java.util.Optional;
import java.util.stream.Collectors;
/**
* 监控管理服务实现
@@ -15,20 +29,66 @@ import java.util.List;
* @date 2021/11/14 13:06
*/
@Service
@Slf4j
public class MonitorServiceImpl implements MonitorService {
@Autowired
private AppService appService;
@Autowired
private JobScheduling jobScheduling;
@Autowired
private MonitorDao monitorDao;
@Autowired
private ParamDao paramDao;
@Override
@Transactional(readOnly = true)
public void detectMonitor(Monitor monitor, List<Param> params) throws MonitorDetectException {
}
@Override
@Transactional(rollbackFor = Exception.class)
public void addMonitor(Monitor monitor, List<Param> params) throws RuntimeException {
// 申请 monitor id
long monitorId = SnowFlakeIdGenerator.generateId();
// 构造采集任务Job实体
Job appDefine = appService.getAppDefine(monitor.getApp());
appDefine.setMonitorId(monitorId);
appDefine.setInterval(monitor.getIntervals());
appDefine.setCyclic(true);
appDefine.setTimestamp(System.currentTimeMillis());
List<Configmap> configmaps = params.stream().map(param -> {
param.setMonitorId(monitorId);
param.setGmtCreate(null);
param.setGmtUpdate(null);
return new Configmap(param.getField(), param.getValue(), param.getType());
}).collect(Collectors.toList());
appDefine.setConfigmap(configmaps);
// 下发采集任务得到jobId
long jobId = jobScheduling.addAsyncCollectJob(appDefine);
// 下发成功后刷库
try {
monitor.setId(monitorId);
monitor.setJobId(jobId);
monitor.setStatus(CommonConstants.AVAILABLE);
monitor.setGmtCreate(null);
monitor.setGmtUpdate(null);
monitorDao.save(monitor);
paramDao.saveAll(params);
} catch (Exception e) {
log.error(e.getMessage(), e);
// 刷库异常取消之前的下发任务
jobScheduling.cancelAsyncCollectJob(jobId);
throw new MonitorDatabaseException(e.getMessage());
}
}
@Override
@Transactional(readOnly = true)
public void validate(MonitorDto monitorDto, boolean isModify) throws IllegalArgumentException {
}
@@ -44,7 +104,17 @@ public class MonitorServiceImpl implements MonitorService {
}
@Override
@Transactional(readOnly = true)
public MonitorDto getMonitor(long id) throws RuntimeException {
return null;
Optional<Monitor> monitorOptional = monitorDao.findById(id);
if (monitorOptional.isPresent()) {
MonitorDto monitorDto = new MonitorDto();
monitorDto.setMonitor(monitorOptional.get());
List<Param> params = paramDao.findParamsByMonitorId(id);
monitorDto.setParams(params);
return monitorDto;
} else {
return null;
}
}
}

View File

@@ -2,6 +2,7 @@ package com.usthe.manager.support;
import com.usthe.common.entity.dto.Message;
import com.usthe.manager.support.exception.MonitorDatabaseException;
import com.usthe.manager.support.exception.MonitorDetectException;
import lombok.extern.slf4j.Slf4j;
import org.springframework.dao.DataAccessException;
@@ -14,6 +15,7 @@ import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestControllerAdvice;
import static com.usthe.common.util.CommonConstants.DETECT_FAILED;
import static com.usthe.common.util.CommonConstants.MONITOR_CONFLICT;
/**
* controller exception handler
@@ -36,6 +38,19 @@ public class GlobalExceptionHandler {
return ResponseEntity.status(HttpStatus.BAD_REQUEST).body(message);
}
/**
* 处理数据库操作异常
* @param exception 探测异常
* @return response
*/
@ExceptionHandler(MonitorDatabaseException.class)
@ResponseBody
ResponseEntity<Message<Void>> handleMonitorDatabaseException(MonitorDatabaseException exception) {
Message<Void> message = Message.<Void>builder().msg(exception.getMessage()).code(MONITOR_CONFLICT).build();
return ResponseEntity.status(HttpStatus.CONFLICT).body(message);
}
/**
* handler the exception thrown for data input verify
* @param exception data input verify exception

View File

@@ -0,0 +1,12 @@
package com.usthe.manager.support.exception;
/**
* 数据库操作异常
* @author tomsun28
* @date 2021/11/15 13:25
*/
public class MonitorDatabaseException extends RuntimeException {
public MonitorDatabaseException(String message) {
super(message);
}
}

View File

@@ -18,7 +18,8 @@ CREATE TABLE monitor
modifier varchar(100) comment '最新修改者',
gmt_create timestamp default current_timestamp comment 'create time',
gmt_update datetime default current_timestamp on update current_timestamp comment 'update time',
primary key (id)
primary key (id),
index query_index (app, host, name)
) ENGINE = InnoDB DEFAULT CHARSET=utf8mb4;
-- ----------------------------
@@ -35,7 +36,8 @@ CREATE TABLE param
gmt_create timestamp default current_timestamp comment 'create time',
gmt_update datetime default current_timestamp on update current_timestamp comment 'update time',
primary key (id),
index monitor_id (monitor_id)
index monitor_id (monitor_id),
unique key unique_param (monitor_id, field)
) ENGINE = InnoDB DEFAULT CHARSET=utf8mb4;
-- ----------------------------
@@ -57,5 +59,5 @@ CREATE TABLE param_define
gmt_create timestamp default current_timestamp comment 'create time',
gmt_update datetime default current_timestamp on update current_timestamp comment 'update time',
primary key (id),
index app_index (app)
unique key unique_param_define (app, field)
) ENGINE = InnoDB DEFAULT CHARSET=utf8mb4;

View File

@@ -4,15 +4,15 @@ app: cloud
# 强制固定必须参数 - host
configmap:
- key: host
type: string
type: 1
- key: port
type: number
type: 0
- key: username
type: string
type: 1
- key: password
type: secret
type: 2
- key: param1
type: string
type: 1
# 指标组列表
metrics:
# 第一个监控指标组 cpu
@@ -25,7 +25,7 @@ metrics:
# 指标信息 包括 field名称, type字段类型:number数字,string字符串, unit:指标单位
- field: usage
type: number
unit: %
unit: '%'
- field: cores
type: number
- field: waitime
@@ -60,11 +60,11 @@ metrics:
ssl: false
# 请求头内容
headers:
apiVersion: 'v1'
apiVersion: v1
# 请求参数内容
params:
- param1: param1
- param2: param2
param1: param1
param2: param2
# 认证
authorization:
# 认证方式: Basic Auth, Digest Auth, Bearer Token
@@ -73,7 +73,7 @@ metrics:
basicAuthPassword: ^_^password^_^
# 响应数据解析方式: default-系统规则,json_path-jsonPath脚本,xml_path-xmlPath脚本,prometheus-Prometheus数据规则
parseType: jsonPath
parseScript: "$.cpu[:1].*"
parseScript: '$.cpu[:1].*'
- name: memory
priority: 1
@@ -83,7 +83,7 @@ metrics:
unit: kb
- field: usage
type: number
unit: %
unit: '%'
- field: speed
type: number
protocol: http
@@ -93,13 +93,13 @@ metrics:
url: /memory
method: GET
headers:
apiVersion: 'v1'
apiVersion: v1
params:
- param1: param1
- param2: param2
param1: param1
param2: param2
authorization:
type: Basic Auth
basicAuthUsername: ^_^username^_^
basicAuthPassword: ^_^password^_^
parseType: jsonPath
parseScript: "$.memory[:1].*"
parseScript: '$.memory[:1].*'