Search in sources :

Example 1 with CommandResponderEvent

use of org.snmp4j.CommandResponderEvent in project LogHub by fbacchella.

the class SnmpTrap method processPdu.

@Override
public void processPdu(CommandResponderEvent trap) {
    try {
        PDU pdu = trap.getPDU();
        Address localaddr = trap.getTransportMapping().getListenAddress();
        Address remoteaddr = trap.getPeerAddress();
        ConnectionContext ctx = ConnectionContext.EMPTY;
        if (localaddr instanceof TransportIpAddress && remoteaddr instanceof TransportIpAddress) {
            InetSocketAddress localinetaddr = getSA((TransportIpAddress) localaddr);
            InetSocketAddress remoteinetaddr = getSA((TransportIpAddress) remoteaddr);
            ctx = new IpConnectionContext(localinetaddr, remoteinetaddr, null);
        }
        Event event = emptyEvent(ctx);
        if (pdu instanceof PDUv1) {
            PDUv1 pduv1 = (PDUv1) pdu;
            String enterprise = (String) convertVar(pduv1.getEnterprise());
            event.put("enterprise", enterprise);
            event.put("agent_addr", pduv1.getAgentAddress().getInetAddress());
            if (pduv1.getGenericTrap() != PDUv1.ENTERPRISE_SPECIFIC) {
                event.put("generic_trap", GENERICTRAP.values()[pduv1.getGenericTrap()].toString());
            } else {
                String resolved = formatter.format(pduv1.getEnterprise(), new Integer32(pduv1.getSpecificTrap()), true);
                event.put("specific_trap", resolved);
            }
            event.put("time_stamp", 1.0 * pduv1.getTimestamp() / 100.0);
        }
        @SuppressWarnings("unchecked") Enumeration<VariableBinding> vbenum = (Enumeration<VariableBinding>) pdu.getVariableBindings().elements();
        for (VariableBinding i : Collections.list(vbenum)) {
            OID vbOID = i.getOid();
            Object value = convertVar(i.getVariable());
            smartPut(event, vbOID, value);
        }
        send(event);
    } catch (Exception e) {
        logger.error(e.getMessage());
        logger.catching(e);
    } finally {
        trap.setProcessed(true);
    }
}
Also used : PDU(org.snmp4j.PDU) Enumeration(java.util.Enumeration) Address(org.snmp4j.smi.Address) IpAddress(org.snmp4j.smi.IpAddress) TransportIpAddress(org.snmp4j.smi.TransportIpAddress) UdpAddress(org.snmp4j.smi.UdpAddress) InetSocketAddress(java.net.InetSocketAddress) GenericAddress(org.snmp4j.smi.GenericAddress) TransportIpAddress(org.snmp4j.smi.TransportIpAddress) InetSocketAddress(java.net.InetSocketAddress) OctetString(org.snmp4j.smi.OctetString) OID(org.snmp4j.smi.OID) IOException(java.io.IOException) Integer32(org.snmp4j.smi.Integer32) UnsignedInteger32(org.snmp4j.smi.UnsignedInteger32) IpConnectionContext(loghub.IpConnectionContext) CommandResponderEvent(org.snmp4j.CommandResponderEvent) Event(loghub.Event) ConnectionContext(loghub.ConnectionContext) IpConnectionContext(loghub.IpConnectionContext) PDUv1(org.snmp4j.PDUv1) VariableBinding(org.snmp4j.smi.VariableBinding)

Example 2 with CommandResponderEvent

use of org.snmp4j.CommandResponderEvent in project LogHub by fbacchella.

the class TestTrap method testtrapv1Generic.

