Search in sources :

Example 1 with DROP_ALL_EXISTING

use of org.graylog2.grok.GrokPatternService.ImportStrategy.DROP_ALL_EXISTING in project graylog2-server by Graylog2.

the class GrokExtractorTest method makeExtractor.

@SuppressForbidden("Allow using default thread factory")
private GrokExtractor makeExtractor(String pattern, Map<String, Object> config) {
    config.put("grok_pattern", pattern);
    final ClusterEventBus clusterEventBus = new ClusterEventBus("cluster-event-bus", Executors.newSingleThreadExecutor());
    final EventBus clusterBus = new EventBus();
    final GrokPatternService grokPatternService = new InMemoryGrokPatternService(clusterEventBus);
    try {
        grokPatternService.saveAll(patternSet, DROP_ALL_EXISTING);
    } catch (Exception e) {
        fail("Could not save grok patter: " + e.getMessage());
    }
    final GrokPatternRegistry grokPatternRegistry = new GrokPatternRegistry(clusterBus, grokPatternService, Executors.newScheduledThreadPool(1));
    try {
        return new GrokExtractor(new LocalMetricRegistry(), grokPatternRegistry, "id", "title", 0, Extractor.CursorStrategy.COPY, "message", "message", config, "admin", Lists.newArrayList(), Extractor.ConditionType.NONE, null);
    } catch (Extractor.ReservedFieldException | ConfigurationException e) {
        fail("Test setup is wrong: " + e.getMessage());
        throw new RuntimeException(e);
    }
}
Also used : ConfigurationException(org.graylog2.ConfigurationException) InMemoryGrokPatternService(org.graylog2.grok.InMemoryGrokPatternService) GrokPatternService(org.graylog2.grok.GrokPatternService) InMemoryGrokPatternService(org.graylog2.grok.InMemoryGrokPatternService) GrokPatternRegistry(org.graylog2.grok.GrokPatternRegistry) EventBus(com.google.common.eventbus.EventBus) ClusterEventBus(org.graylog2.events.ClusterEventBus) ClusterEventBus(org.graylog2.events.ClusterEventBus) ConfigurationException(org.graylog2.ConfigurationException) LocalMetricRegistry(org.graylog2.plugin.LocalMetricRegistry) SuppressForbidden(org.graylog2.shared.SuppressForbidden)

Example 2 with DROP_ALL_EXISTING

use of org.graylog2.grok.GrokPatternService.ImportStrategy.DROP_ALL_EXISTING in project graylog2-server by Graylog2.

the class MongoDbGrokPatternService method saveAll.

@Override
public List<GrokPattern> saveAll(Collection<GrokPattern> patterns, ImportStrategy importStrategy) throws ValidationException {
    final Map<String, GrokPattern> newPatternsByName;
    try {
        newPatternsByName = patterns.stream().collect(Collectors.toMap(GrokPattern::name, Function.identity()));
    } catch (IllegalStateException e) {
        throw new ValidationException("The supplied Grok patterns contain conflicting names: " + e.getLocalizedMessage());
    }
    final Map<String, GrokPattern> existingPatternsByName = loadAll().stream().collect(Collectors.toMap(GrokPattern::name, Function.identity()));
    if (importStrategy == ABORT_ON_CONFLICT) {
        final Sets.SetView<String> conflictingNames = Sets.intersection(newPatternsByName.keySet(), existingPatternsByName.keySet());
        if (!conflictingNames.isEmpty()) {
            final Iterable<String> limited = Iterables.limit(conflictingNames, MAX_DISPLAYED_CONFLICTS);
            throw new ValidationException("The following Grok patterns already exist: " + StringUtils.join(limited, ", ") + (conflictingNames.size() > MAX_DISPLAYED_CONFLICTS ? " (+ " + (conflictingNames.size() - MAX_DISPLAYED_CONFLICTS) + " more)" : "") + ".");
        }
    }
    try {
        if (!validateAll(patterns)) {
            throw new ValidationException("Invalid patterns");
        }
    } catch (GrokException | PatternSyntaxException e) {
        throw new ValidationException("Invalid patterns.\n" + e.getMessage());
    }
    if (importStrategy == DROP_ALL_EXISTING) {
        deleteAll();
    }
    final List<GrokPattern> savedPatterns = patterns.stream().map(newPattern -> {
        final GrokPattern existingPattern = existingPatternsByName.get(newPattern.name());
        if (existingPattern != null) {
            return newPattern.toBuilder().id(existingPattern.id()).build();
        } else {
            return newPattern;
        }
    }).map(dbCollection::save).map(WriteResult::getSavedObject).collect(Collectors.toList());
    clusterBus.post(GrokPatternsUpdatedEvent.create(newPatternsByName.keySet()));
    return savedPatterns;
}
Also used : Iterables(com.google.common.collect.Iterables) DROP_ALL_EXISTING(org.graylog2.grok.GrokPatternService.ImportStrategy.DROP_ALL_EXISTING) LoggerFactory(org.slf4j.LoggerFactory) StringUtils(org.apache.commons.lang3.StringUtils) Function(java.util.function.Function) WriteResult(org.mongojack.WriteResult) Inject(javax.inject.Inject) Strings(com.google.common.base.Strings) Map(java.util.Map) NotFoundException(org.graylog2.database.NotFoundException) ImmutableSet(com.google.common.collect.ImmutableSet) Logger(org.slf4j.Logger) PatternSyntaxException(java.util.regex.PatternSyntaxException) Iterator(java.util.Iterator) GrokException(io.krakens.grok.api.exception.GrokException) DBCursor(org.mongojack.DBCursor) Collection(java.util.Collection) MongoJackObjectMapperProvider(org.graylog2.bindings.providers.MongoJackObjectMapperProvider) Preconditions.checkNotNull(com.google.common.base.Preconditions.checkNotNull) JacksonDBCollection(org.mongojack.JacksonDBCollection) Set(java.util.Set) DBQuery(org.mongojack.DBQuery) IndexOptions(com.mongodb.client.model.IndexOptions) ABORT_ON_CONFLICT(org.graylog2.grok.GrokPatternService.ImportStrategy.ABORT_ON_CONFLICT) Collectors(java.util.stream.Collectors) Sets(com.google.common.collect.Sets) Indexes(com.mongodb.client.model.Indexes) List(java.util.List) ClusterEventBus(org.graylog2.events.ClusterEventBus) Grok(io.krakens.grok.api.Grok) ValidationException(org.graylog2.plugin.database.ValidationException) ObjectId(org.bson.types.ObjectId) Optional(java.util.Optional) GrokCompiler(io.krakens.grok.api.GrokCompiler) MongoConnection(org.graylog2.database.MongoConnection) ValidationException(org.graylog2.plugin.database.ValidationException) Sets(com.google.common.collect.Sets) GrokException(io.krakens.grok.api.exception.GrokException) PatternSyntaxException(java.util.regex.PatternSyntaxException)

