Search in sources :

Example 6 with GrokCompiler

use of io.krakens.grok.api.GrokCompiler in project graylog2-server by Graylog2.

the class InMemoryGrokPatternService method match.

@Override
public Map<String, Object> match(GrokPattern pattern, String sampleData) throws GrokException {
    final Set<GrokPattern> patterns = loadAll();
    final GrokCompiler grokCompiler = GrokCompiler.newInstance();
    for (GrokPattern storedPattern : patterns) {
        grokCompiler.register(storedPattern.name(), storedPattern.pattern());
    }
    grokCompiler.register(pattern.name(), pattern.pattern());
    Grok grok = grokCompiler.compile("%{" + pattern.name() + "}");
    return grok.match(sampleData).captureFlattened();
}
Also used : Grok(io.krakens.grok.api.Grok) GrokCompiler(io.krakens.grok.api.GrokCompiler)

Example 7 with GrokCompiler

use of io.krakens.grok.api.GrokCompiler 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)

Aggregations

GrokCompiler (io.krakens.grok.api.GrokCompiler)7 Grok (io.krakens.grok.api.Grok)3 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 GrokTesterResponse (org.graylog2.rest.resources.tools.responses.GrokTesterResponse)1