From a081f34c1bd532ecbad06290127e5cc327138381 Mon Sep 17 00:00:00 2001 From: tomsun28 Date: Tue, 16 Nov 2021 17:47:15 +0800 Subject: [PATCH] =?UTF-8?q?[monitor]=20=E8=B0=83=E6=95=B4=E7=9B=91?= =?UTF-8?q?=E6=8E=A7=E5=8F=82=E6=95=B0=E5=AE=9A=E4=B9=89=E5=AD=97=E6=AE=B5?= =?UTF-8?q?,=E7=9B=91=E6=8E=A7=E5=8F=82=E6=95=B0=E5=92=8C=E7=9B=91?= =?UTF-8?q?=E6=8E=A7=E9=85=8D=E7=BD=AE=E6=9A=82=E6=97=B6=E5=AD=98=E5=9C=A8?= =?UTF-8?q?=E5=86=85=E5=AD=98?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../server/src/main/resources/application.yml | 3 +- .../com/usthe/common/entity/job/Metrics.java | 3 -- .../com/usthe/common/util/ProtoJsonUtil.java | 8 +++-- .../usthe/manager/pojo/dto/MonitorDto.java | 1 - .../manager/pojo/dto/ParamDefineDto.java | 23 ++++++++++++++ .../usthe/manager/pojo/entity/Monitor.java | 6 ++-- .../manager/pojo/entity/ParamDefine.java | 23 +++++++++----- .../manager/service/impl/AppServiceImpl.java | 31 ++++++++++++++++--- .../service/impl/MonitorServiceImpl.java | 5 ++- manager/src/main/resources/db/schema.sql | 1 + .../main/resources/define/app/A-example.yml | 2 +- .../main/resources/define/param/A-example.yml | 22 +++++++++---- 12 files changed, 96 insertions(+), 32 deletions(-) create mode 100644 manager/src/main/java/com/usthe/manager/pojo/dto/ParamDefineDto.java diff --git a/collector/server/src/main/resources/application.yml b/collector/server/src/main/resources/application.yml index 38f75b9..e2b0708 100644 --- a/collector/server/src/main/resources/application.yml +++ b/collector/server/src/main/resources/application.yml @@ -14,5 +14,6 @@ collector: endpoints: http://139.198.109.64:2379 export: kafka: - servers: localhost:9092 enabled: true + servers: localhost:9092 + topic: async-collect-data diff --git a/common/src/main/java/com/usthe/common/entity/job/Metrics.java b/common/src/main/java/com/usthe/common/entity/job/Metrics.java index da71dde..76efa3e 100644 --- a/common/src/main/java/com/usthe/common/entity/job/Metrics.java +++ b/common/src/main/java/com/usthe/common/entity/job/Metrics.java @@ -68,9 +68,6 @@ public class Metrics { */ private JdbcProtocol jdbc; - /** - * todo 替换指标信息 - */ @Data @AllArgsConstructor @NoArgsConstructor diff --git a/common/src/main/java/com/usthe/common/util/ProtoJsonUtil.java b/common/src/main/java/com/usthe/common/util/ProtoJsonUtil.java index ded1dca..3a1b618 100644 --- a/common/src/main/java/com/usthe/common/util/ProtoJsonUtil.java +++ b/common/src/main/java/com/usthe/common/util/ProtoJsonUtil.java @@ -1,7 +1,6 @@ package com.usthe.common.util; import com.google.protobuf.Message; -import com.google.protobuf.MessageOrBuilder; import com.google.protobuf.util.JsonFormat; import lombok.extern.slf4j.Slf4j; @@ -13,6 +12,9 @@ import lombok.extern.slf4j.Slf4j; @Slf4j public class ProtoJsonUtil { + private static final JsonFormat.Printer PRINTER = JsonFormat.printer(); + private static final JsonFormat.Parser PARSER = JsonFormat.parser(); + /** * protobuf 转 json * @param proto protobuf @@ -20,7 +22,7 @@ public class ProtoJsonUtil { */ public static String toJsonStr(Message proto) { try { - return JsonFormat.printer().print(proto); + return PRINTER.print(proto); } catch (Exception e) { log.error(e.getMessage(), e); return null; @@ -35,7 +37,7 @@ public class ProtoJsonUtil { */ public static Message toProtobuf(String json, Message.Builder builder) { try { - JsonFormat.parser().merge(json, builder); + PARSER.merge(json, builder); return builder.build(); } catch (Exception e) { log.error(e.getMessage(), e); diff --git a/manager/src/main/java/com/usthe/manager/pojo/dto/MonitorDto.java b/manager/src/main/java/com/usthe/manager/pojo/dto/MonitorDto.java index 8ddb749..1d204a8 100644 --- a/manager/src/main/java/com/usthe/manager/pojo/dto/MonitorDto.java +++ b/manager/src/main/java/com/usthe/manager/pojo/dto/MonitorDto.java @@ -5,7 +5,6 @@ import com.usthe.manager.pojo.entity.Param; import io.swagger.annotations.ApiModel; import io.swagger.annotations.ApiModelProperty; import lombok.Data; -import org.springframework.validation.annotation.Validated; import javax.validation.Valid; import javax.validation.constraints.NotNull; diff --git a/manager/src/main/java/com/usthe/manager/pojo/dto/ParamDefineDto.java b/manager/src/main/java/com/usthe/manager/pojo/dto/ParamDefineDto.java new file mode 100644 index 0000000..a6ef0d7 --- /dev/null +++ b/manager/src/main/java/com/usthe/manager/pojo/dto/ParamDefineDto.java @@ -0,0 +1,23 @@ +package com.usthe.manager.pojo.dto; + +import com.usthe.manager.pojo.entity.ParamDefine; +import lombok.AllArgsConstructor; +import lombok.Data; +import lombok.NoArgsConstructor; + +import java.util.List; + +/** + * 参数定义传输实体 + * @author tomsun28 + * @date 2021/11/16 16:50 + */ +@Data +@AllArgsConstructor +@NoArgsConstructor +public class ParamDefineDto { + + private String app; + + private List param; +} diff --git a/manager/src/main/java/com/usthe/manager/pojo/entity/Monitor.java b/manager/src/main/java/com/usthe/manager/pojo/entity/Monitor.java index 0fff1e6..56a48d2 100644 --- a/manager/src/main/java/com/usthe/manager/pojo/entity/Monitor.java +++ b/manager/src/main/java/com/usthe/manager/pojo/entity/Monitor.java @@ -45,14 +45,14 @@ public class Monitor { /** * 监控的名称 */ - @ApiModelProperty(value = "监控名称", example = "Api-bing.com", accessMode = READ_WRITE, position = 2) + @ApiModelProperty(value = "监控名称", example = "Api-TanCloud.cn", accessMode = READ_WRITE, position = 2) @Length(max = 100) private String name; /** * 监控的类型:linux,mysql,jvm... */ - @ApiModelProperty(value = "监控类型", example = "api", accessMode = READ_WRITE, position = 3) + @ApiModelProperty(value = "监控类型", example = "TanCloud", accessMode = READ_WRITE, position = 3) @Length(max = 100) private String app; @@ -81,7 +81,7 @@ public class Monitor { /** * 监控备注描述 */ - @ApiModelProperty(value = "监控备注描述", example = "对搜索网站bing的可用性监控", accessMode = READ_WRITE, position = 7) + @ApiModelProperty(value = "监控备注描述", example = "对SAAS网站TanCloud的可用性监控", accessMode = READ_WRITE, position = 7) @Length(max = 255) private String description; diff --git a/manager/src/main/java/com/usthe/manager/pojo/entity/ParamDefine.java b/manager/src/main/java/com/usthe/manager/pojo/entity/ParamDefine.java index 85944c1..4382450 100644 --- a/manager/src/main/java/com/usthe/manager/pojo/entity/ParamDefine.java +++ b/manager/src/main/java/com/usthe/manager/pojo/entity/ParamDefine.java @@ -19,6 +19,7 @@ import static io.swagger.annotations.ApiModelProperty.AccessMode.READ_ONLY; import static io.swagger.annotations.ApiModelProperty.AccessMode.READ_WRITE; /** + * todo 字段默认值 * 监控参数定义 * @author tomsun28 * @date 2021/11/13 21:49 @@ -40,7 +41,7 @@ public class ParamDefine { /** * 监控应用类型名称 */ - @ApiModelProperty(value = "监控类型", example = "api", accessMode = READ_WRITE, position = 1) + @ApiModelProperty(value = "监控类型", example = "TanCloud", accessMode = READ_WRITE, position = 1) private String app; /** @@ -61,17 +62,23 @@ public class ParamDefine { @ApiModelProperty(value = "字段类型,样式(大部分映射input标签type属性)", example = "number", accessMode = READ_WRITE, position = 4) private String type; + /** + * 是否是必输项 true-必填 false-可选 + */ + @ApiModelProperty(value = "是否是必输项 true-必填 false-可选", example = "true", accessMode = READ_WRITE, position = 5) + private boolean required = false; + /** * 当type为number时,用range表示范围 eg: 0-233 */ - @ApiModelProperty(value = "当type为number时,用range表示范围", example = "0-233", accessMode = READ_WRITE, position = 5) + @ApiModelProperty(value = "当type为number时,用range表示范围", example = "0-233", accessMode = READ_WRITE, position = 6) @Column(name = "param_range") private String range; /** * 当type为text时,用limit表示字符串限制大小.最大255 */ - @ApiModelProperty(value = "当type为text时,用limit表示字符串限制大小.最大255", example = "30", accessMode = READ_WRITE, position = 6) + @ApiModelProperty(value = "当type为text时,用limit表示字符串限制大小.最大255", example = "30", accessMode = READ_WRITE, position = 7) @Column(name = "param_limit") private short limit; @@ -79,31 +86,31 @@ public class ParamDefine { * 当type为radio单选框,checkbox复选框时,option表示可选项值列表 * eg: param3,param4,param5 */ - @ApiModelProperty(value = "当type为radio单选框,checkbox复选框时,option表示可选项值列表", example = "10,20,30", accessMode = READ_WRITE, position = 7) + @ApiModelProperty(value = "当type为radio单选框,checkbox复选框时,option表示可选项值列表", example = "10,20,30", accessMode = READ_WRITE, position = 8) @Column(name = "param_option") private String option; /** * 此条记录创建者 */ - @ApiModelProperty(value = "此条记录创建者", example = "tom", accessMode = READ_ONLY, position = 8) + @ApiModelProperty(value = "此条记录创建者", example = "tom", accessMode = READ_ONLY, position = 9) private String creator; /** * 此条记录最新修改者 */ - @ApiModelProperty(value = "此条记录最新修改者", example = "tom", accessMode = READ_ONLY, position = 9) + @ApiModelProperty(value = "此条记录最新修改者", example = "tom", accessMode = READ_ONLY, position = 10) private String modifier; /** * 记录创建时间 */ - @ApiModelProperty(value = "记录创建时间(毫秒时间戳)", example = "1612198922000", accessMode = READ_ONLY, position = 10) + @ApiModelProperty(value = "记录创建时间(毫秒时间戳)", example = "1612198922000", accessMode = READ_ONLY, position = 11) private LocalDateTime gmtCreate; /** * 记录最新修改时间 */ - @ApiModelProperty(value = "记录最新修改时间(毫秒时间戳)", example = "1612198444000", accessMode = READ_ONLY, position = 11) + @ApiModelProperty(value = "记录最新修改时间(毫秒时间戳)", example = "1612198444000", accessMode = READ_ONLY, position = 12) private LocalDateTime gmtUpdate; } diff --git a/manager/src/main/java/com/usthe/manager/service/impl/AppServiceImpl.java b/manager/src/main/java/com/usthe/manager/service/impl/AppServiceImpl.java index 442b363..aa1a323 100644 --- a/manager/src/main/java/com/usthe/manager/service/impl/AppServiceImpl.java +++ b/manager/src/main/java/com/usthe/manager/service/impl/AppServiceImpl.java @@ -2,6 +2,7 @@ package com.usthe.manager.service.impl; import com.usthe.common.entity.job.Job; import com.usthe.manager.dao.ParamDefineDao; +import com.usthe.manager.pojo.dto.ParamDefineDto; import com.usthe.manager.pojo.entity.ParamDefine; import com.usthe.manager.service.AppService; import lombok.extern.slf4j.Slf4j; @@ -11,7 +12,6 @@ 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; @@ -24,6 +24,7 @@ import java.util.concurrent.ConcurrentHashMap; /** * 监控类型管理实现 + * TODO 暂时将监控配置和参数配置存放内存 之后存入数据库 * @author tomsun28 * @date 2021/11/14 17:17 */ @@ -33,17 +34,18 @@ import java.util.concurrent.ConcurrentHashMap; public class AppServiceImpl implements AppService, CommandLineRunner { private final Map appDefines = new ConcurrentHashMap<>(); + private final Map> paramDefines = new ConcurrentHashMap<>(); @Autowired private ParamDefineDao paramDefineDao; @Override public List getAppParamDefines(String app) { - List paramDefines = paramDefineDao.findParamDefinesByApp(app); - if (paramDefines == null) { - paramDefines = Collections.emptyList(); + List params = paramDefines.get(app); + if (params == null) { + params = Collections.emptyList(); } - return paramDefines; + return params; } @Override @@ -77,5 +79,24 @@ public class AppServiceImpl implements AppService, CommandLineRunner { } } } + // 读取监控参数定义配置加载到数据库中 define/param/*.yml + String defineParamPath = "define" + File.separator + "param"; + url = Thread.currentThread().getContextClassLoader().getResource(defineParamPath); + assert url != null; + directory = new File(url.toURI()); + if (!directory.exists() || directory.listFiles() == null) { + throw new IllegalArgumentException("define param directory not exist"); + } + for (File appFile : Objects.requireNonNull(directory.listFiles())) { + if (appFile.exists()) { + try (FileInputStream fileInputStream = new FileInputStream(appFile)) { + ParamDefineDto paramDefine = yaml.loadAs(fileInputStream, ParamDefineDto.class); + paramDefines.put(paramDefine.getApp(), paramDefine.getParam()); + } catch (IOException e) { + log.error(e.getMessage(), e); + throw new IOException(e); + } + } + } } } diff --git a/manager/src/main/java/com/usthe/manager/service/impl/MonitorServiceImpl.java b/manager/src/main/java/com/usthe/manager/service/impl/MonitorServiceImpl.java index 799525c..06600e2 100644 --- a/manager/src/main/java/com/usthe/manager/service/impl/MonitorServiceImpl.java +++ b/manager/src/main/java/com/usthe/manager/service/impl/MonitorServiceImpl.java @@ -63,7 +63,10 @@ public class MonitorServiceImpl implements MonitorService { appDefine.setConfigmap(configmaps); List collectRep = jobScheduling.addSyncCollectJob(appDefine); // 判断探测结果 失败则抛出探测异常 - if (collectRep == null || collectRep.isEmpty() || collectRep.get(0).getCode() != CollectRep.Code.SUCCESS) { + if (collectRep == null || collectRep.isEmpty()) { + throw new MonitorDetectException("No collector response"); + } + if (collectRep.get(0).getCode() != CollectRep.Code.SUCCESS) { throw new MonitorDetectException(collectRep.get(0).getMsg()); } } diff --git a/manager/src/main/resources/db/schema.sql b/manager/src/main/resources/db/schema.sql index 36fb142..232b715 100644 --- a/manager/src/main/resources/db/schema.sql +++ b/manager/src/main/resources/db/schema.sql @@ -51,6 +51,7 @@ CREATE TABLE param_define name varchar(100) not null comment '参数字段对外显示名称', field varchar(100) not null comment '参数字段标识符', type varchar(20) not null default 'text' comment '字段类型,样式(大部分映射input标签type属性)', + required boolean not null default false comment '是否是必输项 true-必填 false-可选', param_range varchar(100) not null comment '当type为number时,用range表示范围 eg: 0-233', param_limit tinyint unsigned not null comment '当type为text时,用limit表示字符串限制大小.最大255', param_option varchar(255) not null comment '当type为radio单选框,checkbox复选框时,option表示可选项值列表', diff --git a/manager/src/main/resources/define/app/A-example.yml b/manager/src/main/resources/define/app/A-example.yml index d7d1a35..5b34421 100644 --- a/manager/src/main/resources/define/app/A-example.yml +++ b/manager/src/main/resources/define/app/A-example.yml @@ -1,5 +1,5 @@ # 监控应用类型名称(与文件名保持一致) eg: linux windows tomcat mysql aws... -app: cloud +app: TanCloud # 参数映射map. type是参数类型: number数字, string明文字符串, secret加密字符串 # 强制固定必须参数 - host configmap: diff --git a/manager/src/main/resources/define/param/A-example.yml b/manager/src/main/resources/define/param/A-example.yml index 9c6229e..19b750e 100644 --- a/manager/src/main/resources/define/param/A-example.yml +++ b/manager/src/main/resources/define/param/A-example.yml @@ -1,23 +1,33 @@ # 监控应用类型名称(与文件名保持一致) eg: linux windows tomcat mysql aws... -app: cloud +app: TanCloud # 强制固定必须参数 - host(ipv4,ipv6,域名) param: - # field-字段名称标识符 type-字段类型,样式(大部分映射input标签type属性) + # field-字段名称标识符 - field: host + # name-参数字段显示名称 + name: 主机Host + # type-字段类型,样式(大部分映射input标签type属性) type: host + # 是否是必输项 true-必填 false-可选 + required: true - field: port + name: 端口 type: number # 当type为number时,用range表示范围 range: 0-255 + required: true - field: username + name: 用户名 type: text # 当type为text时,用limit表示字符串限制大小 limit: 20 + required: false - field: password + name: 密码 type: password - - field: param1 + required: false + - field: ssl + name: 启动SSL type: radio # 当type为radio单选框,checkbox复选框时,option表示可选项值列表 - option: - - param3 - - param4 \ No newline at end of file + option: Yes,No \ No newline at end of file