Search in sources :

Example 1 with GrokTesterResponse

use of org.graylog2.rest.resources.tools.responses.GrokTesterResponse in project graylog2-server by Graylog2.

the class GrokTesterResource method doTestGrok.

private GrokTesterResponse doTestGrok(String string, String pattern, boolean namedCapturesOnly) throws GrokException {
    final Set<GrokPattern> grokPatterns = grokPatternService.loadAll();
    final GrokCompiler grokCompiler = GrokCompiler.newInstance();
    for (GrokPattern grokPattern : grokPatterns) {
        grokCompiler.register(grokPattern.name(), grokPattern.pattern());
    }
    final Grok grok;
    try {
        grok = grokCompiler.compile(pattern, namedCapturesOnly);
    } catch (Exception e) {
        return GrokTesterResponse.createError(pattern, string, e.getMessage());
    }
    final Match match = grok.match(string);
    final Map<String, Object> matches = match.captureFlattened();
    final GrokTesterResponse response;
    if (matches.isEmpty()) {
        response = GrokTesterResponse.createSuccess(false, Collections.<GrokTesterResponse.Match>emptyList(), pattern, string);
    } else {
        final List<GrokTesterResponse.Match> responseMatches = Lists.newArrayList();
        for (final Map.Entry<String, Object> entry : matches.entrySet()) {
            final Object value = entry.getValue();
            if (value != null) {
                responseMatches.add(GrokTesterResponse.Match.create(entry.getKey(), value.toString()));
            }
        }
        response = GrokTesterResponse.createSuccess(true, responseMatches, pattern, string);
    }
    return response;
}
Also used : Grok(io.krakens.grok.api.Grok) GrokCompiler(io.krakens.grok.api.GrokCompiler) GrokException(io.krakens.grok.api.exception.GrokException) Match(io.krakens.grok.api.Match) GrokPattern(org.graylog2.grok.GrokPattern) Map(java.util.Map) GrokTesterResponse(org.graylog2.rest.resources.tools.responses.GrokTesterResponse)

Example 2 with GrokTesterResponse

use of org.graylog2.rest.resources.tools.responses.GrokTesterResponse in project graylog2-server by Graylog2.

the class GrokTesterResourceTest method testGrokWithInvalidPattern.

@Test
public void testGrokWithInvalidPattern() {
    final GrokTesterResponse response = resource.grokTest("%{NUMBER", "abc 1234", false);
    assertThat(response.matched()).isFalse();
    assertThat(response.pattern()).isEqualTo("%{NUMBER");
    assertThat(response.string()).isEqualTo("abc 1234");
    assertThat(response.errorMessage()).startsWith("Illegal repetition near index 0");
}
Also used : GrokTesterResponse(org.graylog2.rest.resources.tools.responses.GrokTesterResponse) Test(org.junit.Test)

Example 3 with GrokTesterResponse

use of org.graylog2.rest.resources.tools.responses.GrokTesterResponse in project graylog2-server by Graylog2.

the class GrokTesterResourceTest method testGrokWithValidPatternAndMatch.

@Test
public void testGrokWithValidPatternAndMatch() {
    final GrokTesterResponse response = resource.grokTest("%{NUMBER}", "abc 1234", false);
    assertThat(response.matched()).isTrue();
    assertThat(response.pattern()).isEqualTo("%{NUMBER}");
    assertThat(response.string()).isEqualTo("abc 1234");
    assertThat(response.matches()).containsOnly(GrokTesterResponse.Match.create("NUMBER", "1234"));
    assertThat(response.errorMessage()).isNullOrEmpty();
}
Also used : GrokTesterResponse(org.graylog2.rest.resources.tools.responses.GrokTesterResponse) Test(org.junit.Test)

Example 4 with GrokTesterResponse

use of org.graylog2.rest.resources.tools.responses.GrokTesterResponse in project graylog2-server by Graylog2.

the class GrokTesterResourceTest method testGrokWithMissingPattern.

@Test
public void testGrokWithMissingPattern() {
    final GrokTesterResponse response = resource.grokTest("%{FOOBAR} %{NUMBER}", "abc 1234", false);
    assertThat(response.matched()).isFalse();
    assertThat(response.pattern()).isEqualTo("%{FOOBAR} %{NUMBER}");
    assertThat(response.string()).isEqualTo("abc 1234");
    assertThat(response.errorMessage()).isEqualTo("No definition for key 'FOOBAR' found, aborting");
}
Also used : GrokTesterResponse(org.graylog2.rest.resources.tools.responses.GrokTesterResponse) Test(org.junit.Test)

Example 5 with GrokTesterResponse

use of org.graylog2.rest.resources.tools.responses.GrokTesterResponse in project graylog2-server by Graylog2.

the class GrokTesterResourceTest method testGrokWithEmptyPattern.

@Test
public void testGrokWithEmptyPattern() {
    final GrokTesterResponse response = resource.grokTest("", "abc 1234", false);
    assertThat(response.matched()).isFalse();
    assertThat(response.pattern()).isEqualTo("");
    assertThat(response.string()).isEqualTo("abc 1234");
    assertThat(response.errorMessage()).isEqualTo("{pattern} should not be empty or null");
}
Also used : GrokTesterResponse(org.graylog2.rest.resources.tools.responses.GrokTesterResponse) Test(org.junit.Test)

Aggregations

GrokTesterResponse (org.graylog2.rest.resources.tools.responses.GrokTesterResponse)7 Test (org.junit.Test)6 Grok (io.krakens.grok.api.Grok)1 GrokCompiler (io.krakens.grok.api.GrokCompiler)1 Match (io.krakens.grok.api.Match)1 GrokException (io.krakens.grok.api.exception.GrokException)1 Map (java.util.Map)1 GrokPattern (org.graylog2.grok.GrokPattern)1