Search in sources :

Example 1 with GrokExtractor

use of org.graylog2.inputs.extractors.GrokExtractor in project graylog2-server by Graylog2.

the class GrokExtractorTest method testNamedCapturesOnly.

@Test
public void testNamedCapturesOnly() {
    final Map<String, Object> config = new HashMap<>();
    final GrokPattern mynumber = GrokPattern.create("MYNUMBER", "(?:%{BASE10NUM})");
    patternSet.add(mynumber);
    config.put("named_captures_only", true);
    final GrokExtractor extractor1 = makeExtractor("%{MYNUMBER:num}", config);
    config.put("named_captures_only", true);
    final GrokExtractor extractor2 = makeExtractor("%{MYNUMBER:num;int}", config);
    config.put("named_captures_only", false);
    final GrokExtractor extractor3 = makeExtractor("%{MYNUMBER:num}", config);
    final GrokExtractor extractor4 = makeExtractor("%{MYNUMBER:num}");
    assertThat(extractor1.run("2015")).hasSize(1).containsOnly(new Extractor.Result("2015", "num", -1, -1));
    assertThat(extractor2.run("2015")).hasSize(1).containsOnly(new Extractor.Result(2015, "num", -1, -1));
    assertThat(extractor3.run("2015")).hasSize(2).containsOnly(new Extractor.Result("2015", "num", -1, -1), new Extractor.Result("2015", "BASE10NUM", -1, -1));
    assertThat(extractor4.run("2015")).hasSize(2).containsOnly(new Extractor.Result("2015", "num", -1, -1), new Extractor.Result("2015", "BASE10NUM", -1, -1));
}
Also used : GrokPattern(org.graylog2.grok.GrokPattern) HashMap(java.util.HashMap) Extractor(org.graylog2.plugin.inputs.Extractor) Test(org.junit.Test)

Example 2 with GrokExtractor

use of org.graylog2.inputs.extractors.GrokExtractor 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 3 with GrokExtractor

use of org.graylog2.inputs.extractors.GrokExtractor in project graylog2-server by Graylog2.

the class InputFacadeTest method resolveForInstallationGrokPattern.

@Test
@MongoDBFixtures("InputFacadeTest.json")
public void resolveForInstallationGrokPattern() throws NotFoundException {
    final Input input = inputService.find("5ae2ebbeef27464477f0fd8b");
    final InputWithExtractors inputWithExtractors = InputWithExtractors.create(input, inputService.getExtractors(input));
    final GrokExtractor grokExtractor = (GrokExtractor) inputWithExtractors.extractors().iterator().next();
    final ExtractorEntity extractorEntity = ExtractorEntity.create(ValueReference.of(grokExtractor.getTitle()), ValueReference.of(grokExtractor.getType()), ValueReference.of(grokExtractor.getCursorStrategy()), ValueReference.of(grokExtractor.getTargetField()), ValueReference.of(grokExtractor.getSourceField()), ReferenceMapUtils.toReferenceMap(grokExtractor.getExtractorConfig()), Collections.emptyList(), ValueReference.of(grokExtractor.getConditionType()), ValueReference.of(grokExtractor.getConditionValue()), ValueReference.of(grokExtractor.getOrder()));
    List<ExtractorEntity> extractorEntities = new ArrayList<>(1);
    extractorEntities.add(extractorEntity);
    InputEntity inputEntity = InputEntity.create(ValueReference.of(input.getTitle()), ReferenceMapUtils.toReferenceMap(input.getConfiguration()), Collections.emptyMap(), ValueReference.of(input.getType()), ValueReference.of(input.isGlobal()), extractorEntities);
    Entity entity = EntityV1.builder().id(ModelId.of(input.getId())).type(ModelTypes.INPUT_V1).data(objectMapper.convertValue(inputEntity, JsonNode.class)).build();
    final GrokPatternEntity grokPatternEntity = GrokPatternEntity.create("GREEDY", ".*");
    final Entity expectedEntity = EntityV1.builder().id(ModelId.of("dead-feed")).data(objectMapper.convertValue(grokPatternEntity, JsonNode.class)).type(ModelTypes.GROK_PATTERN_V1).build();
    final EntityDescriptor entityDescriptor = expectedEntity.toEntityDescriptor();
    final Map<EntityDescriptor, Entity> entities = new HashMap<>(1);
    entities.put(entityDescriptor, expectedEntity);
    Graph<Entity> graph = facade.resolveForInstallation(entity, Collections.emptyMap(), entities);
    assertThat(graph.nodes()).contains(expectedEntity);
}
Also used : NativeEntity(org.graylog2.contentpacks.model.entities.NativeEntity) ConverterEntity(org.graylog2.contentpacks.model.entities.ConverterEntity) InputEntity(org.graylog2.contentpacks.model.entities.InputEntity) Entity(org.graylog2.contentpacks.model.entities.Entity) ExtractorEntity(org.graylog2.contentpacks.model.entities.ExtractorEntity) LookupTableEntity(org.graylog2.contentpacks.model.entities.LookupTableEntity) GrokPatternEntity(org.graylog2.contentpacks.model.entities.GrokPatternEntity) HashMap(java.util.HashMap) ArrayList(java.util.ArrayList) JsonNode(com.fasterxml.jackson.databind.JsonNode) GrokPatternEntity(org.graylog2.contentpacks.model.entities.GrokPatternEntity) EntityDescriptor(org.graylog2.contentpacks.model.entities.EntityDescriptor) RawUDPInput(org.graylog2.inputs.raw.udp.RawUDPInput) Input(org.graylog2.inputs.Input) FakeHttpMessageInput(org.graylog2.inputs.random.FakeHttpMessageInput) MessageInput(org.graylog2.plugin.inputs.MessageInput) GrokExtractor(org.graylog2.inputs.extractors.GrokExtractor) ExtractorEntity(org.graylog2.contentpacks.model.entities.ExtractorEntity) InputEntity(org.graylog2.contentpacks.model.entities.InputEntity) MongoDBFixtures(org.graylog.testing.mongodb.MongoDBFixtures) Test(org.junit.Test)

Aggregations

HashMap (java.util.HashMap)2 Test (org.junit.Test)2 JsonNode (com.fasterxml.jackson.databind.JsonNode)1 EventBus (com.google.common.eventbus.EventBus)1 ArrayList (java.util.ArrayList)1 MongoDBFixtures (org.graylog.testing.mongodb.MongoDBFixtures)1 ConfigurationException (org.graylog2.ConfigurationException)1 ConverterEntity (org.graylog2.contentpacks.model.entities.ConverterEntity)1 Entity (org.graylog2.contentpacks.model.entities.Entity)1 EntityDescriptor (org.graylog2.contentpacks.model.entities.EntityDescriptor)1 ExtractorEntity (org.graylog2.contentpacks.model.entities.ExtractorEntity)1 GrokPatternEntity (org.graylog2.contentpacks.model.entities.GrokPatternEntity)1 InputEntity (org.graylog2.contentpacks.model.entities.InputEntity)1 LookupTableEntity (org.graylog2.contentpacks.model.entities.LookupTableEntity)1 NativeEntity (org.graylog2.contentpacks.model.entities.NativeEntity)1 ClusterEventBus (org.graylog2.events.ClusterEventBus)1 GrokPattern (org.graylog2.grok.GrokPattern)1 GrokPatternRegistry (org.graylog2.grok.GrokPatternRegistry)1 GrokPatternService (org.graylog2.grok.GrokPatternService)1 InMemoryGrokPatternService (org.graylog2.grok.InMemoryGrokPatternService)1