Search in sources :

Example 1 with TemporaryEventNotificationException

use of org.graylog.events.notifications.TemporaryEventNotificationException 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());
}
Also used : TransportConfigurationException(org.graylog2.plugin.alarms.transports.TransportConfigurationException) PermanentEventNotificationException(org.graylog.events.notifications.PermanentEventNotificationException) MessageSummary(org.graylog2.plugin.MessageSummary) TemporaryEventNotificationException(org.graylog.events.notifications.TemporaryEventNotificationException) Notification(org.graylog2.notifications.Notification) EventNotification(org.graylog.events.notifications.EventNotification) TransportConfigurationException(org.graylog2.plugin.alarms.transports.TransportConfigurationException) TemporaryEventNotificationException(org.graylog.events.notifications.TemporaryEventNotificationException) PermanentEventNotificationException(org.graylog.events.notifications.PermanentEventNotificationException)

Example 2 with TemporaryEventNotificationException

use of org.graylog.events.notifications.TemporaryEventNotificationException in project graylog2-server by Graylog2.

the class HTTPEventNotification method execute.

@Override
public void execute(EventNotificationContext ctx) throws TemporaryEventNotificationException, PermanentEventNotificationException {
    final HTTPEventNotificationConfig config = (HTTPEventNotificationConfig) ctx.notificationConfig();
    final HttpUrl httpUrl = HttpUrl.parse(config.url());
    if (httpUrl == null) {
        throw new TemporaryEventNotificationException("Malformed URL: <" + config.url() + "> in notification <" + ctx.notificationId() + ">");
    }
    ImmutableList<MessageSummary> backlog = notificationCallbackService.getBacklogForEvent(ctx);
    final EventNotificationModelData model = EventNotificationModelData.of(ctx, backlog);
    if (!whitelistService.isWhitelisted(config.url())) {
        if (!NotificationTestData.TEST_NOTIFICATION_ID.equals(ctx.notificationId())) {
            publishSystemNotificationForWhitelistFailure(config.url(), model.eventDefinitionTitle());
        }
        throw new TemporaryEventNotificationException("URL <" + config.url() + "> is not whitelisted.");
    }
    final byte[] body;
    try {
        body = objectMapper.writeValueAsBytes(model);
    } catch (JsonProcessingException e) {
        throw new PermanentEventNotificationException("Unable to serialize notification", e);
    }
    final Request request = new Request.Builder().url(httpUrl).post(RequestBody.create(CONTENT_TYPE, body)).build();
    LOG.debug("Requesting HTTP endpoint at <{}> in notification <{}>", config.url(), ctx.notificationId());
    try (final Response r = httpClient.newCall(request).execute()) {
        if (!r.isSuccessful()) {
            throw new PermanentEventNotificationException("Expected successful HTTP response [2xx] but got [" + r.code() + "]. " + config.url());
        }
    } catch (IOException e) {
        throw new PermanentEventNotificationException(e.getMessage());
    }
}
Also used : Request(okhttp3.Request) IOException(java.io.IOException) HttpUrl(okhttp3.HttpUrl) Response(okhttp3.Response) EventNotificationModelData(org.graylog.events.notifications.EventNotificationModelData) PermanentEventNotificationException(org.graylog.events.notifications.PermanentEventNotificationException) MessageSummary(org.graylog2.plugin.MessageSummary) TemporaryEventNotificationException(org.graylog.events.notifications.TemporaryEventNotificationException) JsonProcessingException(com.fasterxml.jackson.core.JsonProcessingException)

Aggregations

PermanentEventNotificationException (org.graylog.events.notifications.PermanentEventNotificationException)2 TemporaryEventNotificationException (org.graylog.events.notifications.TemporaryEventNotificationException)2 MessageSummary (org.graylog2.plugin.MessageSummary)2 JsonProcessingException (com.fasterxml.jackson.core.JsonProcessingException)1 IOException (java.io.IOException)1 HttpUrl (okhttp3.HttpUrl)1 Request (okhttp3.Request)1 Response (okhttp3.Response)1 EventNotification (org.graylog.events.notifications.EventNotification)1 EventNotificationModelData (org.graylog.events.notifications.EventNotificationModelData)1 Notification (org.graylog2.notifications.Notification)1 TransportConfigurationException (org.graylog2.plugin.alarms.transports.TransportConfigurationException)1