Search in sources :

Example 11 with GelfMessage

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

the class GelfCodec method decode.

@Nullable
@Override
public Message decode(@Nonnull final RawMessage rawMessage) {
    final GELFMessage gelfMessage = new GELFMessage(rawMessage.getPayload(), rawMessage.getRemoteAddress());
    final String json = gelfMessage.getJSON(decompressSizeLimit);
    final JsonNode node;
    try {
        node = objectMapper.readTree(json);
    } catch (final Exception e) {
        log.error("Could not parse JSON, first 400 characters: " + StringUtils.abbreviate(json, 403), e);
        throw new IllegalStateException("JSON is null/could not be parsed (invalid JSON)", e);
    }
    // Timestamp.
    final double messageTimestamp = doubleValue(node, "timestamp");
    final DateTime timestamp;
    if (messageTimestamp <= 0) {
        timestamp = rawMessage.getTimestamp();
    } else {
        // we treat this as a unix timestamp
        timestamp = Tools.dateTimeFromDouble(messageTimestamp);
    }
    final Message message = new Message(stringValue(node, "short_message"), stringValue(node, "host"), timestamp);
    message.addField("full_message", stringValue(node, "full_message"));
    final String file = stringValue(node, "file");
    if (file != null && !file.isEmpty()) {
        message.addField("file", file);
    }
    final long line = longValue(node, "line");
    if (line > -1) {
        message.addField("line", line);
    }
    // Level is set by server if not specified by client.
    final int level = intValue(node, "level");
    if (level > -1) {
        message.addField("level", level);
    }
    // Facility is set by server if not specified by client.
    final String facility = stringValue(node, "facility");
    if (facility != null && !facility.isEmpty()) {
        message.addField("facility", facility);
    }
    // Add additional data if there is some.
    final Iterator<Map.Entry<String, JsonNode>> fields = node.fields();
    while (fields.hasNext()) {
        final Map.Entry<String, JsonNode> entry = fields.next();
        String key = entry.getKey();
        // Do not index useless GELF "version" field.
        if ("version".equals(key)) {
            continue;
        }
        // Don't include GELF syntax underscore in message field key.
        if (key.startsWith("_") && key.length() > 1) {
            key = key.substring(1);
        }
        // We already set short_message and host as message and source. Do not add as fields again.
        if ("short_message".equals(key) || "host".equals(key)) {
            continue;
        }
        // Skip standard or already set fields.
        if (message.getField(key) != null || Message.RESERVED_FIELDS.contains(key) && !Message.RESERVED_SETTABLE_FIELDS.contains(key)) {
            continue;
        }
        // Convert JSON containers to Strings, and pick a suitable number representation.
        final JsonNode value = entry.getValue();
        final Object fieldValue;
        if (value.isContainerNode()) {
            fieldValue = value.toString();
        } else if (value.isFloatingPointNumber()) {
            fieldValue = value.asDouble();
        } else if (value.isIntegralNumber()) {
            fieldValue = value.asLong();
        } else if (value.isNull()) {
            log.debug("Field [{}] is NULL. Skipping.", key);
            continue;
        } else if (value.isTextual()) {
            fieldValue = value.asText();
        } else {
            log.debug("Field [{}] has unknown value type. Skipping.", key);
            continue;
        }
        message.addField(key, fieldValue);
    }
    return message;
}
Also used : RawMessage(org.graylog2.plugin.journal.RawMessage) GELFMessage(org.graylog2.inputs.codecs.gelf.GELFMessage) Message(org.graylog2.plugin.Message) JsonNode(com.fasterxml.jackson.databind.JsonNode) DateTime(org.joda.time.DateTime) GELFMessage(org.graylog2.inputs.codecs.gelf.GELFMessage) Map(java.util.Map) Nullable(javax.annotation.Nullable)

Example 12 with GelfMessage

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

the class GelfOutputTest method testToGELFMessageFullMessage.

@Test
public void testToGELFMessageFullMessage() 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(Message.FIELD_FULL_MESSAGE, "Full Message");
    final GelfMessage gelfMessage = gelfOutput.toGELFMessage(message);
    assertEquals("Full Message", gelfMessage.getFullMessage());
}
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 13 with GelfMessage

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

the class GelfOutputTest method testToGELFMessageWithInvalidStringLevel.

@Test
public void testToGELFMessageWithInvalidStringLevel() 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", "BOOM");
    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 14 with GelfMessage

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

the class GelfOutputTest method testToGELFMessageTimestamp.

@Test
public void testToGELFMessageTimestamp() 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);
    final GelfMessage gelfMessage = gelfOutput.toGELFMessage(message);
    assertEquals(gelfMessage.getTimestamp(), now.getMillis() / 1000.0d, 0.0d);
}
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