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