Search in sources :

Example 36 with SnmpObjId

use of org.opennms.netmgt.snmp.SnmpObjId in project opennms by OpenNMS.

the class Snmp4JStrategyIT method testPreparePduWithTooFewValues.

@Test
public void testPreparePduWithTooFewValues() throws Exception {
    SnmpObjId[] oids = new SnmpObjId[] { SnmpObjId.get(".1.3.5.1.1.3.0"), SnmpObjId.get(".1.3.5.1.1.4.0") };
    SnmpValue[] values = new SnmpValue[] { snmpValue("foo") };
    PDU pdu = m_strategy.buildPdu(new Snmp4JAgentConfig(getAgentConfig()), PDU.SET, oids, values);
    assertNull("PDU should be null", pdu);
}
Also used : PDU(org.snmp4j.PDU) SnmpValue(org.opennms.netmgt.snmp.SnmpValue) SnmpObjId(org.opennms.netmgt.snmp.SnmpObjId) Test(org.junit.Test)

Example 37 with SnmpObjId

use of org.opennms.netmgt.snmp.SnmpObjId in project opennms by OpenNMS.

the class TcaCollectionHandler method process.

/**
 * Process.
 *
 * <p>A sample TCA Data looks like the following:</p>
 * <ul>
 * <li>OID=.1.3.6.1.4.1.27091.3.1.6.1.1.172.19.37.60.1, Type=OctetString, Value=172.19.37.60 </li>
 * <li>OID=.1.3.6.1.4.1.27091.3.1.6.1.2.172.19.37.60.1, Type=OctetString, Value=
 * |25|1327451762,11,0,11,0,1|1327451763,11,0,11,0,1|1327451764,11,0,11,0,1|1327451765,11,0,11,0,1|1327451766,11,0,11,0,1|
 * 1327451767,11,0,11,0,1|1327451768,11,0,11,0,1|1327451769,11,0,11,0,1|1327451770,11,0,11,0,1|1327451771,11,0,11,0,1|
 * 1327451772,11,0,11,0,1|1327451773,11,0,11,0,1|1327451774,11,0,11,0,1|1327451775,11,0,11,0,1|1327451776,11,0,11,0,1|
 * 1327451777,11,0,11,0,1|1327451778,11,0,11,0,1|1327451779,11,0,11,0,1|1327451780,11,0,11,0,1|1327451781,11,0,11,0,1|
 * 1327451782,11,0,11,0,1|1327451783,11,0,11,0,1|1327451784,11,0,11,0,1|1327451785,11,0,11,0,1|1327451786,11,0,11,0,1|</li>
 * </ul>
 *
 * <ul>
 * <li>timestamp (epoch)</li>
 * <li>delay local-remote ~ current inbound-delay</li>
 * <li>jitter local-remote ~ current inbound-jitter</li>
 * <li>delay remote-local ~ current outbound-delay</li>
 * <li>jitter remote-local ~ current outbound-jitter-</li>
 * <li>timesync status (1 = good, time is synced, 0 = bad, out-of sync)</li>
 * </ul>
 *
 * @param tracker the tracker
 * @throws Exception the exception
 */
