Search in sources :

Example 1 with PDUv1

use of org.snmp4j.PDUv1 in project opennms by OpenNMS.

the class Snmp4JUtils method convertPduToBytes.

/**
	 * @param address
	 * @param port
	 * @param community
	 * @param pdu
	 * 
	 * @return Byte array representing the {@link PDU} in either SNMPv1 or SNMPv2 
	 * format, depending on the type of the {@link PDU} object.
	 */
public static byte[] convertPduToBytes(InetAddress address, int port, String community, PDU pdu) throws Exception {
    final CountDownLatch latch = new CountDownLatch(1);
    final AtomicReference<byte[]> bytes = new AtomicReference<>();
    // IP address is optional when using the DummyTransport because
    // all requests are sent to the {@link DummyTransportResponder}
    final DummyTransport<IpAddress> transport = new DummyTransport<IpAddress>(null);
    final AbstractTransportMapping<IpAddress> responder = transport.getResponder(null);
    // Add a DummyTransportResponder listener that will receive the raw bytes of the PDU
    responder.addTransportListener(new TransportListener() {

        @Override
        public void processMessage(TransportMapping transport, Address address, ByteBuffer byteBuffer, TransportStateReference state) {
            byteBuffer.rewind();
            final byte[] byteArray = new byte[byteBuffer.remaining()];
            byteBuffer.get(byteArray);
            bytes.set(byteArray);
            byteBuffer.rewind();
            latch.countDown();
        }
    });
    // Create our own MessageDispatcher since we don't need to do all
    // of the crypto operations necessary to initialize SNMPv3 which is slow
    MessageDispatcher dispatcher = new MessageDispatcherImpl();
    dispatcher.addMessageProcessingModel(new MPv1());
    dispatcher.addMessageProcessingModel(new MPv2c());
    Snmp snmp = new Snmp(dispatcher, responder);
    snmp.listen();
    CommunityTarget target = new CommunityTarget();
    target.setCommunity(new OctetString(community));
    if (pdu instanceof PDUv1) {
        target.setVersion(SnmpConstants.version1);
    } else {
        target.setVersion(SnmpConstants.version2c);
    }
    target.setAddress(Snmp4JAgentConfig.convertAddress(address, port));
    snmp.send(pdu, target, transport);
    latch.await();
    return bytes.get();
}
Also used : OctetString(org.snmp4j.smi.OctetString) Address(org.snmp4j.smi.Address) IpAddress(org.snmp4j.smi.IpAddress) InetAddress(java.net.InetAddress) TransportMapping(org.snmp4j.TransportMapping) AbstractTransportMapping(org.snmp4j.transport.AbstractTransportMapping) MPv2c(org.snmp4j.mp.MPv2c) AtomicReference(java.util.concurrent.atomic.AtomicReference) MessageDispatcherImpl(org.snmp4j.MessageDispatcherImpl) CountDownLatch(java.util.concurrent.CountDownLatch) ByteBuffer(java.nio.ByteBuffer) TransportListener(org.snmp4j.transport.TransportListener) MessageDispatcher(org.snmp4j.MessageDispatcher) TransportStateReference(org.snmp4j.TransportStateReference) Snmp(org.snmp4j.Snmp) IpAddress(org.snmp4j.smi.IpAddress) MPv1(org.snmp4j.mp.MPv1) DummyTransport(org.snmp4j.transport.DummyTransport) PDUv1(org.snmp4j.PDUv1) CommunityTarget(org.snmp4j.CommunityTarget)

Example 2 with PDUv1

use of org.snmp4j.PDUv1 in project opennms by OpenNMS.

the class Snmp4JStrategy method buildAgentConfig.

protected SnmpAgentConfig buildAgentConfig(String address, int port, String community, PDU pdu) throws UnknownHostException {
    SnmpAgentConfig config = new SnmpAgentConfig();
    config.setAddress(InetAddress.getByName(address));
    config.setPort(port);
    config.setVersion(pdu instanceof PDUv1 ? SnmpAgentConfig.VERSION1 : SnmpAgentConfig.VERSION2C);
    return config;
}
Also used : SnmpAgentConfig(org.opennms.netmgt.snmp.SnmpAgentConfig) PDUv1(org.snmp4j.PDUv1)

Example 3 with PDUv1

use of org.snmp4j.PDUv1 in project opennms by OpenNMS.

the class MockProxy method processGetNext.

/**
     * @param request
     * @return
     */
private PDU processGetNext(PDU request) {
    PDU response = request;
    response.setErrorIndex(0);
    response.setErrorStatus(0);
    response.setType(PDU.RESPONSE);
    Vector<? extends VariableBinding> varBinds = response.getVariableBindings();
    for (int i = 0; i < varBinds.size(); i++) {
        VariableBinding varBind = varBinds.get(i);
        VariableBinding nextVarBind = m_agent.getNext(varBind.getOid());
        if (nextVarBind == null) {
            if (response instanceof PDUv1) {
                if (response.getErrorIndex() == 0) {
                    response.setErrorIndex(i + 1);
                    response.setErrorStatus(PDU.noSuchName);
                }
            } else {
                varBind.setVariable(Null.endOfMibView);
            }
        } else {
            response.set(i, nextVarBind);
        }
    }
    return response;
}
Also used : PDU(org.snmp4j.PDU) PDUv1(org.snmp4j.PDUv1) VariableBinding(org.snmp4j.smi.VariableBinding)

