use of org.graylog2.grok.GrokPattern in project graylog2-server by Graylog2.
the class V20191121145100_FixDefaultGrokPatterns method migratePattern.
private void migratePattern(PatternToMigrate patternToMigrate) throws ValidationException {
final Optional<GrokPattern> currentPattern = grokPatternService.loadByName(patternToMigrate.name());
if (!currentPattern.isPresent()) {
log.debug("Couldn't find default pattern '{}'. Skipping migration.", patternToMigrate.name());
return;
}
final GrokPattern pattern = currentPattern.get();
if (!patternToMigrate.migrateFrom().equals(pattern.pattern())) {
log.info("Skipping migration of modified default Grok Pattern '{}'.", pattern.name());
} else {
log.info("Migrating default Grok Pattern '{}'.", pattern.name());
final GrokPattern migratedPattern = pattern.toBuilder().pattern(patternToMigrate.migrateTo()).build();
grokPatternService.update(migratedPattern);
}
}
use of org.graylog2.grok.GrokPattern in project graylog2-server by Graylog2.
the class GrokResource method getPage.
@GET
@Timed
@Path("/paginated")
@ApiOperation("Get existing grok patterns paged")
@Produces(MediaType.APPLICATION_JSON)
public PaginatedResponse<GrokPattern> getPage(@ApiParam(name = "page") @QueryParam("page") @DefaultValue("1") int page, @ApiParam(name = "per_page") @QueryParam("per_page") @DefaultValue("50") int perPage, @ApiParam(name = "query") @QueryParam("query") @DefaultValue("") String query, @ApiParam(name = "sort", value = "The field to sort the result on", required = true, allowableValues = "title,description,id") @DefaultValue(GrokPattern.FIELD_NAME) @QueryParam("sort") String sort, @ApiParam(name = "order", value = "The sort direction", allowableValues = "asc, desc") @DefaultValue("asc") @QueryParam("order") String order) {
checkPermission(RestPermissions.INPUTS_READ);
SearchQuery searchQuery;
try {
searchQuery = searchQueryParser.parse(query);
} catch (IllegalArgumentException e) {
throw new BadRequestException("Invalid argument in search query: " + e.getMessage());
}
final PaginatedList<GrokPattern> result = paginatedGrokPatternService.findPaginated(searchQuery, page, perPage, sort, order);
return PaginatedResponse.create("patterns", result);
}
use of org.graylog2.grok.GrokPattern in project graylog2-server by Graylog2.
the class GrokResource method updatePattern.
@PUT
@Timed
@Path("/{patternId}")
@ApiOperation("Update an existing pattern")
@AuditEvent(type = AuditEventTypes.GROK_PATTERN_UPDATE)
public GrokPattern updatePattern(@ApiParam(name = "patternId", required = true) @PathParam("patternId") String patternId, @ApiParam(name = "pattern", required = true) GrokPattern pattern) throws ValidationException {
checkPermission(RestPermissions.INPUTS_EDIT);
final GrokPattern grokPattern = GrokPattern.builder().id(patternId).name(pattern.name()).pattern(pattern.pattern()).build();
return grokPatternService.update(grokPattern);
}
use of org.graylog2.grok.GrokPattern in project graylog2-server by Graylog2.
the class GrokResourceTest method testPatternWithSampleData.
@Test
public void testPatternWithSampleData() throws Exception {
final String sampleData = "1.2.3.4";
final GrokPattern grokPattern = GrokPattern.create("IP", "\\d.\\d.\\d.\\d");
grokPatternService.save(grokPattern);
final GrokPatternTestRequest grokPatternTestRequest = GrokPatternTestRequest.create(grokPattern, sampleData);
final Map<String, Object> expectedReturn = Collections.singletonMap("IP", "1.2.3.4");
final Response response = grokResource.testPattern(grokPatternTestRequest);
assertThat(response.hasEntity()).isTrue();
assertThat(response.getEntity()).isEqualTo(expectedReturn);
}
use of org.graylog2.grok.GrokPattern in project graylog2-server by Graylog2.
the class GrokResourceTest method bulkUpdatePatternsFromTextFileWithCRLF.
@Test
public void bulkUpdatePatternsFromTextFileWithCRLF() throws Exception {
final String patterns = Arrays.stream(GROK_LINES).collect(Collectors.joining("\r\n"));
final ByteArrayInputStream inputStream = new ByteArrayInputStream(patterns.getBytes(StandardCharsets.UTF_8));
final GrokPattern expectedPattern = GrokPattern.create("TEST_PATTERN_0", "Foo");
final Response response = grokResource.bulkUpdatePatternsFromTextFile(inputStream, true, null);
assertThat(response.getStatusInfo()).isEqualTo(Response.Status.ACCEPTED);
assertThat(response.hasEntity()).isFalse();
await().atMost(Duration.FIVE_SECONDS).until(() -> !subscriber.events.isEmpty());
assertThat(subscriber.events).containsOnly(GrokPatternsUpdatedEvent.create(Collections.singleton(expectedPattern.name())));
}
Aggregations