Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

snmp collect protocol support walk operation #699

Merged
merged 3 commits into from
Mar 8, 2023
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@
import org.snmp4j.Snmp;
import org.snmp4j.mp.SnmpConstants;
import org.snmp4j.smi.*;
import org.snmp4j.util.*;
import org.springframework.util.Assert;
import org.snmp4j.*;
import org.snmp4j.fluent.SnmpBuilder;
Expand All @@ -55,6 +56,9 @@
public class SnmpCollectImpl extends AbstractCollect {

private static final String DEFAULT_PROTOCOL = "udp";
private static final String OPERATION_GET = "get";
private static final String OPERATION_WALK = "walk";
private static final String HEX_SPLIT = ":";
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, }" +
Expand Down Expand Up @@ -108,37 +112,79 @@ public void collect(CollectRep.MetricsData.Builder builder, long appId, String a
.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());
String operation = snmpProtocol.getOperation();
operation = StringUtils.hasText(operation) ? operation : OPERATION_GET;
if (OPERATION_GET.equalsIgnoreCase(operation)) {
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);
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);
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());
} else if (OPERATION_WALK.equalsIgnoreCase(operation)) {
Map<String, String> oidMap = snmpProtocol.getOids();
Assert.notEmpty(oidMap, "snmp oids is required when operation is walk.");
TableUtils tableUtils = new TableUtils(snmpService, new DefaultPDUFactory(PDU.GETBULK));
OID[] oids = oidMap.values().stream().map(OID::new).toArray(OID[]::new);
List<TableEvent> tableEvents = tableUtils.getTable(target, oids, null, null);
Assert.notNull(tableEvents, "snmp walk response empty content.");
long responseTime = System.currentTimeMillis() - startTime;
for (TableEvent tableEvent : tableEvents) {
if (tableEvent == null || tableEvent.isError()) {
continue;
}
VariableBinding[] varBindings = tableEvent.getColumns();
Map<String, String> oidsValueMap = new HashMap<>(varBindings.length);
for (VariableBinding binding : varBindings) {
Variable variable = binding.getVariable();
if (variable instanceof TimeTicks) {
String value = ((TimeTicks) variable).toString(FORMAT_PATTERN);
oidsValueMap.put(binding.getOid().trim().toDottedString(), value);
} else {
oidsValueMap.put(binding.getOid().trim().toDottedString(), bingdingHexValueToString(binding));
}
}
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 = oidMap.get(alias);
String value = oidsValueMap.get(oid);
if (value != null) {
valueRowBuilder.addColumns(value);
} else {
valueRowBuilder.addColumns(CommonConstants.NULL_VALUE);
}
}
}
builder.addValues(valueRowBuilder.build());
}
}
builder.addValues(valueRowBuilder.build());
} catch (ExecutionException | InterruptedException ex) {
String errorMsg = CommonUtil.getMessageFromThrowable(ex);
log.warn("[snmp collect] error: {}", errorMsg, ex);
Expand Down Expand Up @@ -200,4 +246,23 @@ private int getSnmpVersion(String snmpVersion) {
}
return version;
}