@Ignore
@Test
public void testtrapv1Generic() throws InterruptedException, IOException {
    BlockingQueue<Event> receiver = new ArrayBlockingQueue<>(2);
    SnmpTrap r = new SnmpTrap(receiver, new Pipeline(Collections.emptyList(), "testbig", null));
    r.setPort(0);
    Map<String, Object> props = new HashMap<>();
    props.put("mibdirs", new String[] { "/usr/share/snmp/mibs", "/tmp/mibs" });
    Assert.assertTrue(r.configure(new Properties(props)));
    r.start();
    CommandResponderEvent trapEvent = new CommandResponderEvent(new MessageDispatcherImpl(), new DefaultUdpTransportMapping(), TransportIpAddress.parse("127.0.0.1/162"), 0, 0, null, 0, null, null, 0, null);
    PDUv1 pdu = new PDUv1();
    pdu.setEnterprise(new OID("1.3.6.1.4.1.232"));
    pdu.setAgentAddress(new IpAddress());
    pdu.setGenericTrap(1);
    pdu.setTimestamp(10);
    pdu.add(new VariableBinding(new OID("1.3.6.1.6.3.1.1.4.1"), new OctetString("lldpRemTablesChange")));
    trapEvent.setPDU(pdu);
    r.processPdu(trapEvent);
    Event e = receiver.poll();
    Assert.assertEquals(0.1, (Double) e.get("time_stamp"), 1e-10);
    Assert.assertEquals("warmStart", e.get("generic_trap"));
    Assert.assertEquals("compaq", e.get("enterprise"));
    Assert.assertEquals(null, e.get("specific_trap"));
    Assert.assertEquals("lldpRemTablesChange", e.get("snmpTrapOID"));
    Assert.assertEquals(InetAddress.getByName("0.0.0.0"), e.get("agent_addr"));
    r.interrupt();
}
Also used : OctetString(org.snmp4j.smi.OctetString) HashMap(java.util.HashMap) CommandResponderEvent(org.snmp4j.CommandResponderEvent) MessageDispatcherImpl(org.snmp4j.MessageDispatcherImpl) DefaultUdpTransportMapping(org.snmp4j.transport.DefaultUdpTransportMapping) OctetString(org.snmp4j.smi.OctetString) OID(org.snmp4j.smi.OID) Properties(loghub.configuration.Properties) Pipeline(loghub.Pipeline) ArrayBlockingQueue(java.util.concurrent.ArrayBlockingQueue) CommandResponderEvent(org.snmp4j.CommandResponderEvent) Event(loghub.Event) IpAddress(org.snmp4j.smi.IpAddress) TransportIpAddress(org.snmp4j.smi.TransportIpAddress) PDUv1(org.snmp4j.PDUv1) VariableBinding(org.snmp4j.smi.VariableBinding) Ignore(org.junit.Ignore) Test(org.junit.Test)

Example 3 with CommandResponderEvent

