Mercurial > stress-tester
changeset 1129:c34359011cf5
SnmpService - configuration
author | Devel 1 |
---|---|
date | Fri, 05 Jun 2020 15:22:53 +0200 |
parents | abd8ab4d6b80 |
children | f4b74fec7584 |
files | stress-tester/src/main/java/com/passus/st/snmp/SnmpConfigurator.java stress-tester/src/main/java/com/passus/st/snmp/SnmpService.java stress-tester/src/main/java/com/passus/st/snmp/SnmpTargetTransformer.java stress-tester/src/main/java/com/passus/st/utils/ConfigurationContextConsts.java stress-tester/src/test/resources/com/passus/st/config/test_job_config.yml |
diffstat | 5 files changed, 191 insertions(+), 1 deletions(-) [+] |
line wrap: on
line diff
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/stress-tester/src/main/java/com/passus/st/snmp/SnmpConfigurator.java Fri Jun 05 15:22:53 2020 +0200 @@ -0,0 +1,50 @@ +package com.passus.st.snmp; + +import com.passus.commons.annotations.Plugin; +import com.passus.config.CCompositeNode; +import com.passus.config.Configuration; +import com.passus.config.ConfigurationContext; +import com.passus.config.DomainConfigurator; +import com.passus.config.schema.NodeDefinition; +import com.passus.config.validation.Errors; +import com.passus.st.plugin.PluginConstants; + +import static com.passus.st.utils.ConfigurationContextConsts.SNMP_SERVICE; + +/** + * + * @author mikolaj.podbielski + */ +@Plugin(name = SnmpConfigurator.DOMAIN, category = PluginConstants.CATEGORY_DOMAIN_CONFIGURATOR) +public class SnmpConfigurator implements DomainConfigurator { + + public static final String DOMAIN = "snmp"; + + private final NodeDefinition nodeDefinition = SnmpService.SnmpServiceNodeDefCreator.createDef(); + + @Override + public String getDomain() { + return DOMAIN; + } + + @Override + public void validate(Configuration config, Errors errors, ConfigurationContext context) { + CCompositeNode rootNode = config.getRootNode(); + nodeDefinition.validate(rootNode, errors, context); + } + + @Override + public void configure(Configuration config, Errors errors, ConfigurationContext context) { + CCompositeNode rootNode = config.getRootNode(); + nodeDefinition.transform(rootNode, errors, context); + if (!errors.hasError()) { + boolean active = config.getBoolean("active", true); + if (active) { + SnmpService service = new SnmpService(); + service.configure(config, context); + context.add(SNMP_SERVICE, service); + } + } + } + +}
--- a/stress-tester/src/main/java/com/passus/st/snmp/SnmpService.java Fri Jun 05 14:37:26 2020 +0200 +++ b/stress-tester/src/main/java/com/passus/st/snmp/SnmpService.java Fri Jun 05 15:22:53 2020 +0200 @@ -5,9 +5,17 @@ import com.passus.commons.service.ServiceException; import com.passus.commons.time.DefaultScheduledTimerService; import com.passus.commons.time.ScheduledTimerService; +import com.passus.config.Configurable; +import com.passus.config.Configuration; +import com.passus.config.ConfigurationContext; +import com.passus.config.annotations.NodeDefinitionCreate; +import com.passus.config.schema.NodeDefinition; +import com.passus.config.schema.NodeDefinitionCreator; import com.passus.st.Log4jConfigurationFactory; import com.passus.st.metric.MetricSource; import com.passus.st.metric.MetricsContainer; +import com.passus.st.utils.PeriodValueTransformer; +import com.passus.st.validation.PortValidator; import org.apache.logging.log4j.LogManager; import org.apache.logging.log4j.Logger; import org.snmp4j.*; @@ -22,10 +30,20 @@ import java.util.List; import java.util.Set; -public class SnmpService implements Service, MetricSource { +import static com.passus.config.schema.ConfigurationSchemaBuilder.*; +import static com.passus.st.config.CommonNodeDefs.*; + +@NodeDefinitionCreate(SnmpService.SnmpServiceNodeDefCreator.class) +public class SnmpService implements Configurable, Service, MetricSource { private static final Logger LOGGER = LogManager.getLogger(SnmpService.class); + private static final int DEFAULT_PORT = 161; + + private static final long DEFAULT_INTERVAL = 30_000; + + private static final long DEFAULT_INTIAL_DELAY = 0; + private Snmp snmp; private volatile boolean started; @@ -36,6 +54,10 @@ private long interval = 30_000; + private long initialDelay = 30_000; + + private int defaultPort = DEFAULT_PORT; + private Set<OID> oids = SnmpOIDInfo.getAllOIDs(); public long getInterval() { @@ -59,6 +81,16 @@ } @Override + public void configure(Configuration config, ConfigurationContext context) { + defaultPort = config.get("defaultPort", DEFAULT_PORT); + interval = config.get("interval", DEFAULT_INTERVAL); + initialDelay = config.get("initialDelay", DEFAULT_INTIAL_DELAY); + List<SnmpTargetSettings> targetSettings = config.getList("targets"); + targets.clear(); + targetSettings.forEach((setting) -> targets.add(new SnmpTarget(setting))); + } + + @Override public void start() { if (started) { return; @@ -251,4 +283,32 @@ } } + public static class SnmpServiceNodeDefCreator implements NodeDefinitionCreator { + + @Override + public NodeDefinition create() { + return createDef(); + } + + public static NodeDefinition createDef() { + NodeDefinition portDef = valueDefInteger().addValidator(PortValidator.INSTANCE); + NodeDefinition periodDef = valueDef().setTransformer(PeriodValueTransformer.INSTANCE); + + NodeDefinition targetDef = mapDef( + tupleDef("host", STRING_DEF), + tupleDef("port", portDef).setRequired(false), + tupleDef("timeout", periodDef).setRequired(false), + tupleDef("community", STRING_DEF).setRequired(false), + tupleDef("retries", INT_GREATER_EQUAL_ZERO_DEF).setRequired(false) + ).setTransformer(new SnmpTargetTransformer()); + + return mapDef( + tupleDef("active", BOOLEAN_DEF).setRequired(false), + tupleDef("defaultPort", portDef).setRequired(false), + tupleDef("interval", periodDef).setRequired(false), + tupleDef("initialDelay", periodDef).setRequired(false), + tupleDef("targets", listDef(targetDef)) + ); + } + } }
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/stress-tester/src/main/java/com/passus/st/snmp/SnmpTargetTransformer.java Fri Jun 05 15:22:53 2020 +0200 @@ -0,0 +1,67 @@ +package com.passus.st.snmp; + +import com.passus.config.CMapNode; +import com.passus.config.CNode; +import com.passus.config.CTupleNode; +import com.passus.config.CValueNode; +import com.passus.config.ConfigurationContext; +import com.passus.config.ConfigurationUtils; +import com.passus.config.NodeException; +import com.passus.config.schema.NodeTransformer; +import com.passus.config.validation.Errors; +import java.util.List; + +/** + * + * @author mikolaj.podbielski + */ +public class SnmpTargetTransformer implements NodeTransformer<CNode> { + + @Override + public CNode transform(CNode node, Errors errors, ConfigurationContext context) { + CMapNode mapNode = (CMapNode) node; + List<CTupleNode> tuples = mapNode.getChildren(); + SnmpTargetSettings settings = new SnmpTargetSettings(); + String host = SnmpUtils.DEFAULT_ADDRESS; + int port = -1; + for (CTupleNode tuple : tuples) { + String name = tuple.getName(); + CNode valNode = tuple.getNode(); + try { + switch (name) { + case "host": + host = ConfigurationUtils.extractString(valNode); + break; + case "port": + port = ConfigurationUtils.extractInteger(valNode); + break; + case "timeout": + settings.setTimeout(ConfigurationUtils.extractLong(valNode)); + break; + case "community": + settings.setCommunity(ConfigurationUtils.extractString(valNode)); + break; + case "retries": + settings.setRetries(ConfigurationUtils.extractInteger(valNode)); + break; + default: + throw new IllegalArgumentException("Unknown parameter '" + name + "'."); + } + } catch (NodeException ex) { + throw new IllegalArgumentException(ex.getMessage(), ex); + } + } + if (port == -1) { + settings.setAddress(host); + } else { + settings.setAddress(host + "/" + port); + } + return new CValueNode(settings); + } + + @Override + public CNode reverseTransform(CNode node, Errors errors, ConfigurationContext context) { + throw new UnsupportedOperationException("Not supported yet."); + } + +}
--- a/stress-tester/src/main/java/com/passus/st/utils/ConfigurationContextConsts.java Fri Jun 05 14:37:26 2020 +0200 +++ b/stress-tester/src/main/java/com/passus/st/utils/ConfigurationContextConsts.java Fri Jun 05 15:22:53 2020 +0200 @@ -40,6 +40,8 @@ public static final String SCANNER_ANALYZERS = "scanner.analyzers"; + public static final String SNMP_SERVICE = "snmp"; + public static final String PROJECT = "project"; public static final String FLOW_FILTERS = "flowFilters";
--- a/stress-tester/src/test/resources/com/passus/st/config/test_job_config.yml Fri Jun 05 14:37:26 2020 +0200 +++ b/stress-tester/src/test/resources/com/passus/st/config/test_job_config.yml Fri Jun 05 15:22:53 2020 +0200 @@ -3,6 +3,17 @@ host: 2.2.2.2 port: 22222 +snmp: + active: true + interval: 30s + initialDelay: 1s + defaultPort: 161 + targets: + - host: 172.16.60.157 + port: 161 + timeout: 10 + community: "passus" + retries: 1 metrics: active: true