Search in sources :

Example 26 with SnmpObjId

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

the class LaTableMonitor method poll.

/**
     * {@inheritDoc}
     *
     * <P>
     * The poll() method is responsible for polling the specified address for
     * SNMP service availability.
     * </P>
     * @exception RuntimeException
     *                Thrown for any uncrecoverable errors.
     */
public PollStatus poll(MonitoredService svc, Map<String, Object> parameters) {
    PollStatus status = PollStatus.available();
    InetAddress ipaddr = svc.getAddress();
    ArrayList<String> errorStringReturn = new ArrayList<String>();
    // Retrieve this interface's SNMP peer object
    final SnmpAgentConfig agentConfig = getAgentConfig(svc, parameters);
    final String hostAddress = InetAddressUtils.str(ipaddr);
    LOG.debug("poll: setting SNMP peer attribute for interface {}", hostAddress);
    agentConfig.setTimeout(ParameterMap.getKeyedInteger(parameters, "timeout", agentConfig.getTimeout()));
    agentConfig.setRetries(ParameterMap.getKeyedInteger(parameters, "retry", ParameterMap.getKeyedInteger(parameters, "retries", agentConfig.getRetries())));
    agentConfig.setPort(ParameterMap.getKeyedInteger(parameters, "port", agentConfig.getPort()));
    LOG.debug("poll: service= SNMP address= {}", agentConfig);
    try {
        LOG.debug("PrTableMonitor.poll: SnmpAgentConfig address: {}", agentConfig);
        SnmpObjId laTableErrorSnmpObject = SnmpObjId.get(laTableErrorFlag);
        Map<SnmpInstId, SnmpValue> flagResults = SnmpUtils.getOidValues(agentConfig, "LaTableMonitor", laTableErrorSnmpObject);
        if (flagResults.size() == 0) {
            LOG.debug("SNMP poll failed: no results, addr={} oid={}", hostAddress, laTableErrorSnmpObject);
            return PollStatus.unavailable();
        }
        for (Map.Entry<SnmpInstId, SnmpValue> e : flagResults.entrySet()) {
            LOG.debug("poll: SNMPwalk poll succeeded, addr={} oid={} instance={} value={}", hostAddress, laTableErrorSnmpObject, e.getKey(), e.getValue());
            if (e.getValue().toString().equals("1")) {
                LOG.debug("LaTableMonitor.poll: found errorFlag=1");
                SnmpObjId laTableErrorMsgSnmpObject = SnmpObjId.get(laTableErrorMsg + "." + e.getKey().toString());
                String LaErrorMsg = SnmpUtils.get(agentConfig, laTableErrorMsgSnmpObject).toDisplayString();
                //Stash the error in an ArrayList to then enumerate over later
                errorStringReturn.add(LaErrorMsg);
            }
        }
        //Check the arraylist and construct return value
        if (errorStringReturn.size() > 0) {
            return PollStatus.unavailable(errorStringReturn.toString());
        } else {
            return status;
        }
    } catch (NumberFormatException e) {
        String reason1 = "Number operator used on a non-number " + e.getMessage();
        LOG.error(reason1, e);
        return PollStatus.unavailable(reason1);
    } catch (IllegalArgumentException e) {
        String reason1 = "Invalid SNMP Criteria: " + e.getMessage();
        LOG.error(reason1, e);
        return PollStatus.unavailable(reason1);
    } catch (Throwable t) {
        String reason1 = "Unexpected exception during SNMP poll of interface " + hostAddress;
        LOG.warn(reason1);
        return PollStatus.unavailable(reason1);
    }
}
Also used : SnmpAgentConfig(org.opennms.netmgt.snmp.SnmpAgentConfig) PollStatus(org.opennms.netmgt.poller.PollStatus) ArrayList(java.util.ArrayList) SnmpObjId(org.opennms.netmgt.snmp.SnmpObjId) SnmpValue(org.opennms.netmgt.snmp.SnmpValue) SnmpInstId(org.opennms.netmgt.snmp.SnmpInstId) InetAddress(java.net.InetAddress) Map(java.util.Map) ParameterMap(org.opennms.core.utils.ParameterMap)

Example 27 with SnmpObjId

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

the class SnmpIfAdmin method setIfAdmin.

/**
     * <p>
     * Set admin interface status to value.
     * </p>
     *
     * @param ifindex
     *            interface index to set
     * @param value
     *            desired interface status value
     * @return The status of interface after operation
     * @throws SnmpBadConversionException
     *             Throw if returned code is not an integer
     * @throws java.sql.SQLException if any.
     */
public boolean setIfAdmin(int ifindex, int value) throws SQLException {
    if (value != UP && value != DOWN)
        throw new IllegalArgumentException("Value not valid");
    SnmpObjId oid = SnmpObjId.get(snmpObjectId + "." + ifindex);
    SnmpValue val = SnmpUtils.getValueFactory().getInt32(value);
    SnmpValue result = SnmpUtils.set(m_agent, oid, val);
    if (result != null && result.isNumeric()) {
        int retvalue = result.toInt();
        setIfAdminStatusInDB(ifindex, retvalue);
        if (retvalue == value)
            return true;
    }
    return false;
}
Also used : SnmpValue(org.opennms.netmgt.snmp.SnmpValue) SnmpObjId(org.opennms.netmgt.snmp.SnmpObjId)

