Search in sources :

Example 6 with PDUv1

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

the class TrapDTOMapperTest method object2dtoTestV1.

@Test
public void object2dtoTestV1() throws UnknownHostException {
    long testStartTime = new Date().getTime();
    PDUv1 snmp4JV1TrapPdu = new PDUv1();
    snmp4JV1TrapPdu.setType(PDU.V1TRAP);
    snmp4JV1TrapPdu.setEnterprise(new OID(".1.3.6.1.6.3.1.1.4.1.0"));
    snmp4JV1TrapPdu.setGenericTrap(10);
    snmp4JV1TrapPdu.setSpecificTrap(0);
    snmp4JV1TrapPdu.setTimestamp(5000);
    snmp4JV1TrapPdu.add(new VariableBinding(new OID("1.3.6.1.2.1.1.5.0"), new OctetString("mockhost")));
    snmp4JV1TrapPdu.add(new VariableBinding(new OID(".1.3.6.1.2.1.1.3"), new OctetString("mockhost")));
    snmp4JV1TrapPdu.add(new VariableBinding(new OID(".1.3.6.1.6.3.1.1.4.1.0"), new OctetString("mockhost")));
    InetAddress inetAddress = InetAddress.getByName("127.0.0.1");
    ;
    TrapInformation snmp4JV1Trap = new Snmp4JTrapNotifier.Snmp4JV1TrapInformation(inetAddress, "public", snmp4JV1TrapPdu);
    TrapDTO trapDto = new TrapDTO(snmp4JV1Trap);
    System.out.println("trapDto is : " + trapDto);
    System.out.println("trapDto.getBody() is : " + trapDto.getRawMessage());
    System.out.println("trapDto.getCommunity() is : " + trapDto.getCommunity());
    assertEquals(".1.3.6.1.6.3.1.1.4.1.0", trapDto.getTrapIdentity().getEnterpriseId());
    assertEquals(10, trapDto.getTrapIdentity().getGeneric());
    assertEquals(0, trapDto.getTrapIdentity().getSpecific());
    assertEquals(InetAddressUtils.ONE_TWENTY_SEVEN, trapDto.getAgentAddress());
    assertEquals("public", trapDto.getCommunity());
    assertEquals(5000, trapDto.getTimestamp());
    // This is the "default" value from SNMP4J that indicates that the trap has not been forwarded
    assertEquals("v1", trapDto.getVersion());
    // Make sure that the message was created after the start of the test
    assertTrue(trapDto.getCreationTime() >= testStartTime);
}
Also used : OctetString(org.snmp4j.smi.OctetString) PDUv1(org.snmp4j.PDUv1) OID(org.snmp4j.smi.OID) VariableBinding(org.snmp4j.smi.VariableBinding) InetAddress(java.net.InetAddress) Date(java.util.Date) TrapInformation(org.opennms.netmgt.snmp.TrapInformation) Test(org.junit.Test)

Example 7 with PDUv1

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

the class Snmp4JStrategy method sendTest.

public void sendTest(String agentAddress, int port, String community, PDU pdu) {
    for (RegistrationInfo info : s_registrations.values()) {
        if (port == info.getPort()) {
            Snmp snmp = info.getSession();
            MessageDispatcher dispatcher = snmp.getMessageDispatcher();
            TransportMapping<UdpAddress> transport = info.getTransportMapping();
            int securityModel = (pdu instanceof PDUv1 ? SecurityModel.SECURITY_MODEL_SNMPv1 : SecurityModel.SECURITY_MODEL_SNMPv2c);
            int messageModel = (pdu instanceof PDUv1 ? MessageProcessingModel.MPv1 : MessageProcessingModel.MPv2c);
            CommandResponderEvent e = new CommandResponderEvent(dispatcher, transport, new IpAddress(agentAddress), messageModel, securityModel, community.getBytes(), SecurityLevel.NOAUTH_NOPRIV, new PduHandle(), pdu, 1000, null);
            info.getHandler().processPdu(e);
        }
    }
}
Also used : PduHandle(org.snmp4j.mp.PduHandle) MessageDispatcher(org.snmp4j.MessageDispatcher) UdpAddress(org.snmp4j.smi.UdpAddress) CommandResponderEvent(org.snmp4j.CommandResponderEvent) Snmp(org.snmp4j.Snmp) IpAddress(org.snmp4j.smi.IpAddress) PDUv1(org.snmp4j.PDUv1)

Example 8 with PDUv1

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

the class Snmp4JTrapNotifier method processPdu.

