Search in sources :

Example 1 with ResolvableInetSocketAddress

use of org.graylog2.plugin.ResolvableInetSocketAddress in project graylog2-server by Graylog2.

the class SyslogCodec method decode.

@Nullable
@Override
public Message decode(@Nonnull RawMessage rawMessage) {
    final String msg = new String(rawMessage.getPayload(), StandardCharsets.UTF_8);
    try (Timer.Context ignored = this.decodeTime.time()) {
        final ResolvableInetSocketAddress address = rawMessage.getRemoteAddress();
        final InetSocketAddress remoteAddress;
        if (address == null) {
            remoteAddress = null;
        } else {
            remoteAddress = address.getInetSocketAddress();
        }
        return parse(msg, remoteAddress == null ? null : remoteAddress.getAddress(), rawMessage.getTimestamp());
    }
}
Also used : ResolvableInetSocketAddress(org.graylog2.plugin.ResolvableInetSocketAddress) Timer(com.codahale.metrics.Timer) ResolvableInetSocketAddress(org.graylog2.plugin.ResolvableInetSocketAddress) InetSocketAddress(java.net.InetSocketAddress) Nullable(javax.annotation.Nullable)

Example 2 with ResolvableInetSocketAddress

use of org.graylog2.plugin.ResolvableInetSocketAddress in project graylog2-server by Graylog2.

the class RawMessage method getRemoteAddress.

@Nullable
public ResolvableInetSocketAddress getRemoteAddress() {
    if (msgBuilder.hasRemote()) {
        final JournalMessages.RemoteAddress address = msgBuilder.getRemote();
        final InetAddress inetAddr;
        try {
            inetAddr = InetAddress.getByAddress(address.getResolved(), address.getAddress().toByteArray());
        } catch (UnknownHostException e) {
            log.warn("Malformed InetAddress for message {}, expected 4 or 16 bytes, but got {} bytes", id, address.getAddress().toByteArray());
            return null;
        }
        final int port = address.hasPort() ? address.getPort() : 0;
        // TODO PERFORMANCE object creation
        return ResolvableInetSocketAddress.wrap(new InetSocketAddress(inetAddr, port));
    }
    return null;
}
Also used : UnknownHostException(java.net.UnknownHostException) ResolvableInetSocketAddress(org.graylog2.plugin.ResolvableInetSocketAddress) InetSocketAddress(java.net.InetSocketAddress) InetAddress(java.net.InetAddress) Nullable(javax.annotation.Nullable)

Example 3 with ResolvableInetSocketAddress

use of org.graylog2.plugin.ResolvableInetSocketAddress in project graylog2-server by Graylog2.

the class ResolvableInetSocketAddressTest method testReverseLookup.

@Test
@SuppressForbidden("Intentional invocation of InetSocketAddress#getHostName()")
public void testReverseLookup() throws Exception {
    final InetSocketAddress inetSocketAddress = new InetSocketAddress(Inet4Address.getLoopbackAddress(), 12345);
    final ResolvableInetSocketAddress address = new ResolvableInetSocketAddress(inetSocketAddress);
    assertThat(address.isReverseLookedUp()).isFalse();
    assertThat(address.reverseLookup()).isEqualTo(inetSocketAddress.getHostName());
    assertThat(address.isReverseLookedUp()).isTrue();
}
Also used : InetSocketAddress(java.net.InetSocketAddress) Test(org.junit.Test) SuppressForbidden(org.graylog2.shared.SuppressForbidden)

Example 4 with ResolvableInetSocketAddress

use of org.graylog2.plugin.ResolvableInetSocketAddress in project graylog2-server by Graylog2.

the class ResolvableInetSocketAddressTest method testGetHostName.

@Test
@SuppressForbidden("Intentional invocation of InetSocketAddress#getHostName()")
public void testGetHostName() throws Exception {
    final InetSocketAddress inetSocketAddress = new InetSocketAddress(Inet4Address.getLoopbackAddress(), 12345);
    final ResolvableInetSocketAddress address = new ResolvableInetSocketAddress(inetSocketAddress);
    assertThat(address.getHostName()).isNull();
    address.reverseLookup();
    assertThat(address.getHostName()).isEqualTo(inetSocketAddress.getHostName());
}
Also used : InetSocketAddress(java.net.InetSocketAddress) Test(org.junit.Test) SuppressForbidden(org.graylog2.shared.SuppressForbidden)

Example 5 with ResolvableInetSocketAddress

use of org.graylog2.plugin.ResolvableInetSocketAddress in project graylog2-server by Graylog2.

the class DecodingProcessor method postProcessMessage.

