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

This commit is contained in:
tomsun28
2021-12-04 19:55:54 +08:00
parent 4041c83a99
commit a52ad292b5
10 changed files with 233 additions and 1 deletions

View File

@@ -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());
}

View File

@@ -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();
}
}

View File

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

View File

@@ -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;
}

View File

@@ -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();
}
}
}
}
}