Skip to content

Commit

Permalink
[monitor]feature: support snmp collect protocol and windows monitor t…
Browse files Browse the repository at this point in the history
…ype (#192)

* [monitor]fix can not collect cpu metrics in tencent centos (#164)

* [monitor]feature: support oracle multi tablespaces (#163)

* [manager]fix oracle config yml percentage error (#168)

* [snmp ]feature: add snmp protocol (#173)

* [manager]merge snmp

* [monitor] bugfix: fix elasticsearch collect error when need basic auth(#174)

* [manager]update

* [monitor] change oracle database name to service name to reduce ambiguity (#182)

* [manager]support monitor params name i18n (#184)

* [manager]support monitor params name i18n

* [manager]support monitor params name i18n

* [manager]support windows snmp monitor

* [manager]support windows snmp monitor

* [monitor]delete mysql dev yml

* [monitor]add more windows monitor metrics

* [monitor] update docs and support verify warehouse tdengine status

* [home]add windows help docs

* [warehouse]bugfix tdengine Argument list too long

* [warehouse]bugfix tdengine Argument list too long

Co-authored-by: wyt199905 <85098809+wyt199905@users.noreply.github.com>
Co-authored-by: brave4Time <105094014+brave4Time@users.noreply.github.com>
Co-authored-by: ChineseTony <taowang98@163.com>
Co-authored-by: 卫傅庆 <weifuqing1991@163.com>
Co-authored-by: zklmcookle <107192352+zklmcookle@users.noreply.github.com>
  • Loading branch information
6 people committed Jun 19, 2022
1 parent 8495b3c commit d82e0b8
Show file tree
Hide file tree
Showing 26 changed files with 542 additions and 111 deletions.
6 changes: 6 additions & 0 deletions collector/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -125,6 +125,12 @@
<groupId>io.lettuce</groupId>
<artifactId>lettuce-core</artifactId>
</dependency>
<!--snmp-->
<dependency>
<groupId>org.snmp4j</groupId>
<artifactId>snmp4j</artifactId>
<version>3.6.7</version>
</dependency>
</dependencies>

</project>
Original file line number Diff line number Diff line change
@@ -0,0 +1,173 @@
package com.usthe.collector.collect.snmp;

import com.usthe.collector.collect.AbstractCollect;
import com.usthe.collector.util.CollectUtil;
import com.usthe.collector.util.CollectorConstants;
import com.usthe.common.entity.job.Metrics;
import com.usthe.common.entity.job.protocol.SnmpProtocol;
import com.usthe.common.entity.message.CollectRep;
import com.usthe.common.util.CommonConstants;
import lombok.extern.slf4j.Slf4j;
import org.snmp4j.PDU;
import org.snmp4j.Snmp;
import org.snmp4j.mp.SnmpConstants;
import org.snmp4j.smi.*;
import org.springframework.util.Assert;
import org.snmp4j.*;
import org.snmp4j.fluent.SnmpBuilder;
import org.snmp4j.fluent.SnmpCompletableFuture;
import org.snmp4j.fluent.TargetBuilder;
import org.snmp4j.security.SecurityModel;
import org.springframework.util.StringUtils;

import java.util.*;
import java.util.concurrent.ExecutionException;

/**
* Snmp protocol collection implementation
* snmp 协议采集实现
*
* @author wangtao
* @date 2022/6/3
*/
@Slf4j
public class SnmpCollectImpl extends AbstractCollect {

private static final String DEFAULT_PROTOCOL = "udp";
private static final String FORMAT_PATTERN =
"{0,choice,0#|1#1 day, |1<{0,number,integer} days, }" +
"{1,choice,0#|1#1 hour, |1<{1,number,integer} hours, }" +
"{2,choice,0#|1#1 minute, |1<{2,number,integer} minutes, }" +
"{3,choice,0#|1#1 second, |1<{3,number,integer} seconds }";

private SnmpCollectImpl() {
}

public static SnmpCollectImpl getInstance() {
return SnmpCollectImpl.Singleton.INSTANCE;
}

@Override
public void collect(CollectRep.MetricsData.Builder builder, long appId, String app, Metrics metrics) {
long startTime = System.currentTimeMillis();
// 校验参数
try {
validateParams(metrics);
} catch (Exception e) {
builder.setCode(CollectRep.Code.FAIL);
builder.setMsg(e.getMessage());
return;
}
SnmpProtocol snmpProtocol = metrics.getSnmp();
int timeout = CollectUtil.getTimeout(snmpProtocol.getTimeout());
int snmpVersion = getSnmpVersion(snmpProtocol.getVersion());
try {
SnmpBuilder snmpBuilder = new SnmpBuilder();
Snmp snmpService;
if (snmpVersion == SnmpConstants.version3) {
snmpService = snmpBuilder.udp().v3().usm().threads(1).build();
} else if (snmpVersion == SnmpConstants.version1) {
snmpService = snmpBuilder.udp().v1().threads(1).build();
} else {
snmpService = snmpBuilder.udp().v2c().threads(1).build();
}

Target<?> target;
Address targetAddress = GenericAddress.parse(DEFAULT_PROTOCOL + ":" + snmpProtocol.getHost()
+ "/" + snmpProtocol.getPort());
TargetBuilder<?> targetBuilder = snmpBuilder.target(targetAddress);
if (snmpVersion == SnmpConstants.version3) {
target = targetBuilder
.user(snmpProtocol.getUsername())
.auth(TargetBuilder.AuthProtocol.hmac192sha256).authPassphrase(snmpProtocol.getAuthPassphrase())
.priv(TargetBuilder.PrivProtocol.aes128).privPassphrase(snmpProtocol.getPrivPassphrase())
.done()
.timeout(timeout).retries(1)
.build();
} else if (snmpVersion == SnmpConstants.version1) {
target = targetBuilder
.v1()
.community(new OctetString(snmpProtocol.getCommunity()))
.timeout(timeout).retries(1)
.build();
target.setSecurityModel(SecurityModel.SECURITY_MODEL_SNMPv1);
} else {
target = targetBuilder
.v2c()
.community(new OctetString(snmpProtocol.getCommunity()))
.timeout(timeout).retries(1)
.build();
target.setSecurityModel(SecurityModel.SECURITY_MODEL_SNMPv2c);
}

PDU pdu = targetBuilder.pdu().type(PDU.GET).oids(snmpProtocol.getOids().values().toArray(new String[0])).build();
SnmpCompletableFuture snmpRequestFuture = SnmpCompletableFuture.send(snmpService, target, pdu);
List<VariableBinding> vbs = snmpRequestFuture.get().getAll();
long responseTime = System.currentTimeMillis() - startTime;
Map<String, String> oidsMap = snmpProtocol.getOids();
Map<String, String> oidsValueMap = new HashMap<>(oidsMap.size());
for (VariableBinding binding : vbs) {
Variable variable = binding.getVariable();
if (variable instanceof TimeTicks) {
String value = ((TimeTicks) variable).toString(FORMAT_PATTERN);
oidsValueMap.put(binding.getOid().toDottedString(), value);
} else {
oidsValueMap.put(binding.getOid().toDottedString(), binding.toValueString());
}
}
CollectRep.ValueRow.Builder valueRowBuilder = CollectRep.ValueRow.newBuilder();
for (String alias : metrics.getAliasFields()) {
if (CollectorConstants.RESPONSE_TIME.equalsIgnoreCase(alias)) {
valueRowBuilder.addColumns(Long.toString(responseTime));
} else {
String oid = oidsMap.get(alias);
String value = oidsValueMap.get(oid);
if (value != null) {
valueRowBuilder.addColumns(value);
} else {
valueRowBuilder.addColumns(CommonConstants.NULL_VALUE);
}
}
}
builder.addValues(valueRowBuilder.build());
} catch (ExecutionException | InterruptedException ex) {
log.info("[snmp collect] error: {}", ex.getMessage());
builder.setCode(CollectRep.Code.UN_CONNECTABLE);
builder.setMsg(ex.getMessage());
} catch (Exception e) {
log.warn("[snmp collect] error: {}", e.getMessage(), e);
builder.setCode(CollectRep.Code.FAIL);
builder.setMsg(e.getMessage());
}
}

private void validateParams(Metrics metrics) throws Exception {
if (metrics == null || metrics.getSnmp() == null) {
throw new IllegalArgumentException("Snmp collect must has snmp params");
}
SnmpProtocol snmpProtocol = metrics.getSnmp();
Assert.hasText(snmpProtocol.getHost(), "snmp host is required.");
Assert.hasText(snmpProtocol.getPort(), "snmp port is required.");
Assert.notNull(snmpProtocol.getVersion(), "snmp version is required.");
}

private int getSnmpVersion(String snmpVersion) {
int version = SnmpConstants.version2c;
if (!StringUtils.hasText(snmpVersion)) {
return version;
}
if (snmpVersion.equalsIgnoreCase(String.valueOf(SnmpConstants.version1))
|| snmpVersion.equalsIgnoreCase(TargetBuilder.SnmpVersion.v1.name())) {
return SnmpConstants.version1;
}
if (snmpVersion.equalsIgnoreCase(String.valueOf(SnmpConstants.version3))
|| snmpVersion.equalsIgnoreCase(TargetBuilder.SnmpVersion.v3.name())) {
return SnmpConstants.version3;
}
return version;
}

private static class Singleton {
private static final SnmpCollectImpl INSTANCE = new SnmpCollectImpl();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -10,29 +10,33 @@ public interface DispatchConstants {

// Protocol type related 协议类型相关 - start //
/**
* protocol 协议 http
* protocol http
*/
String PROTOCOL_HTTP = "http";
/**
* protocol 协议 icmp
* protocol icmp
*/
String PROTOCOL_ICMP = "icmp";
/**
* protocol 协议 telnet
* protocol telnet
*/
String PROTOCOL_TELNET = "telnet";
/**
* protocol 协议 jdbc
* protocol jdbc
*/
String PROTOCOL_JDBC = "jdbc";
/**
* protocol 协议 ssh
* protocol ssh
*/
String PROTOCOL_SSH = "ssh";
/**
* protocol 协议 ssh
* protocol redis
*/
String PROTOCOL_REDIS = "redis";
/**
* protocol snmp
*/
String PROTOCOL_SNMP = "snmp";
// Protocol type related - end
// 协议类型相关 - end //

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
import com.usthe.collector.collect.http.HttpCollectImpl;
import com.usthe.collector.collect.icmp.IcmpCollectImpl;
import com.usthe.collector.collect.redis.RedisSingleCollectImpl;
import com.usthe.collector.collect.snmp.SnmpCollectImpl;
import com.usthe.collector.collect.ssh.SshCollectImpl;
import com.usthe.collector.collect.telnet.TelnetCollectImpl;
import com.usthe.collector.dispatch.timer.Timeout;
Expand Down Expand Up @@ -137,7 +138,10 @@ public void run() {
break;
case DispatchConstants.PROTOCOL_REDIS:
abstractCollect = RedisSingleCollectImpl.getInstance();
// todo
break;
case DispatchConstants.PROTOCOL_SNMP:
abstractCollect = SnmpCollectImpl.getInstance();
break;
default:
break;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,8 @@
import java.util.concurrent.TimeUnit;

/**
* Collection job management provides api interface
* 采集job管理提供api接口
*
* @author tomsun28
* @date 2021/11/6 13:58
*/
Expand Down Expand Up @@ -49,7 +49,7 @@ public void response(List<CollectRep.MetricsData> responseMetrics) {
try {
countDownLatch.await(100, TimeUnit.SECONDS);
} catch (Exception e) {
log.info("同步任务运行100秒无响应,返回");
log.info("The sync task runs for 100 seconds with no response and returns");
}
return metricsData;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,15 +6,17 @@
import java.util.List;

/**
* One-time collection task response result listener
* 一次性采集任务响应结果监听器
* @author tomsun28
* @date 2021/11/16 10:09
*/
public interface CollectResponseEventListener extends EventListener {

/**
* Collection task completion result notification
* 采集任务完成结果通知
* @param responseMetrics 响应数据
* @param responseMetrics Response Metrics
*/
default void response(List<CollectRep.MetricsData> responseMetrics) {}
}
8 changes: 6 additions & 2 deletions common/src/main/java/com/usthe/common/entity/job/Metrics.java
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
import com.usthe.common.entity.job.protocol.IcmpProtocol;
import com.usthe.common.entity.job.protocol.JdbcProtocol;
import com.usthe.common.entity.job.protocol.RedisProtocol;
import com.usthe.common.entity.job.protocol.SnmpProtocol;
import com.usthe.common.entity.job.protocol.SshProtocol;
import com.usthe.common.entity.job.protocol.TcpUdpProtocol;
import com.usthe.common.entity.job.protocol.TelnetProtocol;
Expand Down Expand Up @@ -65,7 +66,6 @@ public class Metrics {
* https://www.yuque.com/boyan-avfmj/aviatorscript/ban32m
*/
private List<String> calculates;

/**
* Monitoring configuration information using the http protocol
* 使用http协议的监控配置信息
Expand Down Expand Up @@ -96,12 +96,16 @@ public class Metrics {
* 使用公共的ssh协议的监控配置信息
*/
private SshProtocol ssh;

/**
* Monitoring configuration information using the public redis protocol
* 使用公共的redis协议的监控配置信息
*/
private RedisProtocol redis;
/**
* Monitoring configuration information using the public snmp protocol
* 使用公共的snmp协议的监控配置信息
*/
private SnmpProtocol snmp;

@Override
public boolean equals(Object o) {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
package com.usthe.common.entity.job.protocol;

import lombok.AllArgsConstructor;
import lombok.Builder;
import lombok.Data;
import lombok.NoArgsConstructor;

import java.util.Map;

/**
* snmp 协议配置
* @author wangtao
*/
@Data
@Builder
@AllArgsConstructor
@NoArgsConstructor
public class SnmpProtocol {
/**
* 对端主机ip或域名
*/
private String host;
/**
* 对端主机端口
*/
private String port;
/**
* timeout
*/
private String timeout;
/**
* snmp version v1 v2c v3
* 0 = v1
* 1 = v2c
* 3 = v3
*/
private String version;
/**
* community name for v1 v2
* 团体字 v1 v2 版本需要
*/
private String community;
/**
* username (optional)
*/
private String username;
/**
* auth password (optional)
*/
private String authPassphrase;
/**
* password(optional)
*/
private String privPassphrase;
/**
* oid map
*/
private Map<String, String> oids;
}
6 changes: 5 additions & 1 deletion home/docs/help/guide.md
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,11 @@ sidebar_label: 帮助入门

### 操作系统监控

[Linux操作系统监控](linux) &emsp;&emsp;&emsp;&emsp;
[Linux操作系统监控](linux) &emsp;&emsp;&emsp;&emsp; [Windows操作系统监控](windows) &emsp;&emsp;&emsp;&emsp; [Ubuntu操作系统监控](ubuntu) &emsp;&emsp;&emsp;&emsp; [Centos操作系统监控](centos) &emsp;&emsp;&emsp;&emsp;

### 中间件监控

[Zookeeper](zookeeper) &emsp;&emsp;&emsp;&emsp;

## 💡 告警服务

Expand Down
Loading

0 comments on commit d82e0b8

Please sign in to comment.