فهرست منبع

[monitor]feature:support linux cpu usage,memory usage,disk free (#76)

tomsun28 3 سال پیش
والد
کامیت
e1916b937e

+ 4 - 0
collector/src/main/java/com/usthe/collector/dispatch/MetricsCollect.java

@@ -247,6 +247,10 @@ public class MetricsCollect implements Runnable, Comparable<MetricsCollect> {
                         value = aliasFieldValueMap.get(realField);
                     }
                 }
+                // 处理可能带单位的指标数值 比如 34%, 34Mb,并将数值小数点限制到4位
+                if (CommonConstants.TYPE_NUMBER == field.getType()) {
+                    value = CommonUtil.parseDoubleStr(value, field.getUnit());
+                }
                 if (value == null) {
                     value = CommonConstants.NULL_VALUE;
                 }

+ 27 - 1
common/src/main/java/com/usthe/common/util/CommonUtil.java

@@ -2,6 +2,8 @@ package com.usthe.common.util;
 
 import lombok.extern.slf4j.Slf4j;
 
+import java.math.BigDecimal;
+import java.math.RoundingMode;
 import java.util.regex.Matcher;
 import java.util.regex.Pattern;
 
@@ -15,7 +17,7 @@ public class CommonUtil {
 
     private static final Pattern EMAIL_PATTERN = Pattern.compile("\\w+([-+.]\\w+)*@\\w+([-.]\\w+)*\\.\\w+([-.]\\w+)*");
 
-    private static final Pattern PHONE_PATTERN = Pattern.compile("^(((13[0-9])|(15[0-9])|(18[0-9])|(17[0-9]))+\\d{8})?$");
+    private static final Pattern PHONE_PATTERN = Pattern.compile("^(((13[0-9])|(14[0-9])|(15[0-9])|(16[0-9])|(19[0-9])|(18[0-9])|(17[0-9]))+\\d{8})?$");
 
     private static final int PHONE_LENGTH = 11;
 
@@ -37,6 +39,30 @@ public class CommonUtil {
     }
 
     /**
+     * 将字符串str,此字符串可能带单位,转换为double数字类型
+     * 将数值小数点限制到4位
+     * @param str string
+     * @param unit 字符串单位
+     * @return string格式的 double 数字 小数点最大到4位
+     */
+    public static String parseDoubleStr(String str, String unit) {
+        if (str == null || "".equals(str)) {
+            return null;
+        }
+        try {
+            if (unit != null && str.endsWith(unit)) {
+                str = str.substring(0, str.length() - unit.length());
+            }
+            BigDecimal bigDecimal = new BigDecimal(str);
+            double value = bigDecimal.setScale(4, RoundingMode.HALF_UP).doubleValue();
+            return String.valueOf(value);
+        } catch (Exception e) {
+            log.debug(e.getMessage(), e);
+            return null;
+        }
+    }
+
+    /**
      * 邮箱格式校验
      * @param email 邮箱
      * @return 是否校验成功

+ 40 - 0
common/src/test/java/com/usthe/common/util/CommonUtilTest.java

@@ -0,0 +1,40 @@
+package com.usthe.common.util;
+
+import org.junit.jupiter.api.Test;
+
+import static org.junit.jupiter.api.Assertions.*;
+
+/**
+ * @author tom
+ * @date 2022/4/7 17:18
+ */
+class CommonUtilTest {
+
+    @Test
+    void testParseDoubleStr() {
+        assertEquals("9.3454",CommonUtil.parseDoubleStr("9.345435345", null));
+        assertEquals("9.3454",CommonUtil.parseDoubleStr("9.345435345%", "%"));
+        assertEquals("10.0",CommonUtil.parseDoubleStr("10%", "%"));
+        assertEquals("588.0",CommonUtil.parseDoubleStr("588Mb", "Mb"));
+    }
+
+    @Test
+    void validateEmail() {
+        assertTrue(CommonUtil.validateEmail("tom@usthe.com"));
+        assertTrue(CommonUtil.validateEmail("demo@qq.com"));
+        assertFalse(CommonUtil.validateEmail("tom.usthe.com"));
+    }
+
+    @Test
+    void validatePhoneNum() {
+        assertTrue(CommonUtil.validatePhoneNum("19234554432"));
+        assertTrue(CommonUtil.validatePhoneNum("13234554432"));
+        assertTrue(CommonUtil.validatePhoneNum("14234554432"));
+        assertTrue(CommonUtil.validatePhoneNum("16234554432"));
+        assertFalse(CommonUtil.validatePhoneNum("12234554432"));
+        assertFalse(CommonUtil.validatePhoneNum("11234554432"));
+        assertFalse(CommonUtil.validatePhoneNum("35234554432"));
+        assertFalse(CommonUtil.validatePhoneNum("46234554432"));
+    }
+
+}

+ 72 - 1
manager/src/main/resources/define/app/linux.yml

@@ -68,6 +68,26 @@ metrics:
       - field: context_switch
         type: 0
         unit: 个数
+      - field: usage
+        type: 0
+        unit: '%'
+    # (非必须)监控指标别名,与上面的指标名映射。用于采集接口数据字段不直接是最终指标名称,需要此别名做映射转换
+    aliasFields:
+      - info
+      - cores
+      - interrupt
+      - load
+      - context_switch
+      - idle
+    # (非必须)指标计算表达式,与上面的别名一起作用,计算出最终需要的指标值
+    # eg: cores=core1+core2, usage=usage, waitTime=allTime-runningTime
+    calculates:
+      - info=info
+      - cores=cores
+      - interrupt=interrupt
+      - load=load
+      - context_switch=context_switch
+      - usage=100-idle
     # 监控采集使用协议 eg: sql, ssh, http, telnet, wmi, snmp, sdk
     protocol: ssh
     # 当protocol为http协议时具体的采集配置
@@ -79,7 +99,7 @@ metrics:
       username: ^_^username^_^
       password: ^_^password^_^
       timeout: ^_^timeout^_^
-      script: "LANG=C lscpu | awk -F: '/Model name/ {print $2}';awk '/processor/{core++} END{print core}' /proc/cpuinfo;uptime | sed 's/,/ /g' | awk '{for(i=NF-2;i<=NF;i++)print $i }' | xargs;vmstat 1 1 | awk 'NR==3{print $11}';vmstat 1 1 | awk 'NR==3{print $12}'"
+      script: "LANG=C lscpu | awk -F: '/Model name/ {print $2}';awk '/processor/{core++} END{print core}' /proc/cpuinfo;uptime | sed 's/,/ /g' | awk '{for(i=NF-2;i<=NF;i++)print $i }' | xargs;vmstat 1 1 | awk 'NR==3{print $11}';vmstat 1 1 | awk 'NR==3{print $12}';vmstat 1 1 | awk 'NR==3{print $15}'"
       parseType: oneRow
 
   - name: memory
@@ -101,6 +121,25 @@ metrics:
       - field: available
         type: 0
         unit: Mb
+      - field: usage
+        type: 0
+        unit: '%'
+    # (非必须)监控指标别名,与上面的指标名映射。用于采集接口数据字段不直接是最终指标名称,需要此别名做映射转换
+    aliasFields:
+      - total
+      - used
+      - free
+      - buff_cache
+      - available
+    # (非必须)指标计算表达式,与上面的别名一起作用,计算出最终需要的指标值
+    # eg: cores=core1+core2, usage=usage, waitTime=allTime-runningTime
+    calculates:
+      - total=total
+      - used=used
+      - free=free
+      - buff_cache=buff_cache
+      - available=available
+      - usage=(used / total) * 100
     # 监控采集使用协议 eg: sql, ssh, http, telnet, wmi, snmp, sdk
     protocol: ssh
     # 当protocol为http协议时具体的采集配置
@@ -173,4 +212,36 @@ metrics:
       password: ^_^password^_^
       timeout: ^_^timeout^_^
       script: cat /proc/net/dev | tail -n +3 | awk 'BEGIN{ print "interface_name receive_bytes transmit_bytes"} {print $1,$2,$10}'
+      parseType: multiRow
+
+  - name: disk_free
+    priority: 5
+    fields:
+      # 指标信息 包括 field名称   type字段类型:0-number数字,1-string字符串   instance是否为实例主键   unit:指标单位
+      - field: filesystem
+        type: 1
+      - field: used
+        type: 0
+        unit: Mb
+      - field: available
+        type: 0
+        unit: Mb
+      - field: usage
+        type: 0
+        unit: '%'
+      - field: mounted
+        type: 1
+        instance: true
+    # 监控采集使用协议 eg: sql, ssh, http, telnet, wmi, snmp, sdk
+    protocol: ssh
+    # 当protocol为http协议时具体的采集配置
+    ssh:
+      # 主机host: ipv4 ipv6 域名
+      host: ^_^host^_^
+      # 端口
+      port: ^_^port^_^
+      username: ^_^username^_^
+      password: ^_^password^_^
+      timeout: ^_^timeout^_^
+      script: df -m | tail -n +2 | awk 'BEGIN{ print "filesystem used available usage mounted"} {print $1,$3,$4,$5,$6}'
       parseType: multiRow