Search in sources :

Example 1 with GelfMessage

use of org.graylog2.gelfclient.GelfMessage in project graylog2-server by Graylog2.

the class GelfChunkAggregator method checkForCompletion.

/**
     * Checks whether the presented gelf message chunk completes the incoming raw message and returns it if it does.
     * If the message isn't complete, it adds the chunk to the internal buffer and waits for more incoming messages.
     * Outdated chunks are being purged regularly.
     *
     * @param gelfMessage the gelf message chunk
     * @return null or a {@link org.graylog2.plugin.journal.RawMessage raw message} object
     */
@Nullable
private ChannelBuffer checkForCompletion(GELFMessage gelfMessage) {
    if (!chunks.isEmpty() && log.isDebugEnabled()) {
        log.debug("Dumping GELF chunk map [chunks for {} messages]:\n{}", chunks.size(), humanReadableChunkMap());
    }
    // TODO second parameter
    final GELFMessageChunk chunk = new GELFMessageChunk(gelfMessage, null);
    final int sequenceCount = chunk.getSequenceCount();
    final String messageId = chunk.getId();
    ChunkEntry entry = new ChunkEntry(sequenceCount, chunk.getArrival(), messageId);
    final ChunkEntry existing = chunks.putIfAbsent(messageId, entry);
    if (existing == null) {
        // add this chunk entry to the eviction set
        waitingMessages.inc();
        sortedEvictionSet.add(entry);
    } else {
        // the entry is already in the eviction set and chunk map
        entry = existing;
    }
    final int sequenceNumber = chunk.getSequenceNumber();
    if (!entry.payloadArray.compareAndSet(sequenceNumber, null, chunk)) {
        log.error("Received duplicate chunk {} for message {} from {}", sequenceNumber, messageId, gelfMessage.getSourceAddress());
        duplicateChunks.inc();
        return null;
    }
    final int chunkWatermark = entry.chunkSlotsWritten.incrementAndGet();
    if (chunkWatermark > MAX_CHUNKS) {
        getAndCleanupEntry(messageId);
        throw new IllegalStateException("Maximum number of chunks reached, discarding message");
    }
    if (chunkWatermark == sequenceCount) {
        // message is complete by chunk count, assemble and return it.
        // it might still be corrupt etc, but we've seen enough chunks
        // remove before operating on it, to avoid racing too much with the clean up job, some race is inevitable, though.
        entry = getAndCleanupEntry(messageId);
        final byte[][] allChunks = new byte[sequenceCount][];
        for (int i = 0; i < entry.payloadArray.length(); i++) {
            final GELFMessageChunk messageChunk = entry.payloadArray.get(i);
            if (messageChunk == null) {
                log.debug("Couldn't read chunk {} of message {}, skipping this chunk.", i, messageId);
            } else {
                allChunks[i] = messageChunk.getData();
            }
        }
        completeMessages.inc();
        return ChannelBuffers.wrappedBuffer(allChunks);
    }
    // message isn't complete yet, check if we should remove the other parts as well
    if (isOutdated(entry)) {
        // chunks are outdated, the oldest came in over 5 seconds ago, clean them all up
        log.debug("Not all chunks of <{}> arrived within {}ms. Dropping chunks.", messageId, VALIDITY_PERIOD);
        expireEntry(messageId);
    }
    return null;
}
Also used : GELFMessageChunk(org.graylog2.inputs.codecs.gelf.GELFMessageChunk) Nullable(javax.annotation.Nullable)

Example 2 with GelfMessage

use of org.graylog2.gelfclient.GelfMessage in project graylog2-server by Graylog2.

the class GelfOutput method toGELFMessage.

protected GelfMessage toGELFMessage(final Message message) {
    final DateTime timestamp;
    final Object fieldTimeStamp = message.getField(Message.FIELD_TIMESTAMP);
    if (fieldTimeStamp instanceof DateTime) {
        timestamp = (DateTime) fieldTimeStamp;
    } else {
        timestamp = Tools.nowUTC();
    }
    final GelfMessageLevel messageLevel = extractLevel(message.getField(Message.FIELD_LEVEL));
    final String fullMessage = (String) message.getField(Message.FIELD_FULL_MESSAGE);
    final String facility = (String) message.getField("facility");
    final String forwarder = GelfOutput.class.getCanonicalName();
    final GelfMessageBuilder builder = new GelfMessageBuilder(message.getMessage(), message.getSource()).timestamp(timestamp.getMillis() / 1000.0d).additionalField("_forwarder", forwarder).additionalFields(message.getFields());
    if (messageLevel != null) {
        builder.level(messageLevel);
    }
    if (fullMessage != null) {
        builder.fullMessage(fullMessage);
    }
    if (facility != null) {
        builder.additionalField("_facility", facility);
    }
    return builder.build();
}
Also used : GelfMessageLevel(org.graylog2.gelfclient.GelfMessageLevel) GelfMessageBuilder(org.graylog2.gelfclient.GelfMessageBuilder) DateTime(org.joda.time.DateTime)