private void process(TcaData tracker, CollectionSetBuilder builder) {
    LOG.debug("process: processing raw TCA data for {} peers.", tracker.size());
    final NodeLevelResource nodeResource = new NodeLevelResource(m_agent.getNodeId());
    long timestamp = 0;
    for (TcaDataEntry entry : tracker.getEntries()) {
        GenericTypeResource resource = new GenericTypeResource(nodeResource, m_resourceType, entry.getPeerAddress());
        CollectionResource collectionResource = CollectionSetBuilder.toCollectionResource(resource, m_agent);
        long lastTimestamp = getLastTimestamp(collectionResource);
        String[] rawData = entry.getRawData().split("\\|");
        int samples = Integer.parseInt(rawData[1]);
        SnmpObjId entryObjId = SnmpObjId.get(".1.3.6.1.4.1.27091.3.1.6.1.2", entry.getInstance().toString());
        String identifierPrefix = String.format("TCA_%s_", entryObjId);
        for (int i = 0; i < samples; i++) {
            LOG.debug("process: processing row {}: {}", i, rawData[2 + i]);
            String[] rawEntry = rawData[2 + i].split(",");
            timestamp = Long.parseLong(rawEntry[0]);
            if (timestamp > lastTimestamp) {
                resource = new GenericTypeResource(nodeResource, m_resourceType, entry.getPeerAddress());
                resource.setTimestamp(new Date(timestamp * 1000));
                builder.withIdentifiedNumericAttribute(resource, RESOURCE_TYPE_NAME, INBOUND_DELAY, Double.parseDouble(rawEntry[1]), AttributeType.GAUGE, identifierPrefix + INBOUND_DELAY);
                builder.withIdentifiedNumericAttribute(resource, RESOURCE_TYPE_NAME, INBOUND_JITTER, Double.parseDouble(rawEntry[2]), AttributeType.GAUGE, identifierPrefix + INBOUND_JITTER);
                builder.withIdentifiedNumericAttribute(resource, RESOURCE_TYPE_NAME, OUTBOUND_DELAY, Double.parseDouble(rawEntry[3]), AttributeType.GAUGE, identifierPrefix + OUTBOUND_DELAY);
                builder.withIdentifiedNumericAttribute(resource, RESOURCE_TYPE_NAME, OUTBOUND_JITTER, Double.parseDouble(rawEntry[4]), AttributeType.GAUGE, identifierPrefix + OUTBOUND_JITTER);
                builder.withIdentifiedNumericAttribute(resource, RESOURCE_TYPE_NAME, TIMESYNC_STATUS, Double.parseDouble(rawEntry[5]), AttributeType.GAUGE, identifierPrefix + TIMESYNC_STATUS);
            } else {
                LOG.debug("process: skipping row {} {} because it was already processed.", i, rawData[2 + i]);
            }
        }
        setLastTimestamp(collectionResource, timestamp);
    }
}
Also used : CollectionResource(org.opennms.netmgt.collection.api.CollectionResource) GenericTypeResource(org.opennms.netmgt.collection.support.builder.GenericTypeResource) SnmpObjId(org.opennms.netmgt.snmp.SnmpObjId) NodeLevelResource(org.opennms.netmgt.collection.support.builder.NodeLevelResource) Date(java.util.Date)

Example 38 with SnmpObjId

use of org.opennms.netmgt.snmp.SnmpObjId in project opennms by OpenNMS.

the class TcaCollectorIT method testCollector.

/**
 * Test collector.
 *
 * @throws Exception the exception
 */
