Search in sources :

Example 6 with MessageDispatcherImpl

use of org.snmp4j.MessageDispatcherImpl 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)

Example 7 with MessageDispatcherImpl

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

the class SnmpTrap method configure.

@Override
public boolean configure(Properties properties) {
    decoder = Receiver.NULLDECODER;
    if (!reconfigured && properties.containsKey("mibdirs")) {
        reconfigured = true;
        String[] mibdirs = null;
        try {
            mibdirs = Arrays.stream((Object[]) properties.get("mibdirs")).map(i -> i.toString()).toArray(String[]::new);
            formatter = OIDFormatter.register(mibdirs);
        } catch (ClassCastException e) {
            logger.error("mibdirs property is not a string array");
            logger.catching(Level.DEBUG, e.getCause());
            return false;
        }
    } else {
        formatter = OIDFormatter.register();
    }
    threadPool = ThreadPool.create("Trap", 2);
    MultiThreadedMessageDispatcher dispatcher = new MultiThreadedMessageDispatcher(threadPool, new MessageDispatcherImpl());
    dispatcher.addCommandResponder(this);
    dispatcher.addMessageProcessingModel(new MPv1());
    dispatcher.addMessageProcessingModel(new MPv2c());
    Address listenAddress = GenericAddress.parse(protocol + ":" + listen + "/" + port);
    TransportMapping<?> transport;
    try {
        transport = new DefaultUdpTransportMapping((UdpAddress) listenAddress);
    } catch (IOException e) {
        logger.error("can't bind to {}: {}", listenAddress, e.getMessage());
        return false;
    }
    snmp = new Snmp(dispatcher, transport);
    try {
        snmp.listen();
    } catch (IOException e) {
        logger.error("can't listen: {}", e.getMessage());
    }
    return super.configure(properties);
}
Also used : UdpAddress(org.snmp4j.smi.UdpAddress) 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) MPv2c(org.snmp4j.mp.MPv2c) MessageDispatcherImpl(org.snmp4j.MessageDispatcherImpl) DefaultUdpTransportMapping(org.snmp4j.transport.DefaultUdpTransportMapping) OctetString(org.snmp4j.smi.OctetString) IOException(java.io.IOException) Snmp(org.snmp4j.Snmp) MPv1(org.snmp4j.mp.MPv1) MultiThreadedMessageDispatcher(org.snmp4j.util.MultiThreadedMessageDispatcher)

Aggregations

MessageDispatcherImpl (org.snmp4j.MessageDispatcherImpl)7 OctetString (org.snmp4j.smi.OctetString)6 DefaultUdpTransportMapping (org.snmp4j.transport.DefaultUdpTransportMapping)5 MPv1 (org.snmp4j.mp.MPv1)4 MPv2c (org.snmp4j.mp.MPv2c)4 IpAddress (org.snmp4j.smi.IpAddress)4 HashMap (java.util.HashMap)3 ArrayBlockingQueue (java.util.concurrent.ArrayBlockingQueue)3 Event (loghub.Event)3 Pipeline (loghub.Pipeline)3 Properties (loghub.configuration.Properties)3 Ignore (org.junit.Ignore)3 Test (org.junit.Test)3 CommandResponderEvent (org.snmp4j.CommandResponderEvent)3 PDUv1 (org.snmp4j.PDUv1)3 Snmp (org.snmp4j.Snmp)3 OID (org.snmp4j.smi.OID)3 TransportIpAddress (org.snmp4j.smi.TransportIpAddress)3 IOException (java.io.IOException)2 MessageDispatcher (org.snmp4j.MessageDispatcher)2