Search in sources :

Example 1 with Grok

use of oi.thekraken.grok.api.Grok in project graylog2-server by Graylog2.

the class InMemoryGrokPatternService method validate.

@Override
public boolean validate(GrokPattern pattern) {
    final boolean fieldsMissing = !(Strings.isNullOrEmpty(pattern.name()) || Strings.isNullOrEmpty(pattern.pattern()));
    try {
        final Grok grok = new Grok();
        grok.addPattern(pattern.name(), pattern.pattern());
        grok.compile("%{" + pattern.name() + "}");
    } catch (GrokException ignored) {
    // this only checks for null or empty again.
    } catch (PatternSyntaxException e) {
        LOG.warn("Invalid regular expression syntax for '" + pattern.name() + "' with pattern " + pattern.pattern(), e);
        return false;
    }
    return fieldsMissing;
}
Also used : Grok(oi.thekraken.grok.api.Grok) GrokException(oi.thekraken.grok.api.exception.GrokException) PatternSyntaxException(java.util.regex.PatternSyntaxException)

Example 2 with Grok

use of oi.thekraken.grok.api.Grok in project graylog2-server by Graylog2.

the class MongoDbGrokPatternService method validate.

@Override
public boolean validate(GrokPattern pattern) {
    final boolean fieldsMissing = !(Strings.isNullOrEmpty(pattern.name()) || Strings.isNullOrEmpty(pattern.pattern()));
    try {
        final Grok grok = new Grok();
        grok.addPattern(pattern.name(), pattern.pattern());
        grok.compile("%{" + pattern.name() + "}");
    } catch (GrokException ignored) {
    // this only checks for null or empty again.
    } catch (PatternSyntaxException e) {
        log.warn("Invalid regular expression syntax for '" + pattern.name() + "' with pattern " + pattern.pattern(), e);
        return false;
    }
    return fieldsMissing;
}
Also used : Grok(oi.thekraken.grok.api.Grok) GrokException(oi.thekraken.grok.api.exception.GrokException) PatternSyntaxException(java.util.regex.PatternSyntaxException)

Example 3 with Grok

use of oi.thekraken.grok.api.Grok in project graylog2-server by Graylog2.

the class GrokPatternRegistryTest method cachedGrokForPatternWithNamedCaptureOnly.

@Test
public void cachedGrokForPatternWithNamedCaptureOnly() throws Exception {
    final Grok grok = grokPatternRegistry.cachedGrokForPattern("%{TESTNUM}", true);
    assertThat(grok.getPatterns()).containsEntry(GROK_PATTERN.name(), GROK_PATTERN.pattern());
}
Also used : Grok(oi.thekraken.grok.api.Grok) Test(org.junit.Test)

Example 4 with Grok

use of oi.thekraken.grok.api.Grok 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 Grok grok = new Grok();
    for (GrokPattern grokPattern : grokPatterns) {
        grok.addPattern(grokPattern.name(), grokPattern.pattern());
    }
    grok.compile(pattern, namedCapturesOnly);
    final Match match = grok.match(string);
    match.captures();
    final Map<String, Object> matches = match.toMap();
    final GrokTesterResponse response;
    if (matches.isEmpty()) {
        response = GrokTesterResponse.create(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.create(true, responseMatches, pattern, string);
    }
    return response;
}
Also used : GrokPattern(org.graylog2.grok.GrokPattern) Grok(oi.thekraken.grok.api.Grok) Map(java.util.Map) GrokTesterResponse(org.graylog2.rest.resources.tools.responses.GrokTesterResponse) Match(oi.thekraken.grok.api.Match)

Example 5 with Grok

use of oi.thekraken.grok.api.Grok in project graylog2-server by Graylog2.

the class GrokPatternRegistryTest method cachedGrokForPattern.

@Test
public void cachedGrokForPattern() throws Exception {
    final Grok grok = grokPatternRegistry.cachedGrokForPattern("%{TESTNUM}");
    assertThat(grok.getPatterns()).containsEntry(GROK_PATTERN.name(), GROK_PATTERN.pattern());
}
Also used : Grok(oi.thekraken.grok.api.Grok) Test(org.junit.Test)

Aggregations

Grok (oi.thekraken.grok.api.Grok)5 PatternSyntaxException (java.util.regex.PatternSyntaxException)2 GrokException (oi.thekraken.grok.api.exception.GrokException)2 Test (org.junit.Test)2 Map (java.util.Map)1 Match (oi.thekraken.grok.api.Match)1 GrokPattern (org.graylog2.grok.GrokPattern)1 GrokTesterResponse (org.graylog2.rest.resources.tools.responses.GrokTesterResponse)1