Search in sources :

Example 36 with Configuration

use of org.graylog2.Configuration in project graylog2-server by Graylog2.

the class BundleImporter method createMessageInput.

private MessageInput createMessageInput(final String bundleId, final Input inputDescription, final String userName) throws NoSuchInputTypeException, ConfigurationException, ValidationException, NotFoundException, org.graylog2.ConfigurationException, ExtractorFactory.NoSuchExtractorException, org.graylog2.plugin.inputs.Extractor.ReservedFieldException {
    final Configuration inputConfig = new Configuration(inputDescription.getConfiguration());
    final DateTime createdAt = Tools.nowUTC();
    final MessageInput messageInput = messageInputFactory.create(inputDescription.getType(), inputConfig);
    messageInput.setTitle(inputDescription.getTitle());
    messageInput.setGlobal(inputDescription.isGlobal());
    messageInput.setCreatorUserId(userName);
    messageInput.setCreatedAt(createdAt);
    messageInput.setContentPack(bundleId);
    messageInput.checkConfiguration();
    // Don't run if exclusive and another instance is already running.
    if (messageInput.isExclusive() && inputRegistry.hasTypeRunning(messageInput.getClass())) {
        LOG.error("Input type <{}> of input <{}> is exclusive and already has input running.", messageInput.getClass(), messageInput.getTitle());
    }
    final String id = inputDescription.getId();
    final org.graylog2.inputs.Input mongoInput;
    if (id == null) {
        mongoInput = inputService.create(buildMongoDbInput(inputDescription, userName, createdAt, bundleId));
    } else {
        mongoInput = inputService.create(id, buildMongoDbInput(inputDescription, userName, createdAt, bundleId));
    }
    // Persist input.
    final String persistId = inputService.save(mongoInput);
    messageInput.setPersistId(persistId);
    messageInput.initialize();
    addStaticFields(messageInput, inputDescription.getStaticFields());
    addExtractors(messageInput, inputDescription.getExtractors(), userName);
    return messageInput;
}
Also used : Configuration(org.graylog2.plugin.configuration.Configuration) MessageInput(org.graylog2.plugin.inputs.MessageInput) DateTime(org.joda.time.DateTime)

Example 37 with Configuration

use of org.graylog2.Configuration in project graylog2-server by Graylog2.

the class FormattedEmailAlertSender method sendEmails.

@Override
public void sendEmails(Stream stream, EmailRecipients recipients, AlertCondition.CheckResult checkResult, List<Message> backlog) throws TransportConfigurationException, EmailException {
    if (!configuration.isEnabled()) {
        throw new TransportConfigurationException("Email transport is not enabled in server configuration file!");
    }
    if (recipients == null || recipients.isEmpty()) {
        throw new RuntimeException("Cannot send emails: empty recipient list.");
    }
    final Set<String> recipientsSet = recipients.getEmailRecipients();
    if (recipientsSet.size() == 0) {
        final Notification notification = notificationService.buildNow().addNode(nodeId.toString()).addType(Notification.Type.GENERIC).addSeverity(Notification.Severity.NORMAL).addDetail("title", "Stream \"" + stream.getTitle() + "\" is alerted, but no recipients have been defined!").addDetail("description", "To fix this, go to the alerting configuration of the stream and add at least one alert recipient.");
        notificationService.publishIfFirst(notification);
    }
    for (String email : recipientsSet) {
        sendEmail(email, stream, checkResult, backlog);
    }
}
Also used : TransportConfigurationException(org.graylog2.plugin.alarms.transports.TransportConfigurationException) Notification(org.graylog2.notifications.Notification)

Example 38 with Configuration

use of org.graylog2.Configuration in project graylog2-server by Graylog2.

the class EsNodeProviderTest method setupConfig.

private ElasticsearchConfiguration setupConfig(Map<String, String> settings) {
    // required params we don't care about in this test, so we set them to dummy values for all test cases
    settings.put("retention_strategy", "delete");
    ElasticsearchConfiguration configuration = new ElasticsearchConfiguration();
    try {
        new JadConfig(new InMemoryRepository(settings), configuration).process();
    } catch (ValidationException | RepositoryException e) {
        fail(e.getMessage());
    }
    return configuration;
}
Also used : JadConfig(com.github.joschi.jadconfig.JadConfig) InMemoryRepository(com.github.joschi.jadconfig.repositories.InMemoryRepository) ValidationException(com.github.joschi.jadconfig.ValidationException) ElasticsearchConfiguration(org.graylog2.configuration.ElasticsearchConfiguration) RepositoryException(com.github.joschi.jadconfig.RepositoryException)

Example 39 with Configuration

use of org.graylog2.Configuration in project graylog2-server by Graylog2.