@Nullable
private Message postProcessMessage(RawMessage raw, Codec codec, String inputIdOnCurrentNode, String baseMetricName, Message message, long decodeTime) {
    if (message == null) {
        metricRegistry.meter(name(baseMetricName, "failures")).mark();
        return null;
    }
    if (!message.isComplete()) {
        metricRegistry.meter(name(baseMetricName, "incomplete")).mark();
        if (LOG.isDebugEnabled()) {
            LOG.debug("Dropping incomplete message {} on input <{}>. Parsed fields: [{}]", raw, inputIdOnCurrentNode, message.getFields());
        }
        return null;
    }
    message.setJournalOffset(raw.getJournalOffset());
    message.recordTiming(serverStatus, "parse", decodeTime);
    metricRegistry.timer(name(baseMetricName, "parseTime")).update(decodeTime, TimeUnit.NANOSECONDS);
    for (final RawMessage.SourceNode node : raw.getSourceNodes()) {
        switch(node.type) {
            case SERVER:
                // Always use the last source node.
                if (message.getField("gl2_source_input") != null) {
                    LOG.debug("Multiple server nodes ({} {}) set for message id {}", message.getField("gl2_source_input"), node.nodeId, message.getId());
                }
                message.addField("gl2_source_input", node.inputId);
                message.addField("gl2_source_node", node.nodeId);
                break;
            // TODO Due to be removed in Graylog 3.x
            case RADIO:
                // Always use the last source node.
                if (message.getField("gl2_source_radio_input") != null) {
                    LOG.debug("Multiple radio nodes ({} {}) set for message id {}", message.getField("gl2_source_radio_input"), node.nodeId, message.getId());
                }
                message.addField("gl2_source_radio_input", node.inputId);
                message.addField("gl2_source_radio", node.nodeId);
                break;
        }
    }
    if (inputIdOnCurrentNode != null) {
        try {
            message.setSourceInputId(inputIdOnCurrentNode);
        } catch (RuntimeException e) {
            LOG.warn("Unable to find input with id " + inputIdOnCurrentNode + ", not setting input id in this message.", e);
        }
    }
    final ResolvableInetSocketAddress remoteAddress = raw.getRemoteAddress();
    if (remoteAddress != null) {
        final String addrString = InetAddresses.toAddrString(remoteAddress.getAddress());
        message.addField("gl2_remote_ip", addrString);
        if (remoteAddress.getPort() > 0) {
            message.addField("gl2_remote_port", remoteAddress.getPort());
        }
        if (remoteAddress.isReverseLookedUp()) {
            // avoid reverse lookup if the hostname is available
            message.addField("gl2_remote_hostname", remoteAddress.getHostName());
        }
        if (Strings.isNullOrEmpty(message.getSource())) {
            message.setSource(addrString);
        }
    }
    if (codec.getConfiguration() != null && codec.getConfiguration().stringIsSet(Codec.Config.CK_OVERRIDE_SOURCE)) {
        message.setSource(codec.getConfiguration().getString(Codec.Config.CK_OVERRIDE_SOURCE));
    }
    // Make sure that there is a value for the source field.
    if (Strings.isNullOrEmpty(message.getSource())) {
        message.setSource("unknown");
    }
    metricRegistry.meter(name(baseMetricName, "processedMessages")).mark();
    return message;
}
Also used : ResolvableInetSocketAddress(org.graylog2.plugin.ResolvableInetSocketAddress) RawMessage(org.graylog2.plugin.journal.RawMessage) Nullable(javax.annotation.Nullable)

Aggregations

InetSocketAddress (java.net.InetSocketAddress)5 ResolvableInetSocketAddress (org.graylog2.plugin.ResolvableInetSocketAddress)5 RawMessage (org.graylog2.plugin.journal.RawMessage)4 Nullable (javax.annotation.Nullable)3 Message (org.graylog2.plugin.Message)3 NotFoundException (javax.ws.rs.NotFoundException)2 IndexNotFoundException (org.elasticsearch.index.IndexNotFoundException)2 DocumentNotFoundException (org.graylog2.indexer.messages.DocumentNotFoundException)2 ResultMessage (org.graylog2.indexer.results.ResultMessage)2 Configuration (org.graylog2.plugin.configuration.Configuration)2 Codec (org.graylog2.plugin.inputs.codecs.Codec)2 SuppressForbidden (org.graylog2.shared.SuppressForbidden)2 Test (org.junit.Test)2 Timer (com.codahale.metrics.Timer)1 Timed (com.codahale.metrics.annotation.Timed)1 UUID (com.eaio.uuid.UUID)1 TypeLiteral (com.google.inject.TypeLiteral)1 ApiOperation (io.swagger.annotations.ApiOperation)1 ApiResponses (io.swagger.annotations.ApiResponses)1 InetAddress (java.net.InetAddress)1