use of org.graylog2.plugin.MessageSummary in project graylog2-server by Graylog2.
the class EmailEventNotification method execute.
@Override
public void execute(EventNotificationContext ctx) throws TemporaryEventNotificationException, PermanentEventNotificationException {
final EmailEventNotificationConfig config = (EmailEventNotificationConfig) ctx.notificationConfig();
try {
ImmutableList<MessageSummary> backlog = notificationCallbackService.getBacklogForEvent(ctx);
emailSender.sendEmails(config, ctx, backlog);
} catch (EmailSender.ConfigurationError e) {
throw new TemporaryEventNotificationException(e.getMessage());
} catch (TransportConfigurationException e) {
Notification systemNotification = notificationService.buildNow().addNode(nodeId.toString()).addType(Notification.Type.EMAIL_TRANSPORT_CONFIGURATION_INVALID).addSeverity(Notification.Severity.NORMAL).addDetail("exception", e.getMessage());
notificationService.publishIfFirst(systemNotification);
throw new TemporaryEventNotificationException("Notification has email recipients and is triggered, but email transport is not configured. " + e.getMessage());
} catch (Exception e) {
String exceptionDetail = e.toString();
if (e.getCause() != null) {
exceptionDetail += " (" + e.getCause() + ")";
}
final Notification systemNotification = notificationService.buildNow().addNode(nodeId.toString()).addType(Notification.Type.EMAIL_TRANSPORT_FAILED).addSeverity(Notification.Severity.NORMAL).addDetail("exception", exceptionDetail);
notificationService.publishIfFirst(systemNotification);
throw new PermanentEventNotificationException("Notification has email recipients and is triggered, but sending emails failed. " + e.getMessage());
}
LOG.debug("Sending email to addresses <{}> and users <{}> using notification <{}>", Strings.join(config.emailRecipients(), ','), Strings.join(config.userRecipients(), ','), ctx.notificationId());
}
use of org.graylog2.plugin.MessageSummary in project graylog2-server by Graylog2.
the class LegacyAlarmCallbackSender method send.
public void send(LegacyAlarmCallbackEventNotificationConfig config, EventDefinition eventDefinition, EventDto event, List<MessageSummary> backlog) throws Exception {
final String callbackType = config.callbackType();
final Stream stream = findStream(eventDefinition.config());
final AbstractAlertCondition alertCondition = new LegacyAlertCondition(stream, eventDefinition, event);
final AbstractAlertCondition.CheckResult checkResult = new AbstractAlertCondition.CheckResult(true, alertCondition, event.message(), event.processingTimestamp(), backlog);
try {
final AlarmCallback callback = alarmCallbackFactory.create(callbackType, config.configuration());
callback.checkConfiguration();
callback.call(stream, checkResult);
} catch (ClassNotFoundException e) {
LOG.error("Couldn't find implementation class for type <{}>", callbackType);
throw e;
} catch (AlarmCallbackConfigurationException e) {
LOG.error("Invalid legacy alarm callback configuration", e);
throw e;
} catch (ConfigurationException e) {
LOG.error("Invalid configuration for legacy alarm callback <{}>", callbackType, e);
throw e;
} catch (AlarmCallbackException e) {
LOG.error("Couldn't execute legacy alarm callback <{}>", callbackType, e);
throw e;
}
}
use of org.graylog2.plugin.MessageSummary in project graylog2-server by Graylog2.
the class HTTPAlarmCallbackTest method callSucceedsIfRemoteRequestSucceeds.
@Test
public void callSucceedsIfRemoteRequestSucceeds() throws Exception {
when(whitelistService.isWhitelisted(anyString())).thenReturn(true);
server.enqueue(new MockResponse().setResponseCode(200));
server.start();
final Configuration configuration = new Configuration(ImmutableMap.of("url", server.url("/").toString()));
alarmCallback.initialize(configuration);
alarmCallback.checkConfiguration();
final Stream stream = new StreamMock(ImmutableMap.of("_id", "stream-id", "title", "Stream Title", "description", "Stream Description"), ImmutableList.of());
final AlertCondition alertCondition = new DummyAlertCondition(stream, "condition-id", new DateTime(2016, 9, 6, 17, 0, DateTimeZone.UTC), "user", ImmutableMap.of(), "Alert Condition Title");
final List<MessageSummary> messageSummaries = ImmutableList.of(new MessageSummary("graylog_1", new Message("Test message 1", "source1", new DateTime(2016, 9, 6, 17, 0, DateTimeZone.UTC))), new MessageSummary("graylog_2", new Message("Test message 2", "source2", new DateTime(2016, 9, 6, 17, 0, DateTimeZone.UTC))));
final AlertCondition.CheckResult checkResult = new AbstractAlertCondition.CheckResult(true, alertCondition, "Result Description", new DateTime(2016, 9, 6, 17, 0, DateTimeZone.UTC), messageSummaries);
alarmCallback.call(stream, checkResult);
final RecordedRequest request = server.takeRequest();
assertThat(request.getPath()).isEqualTo("/");
assertThat(request.getHeader("Content-Type")).isEqualTo("application/json");
assertThat(request.getBodySize()).isPositive();
final String requestBody = request.getBody().readUtf8();
final JsonNode jsonNode = objectMapper.readTree(requestBody);
assertThat(jsonNode.get("check_result").get("matching_messages").size()).isEqualTo(2);
assertThat(jsonNode.get("check_result").get("triggered").asBoolean()).isTrue();
assertThat(jsonNode.get("check_result").get("triggered_at").asText()).isEqualTo("2016-09-06T17:00:00.000Z");
assertThat(jsonNode.get("stream").get("id").asText()).isEqualTo("stream-id");
}
use of org.graylog2.plugin.MessageSummary in project graylog2-server by Graylog2.
the class HTTPAlarmCallbackTest method callThrowsAlarmCallbackExceptionIfRequestBodyCanNotBeBuilt.
@Test
public void callThrowsAlarmCallbackExceptionIfRequestBodyCanNotBeBuilt() throws Exception {
final Configuration configuration = new Configuration(ImmutableMap.of("url", "http://example.org"));
alarmCallback.initialize(configuration);
final Stream stream = mock(Stream.class);
final AlertCondition alertCondition = mock(AlertCondition.class);
final List<MessageSummary> messageSummaries = ImmutableList.of();
final AlertCondition.CheckResult checkResult = new AbstractAlertCondition.CheckResult(true, alertCondition, "Result Description", new DateTime(2016, 9, 6, 17, 0, DateTimeZone.UTC), messageSummaries) {
@Override
public String getResultDescription() {
throw new RuntimeException("Boom");
}
};
expectedException.expect(AlarmCallbackException.class);
expectedException.expectMessage("Unable to serialize alarm");
alarmCallback.call(stream, checkResult);
}
use of org.graylog2.plugin.MessageSummary in project graylog2-server by Graylog2.
the class EmailAlarmCallback method getAlarmBacklog.
protected List<Message> getAlarmBacklog(AlertCondition.CheckResult result) {
final AlertCondition alertCondition = result.getTriggeredCondition();
final List<MessageSummary> matchingMessages = result.getMatchingMessages();
final int effectiveBacklogSize = Math.min(alertCondition.getBacklog(), matchingMessages.size());
if (effectiveBacklogSize == 0) {
return Collections.emptyList();
}
final List<MessageSummary> backlogSummaries = matchingMessages.subList(0, effectiveBacklogSize);
final List<Message> backlog = Lists.newArrayListWithCapacity(effectiveBacklogSize);
for (MessageSummary messageSummary : backlogSummaries) {
backlog.add(messageSummary.getRawMessage());
}
return backlog;
}
Aggregations