Search in sources :

Example 6 with GrokPattern

use of org.graylog2.grok.GrokPattern in project graylog2-server by Graylog2.

the class MongoDbGrokPatternService method save.

@Override
public GrokPattern save(GrokPattern pattern) throws ValidationException {
    try {
        if (!validate(pattern)) {
            throw new ValidationException("Invalid pattern " + pattern);
        }
    } catch (GrokException | PatternSyntaxException e) {
        throw new ValidationException("Invalid pattern " + pattern + "\n" + e.getMessage());
    }
    if (loadByName(pattern.name()).isPresent()) {
        throw new ValidationException("Grok pattern " + pattern.name() + " already exists");
    }
    final WriteResult<GrokPattern, ObjectId> result = dbCollection.save(pattern);
    final GrokPattern savedGrokPattern = result.getSavedObject();
    clusterBus.post(GrokPatternsUpdatedEvent.create(ImmutableSet.of(savedGrokPattern.name())));
    return savedGrokPattern;
}
Also used : ValidationException(org.graylog2.plugin.database.ValidationException) ObjectId(org.bson.types.ObjectId) GrokException(io.krakens.grok.api.exception.GrokException) PatternSyntaxException(java.util.regex.PatternSyntaxException)

Example 7 with GrokPattern

use of org.graylog2.grok.GrokPattern in project graylog2-server by Graylog2.

the class MongoDbGrokPatternService method delete.

@Override
public int delete(String patternId) {
    final GrokPattern grokPattern;
    try {
        grokPattern = load(patternId);
    } catch (NotFoundException e) {
        log.debug("Couldn't find grok pattern with ID <{}> for deletion", patternId, e);
        return 0;
    }
    final ObjectId id = new ObjectId(patternId);
    final String name = grokPattern.name();
    final int deletedPatterns = dbCollection.removeById(id).getN();
    clusterBus.post(GrokPatternsDeletedEvent.create(ImmutableSet.of(name)));
    return deletedPatterns;
}
Also used : ObjectId(org.bson.types.ObjectId) NotFoundException(org.graylog2.database.NotFoundException)

Example 8 with GrokPattern

use of org.graylog2.grok.GrokPattern in project graylog2-server by Graylog2.

the class InMemoryGrokPatternService method save.

@Override
public GrokPattern save(GrokPattern pattern) throws ValidationException {
    try {
        if (!validate(pattern)) {
            throw new ValidationException("Pattern " + pattern.name() + " invalid.");
        }
    } catch (GrokException | PatternSyntaxException e) {
        throw new ValidationException("Invalid pattern " + pattern + "\n" + e.getMessage());
    }
    GrokPattern toSave;
    if (pattern.id() == null) {
        toSave = pattern.toBuilder().id(createId()).build();
    } else {
        toSave = pattern;
    }
    store.put(toSave.id(), toSave);
    clusterBus.post(GrokPatternsUpdatedEvent.create(ImmutableSet.of(toSave.name())));
    return toSave;
}
Also used : ValidationException(org.graylog2.plugin.database.ValidationException) GrokException(io.krakens.grok.api.exception.GrokException) PatternSyntaxException(java.util.regex.PatternSyntaxException)

Example 9 with GrokPattern

use of org.graylog2.grok.GrokPattern 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 10 with GrokPattern

use of org.graylog2.grok.GrokPattern in project graylog2-server by Graylog2.

the class GrokResource method createPattern.

@POST
@Timed
@ApiOperation(value = "Add a new named pattern", response = GrokPattern.class)
@AuditEvent(type = AuditEventTypes.GROK_PATTERN_CREATE)
public Response createPattern(@ApiParam(name = "pattern", required = true) @Valid @NotNull GrokPattern pattern) throws ValidationException {
    checkPermission(RestPermissions.INPUTS_CREATE);
    // remove the ID from the pattern, this is only used to create new patterns
    final GrokPattern newPattern = grokPatternService.save(pattern.toBuilder().id(null).build());
    final URI patternUri = getUriBuilderToSelf().path(GrokResource.class, "listPattern").build(newPattern.id());
    return Response.created(patternUri).entity(newPattern).build();
}
Also used : GrokPattern(org.graylog2.grok.GrokPattern) URI(java.net.URI) POST(javax.ws.rs.POST) Timed(com.codahale.metrics.annotation.Timed) ApiOperation(io.swagger.annotations.ApiOperation) NoAuditEvent(org.graylog2.audit.jersey.NoAuditEvent) AuditEvent(org.graylog2.audit.jersey.AuditEvent)

Aggregations

GrokPattern (org.graylog2.grok.GrokPattern)28 Test (org.junit.Test)15 ValidationException (org.graylog2.plugin.database.ValidationException)9 Timed (com.codahale.metrics.annotation.Timed)7 ApiOperation (io.swagger.annotations.ApiOperation)7 NativeEntityDescriptor (org.graylog2.contentpacks.model.entities.NativeEntityDescriptor)7 GrokException (io.krakens.grok.api.exception.GrokException)6 AuditEvent (org.graylog2.audit.jersey.AuditEvent)6 GrokPatternEntity (org.graylog2.contentpacks.model.entities.GrokPatternEntity)6 NotFoundException (org.graylog2.database.NotFoundException)5 PatternSyntaxException (java.util.regex.PatternSyntaxException)4 Response (javax.ws.rs.core.Response)4 ByteArrayInputStream (java.io.ByteArrayInputStream)3 POST (javax.ws.rs.POST)3 Path (javax.ws.rs.Path)3 Entity (org.graylog2.contentpacks.model.entities.Entity)3 EntityDescriptor (org.graylog2.contentpacks.model.entities.EntityDescriptor)3 Before (org.junit.Before)3 JsonNode (com.fasterxml.jackson.databind.JsonNode)2 Grok (io.krakens.grok.api.Grok)2