use of loghub.IpConnectionContext 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 loghub.IpConnectionContext in project LogHub by fbacchella.
the class Udp method getConnectionContext.
@Override
public ConnectionContext getConnectionContext(ChannelHandlerContext ctx, DatagramPacket message) {
InetSocketAddress remoteaddr = message.sender();
InetSocketAddress localaddr = message.recipient();
return new IpConnectionContext(localaddr, remoteaddr, null);
}
use of loghub.IpConnectionContext in project LogHub by fbacchella.
the class PacketsTest method testDecode.
@SuppressWarnings("unchecked")
@Test
public void testDecode() {
Decoder nfd = new NetflowDecoder();
IpConnectionContext dummyctx = new IpConnectionContext(new InetSocketAddress(0), new InetSocketAddress(0), null);
Arrays.stream(captures).map(i -> {
logger.debug(i + ": ");
return i;
}).map(i -> "/netflow/packets/" + i).map(i -> getClass().getResourceAsStream(i)).filter(i -> i != null).map(i -> {
try {
ByteArrayOutputStream out = new ByteArrayOutputStream();
byte[] buffer = new byte[8 * 1024];
for (int length; (length = i.read(buffer)) != -1; ) {
out.write(buffer, 0, length);
}
return out;
} catch (Exception e) {
Assert.fail(e.getMessage());
return null;
}
}).filter(i -> i != null).map(i -> Unpooled.wrappedBuffer(i.toByteArray())).forEach(i -> {
try {
while (i.isReadable()) {
Map<String, Object> content = nfd.decode(dummyctx, i);
Assert.assertTrue(content.containsKey("version"));
Assert.assertTrue(content.containsKey("sequenceNumber"));
Assert.assertTrue(content.containsKey("records"));
((List<Map<String, Object>>) content.get("records")).forEach(j -> Assert.assertTrue(j.containsKey("_type")));
if (((Integer) content.get("version")) < 10) {
Assert.assertTrue(content.containsKey("SysUptime"));
}
logger.debug(content);
}
} catch (Exception e) {
Assert.fail(e.getMessage());
}
});
}
use of loghub.IpConnectionContext in project LogHub by fbacchella.
the class ProcessorTest method test.
@Test
public void test() throws IOException, DecodeException, ProcessorException, InterruptedException {
Processor p = new Processor();
InputStream is = getClass().getResourceAsStream("/netflow/packets/ipfix.dat");
ByteArrayOutputStream out = new ByteArrayOutputStream();
byte[] buffer = new byte[8 * 1024];
for (int length; (length = is.read(buffer)) != -1; ) {
out.write(buffer, 0, length);
}
ByteBuf bbuffer = Unpooled.wrappedBuffer(out.toByteArray());
Decoder nfd = new NetflowDecoder();
IpConnectionContext dummyctx = new IpConnectionContext(new InetSocketAddress(0), new InetSocketAddress(0), null);
Map<String, Object> content = nfd.decode(dummyctx, bbuffer);
Event e = Tools.getEvent();
e.setTimestamp((Date) content.remove(Event.TIMESTAMPKEY));
e.putAll(content);
ProcessingStatus ps = Tools.runProcessing(e, "main", Collections.singletonList(p));
logger.debug(ps.mainQueue.remove());
logger.debug(ps.mainQueue.remove());
logger.debug(ps.mainQueue.remove());
logger.debug(ps.mainQueue.remove());
logger.debug(ps.mainQueue.remove());
logger.debug(ps.mainQueue.remove());
logger.debug(ps.mainQueue.remove());
logger.debug(ps.mainQueue.remove());
Assert.assertTrue(ps.mainQueue.isEmpty());
}
use of loghub.IpConnectionContext in project LogHub by fbacchella.
the class NetflowDecoder method decode.
@Override
public Map<String, Object> decode(ConnectionContext ctx, ByteBuf bbuf) throws DecodeException {
InetAddress addr;
if (ctx instanceof IpConnectionContext) {
addr = ((IpConnectionContext) ctx).getRemoteAddress().getAddress();
NetflowPacket packet = PacketFactory.parsePacket(addr, bbuf);
Map<String, Object> ev = new HashMap<>();
ev.put(Event.TIMESTAMPKEY, Date.from(packet.getExportTime()));
ev.put("sequenceNumber", packet.getSequenceNumber());
switch(packet.getVersion()) {
case 5:
Netflow5Packet packet5 = (Netflow5Packet) packet;
ev.put("engine_type", packet5.getEngineType());
ev.put("sampling_interval", packet5.getSamplingInterval());
ev.put("sampling_mode", packet5.getSamplingMode());
ev.put("SysUptime", packet5.getSysUpTime());
break;
case 9:
ev.put("SysUptime", ((Netflow9Packet) packet).getSysUpTime());
break;
case 10:
break;
default:
throw new UnsupportedOperationException();
}
ev.put("version", packet.getVersion());
ev.put("records", packet.getRecords());
return ev;
}
return null;
}
Aggregations