Search in sources :

Example 1 with EmailRecipients

use of org.graylog2.alerts.EmailRecipients 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 2 with EmailRecipients

use of org.graylog2.alerts.EmailRecipients in project graylog2-server by Graylog2.

the class EmailAlarmCallback method call.

@Override
public void call(Stream stream, AlertCondition.CheckResult result) throws AlarmCallbackException {
    // Send alerts.
    final EmailRecipients emailRecipients = this.getEmailRecipients();
    if (emailRecipients.isEmpty()) {
        if (!emailConfiguration.isEnabled()) {
            throw new AlarmCallbackException("Email transport is not enabled in server configuration file!");
        }
        LOG.info("Alarm callback has no email recipients, not sending any emails.");
        return;
    }
    AlertCondition alertCondition = result.getTriggeredCondition();
    try {
        if (alertCondition.getBacklog() > 0 && result.getMatchingMessages() != null) {
            alertSender.sendEmails(stream, emailRecipients, result, getAlarmBacklog(result));
        } else {
            alertSender.sendEmails(stream, emailRecipients, result);
        }
    } catch (TransportConfigurationException e) {
        LOG.warn("Alarm callback has email recipients and is triggered, but email transport is not configured.");
        Notification notification = notificationService.buildNow().addNode(nodeId.toString()).addType(Notification.Type.EMAIL_TRANSPORT_CONFIGURATION_INVALID).addSeverity(Notification.Severity.NORMAL).addDetail("stream_id", stream.getId()).addDetail("exception", e.getMessage());
        notificationService.publishIfFirst(notification);
        throw new AlarmCallbackException(e.getMessage(), e);
    } catch (Exception e) {
        LOG.error("Alarm callback has email recipients and is triggered, but sending emails failed", e);
        String exceptionDetail = e.toString();
        if (e.getCause() != null) {
            exceptionDetail += " (" + e.getCause() + ")";
        }
        Notification notification = notificationService.buildNow().addNode(nodeId.toString()).addType(Notification.Type.EMAIL_TRANSPORT_FAILED).addSeverity(Notification.Severity.NORMAL).addDetail("stream_id", stream.getId()).addDetail("exception", exceptionDetail);
        notificationService.publishIfFirst(notification);
        throw new AlarmCallbackException(e.getMessage(), e);
    }
}
Also used : TransportConfigurationException(org.graylog2.plugin.alarms.transports.TransportConfigurationException) EmailRecipients(org.graylog2.alerts.EmailRecipients) AlertCondition(org.graylog2.plugin.alarms.AlertCondition) AlarmCallbackException(org.graylog2.plugin.alarms.callbacks.AlarmCallbackException) Notification(org.graylog2.notifications.Notification) TransportConfigurationException(org.graylog2.plugin.alarms.transports.TransportConfigurationException) AlarmCallbackConfigurationException(org.graylog2.plugin.alarms.callbacks.AlarmCallbackConfigurationException) ConfigurationException(org.graylog2.plugin.configuration.ConfigurationException) AlarmCallbackException(org.graylog2.plugin.alarms.callbacks.AlarmCallbackException)

Example 3 with EmailRecipients

use of org.graylog2.alerts.EmailRecipients in project graylog2-server by Graylog2.

the class EmailSender method sendEmails.

// TODO: move EmailRecipients class to events code
void sendEmails(EmailEventNotificationConfig notificationConfig, EventNotificationContext ctx, ImmutableList<MessageSummary> backlog) throws TransportConfigurationException, EmailException, ConfigurationError {
    if (!emailFactory.isEmailTransportEnabled()) {
        throw new TransportConfigurationException("Email transport is not enabled in server configuration file!");
    }
    final EmailRecipients emailRecipients = emailRecipientsFactory.create(new ArrayList<>(notificationConfig.userRecipients()), new ArrayList<>(notificationConfig.emailRecipients()));
    if (emailRecipients.isEmpty()) {
        LOG.debug("Cannot send emails: empty recipient list.");
        return;
    }
    final Set<String> recipientsSet = emailRecipients.getEmailRecipients();
    if (recipientsSet.size() == 0) {
        final Notification notification = notificationService.buildNow().addNode(nodeId.toString()).addType(Notification.Type.GENERIC).addSeverity(Notification.Severity.NORMAL).addDetail("title", "No recipients have been defined!").addDetail("description", "To fix this, go to the notification configuration and add at least one alert recipient.");
        notificationService.publishIfFirst(notification);
    }
    final Map<String, Object> model = getModel(ctx, backlog, notificationConfig.timeZone());
    for (String email : recipientsSet) {
        sendEmail(notificationConfig, email, model);
    }
}
Also used : TransportConfigurationException(org.graylog2.plugin.alarms.transports.TransportConfigurationException) EmailRecipients(org.graylog2.alerts.EmailRecipients) Notification(org.graylog2.notifications.Notification)

Aggregations

Notification (org.graylog2.notifications.Notification)3 TransportConfigurationException (org.graylog2.plugin.alarms.transports.TransportConfigurationException)3 EmailRecipients (org.graylog2.alerts.EmailRecipients)2 AlertCondition (org.graylog2.plugin.alarms.AlertCondition)1 AlarmCallbackConfigurationException (org.graylog2.plugin.alarms.callbacks.AlarmCallbackConfigurationException)1 AlarmCallbackException (org.graylog2.plugin.alarms.callbacks.AlarmCallbackException)1 ConfigurationException (org.graylog2.plugin.configuration.ConfigurationException)1