Example 28 with SnmpObjId

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

the class SnmpProtocolCollector method collect.

@Override
public CollectionJob collect(final CollectionJob collectionJob) {
    LOG.info("SnmpProtocolCollector is collecting collectionJob '{}'", collectionJob.getId());
    SnmpAgentConfig snmpAgentConfig = SnmpAgentConfig.parseProtocolConfigurationString(collectionJob.getProtocolConfiguration());
    List<Collectable> trackers = new ArrayList<Collectable>();
    for (final String metricObjId : collectionJob.getAllMetrics()) {
        SnmpObjId requestOid = SnmpObjId.get(metricObjId);
        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) {
                LOG.trace("Collected SnmpValue '{}'", result);
                SnmpValue value = result.getValue();
                String metricType = value == null ? "unknown" : typeToString(value.getType());
                collectionJob.setMetricValue(metricObjId, metricType, value == null ? null : value.toDisplayString());
            }

            @Override
            public void setFailed(boolean failed) {
                super.setFailed(failed);
                LOG.trace("Collection Failed for metricObjId '{}'", metricObjId);
                collectionJob.setMetricValue(metricObjId, "unknown", null);
            }

            @Override
            public void setTimedOut(boolean timedOut) {
                super.setTimedOut(timedOut);
                LOG.trace("Collection timedOut for metricObjId '{}'", metricObjId);
                collectionJob.setMetricValue(metricObjId, "unknown", null);
            }
        };
        trackers.add(instanceTracker);
    }
    // Attempt to determine the location name
    String locationName = null;
    OnmsNode node = m_nodeDao.get(collectionJob.getNodeId());
    if (node != null) {
        OnmsMonitoringLocation monitoringLocation = node.getLocation();
        if (monitoringLocation != null) {
            locationName = monitoringLocation.getLocationName();
        }
    }
    AggregateTracker tracker = new AggregateTracker(trackers);
    CompletableFuture<AggregateTracker> future = m_locationAwareSnmpClient.walk(snmpAgentConfig, tracker).withDescription("NRTG").withLocation(locationName).execute();
    try {
        future.get();
    } catch (ExecutionException e) {
        LOG.warn("Failed to collect SNMP metrics for {}.", snmpAgentConfig.getAddress(), e);
    } catch (InterruptedException e) {
        LOG.warn("Interupted while collectiong SNMP metrics for {}.", snmpAgentConfig.getAddress());
        Thread.interrupted();
    }
    return collectionJob;
}
Also used : SnmpAgentConfig(org.opennms.netmgt.snmp.SnmpAgentConfig) Collectable(org.opennms.netmgt.snmp.Collectable) OnmsNode(org.opennms.netmgt.model.OnmsNode) 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) ExecutionException(java.util.concurrent.ExecutionException) SnmpResult(org.opennms.netmgt.snmp.SnmpResult) OnmsMonitoringLocation(org.opennms.netmgt.model.monitoringLocations.OnmsMonitoringLocation) AggregateTracker(org.opennms.netmgt.snmp.AggregateTracker)

Example 29 with SnmpObjId

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

the class IpAddressTableEntry method getIpAddressNetMask.

/**
     * <p>getIpAdEntNetMask</p>
     *
     * @return a {@link java.net.InetAddress} object.
     */
public InetAddress getIpAddressNetMask() {
    final SnmpValue value = getValue(IP_ADDR_ENT_NETMASK);
    // LOG.debug("getIpAddressNetMask: value = {}", value.toDisplayString());
    final SnmpObjId netmaskRef = value.toSnmpObjId().getInstance(IPAddressTableTracker.IP_ADDRESS_PREFIX_ORIGIN_INDEX);
    if (netmaskRef == null) {
        LOG.warn("Unable to get netmask reference from instance.");
        return null;
    }
    final int[] rawIds = netmaskRef.getIds();
    final int addressType = rawIds[1];
    final int addressLength = rawIds[2];
    final InetAddress address = getInetAddress(rawIds, 3, addressLength);
    final int mask = rawIds[rawIds.length - 1];
    if (addressType == IPAddressTableTracker.TYPE_IPV4) {
        return InetAddressUtils.convertCidrToInetAddressV4(mask);
    } else if (addressType == IPAddressTableTracker.TYPE_IPV6) {
        return InetAddressUtils.convertCidrToInetAddressV6(mask);
    } else if (addressType == IPAddressTableTracker.TYPE_IPV6Z) {
        LOG.debug("Got an IPv6z address, returning {}", address);
    } else {
        LOG.warn("Unsure how to handle IP address type ({})", addressType);
    }
    return address;
}
Also used : SnmpValue(org.opennms.netmgt.snmp.SnmpValue) SnmpObjId(org.opennms.netmgt.snmp.SnmpObjId) InetAddress(java.net.InetAddress) InetAddressUtils.getInetAddress(org.opennms.core.utils.InetAddressUtils.getInetAddress)

