diff --git a/collector/server/pom.xml b/collector/server/pom.xml
index 61dc023..8c4c42a 100644
--- a/collector/server/pom.xml
+++ b/collector/server/pom.xml
@@ -52,6 +52,12 @@
httpclient
4.5.13
+
+
+ commons-net
+ commons-net
+ 3.8.0
+
com.jayway.jsonpath
diff --git a/collector/server/src/main/java/com/usthe/collector/collect/icmp/IcmpCollectImpl.java b/collector/server/src/main/java/com/usthe/collector/collect/icmp/IcmpCollectImpl.java
index d40c179..4c04985 100644
--- a/collector/server/src/main/java/com/usthe/collector/collect/icmp/IcmpCollectImpl.java
+++ b/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());
}
diff --git a/collector/server/src/main/java/com/usthe/collector/collect/telnet/TelnetCollectImpl.java b/collector/server/src/main/java/com/usthe/collector/collect/telnet/TelnetCollectImpl.java
new file mode 100644
index 0000000..a3ac6dc
--- /dev/null
+++ b/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();
+ }
+}
diff --git a/collector/server/src/main/java/com/usthe/collector/dispatch/DispatchConstants.java b/collector/server/src/main/java/com/usthe/collector/dispatch/DispatchConstants.java
index b311d90..783f7de 100644
--- a/collector/server/src/main/java/com/usthe/collector/dispatch/DispatchConstants.java
+++ b/collector/server/src/main/java/com/usthe/collector/dispatch/DispatchConstants.java
@@ -21,6 +21,10 @@ public interface DispatchConstants {
* 协议 icmp
*/
String PROTOCOL_ICMP = "icmp";
+ /**
+ * 协议 telnet
+ */
+ String PROTOCOL_TELNET = "telnet";
/**
* 协议 jdbc
*/
diff --git a/collector/server/src/main/java/com/usthe/collector/dispatch/MetricsCollect.java b/collector/server/src/main/java/com/usthe/collector/dispatch/MetricsCollect.java
index 508b1b1..eced034 100644
--- a/collector/server/src/main/java/com/usthe/collector/dispatch/MetricsCollect.java
+++ b/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 {
break;
case DispatchConstants.PROTOCOL_ICMP:
abstractCollect = IcmpCollectImpl.getInstance();
+ break;
+ case DispatchConstants.PROTOCOL_TELNET:
+ abstractCollect = TelnetCollectImpl.getInstance();
+ break;
// todo
default: break;
}
diff --git a/collector/server/src/test/java/com/usthe/collector/collect/telnet/TelnetCollectImplTest.java b/collector/server/src/test/java/com/usthe/collector/collect/telnet/TelnetCollectImplTest.java
new file mode 100644
index 0000000..4c4e92a
--- /dev/null
+++ b/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();
+ }
+ }
+ }
+ }
+}
\ No newline at end of file
diff --git a/common/src/main/java/com/usthe/common/entity/job/Metrics.java b/common/src/main/java/com/usthe/common/entity/job/Metrics.java
index 751e975..c329588 100644
--- a/common/src/main/java/com/usthe/common/entity/job/Metrics.java
+++ b/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;
@@ -60,6 +61,10 @@ public class Metrics {
* 使用icmp协议进行ping的监控配置信息
*/
private IcmpProtocol icmp;
+ /**
+ * 使用telnet协议的监控配置信息
+ */
+ private TelnetProtocol telnet;
/**
* 使用socket实现的tcp或ucp进行服务端口探测配置信息
*/
diff --git a/common/src/main/java/com/usthe/common/entity/job/protocol/TelnetProtocol.java b/common/src/main/java/com/usthe/common/entity/job/protocol/TelnetProtocol.java
new file mode 100644
index 0000000..252d80c
--- /dev/null
+++ b/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;
+
+}
diff --git a/manager/src/main/resources/define/app/telnet.yml b/manager/src/main/resources/define/app/telnet.yml
new file mode 100644
index 0000000..524c02d
--- /dev/null
+++ b/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^_^
\ No newline at end of file
diff --git a/manager/src/main/resources/define/param/telnet.yml b/manager/src/main/resources/define/param/telnet.yml
new file mode 100644
index 0000000..9a47e93
--- /dev/null
+++ b/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
\ No newline at end of file