Search in sources :

Example 6 with MessageInput

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

the class NettyTransport method launch.

@Override
public void launch(final MessageInput input) throws MisfireException {
    final LinkedHashMap<String, Callable<? extends ChannelHandler>> handlerList = getBaseChannelHandlers(input);
    final LinkedHashMap<String, Callable<? extends ChannelHandler>> finalHandlers = getFinalChannelHandlers(input);
    handlerList.putAll(finalHandlers);
    try {
        bootstrap = getBootstrap();
        bootstrap.setPipelineFactory(getPipelineFactory(handlerList));
        // sigh, bindable bootstraps do not share a common interface
        int receiveBufferSize;
        if (bootstrap instanceof ConnectionlessBootstrap) {
            acceptChannel = ((ConnectionlessBootstrap) bootstrap).bind(socketAddress);
            final DefaultDatagramChannelConfig channelConfig = (DefaultDatagramChannelConfig) acceptChannel.getConfig();
            receiveBufferSize = channelConfig.getReceiveBufferSize();
        } else if (bootstrap instanceof ServerBootstrap) {
            acceptChannel = ((ServerBootstrap) bootstrap).bind(socketAddress);
            final ServerSocketChannelConfig channelConfig = (ServerSocketChannelConfig) acceptChannel.getConfig();
            receiveBufferSize = channelConfig.getReceiveBufferSize();
        } else {
            log.error("Unknown Netty bootstrap class returned: {}. Cannot safely bind.", bootstrap);
            throw new IllegalStateException("Unknown netty bootstrap class returned: " + bootstrap + ". Cannot safely bind.");
        }
        if (receiveBufferSize != getRecvBufferSize()) {
            log.warn("receiveBufferSize (SO_RCVBUF) for input {} should be {} but is {}.", input, getRecvBufferSize(), receiveBufferSize);
        }
    } catch (Exception e) {
        throw new MisfireException(e);
    }
}
Also used : MisfireException(org.graylog2.plugin.inputs.MisfireException) SimpleChannelHandler(org.jboss.netty.channel.SimpleChannelHandler) ChannelHandler(org.jboss.netty.channel.ChannelHandler) DefaultDatagramChannelConfig(org.jboss.netty.channel.socket.DefaultDatagramChannelConfig) ServerSocketChannelConfig(org.jboss.netty.channel.socket.ServerSocketChannelConfig) Callable(java.util.concurrent.Callable) ServerBootstrap(org.jboss.netty.bootstrap.ServerBootstrap) MisfireException(org.graylog2.plugin.inputs.MisfireException) ConnectionlessBootstrap(org.jboss.netty.bootstrap.ConnectionlessBootstrap)

Example 7 with MessageInput

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

the class NettyTransport method getBaseChannelHandlers.

/**
     * Subclasses can override this to add additional ChannelHandlers to the pipeline to support additional features.
     * <p/>
     * Some common use cases are to add SSL/TLS, connection counters or throttling traffic shapers.
     *
     * @param input
     * @return the list of initial channelhandlers to add to the {@link org.jboss.netty.channel.ChannelPipelineFactory}
     */
protected LinkedHashMap<String, Callable<? extends ChannelHandler>> getBaseChannelHandlers(final MessageInput input) {
    LinkedHashMap<String, Callable<? extends ChannelHandler>> handlerList = Maps.newLinkedHashMap();
    handlerList.put("exception-logger", new Callable<ChannelHandler>() {

        @Override
        public ChannelHandler call() throws Exception {
            return new SimpleChannelUpstreamHandler() {

                @SuppressWarnings("ThrowableResultOfMethodCallIgnored")
                @Override
                public void exceptionCaught(ChannelHandlerContext ctx, ExceptionEvent e) throws Exception {
                    if ("Connection reset by peer".equals(e.getCause().getMessage())) {
                        log.trace("{} in Input [{}/{}] (channel {})", e.getCause().getMessage(), input.getName(), input.getId(), e.getChannel());
                    } else {
                        log.error("Error in Input [{}/{}] (channel {})", input.getName(), input.getId(), e.getChannel(), e.getCause());
                    }
                    super.exceptionCaught(ctx, e);
                }
            };
        }
    });
    handlerList.put("packet-meta-dumper", new Callable<ChannelHandler>() {

        @Override
        public ChannelHandler call() throws Exception {
            return new PacketInformationDumper(input);
        }
    });
    handlerList.put("traffic-counter", Callables.returning(throughputCounter));
    return handlerList;
}
Also used : ExceptionEvent(org.jboss.netty.channel.ExceptionEvent) ChannelHandlerContext(org.jboss.netty.channel.ChannelHandlerContext) SimpleChannelHandler(org.jboss.netty.channel.SimpleChannelHandler) ChannelHandler(org.jboss.netty.channel.ChannelHandler) SimpleChannelUpstreamHandler(org.jboss.netty.channel.SimpleChannelUpstreamHandler) PacketInformationDumper(org.graylog2.plugin.inputs.util.PacketInformationDumper) Callable(java.util.concurrent.Callable) MisfireException(org.graylog2.plugin.inputs.MisfireException)

Example 8 with MessageInput

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

the class AbstractTcpTransportTest method getBaseChannelHandlersFailsIfTempDirDoesNotExist.

