From f52247df67f20c038fcfb07e6977bb8def98145c Mon Sep 17 00:00:00 2001 From: tomsun28 Date: Sat, 12 Mar 2022 10:51:22 +0800 Subject: [PATCH] =?UTF-8?q?[collector,manager]linux=E7=9B=91=E6=8E=A7?= =?UTF-8?q?=E7=B1=BB=E5=9E=8B=E6=96=B0=E5=A2=9E=E5=86=85=E5=AD=98=EF=BC=8C?= =?UTF-8?q?=E7=A3=81=E7=9B=98=EF=BC=8C=E7=BD=91=E7=BB=9C=E7=9B=91=E6=8E=A7?= =?UTF-8?q?=E6=8C=87=E6=A0=87?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../collect/common/cache/CommonCache.java | 13 ++- .../collector/collect/ssh/SshCollectImpl.java | 28 ++++-- .../src/main/resources/define/app/linux.yml | 94 ++++++++++++++++++- 3 files changed, 122 insertions(+), 13 deletions(-) diff --git a/collector/src/main/java/com/usthe/collector/collect/common/cache/CommonCache.java b/collector/src/main/java/com/usthe/collector/collect/common/cache/CommonCache.java index 078ec3d..712c01a 100644 --- a/collector/src/main/java/com/usthe/collector/collect/common/cache/CommonCache.java +++ b/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; /** * 默认最大缓存数量 @@ -155,6 +155,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 diff --git a/collector/src/main/java/com/usthe/collector/collect/ssh/SshCollectImpl.java b/collector/src/main/java/com/usthe/collector/collect/ssh/SshCollectImpl.java index 602d27d..44c43fd 100644 --- a/collector/src/main/java/com/usthe/collector/collect/ssh/SshCollectImpl.java +++ b/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 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; } diff --git a/manager/src/main/resources/define/app/linux.yml b/manager/src/main/resources/define/app/linux.yml index 6979346..6a8de26 100644 --- a/manager/src/main/resources/define/app/linux.yml +++ b/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 \ No newline at end of file + 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 \ No newline at end of file