Example 3 with GelfMessage

use of org.graylog2.gelfclient.GelfMessage in project graylog2-server by Graylog2.

the class GelfOutputTest method testToGELFMessageWithInvalidNumericStringLevel.

@Test
public void testToGELFMessageWithInvalidNumericStringLevel() throws Exception {
    final GelfTransport transport = mock(GelfTransport.class);
    final GelfOutput gelfOutput = new GelfOutput(transport);
    final DateTime now = DateTime.now(DateTimeZone.UTC);
    final Message message = new Message("Test", "Source", now);
    message.addField("level", "-1");
    final GelfMessage gelfMessage = gelfOutput.toGELFMessage(message);
    assertEquals(GelfMessageLevel.ALERT, gelfMessage.getLevel());
}
Also used : GelfTransport(org.graylog2.gelfclient.transport.GelfTransport) GelfMessage(org.graylog2.gelfclient.GelfMessage) Message(org.graylog2.plugin.Message) DateTime(org.joda.time.DateTime) GelfMessage(org.graylog2.gelfclient.GelfMessage) Test(org.junit.Test)

Example 4 with GelfMessage

use of org.graylog2.gelfclient.GelfMessage in project graylog2-server by Graylog2.

the class GelfOutputTest method testToGELFMessageWithInvalidTypeLevel.

@Test
public void testToGELFMessageWithInvalidTypeLevel() throws Exception {
    final GelfTransport transport = mock(GelfTransport.class);
    final GelfOutput gelfOutput = new GelfOutput(transport);
    final DateTime now = DateTime.now(DateTimeZone.UTC);
    final Message message = new Message("Test", "Source", now);
    message.addField("level", new Object());
    final GelfMessage gelfMessage = gelfOutput.toGELFMessage(message);
    assertEquals(GelfMessageLevel.ALERT, gelfMessage.getLevel());
}
Also used : GelfTransport(org.graylog2.gelfclient.transport.GelfTransport) GelfMessage(org.graylog2.gelfclient.GelfMessage) Message(org.graylog2.plugin.Message) DateTime(org.joda.time.DateTime) GelfMessage(org.graylog2.gelfclient.GelfMessage) Test(org.junit.Test)

Example 5 with GelfMessage

use of org.graylog2.gelfclient.GelfMessage in project graylog2-server by Graylog2.

the class GelfOutputTest method testToGELFMessageWithValidNumericLevel.

@Test
public void testToGELFMessageWithValidNumericLevel() throws Exception {
    final GelfTransport transport = mock(GelfTransport.class);
    final GelfOutput gelfOutput = new GelfOutput(transport);
    final DateTime now = DateTime.now(DateTimeZone.UTC);
    final Message message = new Message("Test", "Source", now);
    message.addField("level", 6);
    final GelfMessage gelfMessage = gelfOutput.toGELFMessage(message);
    assertEquals(GelfMessageLevel.INFO, gelfMessage.getLevel());
}
Also used : GelfTransport(org.graylog2.gelfclient.transport.GelfTransport) GelfMessage(org.graylog2.gelfclient.GelfMessage) Message(org.graylog2.plugin.Message) DateTime(org.joda.time.DateTime) GelfMessage(org.graylog2.gelfclient.GelfMessage) Test(org.junit.Test)

Aggregations

Message (org.graylog2.plugin.Message)11 DateTime (org.joda.time.DateTime)11 GelfMessage (org.graylog2.gelfclient.GelfMessage)10 GelfTransport (org.graylog2.gelfclient.transport.GelfTransport)10 Test (org.junit.Test)10 Nullable (javax.annotation.Nullable)2 GELFMessage (org.graylog2.inputs.codecs.gelf.GELFMessage)2 JsonNode (com.fasterxml.jackson.databind.JsonNode)1 Map (java.util.Map)1 Nonnull (javax.annotation.Nonnull)1 GelfMessageBuilder (org.graylog2.gelfclient.GelfMessageBuilder)1 GelfMessageLevel (org.graylog2.gelfclient.GelfMessageLevel)1 GELFMessageChunk (org.graylog2.inputs.codecs.gelf.GELFMessageChunk)1 RawMessage (org.graylog2.plugin.journal.RawMessage)1 ChannelBuffer (org.jboss.netty.buffer.ChannelBuffer)1