@Override
public void processPdu(CommandResponderEvent e) {
    PDU command = e.getPDU();
    if (command != null) {
        IpAddress addr = ((IpAddress) e.getPeerAddress());
        if (command.getType() == PDU.INFORM) {
            // Backing up original content
            int errorIndex = command.getErrorIndex();
            int errorStatus = command.getErrorStatus();
            int type = command.getType();
            // Prepare resopnse
            command.setErrorIndex(0);
            command.setErrorStatus(0);
            command.setType(PDU.RESPONSE);
            StatusInformation statusInformation = new StatusInformation();
            StateReference ref = e.getStateReference();
            // Send the response
            try {
                e.getMessageDispatcher().returnResponsePdu(e.getMessageProcessingModel(), e.getSecurityModel(), e.getSecurityName(), e.getSecurityLevel(), command, e.getMaxSizeResponsePDU(), ref, statusInformation);
                LOG.debug("Sent RESPONSE PDU to peer {} acknowledging receipt of INFORM (reqId={})", addr, command.getRequestID());
            } catch (MessageException ex) {
                LOG.error("Error while sending RESPONSE PDU to peer {}: {} acknowledging receipt of INFORM (reqId={})", addr, ex.getMessage(), command.getRequestID());
            } finally {
                // Restoring original settings
                command.setErrorIndex(errorIndex);
                command.setErrorStatus(errorStatus);
                command.setType(type);
            }
        }
        if (command instanceof PDUv1) {
            m_listener.trapReceived(new Snmp4JV1TrapInformation(addr.getInetAddress(), new String(e.getSecurityName()), (PDUv1) command));
        } else {
            m_listener.trapReceived(new Snmp4JV2TrapInformation(addr.getInetAddress(), new String(e.getSecurityName()), command));
        }
    }
}
Also used : PDU(org.snmp4j.PDU) StateReference(org.snmp4j.mp.StateReference) MessageException(org.snmp4j.MessageException) StatusInformation(org.snmp4j.mp.StatusInformation) IpAddress(org.snmp4j.smi.IpAddress) PDUv1(org.snmp4j.PDUv1)

Example 9 with PDUv1

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

the class TrapNotificationSerializationTest method testsnmp4JV1Serialization.

@Test
public void testsnmp4JV1Serialization() throws UnknownHostException {
    // create instance of Snmp4JV1
    PDUv1 snmp4JV1TrapPdu = new PDUv1();
    snmp4JV1TrapPdu.setType(PDU.V1TRAP);
    snmp4JV1TrapPdu.add(new VariableBinding(new OID("1.3.6.1.2.1.1.5.0"), new OctetString("mockhost")));
    snmp4JV1TrapPdu.add(new VariableBinding(new OID(".1.3.6.1.2.1.1.3"), new OctetString("mockhost")));
    snmp4JV1TrapPdu.add(new VariableBinding(new OID(".1.3.6.1.6.3.1.1.4.1.0"), new OctetString("mockhost")));
    TrapInformation snmp4JV1Trap = new Snmp4JTrapNotifier.Snmp4JV1TrapInformation(inetAddress, new String("public"), snmp4JV1TrapPdu);
    assertTrue(writeTrapNotificationObject(snmp4JV1Trap));
}
Also used : OctetString(org.snmp4j.smi.OctetString) PDUv1(org.snmp4j.PDUv1) OID(org.snmp4j.smi.OID) OctetString(org.snmp4j.smi.OctetString) VariableBinding(org.snmp4j.smi.VariableBinding) TrapInformation(org.opennms.netmgt.snmp.TrapInformation) Test(org.junit.Test)

Example 10 with PDUv1

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

the class Snmp4jTrapReceiverIT method processPdu.

@Override
public synchronized void processPdu(final CommandResponderEvent cmdRespEvent) {
    final PDU pdu = cmdRespEvent.getPDU();
    LOG.debug("Received PDU... " + pdu);
    if (pdu != null) {
        LOG.debug(pdu.getClass().getName());
        LOG.debug("trapType = " + pdu.getType());
        LOG.debug("isPDUv1 = " + (pdu instanceof PDUv1));
        LOG.debug("isTrap = " + (pdu.getType() == PDU.TRAP));
        LOG.debug("isInform = " + (pdu.getType() == PDU.INFORM));
        LOG.debug("variableBindings = " + pdu.getVariableBindings());
        m_trapCount++;
    } else {
        LOG.debug("ERROR: Can't create PDU");
    }
}
Also used : PDU(org.snmp4j.PDU) PDUv1(org.snmp4j.PDUv1)

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