Search in sources :

Example 36 with Converter

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

the class SyslogPriFacilityConverterTest method testConvert.

@Test
public void testConvert() throws Exception {
    Converter hc = new SyslogPriFacilityConverter(new HashMap<String, Object>());
    assertNull(hc.convert(null));
    assertEquals("", hc.convert(""));
    assertEquals("lol no number", hc.convert("lol no number"));
    // user-level
    assertEquals("user-level", hc.convert("14"));
    // kernel
    assertEquals("kernel", hc.convert("5"));
    // security/authorization
    assertEquals("security/authorization", hc.convert("87"));
}
Also used : Converter(org.graylog2.plugin.inputs.Converter) Test(org.junit.Test)

Example 37 with Converter

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

the class InputServiceImpl method getConvertersOfExtractor.

@SuppressWarnings("unchecked")
private List<Converter> getConvertersOfExtractor(DBObject extractor) {
    final ImmutableList.Builder<Converter> listBuilder = ImmutableList.builder();
    final BasicDBList converters = (BasicDBList) extractor.get(Extractor.FIELD_CONVERTERS);
    for (final Object element : converters) {
        final DBObject c = (BasicDBObject) element;
        try {
            listBuilder.add(ConverterFactory.factory(Converter.Type.valueOf(((String) c.get(Extractor.FIELD_CONVERTER_TYPE)).toUpperCase(Locale.ENGLISH)), (Map<String, Object>) c.get(Extractor.FIELD_CONVERTER_CONFIG)));
        } catch (ConverterFactory.NoSuchConverterException e1) {
            LOG.error("Cannot build converter from persisted data. No such converter.", e1);
        } catch (Exception e) {
            LOG.error("Cannot build converter from persisted data.", e);
        }
    }
    return listBuilder.build();
}
Also used : BasicDBList(com.mongodb.BasicDBList) BasicDBObject(com.mongodb.BasicDBObject) ImmutableList(com.google.common.collect.ImmutableList) Converter(org.graylog2.plugin.inputs.Converter) DBObject(com.mongodb.DBObject) BasicDBObject(com.mongodb.BasicDBObject) ConverterFactory(org.graylog2.inputs.converters.ConverterFactory) DBObject(com.mongodb.DBObject) BasicDBObject(com.mongodb.BasicDBObject) HashMap(java.util.HashMap) Map(java.util.Map) ImmutableMap(com.google.common.collect.ImmutableMap) NoSuchInputTypeException(org.graylog2.shared.inputs.NoSuchInputTypeException) NotFoundException(org.graylog2.database.NotFoundException) ValidationException(org.graylog2.plugin.database.ValidationException)

Example 38 with Converter

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

the class ExtractorTest method testConvertersWithMultipleFields.

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

        @Nullable
        @Override
        public Object apply(Object input) {
            return ImmutableMap.builder().put("one", 1).put("two", "2").put("message", // Try to overwrite reserved field.
            "message should not be overwritten").build();
        }
    }).build();
    final TestExtractor extractor = new TestExtractor.Builder().converters(Lists.newArrayList(converter)).callback(new Callable<Result[]>() {

        @Override
        public Result[] call() throws Exception {
            return new Result[] { new Result("1", -1, -1) };
        }
    }).build();
    final Message msg = createMessage("the message");
    extractor.runExtractor(msg);
    // With a "multiple fields" converter the target field is not touched, only the additional fields are added.
    assertThat(msg.getField("target")).isEqualTo("1");
    assertThat(msg.getField("one")).isEqualTo(1);
    assertThat(msg.getField("two")).isEqualTo("2");
    // Reserved fields are not overwritten!
    assertThat(msg.getField("message")).isEqualTo("the message");
    // Attempts to overwrite a reserved field are recorded as converter exception.
    assertThat(extractor.getConverterExceptionCount()).isEqualTo(1);
}
Also used : Function(com.google.common.base.Function) Message(org.graylog2.plugin.Message) Callable(java.util.concurrent.Callable) Result(org.graylog2.plugin.inputs.Extractor.Result) Test(org.junit.Test)

