Search in sources :

Example 1 with TransportConfigurationException

use of org.graylog2.plugin.alarms.transports.TransportConfigurationException 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 TransportConfigurationException

use of org.graylog2.plugin.alarms.transports.TransportConfigurationException 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 3 with TransportConfigurationException

use of org.graylog2.plugin.alarms.transports.TransportConfigurationException in project graylog2-server by Graylog2.

the class EmailSender method sendEmail.

private void sendEmail(EmailEventNotificationConfig config, String emailAddress, Map<String, Object> model) throws TransportConfigurationException, EmailException {
    LOG.debug("Sending mail to " + emailAddress);
    if (!emailFactory.isEmailTransportEnabled()) {
        throw new TransportConfigurationException("Email transport is not enabled in server configuration file!");
    }
    final Email email = createEmailWithBody(config, model);
    if (!isNullOrEmpty(config.sender())) {
        email.setFrom(config.sender());
    }
    email.setSubject(buildSubject(config, model));
    email.addTo(emailAddress);
    email.send();
}
Also used : Email(org.apache.commons.mail.Email) HtmlEmail(org.apache.commons.mail.HtmlEmail) SimpleEmail(org.apache.commons.mail.SimpleEmail) TransportConfigurationException(org.graylog2.plugin.alarms.transports.TransportConfigurationException)

Example 4 with TransportConfigurationException

use of org.graylog2.plugin.alarms.transports.TransportConfigurationException in project graylog2-server by Graylog2.

the class StreamAlertResource method sendDummyAlert.

@POST
@Timed
@Path("sendDummyAlert")
@ApiOperation(value = "Send a test mail for a given stream")
@ApiResponses(value = { @ApiResponse(code = 404, message = "Stream not found."), @ApiResponse(code = 400, message = "Invalid ObjectId."), @ApiResponse(code = 400, message = "Stream has no alarm callbacks") })
@NoAuditEvent("only used to test alert emails")
public void sendDummyAlert(@ApiParam(name = "streamId", value = "The stream id the test alert should be sent for.", required = true) @PathParam("streamId") String streamId) throws TransportConfigurationException, EmailException, NotFoundException {
    checkPermission(RestPermissions.STREAMS_EDIT, streamId);
    final Stream stream = streamService.load(streamId);
    final DummyAlertCondition dummyAlertCondition = new DummyAlertCondition(stream, null, Tools.nowUTC(), getSubject().getPrincipal().toString(), Collections.emptyMap(), "Test Alert");
    try {
        AbstractAlertCondition.CheckResult checkResult = dummyAlertCondition.runCheck();
        List<AlarmCallbackConfiguration> callConfigurations = alarmCallbackConfigurationService.getForStream(stream);
        if (callConfigurations.size() == 0) {
            final String message = "Stream has no alarm callbacks, cannot send test alert.";
            LOG.warn(message);
            throw new BadRequestException(message);
        }
        for (AlarmCallbackConfiguration configuration : callConfigurations) {
            AlarmCallback alarmCallback = alarmCallbackFactory.create(configuration);
            alarmCallback.call(stream, checkResult);
        }
    } catch (AlarmCallbackException | ClassNotFoundException | AlarmCallbackConfigurationException e) {
        throw new InternalServerErrorException(e.getMessage(), e);
    }
}
Also used : AlarmCallbackConfigurationException(org.graylog2.plugin.alarms.callbacks.AlarmCallbackConfigurationException) BadRequestException(javax.ws.rs.BadRequestException) InternalServerErrorException(javax.ws.rs.InternalServerErrorException) Stream(org.graylog2.plugin.streams.Stream) AlarmCallback(org.graylog2.plugin.alarms.callbacks.AlarmCallback) AbstractAlertCondition(org.graylog2.alerts.AbstractAlertCondition) DummyAlertCondition(org.graylog2.alerts.types.DummyAlertCondition) AlarmCallbackException(org.graylog2.plugin.alarms.callbacks.AlarmCallbackException) AlarmCallbackConfiguration(org.graylog2.alarmcallbacks.AlarmCallbackConfiguration) Path(javax.ws.rs.Path) POST(javax.ws.rs.POST) Timed(com.codahale.metrics.annotation.Timed) ApiOperation(io.swagger.annotations.ApiOperation) ApiResponses(io.swagger.annotations.ApiResponses) NoAuditEvent(org.graylog2.audit.jersey.NoAuditEvent)

Example 5 with TransportConfigurationException

use of org.graylog2.plugin.alarms.transports.TransportConfigurationException 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)

Aggregations

TransportConfigurationException (org.graylog2.plugin.alarms.transports.TransportConfigurationException)7 Notification (org.graylog2.notifications.Notification)4 Timed (com.codahale.metrics.annotation.Timed)2 ApiOperation (io.swagger.annotations.ApiOperation)2 ApiResponses (io.swagger.annotations.ApiResponses)2 InternalServerErrorException (javax.ws.rs.InternalServerErrorException)2 POST (javax.ws.rs.POST)2 Path (javax.ws.rs.Path)2 Email (org.apache.commons.mail.Email)2 AlarmCallbackConfiguration (org.graylog2.alarmcallbacks.AlarmCallbackConfiguration)2 AbstractAlertCondition (org.graylog2.alerts.AbstractAlertCondition)2 EmailRecipients (org.graylog2.alerts.EmailRecipients)2 DummyAlertCondition (org.graylog2.alerts.types.DummyAlertCondition)2 NoAuditEvent (org.graylog2.audit.jersey.NoAuditEvent)2 AlarmCallback (org.graylog2.plugin.alarms.callbacks.AlarmCallback)2 AlarmCallbackConfigurationException (org.graylog2.plugin.alarms.callbacks.AlarmCallbackConfigurationException)2 AlarmCallbackException (org.graylog2.plugin.alarms.callbacks.AlarmCallbackException)2 Stream (org.graylog2.plugin.streams.Stream)2 BadRequestException (javax.ws.rs.BadRequestException)1 EmailException (org.apache.commons.mail.EmailException)1