private String bingdingHexValueToString(VariableBinding binding) {
// whether if binding is hex
String hexString = binding.toValueString();
if (hexString.contains(HEX_SPLIT)) {
try {
StringBuilder output = new StringBuilder();
String[] hexArr = hexString.split(HEX_SPLIT);
for (String hex : hexArr) {
output.append((char) Integer.parseInt(hex, 16));
}
return output.toString();
} catch (Exception e) {
return hexString;
}
} else {
return hexString;
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,10 @@ public class SnmpProtocol {
* password(optional)
*/
private String privPassphrase;
/**
* operation: get, walk
*/
private String operation = "get";
/**
* oid map
*/
Expand Down
87 changes: 87 additions & 0 deletions manager/src/main/resources/define/app-windows.yml
Original file line number Diff line number Diff line change
Expand Up @@ -90,6 +90,7 @@ metrics:
timeout: ^_^timeout^_^
community: ^_^community^_^
version: ^_^version^_^
operation: get
oids:
name: 1.3.6.1.2.1.1.5.0
descr: 1.3.6.1.2.1.1.1.0
Expand Down Expand Up @@ -198,3 +199,89 @@ metrics:
version: ^_^version^_^
oids:
number: 1.3.6.1.2.1.2.1.0

- name: interfaces
priority: 1
fields:
- field: index
type: 1
- field: descr
type: 1
- field: mtu
type: 0
unit: 'byte'
- field: speed
type: 0
unit: 'byte/s'
- field: in_octets
type: 0
unit: 'byte'
- field: in_discards
type: 0
unit: 'package'
- field: in_errors
type: 0
unit: 'package'
- field: out_octets
type: 0
unit: 'byte'
- field: out_discards
type: 0
unit: 'package'
- field: out_errors
type: 0
unit: 'package'
- field: admin_status
type: 1
- field: oper_status
type: 1
# (非必须)监控指标别名,与上面的指标名映射。用于采集接口数据字段不直接是最终指标名称,需要此别名做映射转换
aliasFields:
- ifIndex
- ifDescr
- ifMtu
- ifSpeed
- ifInOctets
- ifInDiscards
- ifInErrors
- ifOutOctets
- ifOutDiscards
- ifOutErrors
- ifAdminStatus
- ifOperStatus
# (非必须)指标计算表达式,与上面的别名一起作用,计算出最终需要的指标值
# eg: cores=core1+core2, usage=usage, waitTime=allTime-runningTime
calculates:
- index=ifIndex
- descr=ifDescr
- mtu=ifMtu
- speed=ifSpeed
- in_octets=ifInOctets
- in_discards=ifInDiscards
- in_errors=ifInErrors
- out_octets=ifOutOctets
- out_discards=ifOutDiscards
- out_errors=ifOutErrors
- admin_status=ifAdminStatus
- oper_status=ifOperStatus
protocol: snmp
snmp:
host: ^_^host^_^
port: ^_^port^_^
timeout: ^_^timeout^_^
community: ^_^community^_^
version: ^_^version^_^
operation: walk
oids:
ifIndex: 1.3.6.1.2.1.2.2.1.1
ifDescr: 1.3.6.1.2.1.2.2.1.2
ifMtu: 1.3.6.1.2.1.2.2.1.4
ifSpeed: 1.3.6.1.2.1.2.2.1.5
ifInOctets: 1.3.6.1.2.1.2.2.1.10
ifInDiscards: 1.3.6.1.2.1.2.2.1.13
ifInErrors: 1.3.6.1.2.1.2.2.1.14
ifOutOctets: 1.3.6.1.2.1.2.2.1.16
ifOutDiscards: 1.3.6.1.2.1.2.2.1.19
ifOutErrors: 1.3.6.1.2.1.2.2.1.20
ifAdminStatus: 1.3.6.1.2.1.2.2.1.7
ifOperStatus: 1.3.6.1.2.1.2.2.1.8
Original file line number Diff line number Diff line change
Expand Up @@ -136,7 +136,7 @@ private void initTtl(String expireTime) {

@Override
void saveData(CollectRep.MetricsData metricsData) {
if (metricsData.getCode() != CollectRep.Code.SUCCESS) {
if (!isServerAvailable() || metricsData.getCode() != CollectRep.Code.SUCCESS) {
return;
}
if (metricsData.getValuesList().isEmpty()) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -111,7 +111,7 @@ private boolean initTdEngineDatasource(WarehouseProperties.StoreProperties.TdEng

@Override
public void saveData(CollectRep.MetricsData metricsData) {
if (metricsData.getCode() != CollectRep.Code.SUCCESS) {
if (!isServerAvailable() || metricsData.getCode() != CollectRep.Code.SUCCESS) {
return;
}
if (metricsData.getValuesList().isEmpty()) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ export class MonitorDataTableComponent {
this.isTable = false;
this.rowValues = this.valueRows[0].values;
}
} else {
} else if (message.code !== 0) {
this.notifySvc.warning(`${this.metrics}:${message.msg}`, '');
console.info(`${this.metrics}:${message.msg}`);
}
Expand Down