@Test
public void getBaseChannelHandlersFailsIfTempDirDoesNotExist() throws IOException {
    final File tmpDir = temporaryFolder.newFolder();
    assumeTrue(tmpDir.delete());
    System.setProperty("java.io.tmpdir", tmpDir.getAbsolutePath());
    final Configuration configuration = new Configuration(ImmutableMap.of("bind_address", "localhost", "port", 12345, "tls_enable", true));
    final AbstractTcpTransport transport = new AbstractTcpTransport(configuration, throughputCounter, localRegistry, bossPool, workerPool, connectionCounter) {

        @Override
        protected Bootstrap getBootstrap() {
            return super.getBootstrap();
        }

        @Override
        protected LinkedHashMap<String, Callable<? extends ChannelHandler>> getBaseChannelHandlers(MessageInput input) {
            return super.getBaseChannelHandlers(input);
        }
    };
    expectedException.expect(IllegalStateException.class);
    expectedException.expectMessage("Couldn't write to temporary directory: " + tmpDir.getAbsolutePath());
    transport.getBaseChannelHandlers(input);
}
Also used : Configuration(org.graylog2.plugin.configuration.Configuration) MessageInput(org.graylog2.plugin.inputs.MessageInput) ChannelHandler(org.jboss.netty.channel.ChannelHandler) File(java.io.File) Callable(java.util.concurrent.Callable) Test(org.junit.Test)

Example 9 with MessageInput

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

the class AbstractTcpTransportTest method getBaseChannelHandlersFailsIfTempDirIsNotWritable.

@Test
public void getBaseChannelHandlersFailsIfTempDirIsNotWritable() throws IOException {
    final File tmpDir = temporaryFolder.newFolder();
    assumeTrue(tmpDir.setWritable(false));
    assumeFalse(tmpDir.canWrite());
    System.setProperty("java.io.tmpdir", tmpDir.getAbsolutePath());
    final Configuration configuration = new Configuration(ImmutableMap.of("bind_address", "localhost", "port", 12345, "tls_enable", true));
    final AbstractTcpTransport transport = new AbstractTcpTransport(configuration, throughputCounter, localRegistry, bossPool, workerPool, connectionCounter) {

        @Override
        protected Bootstrap getBootstrap() {
            return super.getBootstrap();
        }

        @Override
        protected LinkedHashMap<String, Callable<? extends ChannelHandler>> getBaseChannelHandlers(MessageInput input) {
            return super.getBaseChannelHandlers(input);
        }
    };
    expectedException.expect(IllegalStateException.class);
    expectedException.expectMessage("Couldn't write to temporary directory: " + tmpDir.getAbsolutePath());
    transport.getBaseChannelHandlers(input);
}
Also used : Configuration(org.graylog2.plugin.configuration.Configuration) MessageInput(org.graylog2.plugin.inputs.MessageInput) ChannelHandler(org.jboss.netty.channel.ChannelHandler) File(java.io.File) Callable(java.util.concurrent.Callable) Test(org.junit.Test)

Example 10 with MessageInput

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

the class AbstractTcpTransportTest method getBaseChannelHandlersGeneratesSelfSignedCertificates.

@Test
public void getBaseChannelHandlersGeneratesSelfSignedCertificates() {
    final Configuration configuration = new Configuration(ImmutableMap.of("bind_address", "localhost", "port", 12345, "tls_enable", true));
    final AbstractTcpTransport transport = new AbstractTcpTransport(configuration, throughputCounter, localRegistry, bossPool, workerPool, connectionCounter) {

        @Override
        protected Bootstrap getBootstrap() {
            return super.getBootstrap();
        }

        @Override
        protected LinkedHashMap<String, Callable<? extends ChannelHandler>> getBaseChannelHandlers(MessageInput input) {
            return super.getBaseChannelHandlers(input);
        }
    };
    final MessageInput input = mock(MessageInput.class);
    assertThat(transport.getBaseChannelHandlers(input)).containsKey("tls");
}
Also used : Configuration(org.graylog2.plugin.configuration.Configuration) MessageInput(org.graylog2.plugin.inputs.MessageInput) ChannelHandler(org.jboss.netty.channel.ChannelHandler) Callable(java.util.concurrent.Callable) Test(org.junit.Test)

Aggregations

MessageInput (org.graylog2.plugin.inputs.MessageInput)47 Test (org.junit.Test)18 Callable (java.util.concurrent.Callable)17 NotFoundException (org.graylog2.database.NotFoundException)10 Configuration (org.graylog2.plugin.configuration.Configuration)9 ChannelHandler (io.netty.channel.ChannelHandler)8 LinkedHashMap (java.util.LinkedHashMap)8 Input (org.graylog2.inputs.Input)8 MisfireException (org.graylog2.plugin.inputs.MisfireException)7 ChannelHandler (org.jboss.netty.channel.ChannelHandler)7 Timed (com.codahale.metrics.annotation.Timed)6 ApiOperation (io.swagger.annotations.ApiOperation)6 ApiResponses (io.swagger.annotations.ApiResponses)6 EventBus (com.google.common.eventbus.EventBus)5 AuditEvent (org.graylog2.audit.jersey.AuditEvent)5 Subscribe (com.google.common.eventbus.Subscribe)4 Produces (javax.ws.rs.Produces)4 IOState (org.graylog2.plugin.IOState)4 LocalMetricRegistry (org.graylog2.plugin.LocalMetricRegistry)4 Extractor (org.graylog2.plugin.inputs.Extractor)4