Search in sources :

Example 26 with Result

use of org.graylog2.plugin.inputs.Extractor.Result in project graylog2-server by Graylog2.

the class RegexReplaceTesterResource method testRegexReplaceExtractor.

private RegexReplaceTesterResponse testRegexReplaceExtractor(String example, String regex, String replacement, boolean replaceAll) {
    final Map<String, Object> config = ImmutableMap.<String, Object>of("regex", regex, "replacement", replacement, "replace_all", replaceAll);
    final RegexReplaceExtractor extractor;
    try {
        extractor = new RegexReplaceExtractor(new MetricRegistry(), "test", "Test", 0L, Extractor.CursorStrategy.COPY, "test", "test", config, getCurrentUser().getName(), Collections.<Converter>emptyList(), Extractor.ConditionType.NONE, "");
    } catch (Extractor.ReservedFieldException e) {
        throw new BadRequestException("Trying to overwrite a reserved message field", e);
    } catch (ConfigurationException e) {
        throw new BadRequestException("Invalid extractor configuration", e);
    }
    final Extractor.Result result = extractor.runExtractor(example);
    final RegexReplaceTesterResponse.Match match = result == null ? null : RegexReplaceTesterResponse.Match.create(String.valueOf(result.getValue()), result.getBeginIndex(), result.getEndIndex());
    return RegexReplaceTesterResponse.create(result != null, match, regex, replacement, replaceAll, example);
}
Also used : MetricRegistry(com.codahale.metrics.MetricRegistry) RegexReplaceExtractor(org.graylog2.inputs.extractors.RegexReplaceExtractor) RegexReplaceTesterResponse(org.graylog2.rest.models.tools.responses.RegexReplaceTesterResponse) ConfigurationException(org.graylog2.ConfigurationException) Converter(org.graylog2.plugin.inputs.Converter) BadRequestException(javax.ws.rs.BadRequestException) Extractor(org.graylog2.plugin.inputs.Extractor) RegexReplaceExtractor(org.graylog2.inputs.extractors.RegexReplaceExtractor)

Example 27 with Result

use of org.graylog2.plugin.inputs.Extractor.Result in project graylog2-server by Graylog2.

the class InputStatesResource method start.

@PUT
@Path("/{inputId}")
@Timed
@ApiOperation(value = "(Re-)Start specified input on this node")
@ApiResponses(value = { @ApiResponse(code = 404, message = "No such input on this node.") })
@AuditEvent(type = AuditEventTypes.MESSAGE_INPUT_START)
public InputCreated start(@ApiParam(name = "inputId", required = true) @PathParam("inputId") String inputId) throws org.graylog2.database.NotFoundException {
    inputService.find(inputId);
    final InputCreated result = InputCreated.create(inputId);
    this.serverEventBus.post(result);
    return result;
}
Also used : InputCreated(org.graylog2.rest.models.system.inputs.responses.InputCreated) Path(javax.ws.rs.Path) Timed(com.codahale.metrics.annotation.Timed) ApiOperation(io.swagger.annotations.ApiOperation) AuditEvent(org.graylog2.audit.jersey.AuditEvent) PUT(javax.ws.rs.PUT) ApiResponses(io.swagger.annotations.ApiResponses)

Example 28 with Result

use of org.graylog2.plugin.inputs.Extractor.Result in project graylog2-server by Graylog2.

the class ExtractorsResource method create.

