-
Notifications
You must be signed in to change notification settings - Fork 999
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[monitor]feature: support snmp collect protocol and windows monitor t…
…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
1 parent
8495b3c
commit d82e0b8
Showing
26 changed files
with
542 additions
and
111 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
173 changes: 173 additions & 0 deletions
173
collector/src/main/java/com/usthe/collector/collect/snmp/SnmpCollectImpl.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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(); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
59 changes: 59 additions & 0 deletions
59
common/src/main/java/com/usthe/common/entity/job/protocol/SnmpProtocol.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.