Search in sources :

Example 1 with ConnectionContext

use of loghub.ConnectionContext 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 ConnectionContext

use of loghub.ConnectionContext in project LogHub by fbacchella.

the class Kafka method run.

@Override
public void run() {
    consumer.subscribe(Collections.singletonList(topic));
    boolean broke = false;
    while (!isInterrupted()) {
        ConsumerRecords<Long, byte[]> consumerRecords = consumer.poll(100);
        if (consumerRecords.count() == 0) {
            continue;
        }
        for (ConsumerRecord<Long, byte[]> record : consumerRecords) {
            ConnectionContext ctxt = new KafkaContext(record.topic());
            Event event = emptyEvent(ctxt);
            if (record.timestampType() == TimestampType.CREATE_TIME) {
                event.setTimestamp(new Date(record.timestamp()));
            }
            Header[] headers = record.headers().toArray();
            if (headers.length > 0) {
                Map<String, byte[]> headersMap = new HashMap<>(headers.length);
                Arrays.stream(headers).forEach(i -> headersMap.put(i.key(), i.value()));
                event.put("headers", headersMap);
            }
            byte[] content = record.value();
            try {
                event.putAll(decoder.decode(ctxt, content, 0, content.length));
                send(event);
            } catch (DecodeException e) {
                logger.error(e.getMessage());
                logger.catching(e);
            }
            if (isInterrupted()) {
                consumer.commitSync(Collections.singletonMap(new TopicPartition(record.topic(), record.partition()), new OffsetAndMetadata(record.offset())));
                broke = true;
                break;
            }
        }
        if (!broke) {
            consumer.commitAsync();
        } else {
            break;
        }
    }
    consumer.close();
}
Also used : HashMap(java.util.HashMap) DecodeException(loghub.Decoder.DecodeException) Date(java.util.Date) Header(org.apache.kafka.common.header.Header) TopicPartition(org.apache.kafka.common.TopicPartition) OffsetAndMetadata(org.apache.kafka.clients.consumer.OffsetAndMetadata) Event(loghub.Event) ConnectionContext(loghub.ConnectionContext)

Aggregations

ConnectionContext (loghub.ConnectionContext)2 Event (loghub.Event)2 IOException (java.io.IOException)1 InetSocketAddress (java.net.InetSocketAddress)1 Date (java.util.Date)1 Enumeration (java.util.Enumeration)1 HashMap (java.util.HashMap)1 DecodeException (loghub.Decoder.DecodeException)1 IpConnectionContext (loghub.IpConnectionContext)1 OffsetAndMetadata (org.apache.kafka.clients.consumer.OffsetAndMetadata)1 TopicPartition (org.apache.kafka.common.TopicPartition)1 Header (org.apache.kafka.common.header.Header)1 CommandResponderEvent (org.snmp4j.CommandResponderEvent)1 PDU (org.snmp4j.PDU)1 PDUv1 (org.snmp4j.PDUv1)1 Address (org.snmp4j.smi.Address)1 GenericAddress (org.snmp4j.smi.GenericAddress)1 Integer32 (org.snmp4j.smi.Integer32)1 IpAddress (org.snmp4j.smi.IpAddress)1 OID (org.snmp4j.smi.OID)1