Skip to content

Commit

Permalink
[collector]feature: optimize zookeeper collector, based telnet (#957)
Browse files Browse the repository at this point in the history
  • Loading branch information
SPPan authored May 15, 2023
1 parent dde22ff commit d99c663
Show file tree
Hide file tree
Showing 3 changed files with 55 additions and 59 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,10 @@
import org.apache.commons.net.telnet.TelnetClient;

import java.io.IOException;
import java.io.OutputStream;
import java.net.ConnectException;
import java.util.*;
import java.util.stream.Collectors;

/**
* telnet协议采集实现
Expand Down Expand Up @@ -64,16 +67,22 @@ public void collect(CollectRep.MetricsData.Builder builder, long appId, String a
//指明Telnet终端类型,否则会返回来的数据中文会乱码
telnetClient = new TelnetClient("vt200");
telnetClient.setConnectTimeout(timeout);
telnetClient.connect(telnet.getHost(),Integer.parseInt(telnet.getPort()));
long responseTime = System.currentTimeMillis() - startTime;
telnetClient.connect(telnet.getHost(), Integer.parseInt(telnet.getPort()));
if (telnetClient.isConnected()) {
long responseTime = System.currentTimeMillis() - startTime;
List<String> aliasFields = metrics.getAliasFields();
Map<String, String> resultMap = execCmdAndParseResult(telnetClient, telnet.getCmd());
resultMap.put(CollectorConstants.RESPONSE_TIME, Long.toString(responseTime));
if (resultMap.size() < aliasFields.size()) {
log.error("telnet response data not enough: {}", resultMap);
builder.setCode(CollectRep.Code.FAIL);
builder.setMsg("The cmd execution results do not match the expected number of metrics.");
return;
}
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);
}
for (String field : aliasFields) {
String fieldValue = resultMap.get(field);
valueRowBuilder.addColumns(Objects.requireNonNullElse(fieldValue, CommonConstants.NULL_VALUE));
}
builder.addValues(valueRowBuilder.build());
} else {
Expand Down Expand Up @@ -112,4 +121,27 @@ public void collect(CollectRep.MetricsData.Builder builder, long appId, String a
public String supportProtocol() {
return DispatchConstants.PROTOCOL_TELNET;
}

private static Map<String, String> execCmdAndParseResult(TelnetClient telnetClient, String cmd) throws IOException {
if (cmd == null || cmd.trim().length() == 0) {
return new HashMap<>(16);
}
OutputStream outputStream = telnetClient.getOutputStream();
outputStream.write(cmd.getBytes());
outputStream.flush();
String result = new String(telnetClient.getInputStream().readAllBytes());
String[] lines = result.split("\n");
boolean contains = lines[0].contains("=");
Map<String, String> mapValue = Arrays.stream(lines)
.map(item -> {
if (contains) {
return item.split("=");
} else {
return item.split("\t");
}
})
.filter(item -> item.length == 2)
.collect(Collectors.toMap(x -> x[0], x -> x[1]));
return mapValue;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -48,4 +48,9 @@ public class TelnetProtocol {
*/
private String timeout;

/**
* 发送的命令
*/
private String cmd;

}
61 changes: 10 additions & 51 deletions manager/src/main/resources/define/app-zookeeper.yml
Original file line number Diff line number Diff line change
Expand Up @@ -81,37 +81,6 @@ params:
# default value
# 默认值
defaultValue: 6000
# field-param field key
# field-变量字段标识符
- field: username
# name-param field display i18n name
# name-参数字段显示名称
name:
zh-CN: 用户名
en-US: Username
# type-param field type(most mapping the html input type)
# type-字段类型,样式(大部分映射input标签type属性)
type: text
# when type is text, use limit to limit string length
# 当type为text时,用limit表示字符串限制大小
limit: 20
# required-true or false
# required-是否是必输项 true-必填 false-可选
required: true
# field-param field key
# field-变量字段标识符
- field: password
# name-param field display i18n name
# name-参数字段显示名称
name:
zh-CN: 密码
en-US: Password
# type-param field type(most mapping the html input tag)
# type-字段类型,样式(大部分映射input标签type属性)
type: password
# required-true or false
# required-是否是必输项 true-必填 false-可选
required: false
# collect metrics config list
# 采集指标组配置列表
metrics:
Expand Down Expand Up @@ -154,23 +123,16 @@ metrics:
- field: serverId
type: 0
# the protocol used for monitoring, eg: sql, ssh, http, telnet, wmi, snmp, sdk
protocol: ssh
ssh:
# ssh host: ipv4 ipv6 domain
protocol: telnet
telnet:
# telnet host: ipv4 ipv6 domain
host: ^_^host^_^
# ssh port: 22
# telnet port: 22
port: ^_^port^_^
# ssh account username
username: ^_^username^_^
# ssh account password
password: ^_^password^_^
# ssh connect timeout
# telnet connect timeout
timeout: ^_^timeout^_^
# ssh collect script
script: echo conf | nc 121.40.113.44 2181
# ssh parse response data type
parseType: netcat

# telnet collect cmd
cmd: conf
- name: stats
priority: 1
fields:
Expand Down Expand Up @@ -217,12 +179,9 @@ metrics:
- field: zk_min_latency
type: 0
unit: ms
protocol: ssh
ssh:
protocol: telnet
telnet:
host: ^_^host^_^
port: ^_^port^_^
username: ^_^username^_^
password: ^_^password^_^
timeout: ^_^timeout^_^
script: echo mntr | nc 121.40.113.44 2181
parseType: netcat
cmd: mntr

0 comments on commit d99c663

Please sign in to comment.