Example 39 with Converter

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

the class ExtractorTest method testConvertersWithExceptions.

@Test
public void testConvertersWithExceptions() throws Exception {
    final Converter converter1 = new TestConverter.Builder().callback(new Function<Object, Object>() {

        @Nullable
        @Override
        public Object apply(Object input) {
            throw new NullPointerException("EEK");
        }
    }).build();
    final Converter converter2 = new TestConverter.Builder().callback(new Function<Object, Object>() {

        @Nullable
        @Override
        public Object apply(Object input) {
            return input + "2";
        }
    }).build();
    final Converter converter3 = new TestConverter.Builder().callback(new Function<Object, Object>() {

        @Nullable
        @Override
        public Object apply(Object input) {
            throw new NullPointerException("EEK");
        }
    }).build();
    final TestExtractor extractor = new TestExtractor.Builder().converters(Lists.newArrayList(converter1, converter2, converter3)).callback(new Callable<Result[]>() {

        @Override
        public Result[] call() throws Exception {
            return new Result[] { new Result("converter", -1, -1) };
        }
    }).build();
    final Message msg = createMessage("message");
    extractor.runExtractor(msg);
    // The two exceptions should have been recorded.
    assertThat(extractor.getConverterExceptionCount()).isEqualTo(2);
    // It ignores all converters which throw an exception but executes the ones that don't.
    // TODO: Is this really the expected behaviour? The converters are executed in order and basically depend on the output of the previous. This might not work for all converters.
    assertThat(msg.getField("target")).isEqualTo("converter2");
}
Also used : Function(com.google.common.base.Function) Message(org.graylog2.plugin.Message) Callable(java.util.concurrent.Callable) Result(org.graylog2.plugin.inputs.Extractor.Result) Test(org.junit.Test)

Example 40 with Converter

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

the class ExtractorTest method testMultipleConvertersWithFirstReturningNullValue.

@Test
public void testMultipleConvertersWithFirstReturningNullValue() throws Exception {
    final Converter converter1 = new TestConverter.Builder().callback(new Function<Object, Object>() {

        @Nullable
        @Override
        public Object apply(Object input) {
            return null;
        }
    }).build();
    final Converter converter2 = new TestConverter.Builder().callback(new Function<Object, Object>() {

        @Nullable
        @Override
        public Object apply(Object input) {
            return input + "2";
        }
    }).build();
    final TestExtractor extractor = new TestExtractor.Builder().converters(Lists.newArrayList(converter1, converter2)).callback(new Callable<Result[]>() {

        @Override
        public Result[] call() throws Exception {
            return new Result[] { new Result("converter", -1, -1) };
        }
    }).build();
    final Message msg = createMessage("message");
    extractor.runExtractor(msg);
    // If the first converter returns null, the second will not be executed because the value is not a string anymore.
    assertThat(msg.getField("target")).isNull();
}
Also used : Function(com.google.common.base.Function) 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)37 Converter (org.graylog2.plugin.inputs.Converter)22 Message (org.graylog2.plugin.Message)14 DateTime (org.joda.time.DateTime)10 Function (com.google.common.base.Function)8 Result (org.graylog2.plugin.inputs.Extractor.Result)8 Callable (java.util.concurrent.Callable)7 MetricRegistry (com.codahale.metrics.MetricRegistry)6 Extractor (org.graylog2.plugin.inputs.Extractor)6 BadRequestException (javax.ws.rs.BadRequestException)2 ConfigurationException (org.graylog2.ConfigurationException)2 ImmutableList (com.google.common.collect.ImmutableList)1 ImmutableMap (com.google.common.collect.ImmutableMap)1 BasicDBList (com.mongodb.BasicDBList)1 BasicDBObject (com.mongodb.BasicDBObject)1 DBObject (com.mongodb.DBObject)1 HashMap (java.util.HashMap)1 Map (java.util.Map)1 NotFoundException (org.graylog2.database.NotFoundException)1 ConverterFactory (org.graylog2.inputs.converters.ConverterFactory)1