Search in sources :

Example 11 with SnmpResult

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;
}
Also used : SnmpAgentConfig(org.opennms.netmgt.snmp.SnmpAgentConfig) Collectable(org.opennms.netmgt.snmp.Collectable) SnmpWalker(org.opennms.netmgt.snmp.SnmpWalker) ArrayList(java.util.ArrayList) SnmpObjId(org.opennms.netmgt.snmp.SnmpObjId) SnmpValue(org.opennms.netmgt.snmp.SnmpValue) SingleInstanceTracker(org.opennms.netmgt.snmp.SingleInstanceTracker) SnmpInstId(org.opennms.netmgt.snmp.SnmpInstId) CollectionTracker(org.opennms.netmgt.snmp.CollectionTracker) SnmpResult(org.opennms.netmgt.snmp.SnmpResult) AggregateTracker(org.opennms.netmgt.snmp.AggregateTracker)

Example 12 with SnmpResult

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());
}
Also used : HashMap(java.util.HashMap) CollectionSetVisitor(org.opennms.netmgt.collection.api.CollectionSetVisitor) MockDataCollectionConfig(org.opennms.netmgt.mock.MockDataCollectionConfig) SnmpInstId(org.opennms.netmgt.snmp.SnmpInstId) RrdPersisterFactory(org.opennms.netmgt.collection.persistence.rrd.RrdPersisterFactory) RrdDataSource(org.opennms.netmgt.rrd.RrdDataSource) MockPlatformTransactionManager(org.opennms.core.test.MockPlatformTransactionManager) CollectionSetVisitorWrapper(org.opennms.netmgt.collection.support.CollectionSetVisitorWrapper) SnmpResult(org.opennms.netmgt.snmp.SnmpResult) OnmsNode(org.opennms.netmgt.model.OnmsNode) RrdRepository(org.opennms.netmgt.rrd.RrdRepository) CollectionAttribute(org.opennms.netmgt.collection.api.CollectionAttribute) OnmsIpInterface(org.opennms.netmgt.model.OnmsIpInterface) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) AttributeGroupType(org.opennms.netmgt.collection.api.AttributeGroupType) MibObject(org.opennms.netmgt.config.datacollection.MibObject) ServiceParameters(org.opennms.netmgt.collection.api.ServiceParameters) MibObject(org.opennms.netmgt.config.datacollection.MibObject) Ignore(org.junit.Ignore)

Example 13 with SnmpResult

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;
}
Also used : Argument(org.apache.felix.gogo.commands.Argument) SnmpAgentConfigFactory(org.opennms.netmgt.config.api.SnmpAgentConfigFactory) Logger(org.slf4j.Logger) OsgiCommandSupport(org.apache.karaf.shell.console.OsgiCommandSupport) LoggerFactory(org.slf4j.LoggerFactory) TimeoutException(java.util.concurrent.TimeoutException) CompletableFuture(java.util.concurrent.CompletableFuture) Collectors(java.util.stream.Collectors) InetAddress(java.net.InetAddress) TimeUnit(java.util.concurrent.TimeUnit) SnmpAgentConfig(org.opennms.netmgt.snmp.SnmpAgentConfig) List(java.util.List) SnmpResult(org.opennms.netmgt.snmp.SnmpResult) Command(org.apache.felix.gogo.commands.Command) Option(org.apache.felix.gogo.commands.Option) LocationAwareSnmpClient(org.opennms.netmgt.snmp.proxy.LocationAwareSnmpClient) SnmpObjId(org.opennms.netmgt.snmp.SnmpObjId) SnmpAgentConfig(org.opennms.netmgt.snmp.SnmpAgentConfig) SnmpObjId(org.opennms.netmgt.snmp.SnmpObjId) List(java.util.List) TimeoutException(java.util.concurrent.TimeoutException)

Example 14 with SnmpResult

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));
}
Also used : SnmpValue(org.opennms.netmgt.snmp.SnmpValue) SnmpResult(org.opennms.netmgt.snmp.SnmpResult) Test(org.junit.Test)

Example 15 with SnmpResult

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;
    });
}
Also used : ArrayList(java.util.ArrayList) SnmpObjId(org.opennms.netmgt.snmp.SnmpObjId) SnmpResult(org.opennms.netmgt.snmp.SnmpResult)

Aggregations

SnmpResult (org.opennms.netmgt.snmp.SnmpResult)17 SnmpInstId (org.opennms.netmgt.snmp.SnmpInstId)9 Test (org.junit.Test)8 SnmpValue (org.opennms.netmgt.snmp.SnmpValue)8 SnmpObjId (org.opennms.netmgt.snmp.SnmpObjId)7 ArrayList (java.util.ArrayList)5 HashMap (java.util.HashMap)4 AttributeGroupType (org.opennms.netmgt.collection.api.AttributeGroupType)4 ServiceParameters (org.opennms.netmgt.collection.api.ServiceParameters)4 MibObject (org.opennms.netmgt.config.datacollection.MibObject)4 MockDataCollectionConfig (org.opennms.netmgt.mock.MockDataCollectionConfig)4 RrdRepository (org.opennms.netmgt.rrd.RrdRepository)4 CollectionResource (org.opennms.netmgt.collection.api.CollectionResource)3 RrdPersistOperationBuilder (org.opennms.netmgt.collection.persistence.rrd.RrdPersistOperationBuilder)3 SnmpAgentConfig (org.opennms.netmgt.snmp.SnmpAgentConfig)3 File (java.io.File)2 CompletableFuture (java.util.concurrent.CompletableFuture)2 OnmsNode (org.opennms.netmgt.model.OnmsNode)2 AggregateTracker (org.opennms.netmgt.snmp.AggregateTracker)2 Collectable (org.opennms.netmgt.snmp.Collectable)2