@Test
public void testCollector() throws Exception {
    Map<String, Object> parameters = new HashMap<String, Object>();
    parameters.put("collection", "default");
    // Create Collection Set
    TcaCollector collector = new TcaCollector();
    collector.setConfigDao(m_configDao);
    collector.setResourceStorageDao(m_resourceStorageDao);
    collector.setResourceTypesDao(m_resourceTypesDao);
    collector.setLocationAwareSnmpClient(m_client);
    CollectionSetVisitor persister = m_persisterFactory.createOneToOnePersister(new ServiceParameters(parameters), collector.getRrdRepository("default"), false, false);
    // Setup SNMP Value Handling
    SnmpValueFactory valFac = SnmpUtils.getValueFactory();
    SnmpObjId peer1 = SnmpObjId.get(".1.3.6.1.4.1.27091.3.1.6.1.2.171.19.37.60");
    SnmpObjId peer2 = SnmpObjId.get(".1.3.6.1.4.1.27091.3.1.6.1.2.171.19.38.70");
    // Collect and Persist Data - Step 1
    CollectionSet collectionSet = collector.collect(m_collectionAgent, parameters);
    validateCollectionSet(collectionSet);
    collectionSet.visit(persister);
    // Generate new SNMP Data
    final StringBuilder sb = new StringBuilder("|25|");
    long ts = 1327451787l;
    for (int i = 0; i < 25; i++) {
        sb.append(ts++);
        sb.append(",12,-1,12,-2,1|");
    }
    // Get Current Values
    SnmpValue v1a = SnmpUtils.get(m_collectionAgent.getAgentConfig(), peer1);
    SnmpValue v2a = SnmpUtils.get(m_collectionAgent.getAgentConfig(), peer2);
    // Set New Values
    SnmpUtils.set(m_collectionAgent.getAgentConfig(), peer1, valFac.getOctetString(sb.toString().getBytes()));
    SnmpUtils.set(m_collectionAgent.getAgentConfig(), peer2, valFac.getOctetString(sb.toString().getBytes()));
    // Validate New Values
    SnmpValue v1b = SnmpUtils.get(m_collectionAgent.getAgentConfig(), peer1);
    SnmpValue v2b = SnmpUtils.get(m_collectionAgent.getAgentConfig(), peer2);
    Assert.assertFalse(v1a.toDisplayString().equals(v1b.toDisplayString()));
    Assert.assertFalse(v2a.toDisplayString().equals(v2b.toDisplayString()));
    // Collect and Persist Data - Step 2
    collectionSet = collector.collect(m_collectionAgent, parameters);
    validateCollectionSet(collectionSet);
    collectionSet.visit(persister);
    // Validate Persisted Data
    Path pathToJrbFile = getSnmpRoot().toPath().resolve(Paths.get("1", TcaCollectionHandler.RESOURCE_TYPE_NAME, "171.19.37.60", TcaCollectionHandler.INBOUND_DELAY + m_rrdStrategy.getDefaultFileExtension()));
    RrdDb jrb = new RrdDb(pathToJrbFile.toString());
    // According with the Fixed Step
    Assert.assertEquals(1, jrb.getArchive(0).getArcStep());
    // According with the Sample Data
    Assert.assertEquals(ts - 1, jrb.getArchive(0).getEndTime());
    Robin inboundDelay = jrb.getArchive(0).getRobin(0);
    for (int i = inboundDelay.getSize() - 49; i < inboundDelay.getSize() - 25; i++) {
        Assert.assertEquals(new Double(11), Double.valueOf(inboundDelay.getValue(i)));
    }
    for (int i = inboundDelay.getSize() - 24; i < inboundDelay.getSize(); i++) {
        Assert.assertEquals(new Double(12), Double.valueOf(inboundDelay.getValue(i)));
    }
}
Also used : Path(java.nio.file.Path) HashMap(java.util.HashMap) CollectionSetVisitor(org.opennms.netmgt.collection.api.CollectionSetVisitor) SnmpObjId(org.opennms.netmgt.snmp.SnmpObjId) CollectionSet(org.opennms.netmgt.collection.api.CollectionSet) SnmpValueFactory(org.opennms.netmgt.snmp.SnmpValueFactory) SnmpValue(org.opennms.netmgt.snmp.SnmpValue) RrdDb(org.jrobin.core.RrdDb) ServiceParameters(org.opennms.netmgt.collection.api.ServiceParameters) Robin(org.jrobin.core.Robin) Test(org.junit.Test)

Example 39 with SnmpObjId

use of org.opennms.netmgt.snmp.SnmpObjId in project opennms by OpenNMS.

the class EventCreator method createEventFrom.

