Search in sources :

Example 31 with SnmpObjId

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

the class DiskUsageDetector method isServiceDetected.

/**
 * {@inheritDoc}
 *
 * Returns true if the protocol defined by this plugin is supported. If the
 * protocol is not supported then a false value is returned to the caller.
 * The qualifier map passed to the method is used by the plugin to return
 * additional information by key-name. These key-value pairs can be added to
 * service events if needed.
 */
@Override
public boolean isServiceDetected(final InetAddress address, final SnmpAgentConfig agentConfig) {
    int matchType = MATCH_TYPE_EXACT;
    try {
        if (getPort() > 0) {
            agentConfig.setPort(getPort());
        }
        if (getTimeout() > 0) {
            agentConfig.setTimeout(getTimeout());
        }
        if (getRetries() > -1) {
            agentConfig.setRetries(getRetries());
        }
        if (getForceVersion() != null) {
            String version = getForceVersion();
            // @see http://issues.opennms.org/browse/NMS-7518
            if ("v1".equalsIgnoreCase(version) || "snmpv1".equalsIgnoreCase(version)) {
                agentConfig.setVersion(SnmpAgentConfig.VERSION1);
            } else if ("v2".equalsIgnoreCase(version) || "v2c".equalsIgnoreCase(version) || "snmpv2".equalsIgnoreCase(version) || "snmpv2c".equalsIgnoreCase(version)) {
                agentConfig.setVersion(SnmpAgentConfig.VERSION2C);
            } else if ("v3".equalsIgnoreCase(version) || "snmpv3".equalsIgnoreCase(version)) {
                agentConfig.setVersion(SnmpAgentConfig.VERSION3);
            }
        }
        // 
        if (!"".equals(getMatchType())) {
            String matchTypeStr = getMatchType();
            if (matchTypeStr.equalsIgnoreCase("exact")) {
                matchType = MATCH_TYPE_EXACT;
            } else if (matchTypeStr.equalsIgnoreCase("startswith")) {
                matchType = MATCH_TYPE_STARTSWITH;
            } else if (matchTypeStr.equalsIgnoreCase("endswith")) {
                matchType = MATCH_TYPE_ENDSWITH;
            } else if (matchTypeStr.equalsIgnoreCase("regex")) {
                matchType = MATCH_TYPE_REGEX;
            } else {
                throw new RuntimeException("Unknown value '" + matchTypeStr + "' for parameter 'match-type'");
            }
        }
        SnmpObjId hrStorageDescrSnmpObject = SnmpObjId.get(getHrStorageDescr());
        Map<SnmpInstId, SnmpValue> descrResults = SnmpUtils.getOidValues(agentConfig, "DiskUsagePoller", hrStorageDescrSnmpObject);
        if (descrResults.size() == 0) {
            return false;
        }
        for (Map.Entry<SnmpInstId, SnmpValue> e : descrResults.entrySet()) {
            LOG.debug("capsd: SNMPwalk succeeded, addr={} oid={} instance={} value={}", InetAddressUtils.str(agentConfig.getAddress()), hrStorageDescrSnmpObject, e.getKey(), e.getValue());
            if (isMatch(e.getValue().toString(), getDisk(), matchType)) {
                LOG.debug("Found disk '{}' (matching hrStorageDescr was '{}')", getDisk(), e.getValue());
                return true;
            }
        }
        return false;
    } catch (Throwable t) {
        throw new UndeclaredThrowableException(t);
    }
}
Also used : SnmpValue(org.opennms.netmgt.snmp.SnmpValue) UndeclaredThrowableException(java.lang.reflect.UndeclaredThrowableException) SnmpInstId(org.opennms.netmgt.snmp.SnmpInstId) SnmpObjId(org.opennms.netmgt.snmp.SnmpObjId) Map(java.util.Map)

Example 32 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 33 with SnmpObjId

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

the class MockSnmpStrategy method getNext.

@Override
public SnmpValue[] getNext(final SnmpAgentConfig agentConfig, final SnmpObjId[] oids) {
    final PropertyOidContainer oidContainer = getOidContainer(agentConfig);
    if (oidContainer == null)
        return null;
    final List<SnmpValue> values = new ArrayList<>();
    for (final SnmpObjId oid : oids) {
        values.add(oidContainer.findNextValueForOid(oid));
    }
    return values.toArray(EMPTY_SNMP_VALUE_ARRAY);
}
Also used : SnmpValue(org.opennms.netmgt.snmp.SnmpValue) ArrayList(java.util.ArrayList) SnmpObjId(org.opennms.netmgt.snmp.SnmpObjId)

Example 34 with SnmpObjId

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

the class MockSnmpStrategy method get.

@Override
public SnmpValue[] get(final SnmpAgentConfig agentConfig, final SnmpObjId[] oids) {
    final PropertyOidContainer container = getOidContainer(agentConfig);
    if (container == null)
        return new SnmpValue[oids.length];
    final List<SnmpValue> values = new ArrayList<>();
    for (final SnmpObjId oid : oids) {
        values.add(container.findValueForOid(oid));
    }
    return values.toArray(EMPTY_SNMP_VALUE_ARRAY);
}
Also used : SnmpValue(org.opennms.netmgt.snmp.SnmpValue) ArrayList(java.util.ArrayList) SnmpObjId(org.opennms.netmgt.snmp.SnmpObjId)

Example 35 with SnmpObjId

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

the class Snmp4JStrategy method buildPdu.

protected PDU buildPdu(Snmp4JAgentConfig agentConfig, int pduType, SnmpObjId[] oids, SnmpValue[] values) {
    PDU pdu = agentConfig.createPdu(pduType);
    if (values == null) {
        for (SnmpObjId oid : oids) {
            pdu.add(new VariableBinding(new OID(oid.toString())));
        }
    } else {
        // TODO should this throw an exception?  This situation is fairly bogus and probably signifies a coding error.
        if (oids.length != values.length) {
            Exception e = new SnmpException("PDU values do not match OIDs");
            LOG.error("PDU to prepare has object values but not the same number as there are OIDs.  There are {} OIDs and {} object values.", oids.length, values.length, e);
            return null;
        }
        for (int i = 0; i < oids.length; i++) {
            pdu.add(new VariableBinding(new OID(oids[i].toString()), new Snmp4JValue(values[i].getType(), values[i].getBytes()).getVariable()));
        }
    }
    // TODO should this throw an exception?  This situation is fairly bogus.
    if (pdu.getVariableBindings().size() != oids.length) {
        Exception e = new SnmpException("PDU bindings do not match OIDs");
        LOG.error("Prepared PDU does not have as many variable bindings as there are OIDs.  There are {} OIDs and {} variable bindings.", oids.length, pdu.getVariableBindings(), e);
        return null;
    }
    return pdu;
}
Also used : PDU(org.snmp4j.PDU) ScopedPDU(org.snmp4j.ScopedPDU) SnmpException(org.opennms.netmgt.snmp.SnmpException) SnmpObjId(org.opennms.netmgt.snmp.SnmpObjId) OID(org.snmp4j.smi.OID) VariableBinding(org.snmp4j.smi.VariableBinding) SnmpException(org.opennms.netmgt.snmp.SnmpException) SocketException(java.net.SocketException) IOException(java.io.IOException) UnknownHostException(java.net.UnknownHostException)

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