use of org.opennms.netmgt.snmp.SnmpResult in project opennms by OpenNMS.
the class TcaProtocolCollector method collect.
@Override
public CollectionJob collect(final CollectionJob collectionJob) {
logger.info("TcaProtocolCollector is collecting collectionJob '{}'", collectionJob);
SnmpAgentConfig snmpAgentConfig = SnmpAgentConfig.parseProtocolConfigurationString(collectionJob.getProtocolConfiguration());
List<Collectable> trackers = new ArrayList<>();
for (final String metricObjId : collectionJob.getAllMetrics()) {
final String keyword = metricObjId.substring(metricObjId.lastIndexOf("_") + 1);
final SnmpObjId requestOid = SnmpObjId.get(metricObjId.substring(0, metricObjId.lastIndexOf("_")));
SnmpObjId base = requestOid.getPrefix(requestOid.length() - 1);
int lastId = requestOid.getLastSubId();
SingleInstanceTracker instanceTracker = new SingleInstanceTracker(base, new SnmpInstId(lastId)) {
@Override
protected void storeResult(SnmpResult result) {
logger.trace("Collected SnmpValue '{}'", result);
SnmpValue value = result.getValue();
String compositeResult = getCompositeValue(keyword, value.toDisplayString());
collectionJob.setMetricValue(metricObjId, "int32", compositeResult);
}
@Override
public void setFailed(boolean failed) {
super.setFailed(failed);
logger.trace("Collection Failed for metricObjId '{}'", metricObjId);
collectionJob.setMetricValue(metricObjId, "unknown", null);
}
@Override
public void setTimedOut(boolean timedOut) {
super.setTimedOut(timedOut);
logger.trace("Collection timedOut for metricObjId '{}'", metricObjId);
collectionJob.setMetricValue(metricObjId, "unknown", null);
}
};
trackers.add(instanceTracker);
}
CollectionTracker tracker = new AggregateTracker(trackers);
try (SnmpWalker walker = m_snmpStrategy.createWalker(snmpAgentConfig, "SnmpProtocolCollector for " + snmpAgentConfig.getAddress(), tracker)) {
walker.start();
try {
walker.waitFor();
} catch (InterruptedException e) {
logger.error("Interuppted while waiting for collector. Results may be incomplete.", e);
}
}
return collectionJob;
}
use of org.opennms.netmgt.snmp.SnmpResult in project opennms by OpenNMS.
the class SnmpAttributeTest method testPersisting.
@Ignore
private void testPersisting(String matchValue, SnmpValue snmpValue) throws Exception {
OnmsNode node = new OnmsNode();
node.setId(3);
OnmsIpInterface ipInterface = new OnmsIpInterface();
ipInterface.setId(1);
ipInterface.setNode(node);
ipInterface.setIpAddress(InetAddressUtils.addr("192.168.1.1"));
// It used to be 3, but I think it is more correct to use getStoreDir from DefaultCollectionAgentService on DefaultCollectionAgent (NMS-7516)
expect(m_ipInterfaceDao.load(1)).andReturn(ipInterface).times(5);
expect(m_rrdStrategy.getDefaultFileExtension()).andReturn(".myLittleEasyMockedStrategyAndMe").anyTimes();
expect(m_rrdStrategy.createDefinition(isA(String.class), isA(String.class), isA(String.class), anyInt(), isAList(RrdDataSource.class), isAList(String.class))).andReturn(new Object());
m_rrdStrategy.createFile(isA(Object.class));
expect(m_rrdStrategy.openFile(isA(String.class))).andReturn(new Object());
m_rrdStrategy.updateFile(isA(Object.class), isA(String.class), matches(".*:" + matchValue));
m_rrdStrategy.closeFile(isA(Object.class));
m_mocks.replayAll();
SnmpCollectionAgent agent = DefaultSnmpCollectionAgent.create(ipInterface.getId(), m_ipInterfaceDao, new MockPlatformTransactionManager());
OnmsSnmpCollection snmpCollection = new OnmsSnmpCollection(agent, new ServiceParameters(new HashMap<String, Object>()), new MockDataCollectionConfig(), m_locationAwareSnmpClient);
NodeResourceType resourceType = new NodeResourceType(agent, snmpCollection);
NodeInfo nodeInfo = resourceType.getNodeInfo();
MibObject mibObject = new MibObject();
mibObject.setOid(".1.3.6.1.4.1.12238.55.9997.4.1.2.9.116.101.109.112.95.117.108.107.111");
mibObject.setInstance("1");
mibObject.setAlias("temp_ulko");
mibObject.setType("gauge");
NumericAttributeType attributeType = new NumericAttributeType(resourceType, snmpCollection.getName(), mibObject, new AttributeGroupType("foo", AttributeGroupType.IF_TYPE_IGNORE));
attributeType.storeResult(new SnmpCollectionSet(agent, snmpCollection, m_locationAwareSnmpClient), null, new SnmpResult(mibObject.getSnmpObjId(), new SnmpInstId(mibObject.getInstance()), snmpValue));
RrdRepository repository = createRrdRepository();
repository.setRraList(Collections.singletonList("RRA:AVERAGE:0.5:1:2016"));
RrdPersisterFactory persisterFactory = new RrdPersisterFactory();
persisterFactory.setRrdStrategy(m_rrdStrategy);
persisterFactory.setResourceStorageDao(m_resourceStorageDao);
CollectionSetVisitor persister = persisterFactory.createPersister(new ServiceParameters(Collections.emptyMap()), repository);
final AtomicInteger count = new AtomicInteger(0);
nodeInfo.visit(new CollectionSetVisitorWrapper(persister) {
@Override
public void visitAttribute(CollectionAttribute attribute) {
super.visitAttribute(attribute);
count.incrementAndGet();
}
});
assertEquals(1, count.get());
}
use of org.opennms.netmgt.snmp.SnmpResult in project opennms by OpenNMS.
the class WalkCommand method doExecute.
@Override
protected Object doExecute() throws Exception {
LOG.debug("snmp:walk {} {} {}", m_location != null ? "-l " + m_location : "", m_host, m_oids);
final List<SnmpObjId> snmpObjIds = m_oids.stream().map(oid -> SnmpObjId.get(oid)).collect(Collectors.toList());
final SnmpAgentConfig agent = snmpAgentConfigFactory.getAgentConfig(InetAddress.getByName(m_host), m_location);
final CompletableFuture<List<SnmpResult>> future = locationAwareSnmpClient.walk(agent, snmpObjIds).withDescription("snmp:walk").withLocation(m_location).execute();
while (true) {
try {
future.get(1, TimeUnit.SECONDS).stream().forEach(res -> {
System.out.printf("[%s].[%s] = %s%n", res.getBase(), res.getInstance(), res.getValue());
});
break;
} catch (TimeoutException e) {
// pass
}
System.out.print(".");
}
return null;
}
use of org.opennms.netmgt.snmp.SnmpResult in project opennms by OpenNMS.
the class SingleInstanceTrackerProxyTest method canHandleValidResponses.
@Test
public void canHandleValidResponses() {
// Build a response with the requested OID
SnmpValue value = mock(SnmpValue.class);
SnmpResult result = new SnmpResult(base, instance, value);
WalkResponse response = new WalkResponse(Collections.singletonList(result));
// Resolve the walker
tracker.handleWalkResponses(Collections.singletonList(response));
// We should be finished, and have captured the expected value
assertThat(tracker.isFinished(), equalTo(true));
assertThat(gatherer.getResults(), hasSize(1));
assertThat(gatherer.getResults().get(0).getValue(), equalTo(value));
}
use of org.opennms.netmgt.snmp.SnmpResult in project opennms by OpenNMS.
the class SnmpProxyRpcModule method get.
private CompletableFuture<SnmpResponseDTO> get(SnmpRequestDTO request, SnmpGetRequestDTO get) {
final SnmpObjId[] oids = get.getOids().toArray(new SnmpObjId[get.getOids().size()]);
final CompletableFuture<SnmpValue[]> future = SnmpUtils.getAsync(request.getAgent(), oids);
return future.thenApply(values -> {
final List<SnmpResult> results = new ArrayList<>(oids.length);
for (int i = 0; i < oids.length; i++) {
final SnmpResult result = new SnmpResult(oids[i], null, values[i]);
results.add(result);
}
final SnmpResponseDTO responseDTO = new SnmpResponseDTO();
responseDTO.setCorrelationId(get.getCorrelationId());
responseDTO.setResults(results);
return responseDTO;
});
}
Aggregations