public Event createEventFrom(final TrapDTO trapDTO, final String systemId, final String location, final InetAddress trapAddress) {
    LOG.debug("{} trap - trapInterface: {}", trapDTO.getVersion(), trapDTO.getAgentAddress());
    // Set event data
    final EventBuilder eventBuilder = new EventBuilder(null, "trapd");
    eventBuilder.setTime(new Date(trapDTO.getCreationTime()));
    eventBuilder.setCommunity(trapDTO.getCommunity());
    eventBuilder.setSnmpTimeStamp(trapDTO.getTimestamp());
    eventBuilder.setSnmpVersion(trapDTO.getVersion());
    eventBuilder.setSnmpHost(str(trapAddress));
    eventBuilder.setInterface(trapAddress);
    eventBuilder.setHost(InetAddressUtils.toIpAddrString(trapDTO.getAgentAddress()));
    // Handle trap identity
    final TrapIdentityDTO trapIdentity = trapDTO.getTrapIdentity();
    if (trapIdentity != null) {
        LOG.debug("Trap Identity {}", trapIdentity);
        eventBuilder.setGeneric(trapIdentity.getGeneric());
        eventBuilder.setSpecific(trapIdentity.getSpecific());
        eventBuilder.setEnterpriseId(trapIdentity.getEnterpriseId());
    }
    // Handle var bindings
    for (SnmpResult eachResult : trapDTO.getResults()) {
        final SnmpObjId name = eachResult.getBase();
        final SnmpValue value = eachResult.getValue();
        eventBuilder.addParam(SyntaxToEvent.processSyntax(name.toString(), value));
        if (EventConstants.OID_SNMP_IFINDEX.isPrefixOf(name)) {
            eventBuilder.setIfIndex(value.toInt());
        }
    }
    // Resolve Node id and set, if known by OpenNMS
    resolveNodeId(location, trapAddress).ifPresent(eventBuilder::setNodeid);
    // systemId of the local system if it remains null here.
    if (systemId != null) {
        eventBuilder.setDistPoller(systemId);
    }
    // Get event template and set uei, if unknown
    final Event event = eventBuilder.getEvent();
    final org.opennms.netmgt.xml.eventconf.Event econf = eventConfDao.findByEvent(event);
    if (econf == null || econf.getUei() == null) {
        event.setUei("uei.opennms.org/default/trap");
    } else {
        event.setUei(econf.getUei());
    }
    return event;
}
Also used : EventBuilder(org.opennms.netmgt.model.events.EventBuilder) SnmpValue(org.opennms.netmgt.snmp.SnmpValue) SyntaxToEvent(org.opennms.netmgt.model.events.snmp.SyntaxToEvent) Event(org.opennms.netmgt.xml.event.Event) SnmpObjId(org.opennms.netmgt.snmp.SnmpObjId) Date(java.util.Date) SnmpResult(org.opennms.netmgt.snmp.SnmpResult)

Example 40 with SnmpObjId

use of org.opennms.netmgt.snmp.SnmpObjId 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)

Aggregations

SnmpObjId (org.opennms.netmgt.snmp.SnmpObjId)73 SnmpValue (org.opennms.netmgt.snmp.SnmpValue)47 SnmpInstId (org.opennms.netmgt.snmp.SnmpInstId)23 SnmpAgentConfig (org.opennms.netmgt.snmp.SnmpAgentConfig)21 InetAddress (java.net.InetAddress)19 ArrayList (java.util.ArrayList)15 Test (org.junit.Test)14 Map (java.util.Map)13 PollStatus (org.opennms.netmgt.poller.PollStatus)12 PDU (org.snmp4j.PDU)10 ParameterMap (org.opennms.core.utils.ParameterMap)9 SnmpResult (org.opennms.netmgt.snmp.SnmpResult)7 SnmpTrapBuilder (org.opennms.netmgt.snmp.SnmpTrapBuilder)6 List (java.util.List)5 UndeclaredThrowableException (java.lang.reflect.UndeclaredThrowableException)4 SnmpWalker (org.opennms.netmgt.snmp.SnmpWalker)4 IOException (java.io.IOException)3 Date (java.util.Date)3 HashMap (java.util.HashMap)3 ExecutionException (java.util.concurrent.ExecutionException)3