1
 final Cli<CliCommand> cli = builder.build();
    final Runnable command = cli.parse("server", "-f", graylogConfigFile);
    final Thread graylogThread = new Thread(command);

    /*
    This is a nasty workaround to get Graylog's Swagger instance working when embedded within SpringBoot.
    DocumentationBrowserResource uses the SystemClassLoader (which it 100% should not be doing) which means
    it fails to load resources when run in an environment like SpringBoot
     */
    try {
        Field sclField = ReflectionUtils.findField(ClassLoader.class, "scl");
        ReflectionUtils.makeAccessible(sclField);
        ReflectionUtils.setField(sclField, null, new DecoratingClassLoader() {
            @Override
            public URL getResource(String name) {
                if (name.startsWith("swagger/")) {
                    return Thread.currentThread().getContextClassLoader().getResource(name);
                }
                return super.getResource(name);
            }
        });
    } catch(Exception e) {
        LOGGER.error("Failed to replace SystemClassLoader, this means Graylog's Swagger won't work.", e);
    }

    graylogThread.start();

    LOGGER.info("Graylog started");

    configureGraylog();
}

private void configureGraylog() {
    try {
        graylogRestInterface.inputExists("test");
        LOGGER.info("Graylog rest interface ready to go!");

        setupUdpInput();
        setupHttpInput();
        setupDashboard();
    } catch (Exception e) {
        LOGGER.warn("Graylog rest interface not ready yet, rescheduling...");
        delay(this::configureGraylog, 1);
    }
}

private void delay(Runnable runnable, int delaySeconds) {
    Calendar cal = Calendar.getInstance();
    cal.add(Calendar.SECOND, delaySeconds);
    scheduler.schedule(runnable, cal.getTime());
}

private void setupUdpInput() {
    String inputName = "SpringBoot GELF UDP";
    if (graylogRestInterface.inputExists(inputName)) {
        LOGGER.info("Graylog UDP input already exists, skipping...");
        return;
    }
    setupInput(inputName, GELFUDPInput.class, 12201);
}

private void setupHttpInput() {
    String inputName = "SpringBoot GELF HTTP";
    if (graylogRestInterface.inputExists(inputName)) {
        LOGGER.info("Graylog HTTP input already exists, skipping...");
        return;
    }
    String inputId = setupInput(inputName, GELFHttpInput.class, 12202);

    Map<String, Object> extractorConfig = new HashMap<>();
    extractorConfig.put("key_separator", "_");
    extractorConfig.put("list_separator", ", ");
    extractorConfig.put("kv_separator", "=");
    CreateExtractorRequest extractorRequest = CreateExtractorRequest.create(
            "Quote JSON",
            "copy",
            "full_message",
            "",
            "json",
            extractorConfig,
            Collections.emptyMap(),
            "none",
            "",
            0
    );
    graylogRestInterface.createExtractor(inputId, extractorRequest);
    LOGGER.info("Graylog JSON extractor created.");
}

private String setupInput(String name, Class<? extends MessageInput> inputType, int port) {
    Map<String, Object> properties = new HashMap<>();
    properties.put("use_null_delimiter", true);
    properties.put("bind_address", "0.0.0.0");
    properties.put("port", port);

    InputCreateRequest request = InputCreateRequest.create(
            name,
            inputType.getName(),
            true,
            properties,
            null);

    String inputId = graylogRestInterface.createInput(request);
    LOGGER.info(String.format("%s input created.", name));
    return inputId;
}

