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

[monitor] 支持TELNET监测服务端口可用性监控类型

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

+ 6 - 0
collector/server/pom.xml

@@ -52,6 +52,12 @@
             <artifactId>httpclient</artifactId>
             <version>4.5.13</version>
         </dependency>
+        <!--network-->
+        <dependency>
+            <groupId>commons-net</groupId>
+            <artifactId>commons-net</artifactId>
+            <version>3.8.0</version>
+        </dependency>
         <!--json path parser-->
         <dependency>
             <groupId>com.jayway.jsonpath</groupId>

+ 1 - 1
collector/server/src/main/java/com/usthe/collector/collect/icmp/IcmpCollectImpl.java

@@ -40,7 +40,7 @@ public class IcmpCollectImpl extends AbstractCollect {
         // 超时时间默认300毫秒
         int timeout = 300;
         try {
-            timeout = Integer.parseInt(metrics.getIcmp().getTimeout());
+            timeout = Integer.parseInt(icmp.getTimeout());
         } catch (Exception e) {
             log.warn(e.getMessage());
         }

+ 93 - 0
collector/server/src/main/java/com/usthe/collector/collect/telnet/TelnetCollectImpl.java

@@ -0,0 +1,93 @@
+package com.usthe.collector.collect.telnet;
+
+import com.usthe.collector.collect.AbstractCollect;
+import com.usthe.collector.util.CollectorConstants;
+import com.usthe.common.entity.job.Metrics;
+import com.usthe.common.entity.job.protocol.TelnetProtocol;
+import com.usthe.common.entity.message.CollectRep;
+import com.usthe.common.util.CommonConstants;
+import lombok.extern.slf4j.Slf4j;
+import org.apache.commons.net.telnet.TelnetClient;
+
+import java.io.IOException;
+import java.net.ConnectException;
+
+/**
+ * icmp协议采集实现 - ping
+ * @author tom
+ * @date 2021/12/4 12:32
+ */
+@Slf4j
+public class TelnetCollectImpl extends AbstractCollect {
+
+    private TelnetCollectImpl(){}
+
+    public static TelnetCollectImpl getInstance() {
+        return TelnetCollectImpl.Singleton.INSTANCE;
+    }
+
+
+    @Override
+    public void collect(CollectRep.MetricsData.Builder builder, long appId, String app, Metrics metrics) {
+        long startTime = System.currentTimeMillis();
+        // 简单校验必有参数
+        if (metrics == null || metrics.getTelnet() == null) {
+            builder.setCode(CollectRep.Code.FAIL);
+            builder.setMsg("Telnet collect must has telnet params");
+            return;
+        }
+
+        TelnetProtocol telnet = metrics.getTelnet();
+        // 超时时间默认300毫秒
+        int timeout = 300;
+        try {
+            timeout = Integer.parseInt(telnet.getTimeout());
+        } catch (Exception e) {
+            log.warn(e.getMessage());
+        }
+        TelnetClient telnetClient = null;
+        try {
+            //指明Telnet终端类型,否则会返回来的数据中文会乱码
+            telnetClient = new TelnetClient("vt200");
+            telnetClient.setConnectTimeout(timeout);
+            telnetClient.connect(telnet.getHost(),Integer.parseInt(telnet.getPort()));
+            long responseTime = System.currentTimeMillis() - startTime;
+            if (telnetClient.isConnected()) {
+                CollectRep.ValueRow.Builder valueRowBuilder = CollectRep.ValueRow.newBuilder();
+                for (String alias : metrics.getAliasFields()) {
+                    if (CollectorConstants.RESPONSE_TIME.equalsIgnoreCase(alias)) {
+                        valueRowBuilder.addColumns(Long.toString(responseTime));
+                    } else {
+                        valueRowBuilder.addColumns(CommonConstants.NULL_VALUE);
+                    }
+                }
+                builder.addValues(valueRowBuilder.build());
+            } else {
+                builder.setCode(CollectRep.Code.UN_CONNECTABLE);
+                builder.setMsg("对端连接失败,Timeout " + timeout + "ms");
+                return;
+            }
+            telnetClient.disconnect();
+        } catch (ConnectException connectException) {
+            log.debug(connectException.getMessage());
+            builder.setCode(CollectRep.Code.UN_CONNECTABLE);
+            builder.setMsg("对端拒绝连接:服务未启动端口监听或防火墙");
+        } catch (IOException ioException) {
+            log.debug(ioException.getMessage());
+            builder.setCode(CollectRep.Code.UN_CONNECTABLE);
+            builder.setMsg("对端连接失败 " + ioException.getMessage());
+        } finally {
+            if (telnetClient != null) {
+                try {
+                    telnetClient.disconnect();
+                } catch (Exception e) {
+                    log.warn(e.getMessage());
+                }
+            }
+        }
+    }
+
+    private static class Singleton {
+        private static final TelnetCollectImpl INSTANCE = new TelnetCollectImpl();
+    }
+}

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

@@ -22,6 +22,10 @@ public interface DispatchConstants {
      */
     String PROTOCOL_ICMP = "icmp";
     /**
+     * 协议 telnet
+     */
+    String PROTOCOL_TELNET = "telnet";
+    /**
      * 协议 jdbc
      */
     String PROTOCOL_JDBC = "jdbc";

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

@@ -5,6 +5,7 @@ import com.googlecode.aviator.Expression;
 import com.usthe.collector.collect.AbstractCollect;
 import com.usthe.collector.collect.http.HttpCollectImpl;
 import com.usthe.collector.collect.icmp.IcmpCollectImpl;
+import com.usthe.collector.collect.telnet.TelnetCollectImpl;
 import com.usthe.collector.dispatch.timer.Timeout;
 import com.usthe.collector.dispatch.timer.WheelTimerTask;
 import com.usthe.common.entity.job.Job;
@@ -101,6 +102,10 @@ public class MetricsCollect implements Runnable, Comparable<MetricsCollect> {
                 break;
             case DispatchConstants.PROTOCOL_ICMP:
                 abstractCollect = IcmpCollectImpl.getInstance();
+                break;
+            case DispatchConstants.PROTOCOL_TELNET:
+                abstractCollect = TelnetCollectImpl.getInstance();
+                break;
                 // todo
             default: break;
         }

+ 37 - 0
collector/server/src/test/java/com/usthe/collector/collect/telnet/TelnetCollectImplTest.java

@@ -0,0 +1,37 @@
+package com.usthe.collector.collect.telnet;
+
+import org.apache.commons.net.telnet.TelnetClient;
+import org.junit.jupiter.api.Test;
+
+import java.io.IOException;
+
+import static org.junit.jupiter.api.Assertions.*;
+
+/**
+ * @author tom
+ * @date 2021/12/4 19:39
+ */
+class TelnetCollectImplTest {
+
+    @Test
+    void telnet() {
+        TelnetClient telnetClient = null;
+        try {
+            telnetClient = new TelnetClient("vt200");
+            telnetClient.setConnectTimeout(5000);
+            TelnetClient finalTelnetClient = telnetClient;
+            assertDoesNotThrow(() -> finalTelnetClient.connect("baidu.com",80));
+            telnetClient.disconnect();
+        } catch (IOException e) {
+            e.printStackTrace();
+        } finally {
+            if (telnetClient != null) {
+                try {
+                    telnetClient.disconnect();
+                } catch (Exception e) {
+                    e.printStackTrace();
+                }
+            }
+        }
+    }
+}

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

@@ -4,6 +4,7 @@ import com.usthe.common.entity.job.protocol.HttpProtocol;
 import com.usthe.common.entity.job.protocol.IcmpProtocol;
 import com.usthe.common.entity.job.protocol.JdbcProtocol;
 import com.usthe.common.entity.job.protocol.TcpUdpProtocol;
+import com.usthe.common.entity.job.protocol.TelnetProtocol;
 import lombok.AllArgsConstructor;
 import lombok.Builder;
 import lombok.Data;
@@ -61,6 +62,10 @@ public class Metrics {
      */
     private IcmpProtocol icmp;
     /**
+     * 使用telnet协议的监控配置信息
+     */
+    private TelnetProtocol telnet;
+    /**
      * 使用socket实现的tcp或ucp进行服务端口探测配置信息
      */
     private TcpUdpProtocol tcpUdp;

+ 34 - 0
common/src/main/java/com/usthe/common/entity/job/protocol/TelnetProtocol.java

@@ -0,0 +1,34 @@
+package com.usthe.common.entity.job.protocol;
+
+import lombok.AllArgsConstructor;
+import lombok.Builder;
+import lombok.Data;
+import lombok.NoArgsConstructor;
+
+/**
+ * telnet协议配置
+ * @author tomsun28
+ * @date 2021/10/31 16:41
+ */
+@Data
+@Builder
+@AllArgsConstructor
+@NoArgsConstructor
+public class TelnetProtocol {
+
+    /**
+     * 对端主机ip或域名
+     */
+    private String host;
+
+    /**
+     * 对端主机端口
+     */
+    private String port;
+
+    /**
+     * 超时时间
+     */
+    private String timeout;
+
+}

+ 21 - 0
manager/src/main/resources/define/app/telnet.yml

@@ -0,0 +1,21 @@
+app: telnet
+configmap:
+  - key: host
+    type: 1
+  - key: port
+    type: 0
+  - key: timeout
+    type: 0
+metrics:
+  - name: summary
+    priority: 0
+    fields:
+      - field: responseTime
+        type: 0
+        unit: ms
+    protocol: telnet
+# 当protocol为telnet协议时具体的采集配置
+    telnet:
+      host: ^_^host^_^
+      port: ^_^port^_^
+      timeout: ^_^timeout^_^

+ 27 - 0
manager/src/main/resources/define/param/telnet.yml

@@ -0,0 +1,27 @@
+# 监控应用类型名称(与文件名保持一致) eg: linux windows tomcat mysql aws...
+app: telnet
+# 强制固定必须参数 - host(ipv4,ipv6,域名)
+param:
+    # 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,65535]'
+    required: true
+    defaultValue: 80
+  - field: timeout
+    name: Telnet超时时间
+    type: number
+    # 当type为number时,用range表示范围
+    range: '[0,100000]'
+    required: true
+    placeholder: '请输入超时时间,单位毫秒'
+    defaultValue: 3000