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);
}
}
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();
}
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);
}
}
}
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();
}
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();
}
Aggregations