private void setupDashboard() {
    String dashboardName = "Stock Market";
    if (graylogRestInterface.dashboardExists(dashboardName)) {
        LOGGER.info("Graylog dashboard already exists, skipping...");
        return;
    }
    CreateDashboardRequest dashboardRequest = CreateDashboardRequest.create(
            dashboardName,
            "Measurements from Stock Market example application"
    );

    String dashboardId = graylogRestInterface.createDashboard(dashboardRequest);
    LOGGER.info("Graylog dashboard created.");

    Map<String, Object> timerangeMap = new HashMap<String, Object>() {{
        put("type", "relative");
        put("range", 300);
    }};

    AddWidgetRequest widgetRequest = AddWidgetRequest.create(
            "Stock Quotes - Symbols",
            "QUICKVALUES",
            10,
            new HashMap<String, Object>(){{
                    put("timerange", timerangeMap);
                    put("field", "symbol");
                    put("show_pie_chart", true);
                    put("query", "");
                    put("show_data_table", true);
            }}
    );
    graylogRestInterface.createWidget(dashboardId, widgetRequest);
    LOGGER.info("Symbol widget created.");

    widgetRequest = AddWidgetRequest.create(
            "Generated Quote Count",
            "SEARCH_RESULT_COUNT",
            10,
            new HashMap<String, Object>(){{
                put("timerange", timerangeMap);
                put("lower_is_better", false);
                put("trend", false);
                put("query", "_exists_:symbol");
            }}
    );
    graylogRestInterface.createWidget(dashboardId, widgetRequest);
    LOGGER.info("Quote count widget created.");

    widgetRequest = AddWidgetRequest.create(
            "Quote Generation Time (us)",
            "FIELD_CHART",
            10,
            new HashMap<String, Object>(){{
                put("timerange", new HashMap<String, Object>() {{
                    put("type", "relative");
                    put("range", 300);
                }});
                put("valuetype", "mean");
                put("renderer", "line");
                put("interpolation", "linear");
                put("field", "elapsed_time");
                put("interval", "minute");
                put("rangeType", "relative");
                put("relative", 300);
                put("query", "_exists_:elapsed_time");
            }}
    );
    graylogRestInterface.createWidget(dashboardId, widgetRequest);
    LOGGER.info("Quote generation time widget created.");

    widgetRequest = AddWidgetRequest.create(
            "Error Count",
            "SEARCH_RESULT_COUNT",
            10,
            new HashMap<String, Object>(){{
                put("timerange", new HashMap<String, Object>() {{
                    put("type", "relative");
                    put("range", 300);
                }});
                put("lower_is_better", false);
                put("trend", false);
                put("query", "_exists_:stacktrace");
            }}
    );
    graylogRestInterface.createWidget(dashboardId, widgetRequest);
    LOGGER.info("Error count widget created.");
}

