Search in sources :

Example 36 with Configuration

use of org.graylog2.plugin.configuration.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 37 with Configuration

use of org.graylog2.plugin.configuration.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 38 with Configuration

use of org.graylog2.plugin.configuration.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 39 with Configuration

use of org.graylog2.plugin.configuration.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)

Example 40 with Configuration

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

the class FieldContentValueAlertConditionTest method testCorrectUsageOfRelativeRange.

@Test
public void testCorrectUsageOfRelativeRange() throws Exception {
    final Stream stream = mock(Stream.class);
    final Searches searches = mock(Searches.class);
    final Configuration configuration = mock(Configuration.class);
    final SearchResult searchResult = mock(SearchResult.class);
    final int alertCheckInterval = 42;
    final RelativeRange relativeRange = RelativeRange.create(alertCheckInterval);
    when(configuration.getAlertCheckInterval()).thenReturn(alertCheckInterval);
    when(searches.search(anyString(), anyString(), eq(relativeRange), anyInt(), anyInt(), any(Sorting.class))).thenReturn(searchResult);
    final FieldContentValueAlertCondition alertCondition = new FieldContentValueAlertCondition(searches, configuration, stream, null, DateTime.now(DateTimeZone.UTC), "mockuser", ImmutableMap.<String, Object>of("field", "test", "value", "test"), "Field Content Value Test COndition");
    final AbstractAlertCondition.CheckResult result = alertCondition.runCheck();
}
Also used : Searches(org.graylog2.indexer.searches.Searches) Configuration(org.graylog2.Configuration) RelativeRange(org.graylog2.plugin.indexer.searches.timeranges.RelativeRange) Stream(org.graylog2.plugin.streams.Stream) SearchResult(org.graylog2.indexer.results.SearchResult) AbstractAlertCondition(org.graylog2.alerts.AbstractAlertCondition) Sorting(org.graylog2.indexer.searches.Sorting) Test(org.junit.Test) AlertConditionTest(org.graylog2.alerts.AlertConditionTest)

Aggregations

Test (org.junit.Test)34 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.graylog2.Configuration)10 POST (javax.ws.rs.POST)9 EmailConfiguration (org.graylog2.configuration.EmailConfiguration)9 URI (java.net.URI)8