the class ConfigurationMapConverter method convertValues.

/**
     * Converts the values in the map to the requested types. This has been copied from the Graylog web interface
     * and should be removed once we have better configuration objects.
     */
public static Map<String, Object> convertValues(final Map<String, Object> data, final ConfigurationRequest configurationRequest) throws ValidationException {
    final Map<String, Object> configuration = Maps.newHashMapWithExpectedSize(data.size());
    final Map<String, Map<String, Object>> configurationFields = configurationRequest.asList();
    for (final Map.Entry<String, Object> entry : data.entrySet()) {
        final String field = entry.getKey();
        final Map<String, Object> fieldDescription = configurationFields.get(field);
        if (fieldDescription == null || fieldDescription.isEmpty()) {
            throw new ValidationException(field, "Unknown configuration field description for field \"" + field + "\"");
        }
        final String type = (String) fieldDescription.get("type");
        // Decide what to cast to. (string, bool, number)
        Object value;
        switch(type) {
            case "text":
            case "dropdown":
                value = entry.getValue() == null ? "" : String.valueOf(entry.getValue());
                break;
            case "number":
                try {
                    value = Integer.parseInt(String.valueOf(entry.getValue()));
                } catch (NumberFormatException e) {
                    // If a numeric field is optional and not provided, use null as value
                    if ("true".equals(String.valueOf(fieldDescription.get("is_optional")))) {
                        value = null;
                    } else {
                        throw new ValidationException(field, e.getMessage());
                    }
                }
                break;
            case "boolean":
                value = "true".equalsIgnoreCase(String.valueOf(entry.getValue()));
                break;
            case "list":
                final List<?> valueList = entry.getValue() == null ? Collections.emptyList() : (List<?>) entry.getValue();
                value = valueList.stream().filter(o -> o != null && o instanceof String).map(String::valueOf).collect(Collectors.toList());
                break;
            default:
                throw new ValidationException(field, "Unknown configuration field type \"" + type + "\"");
        }
        configuration.put(field, value);
    }
    return configuration;
}
Also used : ConfigurationRequest(org.graylog2.plugin.configuration.ConfigurationRequest) List(java.util.List) ValidationException(org.graylog2.plugin.database.ValidationException) Map(java.util.Map) Maps(com.google.common.collect.Maps) Collections(java.util.Collections) Collectors(java.util.stream.Collectors) ValidationException(org.graylog2.plugin.database.ValidationException) Map(java.util.Map)

Example 40 with Configuration

use of org.graylog2.Configuration in project graylog2-server by Graylog2.

the class FormattedEmailAlertSenderTest method buildBodyUsesCustomBody.

@Test
public void buildBodyUsesCustomBody() throws Exception {
    Configuration pluginConfig = new Configuration(Collections.<String, Object>singletonMap("body", "Test: ${stream.id}"));
    emailAlertSender.initialize(pluginConfig);
    Stream stream = mock(Stream.class);
    when(stream.getId()).thenReturn("123456");
    AlertCondition.CheckResult checkResult = mock(AbstractAlertCondition.CheckResult.class);
    String body = emailAlertSender.buildBody(stream, checkResult, Collections.<Message>emptyList());
    assertThat(body).isEqualTo("Test: 123456");
}
Also used : Configuration(org.graylog2.plugin.configuration.Configuration) EmailConfiguration(org.graylog2.configuration.EmailConfiguration) AlertCondition(org.graylog2.plugin.alarms.AlertCondition) Stream(org.graylog2.plugin.streams.Stream) Test(org.junit.Test)

Aggregations

Test (org.junit.Test)36 Configuration (org.graylog2.plugin.configuration.Configuration)29 ApiOperation (io.swagger.annotations.ApiOperation)24 Timed (com.codahale.metrics.annotation.Timed)23 BadRequestException (javax.ws.rs.BadRequestException)19 Path (javax.ws.rs.Path)18 AuditEvent (org.graylog2.audit.jersey.AuditEvent)17 Consumes (javax.ws.rs.Consumes)13 AlertCondition (org.graylog2.plugin.alarms.AlertCondition)13 MessageInput (org.graylog2.plugin.inputs.MessageInput)13 Stream (org.graylog2.plugin.streams.Stream)13 ApiResponses (io.swagger.annotations.ApiResponses)12 PUT (javax.ws.rs.PUT)11 ValidationException (org.graylog2.plugin.database.ValidationException)11 DateTime (org.joda.time.DateTime)11 Produces (javax.ws.rs.Produces)10 Configuration (org.apache.commons.configuration2.Configuration)10 Configuration (org.graylog2.Configuration)10 POST (javax.ws.rs.POST)9 EmailConfiguration (org.graylog2.configuration.EmailConfiguration)9