2016-08-17 10:53:29,439 INFO GraylogInstance:101 : Graylog started 2016-08-17 10:53:29,763 WARN GraylogInstance:115 : Graylog rest interface not ready yet, rescheduling... 2016-08-17 10:53:30.583 WARN 4037 --- [ Thread-1] o.graylog2.shared.plugins.PluginLoader : Plugin directory /home/govindj/projectworkspace/graylog-springboot-master/data/plugin does not exist, not loading plugins. 2016-08-17 10:53:30.592 INFO 4037 --- [ Thread-1] org.graylog2.bootstrap.CmdLineTool : Loaded plugins: [] 2016-08-17 10:53:30.593 INFO 4037 --- [ Thread-1] com.github.joschi.jadconfig.JadConfig : Added configuration bean org.graylog2.Configuration@8811ccc 2016-08-17 10:53:30.594 INFO 4037 --- [ Thread-1] com.github.joschi.jadconfig.JadConfig : Added configuration bean org.graylog2.configuration.ElasticsearchConfiguration@adafa1b 2016-08-17 10:53:30.594 INFO 4037 --- [ Thread-1] com.github.joschi.jadconfig.JadConfig : Added configuration bean org.graylog2.configuration.EmailConfiguration@61bd14b1 2016-08-17 10:53:30.594 INFO 4037 --- [ Thread-1] com.github.joschi.jadconfig.JadConfig : Added configuration bean org.graylog2.configuration.MongoDbConfiguration@6019bdc6 2016-08-17 10:53:30.594 INFO 4037 --- [ Thread-1] com.github.joschi.jadconfig.JadConfig : Added configuration bean org.graylog2.configuration.VersionCheckConfiguration@60feba75 2016-08-17 10:53:30.594 INFO 4037 --- [ Thread-1] com.github.joschi.jadconfig.JadConfig : Added configuration bean org.graylog2.plugin.KafkaJournalConfiguration@3d299e53 2016-08-17 10:53:30.705 INFO 4037 --- [ Thread-1] org.graylog2.bootstrap.CmdLineTool : Running with JVM arguments: -Dfile.encoding=UTF-8 2016-08-17 10:53:30,775 WARN GraylogInstance:115 : Graylog rest interface not ready yet, rescheduling... 2016-08-17 10:53:31.163 INFO 4037 --- [ main] s.w.s.m.m.a.RequestMappingHandlerMapping : Mapped "{[/quotes/{symbol}],methods=[GET]}" onto public void com.objectpartners.plummer.graylog.controller.QuotesController.getQuote(java.lang.String) 2016-08-17 10:53:31.172 INFO 4037 --- [ main] s.w.s.m.m.a.RequestMappingHandlerMapping : Mapped "{[/v2/api-docs],methods=[GET],produces=[application/json || application/hal+json]}" onto public org.springframework.http.ResponseEntity springfox.documentation.swagger2.web.Swagger2Controller.getDocumentation(java.lang.String,javax.servlet.http.HttpServletRequest) 2016-08-17 10:53:31.178 INFO 4037 --- [ main] s.w.s.m.m.a.RequestMappingHandlerMapping : Mapped "{[/configuration/security]}" onto org.springframework.http.ResponseEntity springfox.documentation.swagger.web.ApiResourceController.securityConfiguration() 2016-08-17 10:53:31.185 INFO 4037 --- [ main] s.w.s.m.m.a.RequestMappingHandlerMapping : Mapped "{[/swagger-resources]}" onto org.springframework.http.ResponseEntity> springfox.documentation.swagger.web.ApiResourceController.swaggerResources() 2016-08-17 10:53:31.188 INFO 4037 --- [ main] s.w.s.m.m.a.RequestMappingHandlerMapping : Mapped "{[/configuration/ui]}" onto org.springframework.http.ResponseEntity springfox.documentation.swagger.web.ApiResourceController.uiConfiguration() 2016-08-17 10:53:31.199 INFO 4037 --- [ main] s.w.s.m.m.a.RequestMappingHandlerMapping : Mapped "{[/error],produces=[text/html]}" onto public org.springframework.web.servlet.ModelAndView org.springframework.boot.autoconfigure.web.BasicErrorController.errorHtml(javax.servlet.http.HttpServletRequest,javax.servlet.http.HttpServletResponse) 2016-08-17 10:53:31.204 INFO 4037 --- [ main] s.w.s.m.m.a.RequestMappingHandlerMapping : Mapped "{[/error]}" onto public org.springframework.http.ResponseEntity> org.springframework.boot.autoconfigure.web.BasicErrorController.error(javax.servlet.http.HttpServletRequest) Exception in thread "Thread-1" java.lang.NoClassDefFoundError: kafka/consumer/TopicFilter at java.lang.Class.getDeclaredConstructors0(Native Method) at java.lang.Class.privateGetDeclaredConstructors(Class.java:2671) at java.lang.Class.getDeclaredConstructors(Class.java:2020) at com.google.inject.assistedinject.FactoryProvider2.findMatchingConstructorInjectionPoint(FactoryProvider2.java:487) at com.google.inject.assistedinject.FactoryProvider2.(FactoryProvider2.java:286) at com.google.inject.assistedinject.FactoryModuleBuilder$1.configure(FactoryModuleBuilder.java:334) at com.google.inject.AbstractModule.configure(AbstractModule.java:62) at com.google.inject.spi.Elements$RecordingBinder.install(Elements.java:340) at com.google.inject.AbstractModule.install(AbstractModule.java:122) at org.graylog2.plugin.inject.Graylog2Module.installTransport(Graylog2Module.java:84) at org.graylog2.plugin.inject.Graylog2Module.installTransport(Graylog2Module.java:74) at org.graylog2.inputs.transports.TransportsModule.configure(TransportsModule.java:36) at com.google.inject.AbstractModule.configure(AbstractModule.java:62) at com.google.inject.spi.Elements$RecordingBinder.install(Elements.java:340) at com.google.inject.AbstractModule.install(AbstractModule.java:122) at org.graylog2.shared.bindings.MessageInputBindings.configure(MessageInputBindings.java:43) at com.google.inject.AbstractModule.configure(AbstractModule.java:62) at com.google.inject.spi.Elements$RecordingBinder.install(Elements.java:340) at com.google.inject.spi.Elements.getElements(Elements.java:110) at com.google.inject.internal.InjectorShell$Builder.build(InjectorShell.java:138) at com.google.inject.internal.InternalInjectorCreator.build(InternalInjectorCreator.java:104) at com.google.inject.Guice.createInjector(Guice.java:96) at org.graylog2.shared.bindings.Hk2GuiceBridgeJitInjector.create(Hk2GuiceBridgeJitInjector.java:60) at org.graylog2.shared.bindings.GuiceInjectorHolder.createInjector(GuiceInjectorHolder.java:32) at org.graylog2.bootstrap.CmdLineTool.setupInjector(CmdLineTool.java:379) at org.graylog2.bootstrap.CmdLineTool.run(CmdLineTool.java:193) at java.lang.Thread.run(Thread.java:745) Caused by: java.lang.ClassNotFoundException: kafka.consumer.TopicFilter at java.net.URLClassLoader.findClass(URLClassLoader.java:381) at java.lang.ClassLoader.loadClass(ClassLoader.java:424) at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:331) at java.lang.ClassLoader.loadClass(ClassLoader.java:357) ... 27 more 2016-08-17 10:53:31.643 INFO 4037 --- [ main] o.s.w.s.handler.SimpleUrlHandlerMapping : Mapped URL path [/swagger-ui.html] onto handler of type [class org.springframework.web.servlet.resource.ResourceHttpRequestHandler] 2016-08-17 10:53:31.644 INFO 4037 --- [ main] o.s.w.s.handler.SimpleUrlHandlerMapping : Mapped URL path [/webjars/**] onto handler of type [class org.springframework.web.servlet.resource.ResourceHttpRequestHandler] 2016-08-17 10:53:31.762 INFO 4037 --- [ main] s.w.s.m.m.a.RequestMappingHandlerAdapter : Looking for @ControllerAdvice: org.springframework.boot.context.embedded.AnnotationConfigEmbeddedWebApplicationContext@7eac9008: startup date [Wed Aug 17 10:53:26 IST 2016]; root of context hierarchy 2016-08-17 10:53:31,777 WARN GraylogInstance:115 : Graylog rest interface not ready yet, rescheduling... 2016-08-17 10:53:32.330 INFO 4037 --- [ main] org.elasticsearch.node : [Monet St. Croix] version[2.2.2], pid[4037], build[fcc01dd/2016-03-29T08:49:35Z] 2016-08-17 10:53:32.339 INFO 4037 --- [ main] org.elasticsearch.node : [Monet St. Croix] initializing ... 2016-08-17 10:53:32.361 INFO 4037 --- [ main] org.elasticsearch.plugins : [Monet St. Croix] modules [], plugins [], sites [] 2016-08-17 10:53:32.423 INFO 4037 --- [ main] org.elasticsearch.env : [Monet St. Croix] using [1] data paths, mounts [[/ (/dev/mapper/mint--vg-root)]], net usable_space [202.5gb], net total_space [225gb], spins? [possibly], types [ext4] 2016-08-17 10:53:32.430 INFO 4037 --- [ main] org.elasticsearch.env : [Monet St. Croix] heap size [848mb], compressed ordinary object pointers [true] 2016-08-17 10:53:32.432 WARN 4037 --- [ main] org.elasticsearch.env : [Monet St. Croix] max file descriptors [4096] for elasticsearch process likely too low, consider increasing to at least [65536] 2016-08-17 10:53:32,780 WARN GraylogInstance:115 : Graylog rest interface not ready yet, rescheduling... 2016-08-17 10:53:33,782 WARN GraylogInstance:115 : Graylog rest interface not ready yet, rescheduling...****


DanielBarbarian
  • 5,093
  • 12
  • 35
  • 44

0 Answers0