Example 4 with PDUv1

use of org.snmp4j.PDUv1 in project opennms by OpenNMS.

the class MockProxy method processGet.

/**
     * @param request
     * @return
     */
private PDU processGet(PDU request) {
    PDU response = request;
    response.setErrorIndex(0);
    response.setErrorStatus(0);
    response.setType(PDU.RESPONSE);
    Vector<? extends VariableBinding> varBinds = response.getVariableBindings();
    for (int i = 0; i < varBinds.size(); i++) {
        VariableBinding varBind = varBinds.get(i);
        VariableBinding nextVarBind = m_agent.get(varBind.getOid());
        if (nextVarBind == null) {
            if (response instanceof PDUv1) {
                if (response.getErrorIndex() == 0) {
                    response.setErrorIndex(i + 1);
                    response.setErrorStatus(PDU.noSuchName);
                }
            } else {
                varBind.setVariable(Null.endOfMibView);
            }
        } else {
            response.set(i, nextVarBind);
        }
    }
    return response;
}
Also used : PDU(org.snmp4j.PDU) PDUv1(org.snmp4j.PDUv1) VariableBinding(org.snmp4j.smi.VariableBinding)

Example 5 with PDUv1

use of org.snmp4j.PDUv1 in project camel by apache.

the class SnmpConverters method toString.

/**
     * Converts the given snmp pdu to a String body.
     *
     * @param pdu       the snmp pdu
     * @return  the text content
     */
@Converter
public static String toString(PDU pdu) {
    // the output buffer
    StringBuilder sb = new StringBuilder();
    // prepare the header
    if (pdu.getType() == PDU.V1TRAP) {
        sb.append("<" + SNMP_TAG + " messageType=\"v1\">");
    } else {
        sb.append(SNMP_TAG_OPEN);
    }
    // Extract SNMPv1 specific variables
    if (pdu.getType() == PDU.V1TRAP) {
        PDUv1 v1pdu = (PDUv1) pdu;
        entryAppend(sb, "enterprise", v1pdu.getEnterprise().toString());
        entryAppend(sb, "agent-addr", v1pdu.getAgentAddress().toString());
        entryAppend(sb, "generic-trap", Integer.toString(v1pdu.getGenericTrap()));
        entryAppend(sb, "specific-trap", Integer.toString(v1pdu.getSpecificTrap()));
        entryAppend(sb, "time-stamp", Long.toString(v1pdu.getTimestamp()));
    }
    // now loop all variables of the response
    for (Object o : pdu.getVariableBindings()) {
        VariableBinding b = (VariableBinding) o;
        sb.append(ENTRY_TAG_OPEN);
        sb.append(OID_TAG_OPEN);
        sb.append(b.getOid().toString());
        sb.append(OID_TAG_CLOSE);
        sb.append(VALUE_TAG_OPEN);
        sb.append(StringHelper.xmlEncode(b.getVariable().toString()));
        sb.append(VALUE_TAG_CLOSE);
        sb.append(ENTRY_TAG_CLOSE);
    }
    // prepare the footer
    sb.append(SNMP_TAG_CLOSE);
    return sb.toString();
}
Also used : PDUv1(org.snmp4j.PDUv1) VariableBinding(org.snmp4j.smi.VariableBinding) Converter(org.apache.camel.Converter)

Aggregations

PDUv1 (org.snmp4j.PDUv1)12 VariableBinding (org.snmp4j.smi.VariableBinding)7 PDU (org.snmp4j.PDU)6 OctetString (org.snmp4j.smi.OctetString)5 IpAddress (org.snmp4j.smi.IpAddress)4 OID (org.snmp4j.smi.OID)4 InetAddress (java.net.InetAddress)3 Snmp (org.snmp4j.Snmp)3 Test (org.junit.Test)2 TrapInformation (org.opennms.netmgt.snmp.TrapInformation)2 CommunityTarget (org.snmp4j.CommunityTarget)2 MessageDispatcher (org.snmp4j.MessageDispatcher)2 Address (org.snmp4j.smi.Address)2 UdpAddress (org.snmp4j.smi.UdpAddress)2 ByteBuffer (java.nio.ByteBuffer)1 Date (java.util.Date)1 CountDownLatch (java.util.concurrent.CountDownLatch)1 AtomicReference (java.util.concurrent.atomic.AtomicReference)1 Converter (org.apache.camel.Converter)1 SnmpAgentConfig (org.opennms.netmgt.snmp.SnmpAgentConfig)1