use of org.snmp4j.CommandResponderEvent 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()) {
            final Snmp snmp = info.getSession();
            Snmp4JStrategy.trackSession(snmp);
            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 4 with CommandResponderEvent

use of org.snmp4j.CommandResponderEvent in project LogHub by fbacchella.

the class TestTrap method testbig.

// @Test
// public void testone() throws InterruptedException, IOException {
// BlockingQueue<Event> receiver = new ArrayBlockingQueue<>(1);
// SnmpTrap r = new SnmpTrap(receiver, new Pipeline(Collections.emptyList(), "testone", null));
// r.setPort(0);
// Assert.assertTrue("Failed to configure trap receiver", r.configure(new Properties(Collections.emptyMap())));;
// List<String> content = r.smartPrint(new OID("1.0.8802.1.1.2.1.1.2.5"));
// Assert.assertEquals(1, content.size());
// Assert.assertEquals("lldpMessageTxHoldMultiplier", content.get(0));
// r.close();
// }
@Ignore
@Test
public void testbig() throws InterruptedException, IOException {
    BlockingQueue<Event> receiver = new ArrayBlockingQueue<>(2);
    SnmpTrap r = new SnmpTrap(receiver, new Pipeline(Collections.emptyList(), "testbig", null));
    r.setPort(0);
    Map<String, Object> props = new HashMap<>();
    props.put("mibdirs", new String[] { "/usr/share/snmp/mibs", "/tmp/mibs" });
    Assert.assertTrue(r.configure(new Properties(props)));
    r.start();
    CommandResponderEvent trapEvent = new CommandResponderEvent(new MessageDispatcherImpl(), new DefaultUdpTransportMapping(), TransportIpAddress.parse("127.0.0.1/162"), 0, 0, null, 0, null, null, 0, null);
    PDU pdu = new PDU();
    pdu.add(new VariableBinding(new OID("1.0.8802.1.1.2.1.4.1.1.8.207185300.2.15079"), new OctetString("vnet7")));
    pdu.add(new VariableBinding(new OID("1.0.8802.1.1.2.1.3.7.1.4.2"), new OctetString("eth0")));
    pdu.add(new VariableBinding(new OID("1.0.8802.1.1.2.1.4.1.1.9.207185300.2.15079"), new OctetString("localhost")));
    pdu.add(new VariableBinding(new OID("1.3.6.1.6.3.1.1.4.1"), new OctetString("lldpRemTablesChange")));
    trapEvent.setPDU(pdu);
    r.processPdu(trapEvent);
    Event e = receiver.poll();
    logger.debug(e.getClass());
    @SuppressWarnings("unchecked") Map<String, ?> details = (Map<String, ?>) e.get("lldpRemSysName");
    Assert.assertEquals(3, Array.getLength(details.get("index")));
    Assert.assertEquals("localhost", details.get("value"));
    r.interrupt();
}
Also used : PDU(org.snmp4j.PDU) OctetString(org.snmp4j.smi.OctetString) HashMap(java.util.HashMap) CommandResponderEvent(org.snmp4j.CommandResponderEvent) MessageDispatcherImpl(org.snmp4j.MessageDispatcherImpl) DefaultUdpTransportMapping(org.snmp4j.transport.DefaultUdpTransportMapping) OctetString(org.snmp4j.smi.OctetString) OID(org.snmp4j.smi.OID) Properties(loghub.configuration.Properties) Pipeline(loghub.Pipeline) ArrayBlockingQueue(java.util.concurrent.ArrayBlockingQueue) CommandResponderEvent(org.snmp4j.CommandResponderEvent) Event(loghub.Event) VariableBinding(org.snmp4j.smi.VariableBinding) HashMap(java.util.HashMap) Map(java.util.Map) Ignore(org.junit.Ignore) Test(org.junit.Test)

Example 5 with CommandResponderEvent

use of org.snmp4j.CommandResponderEvent in project LogHub by fbacchella.

the class TestTrap method testtrapv1Specific.

@Ignore
@Test
public void testtrapv1Specific() throws InterruptedException, IOException {
    BlockingQueue<Event> receiver = new ArrayBlockingQueue<>(2);
    SnmpTrap r = new SnmpTrap(receiver, new Pipeline(Collections.emptyList(), "testbig", null));
    r.setPort(0);
    Map<String, Object> props = new HashMap<>();
    props.put("mibdirs", new String[] { "/usr/share/snmp/mibs", "/tmp/mibs" });
    Assert.assertTrue(r.configure(new Properties(props)));
    r.start();
    CommandResponderEvent trapEvent = new CommandResponderEvent(new MessageDispatcherImpl(), new DefaultUdpTransportMapping(), TransportIpAddress.parse("127.0.0.1/162"), 0, 0, null, 0, null, null, 0, null);
    PDUv1 pdu = new PDUv1();
    pdu.setEnterprise(new OID("1.3.6.1.4.1.232"));
    pdu.setAgentAddress(new IpAddress());
    pdu.setGenericTrap(6);
    pdu.setSpecificTrap(6013);
    pdu.setTimestamp(10);
    trapEvent.setPDU(pdu);
    r.processPdu(trapEvent);
    Event e = receiver.poll();
    Assert.assertEquals(0.1, (Double) e.get("time_stamp"), 1e-10);
    Assert.assertEquals(null, e.get("generic_trap"));
    Assert.assertEquals("compaq", e.get("enterprise"));
    Assert.assertEquals("cpqHePostError", e.get("specific_trap"));
    Assert.assertEquals(InetAddress.getByName("0.0.0.0"), e.get("agent_addr"));
    r.interrupt();
}
Also used : HashMap(java.util.HashMap) CommandResponderEvent(org.snmp4j.CommandResponderEvent) MessageDispatcherImpl(org.snmp4j.MessageDispatcherImpl) DefaultUdpTransportMapping(org.snmp4j.transport.DefaultUdpTransportMapping) OctetString(org.snmp4j.smi.OctetString) OID(org.snmp4j.smi.OID) Properties(loghub.configuration.Properties) Pipeline(loghub.Pipeline) ArrayBlockingQueue(java.util.concurrent.ArrayBlockingQueue) CommandResponderEvent(org.snmp4j.CommandResponderEvent) Event(loghub.Event) IpAddress(org.snmp4j.smi.IpAddress) TransportIpAddress(org.snmp4j.smi.TransportIpAddress) PDUv1(org.snmp4j.PDUv1) Ignore(org.junit.Ignore) Test(org.junit.Test)

Aggregations

CommandResponderEvent (org.snmp4j.CommandResponderEvent)6 IpAddress (org.snmp4j.smi.IpAddress)5 OID (org.snmp4j.smi.OID)5 OctetString (org.snmp4j.smi.OctetString)5 Event (loghub.Event)4 PDUv1 (org.snmp4j.PDUv1)4 VariableBinding (org.snmp4j.smi.VariableBinding)4 DefaultUdpTransportMapping (org.snmp4j.transport.DefaultUdpTransportMapping)4 HashMap (java.util.HashMap)3 ArrayBlockingQueue (java.util.concurrent.ArrayBlockingQueue)3 Pipeline (loghub.Pipeline)3 Properties (loghub.configuration.Properties)3 Ignore (org.junit.Ignore)3 Test (org.junit.Test)3 MessageDispatcherImpl (org.snmp4j.MessageDispatcherImpl)3 TransportIpAddress (org.snmp4j.smi.TransportIpAddress)3 UdpAddress (org.snmp4j.smi.UdpAddress)3 PDU (org.snmp4j.PDU)2 Snmp (org.snmp4j.Snmp)2 Address (org.snmp4j.smi.Address)2