Example 3 with DROP_ALL_EXISTING

use of org.graylog2.grok.GrokPatternService.ImportStrategy.DROP_ALL_EXISTING in project graylog2-server by Graylog2.

the class InMemoryGrokPatternService method saveAll.

@Override
public List<GrokPattern> saveAll(Collection<GrokPattern> patterns, ImportStrategy importStrategy) throws ValidationException {
    if (importStrategy == ABORT_ON_CONFLICT) {
        for (GrokPattern pattern : loadAll()) {
            final boolean patternExists = patterns.stream().anyMatch(p -> p.name().equals(pattern.name()));
            if (patternExists) {
                throw new ValidationException("Grok pattern " + pattern.name() + " already exists");
            }
        }
    }
    try {
        if (!validateAll(patterns)) {
            throw new ValidationException("Patterns invalid.");
        }
    } catch (GrokException | PatternSyntaxException e) {
        throw new ValidationException("Invalid patterns.\n" + e.getMessage());
    }
    if (importStrategy == DROP_ALL_EXISTING) {
        deleteAll();
    }
    final List<GrokPattern> grokPatterns = patterns.stream().map(this::uncheckedSave).collect(Collectors.toList());
    final Set<String> patternNames = grokPatterns.stream().map(GrokPattern::name).collect(Collectors.toSet());
    if (!patternNames.isEmpty()) {
        clusterBus.post(GrokPatternsUpdatedEvent.create(patternNames));
    }
    return grokPatterns;
}
Also used : ValidationException(org.graylog2.plugin.database.ValidationException) GrokException(io.krakens.grok.api.exception.GrokException) PatternSyntaxException(java.util.regex.PatternSyntaxException)

Aggregations

GrokException (io.krakens.grok.api.exception.GrokException)2 PatternSyntaxException (java.util.regex.PatternSyntaxException)2 ClusterEventBus (org.graylog2.events.ClusterEventBus)2 ValidationException (org.graylog2.plugin.database.ValidationException)2 Preconditions.checkNotNull (com.google.common.base.Preconditions.checkNotNull)1 Strings (com.google.common.base.Strings)1 ImmutableSet (com.google.common.collect.ImmutableSet)1 Iterables (com.google.common.collect.Iterables)1 Sets (com.google.common.collect.Sets)1 EventBus (com.google.common.eventbus.EventBus)1 IndexOptions (com.mongodb.client.model.IndexOptions)1 Indexes (com.mongodb.client.model.Indexes)1 Grok (io.krakens.grok.api.Grok)1 GrokCompiler (io.krakens.grok.api.GrokCompiler)1 Collection (java.util.Collection)1 Iterator (java.util.Iterator)1 List (java.util.List)1 Map (java.util.Map)1 Optional (java.util.Optional)1 Set (java.util.Set)1