Selaa lähdekoodia

[monitor] 调整监控参数定义字段,监控参数和监控配置暂时存在内存

tomsun28 4 vuotta sitten
vanhempi
commit
94a44a7f2e

+ 0 - 3
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

+ 5 - 3
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);

+ 0 - 1
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;

+ 23 - 0
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<ParamDefine> param;
+}

+ 3 - 3
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;
 

+ 15 - 8
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;
 
     /**
@@ -62,16 +63,22 @@ public class ParamDefine {
     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;
 }

+ 26 - 5
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<String, Job> appDefines = new ConcurrentHashMap<>();
+    private final Map<String, List<ParamDefine>> paramDefines = 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();
+        List<ParamDefine> 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);
+                }
+            }
+        }
     }
 }

+ 4 - 1
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.MetricsData> 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());
         }
     }

+ 1 - 0
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表示可选项值列表',

+ 1 - 1
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:

+ 16 - 6
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
+    option: Yes,No