Преглед на файлове

[collector,manager]linux监控类型新增内存,磁盘,网络监控指标

tomsun28 преди 3 години
родител
ревизия
f52247df67

+ 11 - 2
collector/src/main/java/com/usthe/collector/collect/common/cache/CommonCache.java

@@ -21,9 +21,9 @@ import java.util.concurrent.TimeUnit;
 public class CommonCache {
 
     /**
-     * 默认缓存时间 30minute
+     * 默认缓存时间 10minute
      */
-    private static final long DEFAULT_CACHE_TIMEOUT = 30 * 60 * 1000L;
+    private static final long DEFAULT_CACHE_TIMEOUT = 10 * 60 * 1000L;
 
     /**
      * 默认最大缓存数量
@@ -156,6 +156,15 @@ public class CommonCache {
     }
 
     /**
+     * 新增或更新cache
+     * @param key 存储对象key
+     * @param value 存储对象
+     */
+    public void addCache(Object key, Object value) {
+        addCache(key, value, DEFAULT_CACHE_TIMEOUT);
+    }
+
+    /**
      * 根据缓存key获取缓存对象
      * @param key key
      * @param refreshCache 是否刷新命中的缓存的存活时间 true是,false否

+ 18 - 10
collector/src/main/java/com/usthe/collector/collect/ssh/SshCollectImpl.java

@@ -57,23 +57,23 @@ public class SshCollectImpl extends AbstractCollect {
         }
         SshProtocol sshProtocol = metrics.getSsh();
         // 超时时间默认300毫秒
-        int timeout = 300;
+        int timeout = 3000;
         try {
             timeout = Integer.parseInt(sshProtocol.getTimeout());
         } catch (Exception e) {
             log.warn(e.getMessage());
         }
         try {
-            ClientSession clientSession = getConnectSession(sshProtocol);
+            ClientSession clientSession = getConnectSession(sshProtocol, timeout);
             ClientChannel channel = clientSession.createExecChannel(sshProtocol.getScript());
             ByteArrayOutputStream response = new ByteArrayOutputStream();
             channel.setOut(response);
-            if (!channel.open().verify(Integer.parseInt(sshProtocol.getTimeout())).isOpened()) {
+            if (!channel.open().verify(timeout).isOpened()) {
                 throw new Exception("open failed");
             }
             List<ClientChannelEvent> list = new ArrayList<>();
             list.add(ClientChannelEvent.CLOSED);
-            channel.waitFor(list, Integer.parseInt(sshProtocol.getTimeout()));
+            channel.waitFor(list, timeout);
             Long responseTime  = System.currentTimeMillis() - startTime;
             channel.close();
             String result = response.toString();
@@ -109,8 +109,16 @@ public class SshCollectImpl extends AbstractCollect {
             log.error("ssh response data not enough: {}",  result);
         }
         CollectRep.ValueRow.Builder valueRowBuilder = CollectRep.ValueRow.newBuilder();
-        for (String value : lines) {
-            valueRowBuilder.addColumns(value);
+        int aliasIndex = 0;
+        int lineIndex = 0;
+        while (aliasIndex < aliasFields.size()) {
+            if (CollectorConstants.RESPONSE_TIME.equalsIgnoreCase(aliasFields.get(aliasIndex))) {
+                valueRowBuilder.addColumns(responseTime.toString());
+            } else {
+                valueRowBuilder.addColumns(lines[lineIndex].trim());
+                lineIndex++;
+            }
+            aliasIndex++;
         }
         builder.addValues(valueRowBuilder.build());
     }
@@ -145,7 +153,7 @@ public class SshCollectImpl extends AbstractCollect {
         }
     }
 
-    private ClientSession getConnectSession(SshProtocol sshProtocol) throws IOException {
+    private ClientSession getConnectSession(SshProtocol sshProtocol, int timeout) throws IOException {
         CacheIdentifier identifier = CacheIdentifier.builder()
                 .ip(sshProtocol.getHost()).port(sshProtocol.getPort())
                 .username(sshProtocol.getUsername()).password(sshProtocol.getPassword())
@@ -170,15 +178,15 @@ public class SshCollectImpl extends AbstractCollect {
         }
         SshClient sshClient = CommonSshClient.getSshClient();
         clientSession = sshClient.connect(sshProtocol.getUsername(), sshProtocol.getHost(), Integer.parseInt(sshProtocol.getPort()))
-                .verify(Long.parseLong(sshProtocol.getTimeout()), TimeUnit.MILLISECONDS).getSession();
+                .verify(timeout, TimeUnit.MILLISECONDS).getSession();
         if (StringUtils.hasText(sshProtocol.getPassword())) {
             clientSession.addPasswordIdentity(sshProtocol.getPassword());
         }
         // 进行认证
-        if (!clientSession.auth().verify(Long.parseLong(sshProtocol.getTimeout()), TimeUnit.MILLISECONDS).isSuccess()) {
+        if (!clientSession.auth().verify(timeout, TimeUnit.MILLISECONDS).isSuccess()) {
             throw new IllegalArgumentException("认证失败");
         }
-        CommonCache.getInstance().addCache(identifier, clientSession, 10000L);
+        CommonCache.getInstance().addCache(identifier, clientSession);
         return clientSession;
     }
 

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

@@ -56,12 +56,15 @@ metrics:
         type: 1
       - field: cores
         type: 0
+        unit: 核数
       - field: interrupt
         type: 0
+        unit: 个数
       - field: load
         type: 1
       - field: context_switch
         type: 0
+        unit: 个数
     # 监控采集使用协议 eg: sql, ssh, http, telnet, wmi, snmp, sdk
     protocol: ssh
     # 当protocol为http协议时具体的采集配置
@@ -73,4 +76,93 @@ metrics:
       username: ^_^username^_^
       password: ^_^password^_^
       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}'"
-      parseType: oneRow
+      parseType: oneRow
+
+  - name: memory
+    priority: 2
+    fields:
+      # 指标信息 包括 field名称   type字段类型:0-number数字,1-string字符串   instance是否为实例主键   unit:指标单位
+      - field: total
+        type: 0
+        unit: Mb
+      - field: used
+        type: 0
+        unit: Mb
+      - field: free
+        type: 0
+        unit: Mb
+      - field: buff_cache
+        type: 0
+        unit: Mb
+      - field: available
+        type: 0
+        unit: Mb
+    # 监控采集使用协议 eg: sql, ssh, http, telnet, wmi, snmp, sdk
+    protocol: ssh
+    # 当protocol为http协议时具体的采集配置
+    ssh:
+      # 主机host: ipv4 ipv6 域名
+      host: ^_^host^_^
+      # 端口
+      port: ^_^port^_^
+      username: ^_^username^_^
+      password: ^_^password^_^
+      script: free -m | grep Mem | awk 'BEGIN{print "total used free buff_cache available"} {print $2,$3,$4,$6,$7}'
+      parseType: multiRow
+
+  - name: disk
+    priority: 3
+    fields:
+      # 指标信息 包括 field名称   type字段类型:0-number数字,1-string字符串   instance是否为实例主键   unit:指标单位
+      - field: disk_num
+        type: 0
+        unit: 块数
+      - field: partition_num
+        type: 0
+        unit: 分区数
+      - field: block_write
+        type: 0
+        unit: 块数
+      - field: block_read
+        type: 0
+        unit: 块数
+      - field: write_rate
+        type: 0
+        unit: iops
+    # 监控采集使用协议 eg: sql, ssh, http, telnet, wmi, snmp, sdk
+    protocol: ssh
+    # 当protocol为http协议时具体的采集配置
+    ssh:
+      # 主机host: ipv4 ipv6 域名
+      host: ^_^host^_^
+      # 端口
+      port: ^_^port^_^
+      username: ^_^username^_^
+      password: ^_^password^_^
+      script: vmstat -D | awk 'NR==1{print $1}';vmstat -D | awk 'NR==2{print $1}';vmstat 1 1 | awk 'NR==3{print $10}';vmstat 1 1 | awk 'NR==3{print $9}';vmstat 1 1 | awk 'NR==3{print $16}'
+      parseType: oneRow
+
+  - name: interface
+    priority: 4
+    fields:
+      # 指标信息 包括 field名称   type字段类型:0-number数字,1-string字符串   instance是否为实例主键   unit:指标单位
+      - field: interface_name
+        type: 1
+      - field: receive_bytes
+        type: 0
+        unit: byte
+      - field: transmit_bytes
+        type: 0
+        unit: byte
+    # 监控采集使用协议 eg: sql, ssh, http, telnet, wmi, snmp, sdk
+    protocol: ssh
+    # 当protocol为http协议时具体的采集配置
+    ssh:
+      # 主机host: ipv4 ipv6 域名
+      host: ^_^host^_^
+      # 端口
+      port: ^_^port^_^
+      username: ^_^username^_^
+      password: ^_^password^_^
+      script: cat /proc/net/dev | tail -n +3 | awk 'BEGIN{ print "interface_name receive_bytes transmit_bytes"} {print $1,$2,$10}'
+      parseType: multiRow