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);
}
}
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;
}
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;
}
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");
}
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();
}
Aggregations