Example 30 with SnmpObjId

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

the class TableStrategy method call.

@Override
public OnmsAccessPointCollection call() throws IOException {
    OnmsAccessPointCollection apsUp = new OnmsAccessPointCollection();
    InetAddress ipaddr = m_iface.getIpAddress();
    // Retrieve this interface's SNMP peer object
    SnmpAgentConfig agentConfig = SnmpPeerFactory.getInstance().getAgentConfig(ipaddr);
    if (agentConfig == null) {
        throw new IllegalStateException("SnmpAgentConfig object not available for interface " + ipaddr);
    }
    final String hostAddress = InetAddressUtils.str(ipaddr);
    LOG.debug("poll: setting SNMP peer attribute for interface {}", hostAddress);
    // Get configuration parameters
    String oid = ParameterMap.getKeyedString(m_parameters, "oid", null);
    if (oid == null) {
        throw new IllegalStateException("oid parameter is not set.");
    }
    agentConfig.hashCode();
    // Set timeout and retries on SNMP peer object
    agentConfig.setTimeout(ParameterMap.getKeyedInteger(m_parameters, "timeout", agentConfig.getTimeout()));
    agentConfig.setRetries(ParameterMap.getKeyedInteger(m_parameters, "retry", ParameterMap.getKeyedInteger(m_parameters, "retries", agentConfig.getRetries())));
    agentConfig.setPort(ParameterMap.getKeyedInteger(m_parameters, "port", agentConfig.getPort()));
    LOG.debug("TableStrategy.poll: SnmpAgentConfig address={}", agentConfig);
    // Establish SNMP session with interface
    try {
        SnmpObjId snmpObjectId = SnmpObjId.get(oid);
        Map<SnmpInstId, SnmpValue> map = SnmpUtils.getOidValues(agentConfig, "AccessPointMonitor::TableStrategy", snmpObjectId);
        if (map.size() <= 0) {
            throw new IOException("No entries found in table (possible timeout).");
        }
        for (Map.Entry<SnmpInstId, SnmpValue> entry : map.entrySet()) {
            SnmpValue value = entry.getValue();
            String physAddr = getPhysAddrFromValue(value);
            LOG.debug("AP at value '{}' with MAC '{}' is considered to be ONLINE on controller '{}'", value.toHexString(), physAddr, m_iface.getIpAddress());
            OnmsAccessPoint ap = m_accessPointDao.get(physAddr);
            if (ap != null) {
                if (ap.getPollingPackage().compareToIgnoreCase(getPackage().getName()) == 0) {
                    // Save the controller's IP address
                    ap.setControllerIpAddress(ipaddr);
                    apsUp.add(ap);
                } else {
                    LOG.info("AP with MAC '{}' is in a different package.", physAddr);
                }
            } else {
                LOG.info("No matching AP in database for value '{}'.", value.toHexString());
            }
        }
    } catch (InterruptedException e) {
        LOG.error("Interrupted while polling {}", hostAddress, e);
    }
    return apsUp;
}
Also used : SnmpAgentConfig(org.opennms.netmgt.snmp.SnmpAgentConfig) OnmsAccessPoint(org.opennms.netmgt.model.OnmsAccessPoint) SnmpObjId(org.opennms.netmgt.snmp.SnmpObjId) OnmsAccessPointCollection(org.opennms.netmgt.model.OnmsAccessPointCollection) IOException(java.io.IOException) SnmpValue(org.opennms.netmgt.snmp.SnmpValue) SnmpInstId(org.opennms.netmgt.snmp.SnmpInstId) InetAddress(java.net.InetAddress) Map(java.util.Map) ParameterMap(org.opennms.core.utils.ParameterMap)

Aggregations

SnmpObjId (org.opennms.netmgt.snmp.SnmpObjId)71 SnmpValue (org.opennms.netmgt.snmp.SnmpValue)48 SnmpInstId (org.opennms.netmgt.snmp.SnmpInstId)22 SnmpAgentConfig (org.opennms.netmgt.snmp.SnmpAgentConfig)20 InetAddress (java.net.InetAddress)18 ArrayList (java.util.ArrayList)15 Map (java.util.Map)14 Test (org.junit.Test)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 List (java.util.List)5 UndeclaredThrowableException (java.lang.reflect.UndeclaredThrowableException)4 ExecutionException (java.util.concurrent.ExecutionException)4 SnmpTrapBuilder (org.opennms.netmgt.snmp.SnmpTrapBuilder)4 SnmpWalker (org.opennms.netmgt.snmp.SnmpWalker)4 IOException (java.io.IOException)3 Date (java.util.Date)3 HashMap (java.util.HashMap)3