Просмотр исходного кода

[collector] jsonPath解析http响应数据和calculates表达式计算实现

tomsun28 4 лет назад
Родитель
Сommit
78527d48a6

+ 4 - 0
common/src/main/java/com/usthe/common/entity/job/Metrics.java

@@ -99,6 +99,10 @@ public class Metrics {
          */
         private String type;
         /**
+         * 此字段是否为实例主键
+         */
+        private boolean instance = false;
+        /**
          * 指标单位
          */
         private String unit;

+ 5 - 0
common/src/main/java/com/usthe/common/util/CommonConstants.java

@@ -57,4 +57,9 @@ public interface CommonConstants {
      */
     byte SUSPENDING = 0x04;
 
+
+    /**
+     * null空值占位符
+     */
+    String NULL_VALUE = " ";
 }

+ 30 - 0
common/src/main/java/com/usthe/common/util/CommonUtil.java

@@ -0,0 +1,30 @@
+package com.usthe.common.util;
+
+import lombok.extern.slf4j.Slf4j;
+
+/**
+ * 公共工具类
+ * @author tomsun28
+ * @date 2021/11/20 17:29
+ */
+@Slf4j
+public class CommonUtil {
+
+    /**
+     * 将字符串str转换为double数字类型
+     * @param str string
+     * @return double 数字
+     */
+    public static Double parseDoubleStr(String str) {
+        if (str == null || "".equals(str)) {
+            return null;
+        }
+        try {
+            return Double.parseDouble(str);
+        } catch (Exception e) {
+            log.debug(e.getMessage(), e);
+            return null;
+        }
+    }
+
+}

+ 3 - 0
common/src/main/java/com/usthe/common/util/IpDomainUtil.java

@@ -45,6 +45,9 @@ public class IpDomainUtil {
      * @return 存在true
      */
     public static boolean isHasSchema(String domainIp) {
+        if (domainIp == null || "".equals(domainIp)) {
+            return false;
+        }
         return DOMAIN_SCHEMA.matcher(domainIp).matches();
     }
 

+ 14 - 8
manager/src/main/resources/define/app/A-example.yml

@@ -20,17 +20,21 @@ metrics:
     priority: 0
     # 指标组中的具体监控指标
     fields:
-      # 指标信息 包括 field名称, type字段类型:number数字,string字符串, unit:指标单位
+      # 指标信息 包括 field名称, type字段类型:number数字,string字符串,instance是否为实例主键   unit:指标单位
+      - field: hostname
+        type: instance
+        instance: true
       - field: usage
         type: number
         unit: '%'
       - field: cores
         type: number
-      - field: waitime
+      - field: waitTime
         type: number
         unit: s
 # (非必须)监控指标别名,与上面的指标名映射。用于采集接口数据字段不直接是最终指标名称,需要此别名做映射转换
     aliasFields:
+      - hostname
       - core1
       - core2
       - usage
@@ -39,6 +43,7 @@ metrics:
 # (非必须)指标计算表达式,与上面的别名一起作用,计算出最终需要的指标值
 # eg: cores=core1+core2, usage=usage, waitTime=allTime-runningTime
     calculates:
+      - hostname=hostname
       - cores=core1+core2
       - usage=usage
       - waitTime=allTime-runningTime
@@ -51,7 +56,7 @@ metrics:
       # 端口
       port: ^_^port^_^
       # url请求接口路径
-      url: /cpu
+      url: /metrics/cpu
       # 请求方式 GET POST PUT DELETE PATCH
       method: GET
       # 是否启用ssl/tls,即是http还是https,默认false
@@ -69,13 +74,15 @@ metrics:
         type: Basic Auth
         basicAuthUsername: ^_^username^_^
         basicAuthPassword: ^_^password^_^
-      # 响应数据解析方式: default-系统规则,json_path-jsonPath脚本,xml_path-xmlPath脚本,prometheus-Prometheus数据规则
+      # 响应数据解析方式: default-系统规则,jsonPath-jsonPath脚本,xmlPath-xmlPath脚本,prometheus-Prometheus数据规则
       parseType: jsonPath
-      parseScript: '$.cpu[:1].*'
+      parseScript: '$'
 
   - name: memory
     priority: 1
     fields:
+      - field: hostname
+        type: string
       - field: total
         type: number
         unit: kb
@@ -88,7 +95,7 @@ metrics:
     http:
       host: ^_^host^_^
       port: ^_^port^_^
-      url: /memory
+      url: /metrics/memory
       method: GET
       headers:
         apiVersion: v1
@@ -99,5 +106,4 @@ metrics:
         type: Basic Auth
         basicAuthUsername: ^_^username^_^
         basicAuthPassword: ^_^password^_^
-      parseType: jsonPath
-      parseScript: '$.memory[:1].*'
+      parseType: default

+ 1 - 1
manager/src/main/resources/define/param/A-example.yml

@@ -14,7 +14,7 @@ param:
     name: 端口
     type: number
     # 当type为number时,用range表示范围
-    range: '[0,255]'
+    range: '[0,65535]'
     required: true
   - field: username
     name: 用户名