@POST
@Timed
@Consumes(MediaType.APPLICATION_JSON)
@Produces(MediaType.APPLICATION_JSON)
@ApiOperation(value = "Add an extractor to an input", response = ExtractorCreated.class)
@ApiResponses(value = { @ApiResponse(code = 404, message = "No such input on this node."), @ApiResponse(code = 400, message = "No such extractor type."), @ApiResponse(code = 400, message = "Field the extractor should write on is reserved."), @ApiResponse(code = 400, message = "Missing or invalid configuration.") })
@AuditEvent(type = AuditEventTypes.EXTRACTOR_CREATE)
public Response create(@ApiParam(name = "inputId", required = true) @PathParam("inputId") String inputId, @ApiParam(name = "JSON body", required = true) @Valid @NotNull CreateExtractorRequest cer) throws NotFoundException {
    checkPermission(RestPermissions.INPUTS_EDIT, inputId);
    final Input mongoInput = inputService.find(inputId);
    final String id = new com.eaio.uuid.UUID().toString();
    final Extractor extractor = buildExtractorFromRequest(cer, id);
    try {
        inputService.addExtractor(mongoInput, extractor);
    } catch (ValidationException e) {
        final String msg = "Extractor persist validation failed.";
        LOG.error(msg, e);
        throw new BadRequestException(msg, e);
    }
    final String msg = "Added extractor <" + id + "> of type [" + cer.extractorType() + "] to input <" + inputId + ">.";
    LOG.info(msg);
    activityWriter.write(new Activity(msg, ExtractorsResource.class));
    final ExtractorCreated result = ExtractorCreated.create(id);
    final URI extractorUri = getUriBuilderToSelf().path(ExtractorsResource.class).path("{inputId}").build(mongoInput.getId());
    return Response.created(extractorUri).entity(result).build();
}
Also used : ExtractorCreated(org.graylog2.rest.models.system.inputs.extractors.responses.ExtractorCreated) Input(org.graylog2.inputs.Input) MessageInput(org.graylog2.plugin.inputs.MessageInput) ValidationException(org.graylog2.plugin.database.ValidationException) BadRequestException(javax.ws.rs.BadRequestException) Activity(org.graylog2.shared.system.activities.Activity) Extractor(org.graylog2.plugin.inputs.Extractor) URI(java.net.URI) POST(javax.ws.rs.POST) Consumes(javax.ws.rs.Consumes) Produces(javax.ws.rs.Produces) Timed(com.codahale.metrics.annotation.Timed) ApiOperation(io.swagger.annotations.ApiOperation) AuditEvent(org.graylog2.audit.jersey.AuditEvent) ApiResponses(io.swagger.annotations.ApiResponses)

Example 29 with Result

use of org.graylog2.plugin.inputs.Extractor.Result in project graylog2-server by Graylog2.

the class ExtractorTest method testConvertersWithMultipleFieldsAndNull.

@Test
public void testConvertersWithMultipleFieldsAndNull() throws Exception {
    final Converter converter = new TestConverter.Builder().multiple(true).callback(new Function<Object, Object>() {

        @Nullable
        @Override
        public Object apply(Object input) {
            return null;
        }
    }).build();
    final TestExtractor extractor = new TestExtractor.Builder().converters(Collections.singletonList(converter)).callback(() -> new Result[] { new Result("1", -1, -1) }).build();
    final Message msg = createMessage("the message");
    extractor.runExtractor(msg);
    assertThat(msg.getField("message")).isEqualTo("the message");
    assertThat(extractor.getConverterExceptionCount()).isEqualTo(0L);
}
Also used : Function(com.google.common.base.Function) Message(org.graylog2.plugin.Message) Result(org.graylog2.plugin.inputs.Extractor.Result) Test(org.junit.Test)

Example 30 with Result

use of org.graylog2.plugin.inputs.Extractor.Result in project graylog2-server by Graylog2.

the class ExtractorTest method testCursorStrategyCutWithAllTextCut.

@Test
public void testCursorStrategyCutWithAllTextCut() throws Exception {
    final TestExtractor extractor = new TestExtractor.Builder().cursorStrategy(CUT).sourceField("msg").callback(new Callable<Result[]>() {

        @Override
        public Result[] call() throws Exception {
            return new Result[] { new Result("the hello", 0, 9) };
        }
    }).build();
    final Message msg = createMessage("message");
    msg.addField("msg", "the hello");
    extractor.runExtractor(msg);
    // If all data is cut from the source field, the "fullyCutByExtractor" string gets inserted.
    assertThat(msg.getField("msg")).isEqualTo("fullyCutByExtractor");
}
Also used : Message(org.graylog2.plugin.Message) Callable(java.util.concurrent.Callable) Result(org.graylog2.plugin.inputs.Extractor.Result) Test(org.junit.Test)

Aggregations

Test (org.junit.Test)73 Message (org.graylog2.plugin.Message)51 Result (org.graylog2.plugin.inputs.Extractor.Result)27 Callable (java.util.concurrent.Callable)26 Stream (org.graylog2.plugin.streams.Stream)20 StreamRule (org.graylog2.plugin.streams.StreamRule)19 DateTime (org.joda.time.DateTime)18 Timed (com.codahale.metrics.annotation.Timed)13 ApiOperation (io.swagger.annotations.ApiOperation)13 AlertCondition (org.graylog2.plugin.alarms.AlertCondition)12 ApiResponses (io.swagger.annotations.ApiResponses)11 Produces (javax.ws.rs.Produces)9 AuditEvent (org.graylog2.audit.jersey.AuditEvent)9 Function (com.google.common.base.Function)8 UsingDataSet (com.lordofthejars.nosqlunit.annotation.UsingDataSet)8 ZonedDateTime (java.time.ZonedDateTime)8 AbstractAlertCondition (org.graylog2.alerts.AbstractAlertCondition)8 Sorting (org.graylog2.indexer.searches.Sorting)8 URI (java.net.URI)7 MessageSummary (org.graylog2.plugin.MessageSummary)7