use of org.graylog2.plugin.alarms.transports.TransportConfigurationException in project graylog2-server by Graylog2.
the class FormattedEmailAlertSender method sendEmail.
private void sendEmail(String emailAddress, Stream stream, AlertCondition.CheckResult checkResult, List<Message> backlog) throws TransportConfigurationException, EmailException {
LOG.debug("Sending mail to " + emailAddress);
if (!configuration.isEnabled()) {
throw new TransportConfigurationException("Email transport is not enabled in server configuration file!");
}
final Email email = emailFactory.simpleEmail();
if (pluginConfig != null && !isNullOrEmpty(pluginConfig.getString("sender"))) {
email.setFrom(pluginConfig.getString("sender"));
}
email.setSubject(buildSubject(stream, checkResult, backlog));
email.setMsg(buildBody(stream, checkResult, backlog));
email.addTo(emailAddress);
email.send();
}
use of org.graylog2.plugin.alarms.transports.TransportConfigurationException in project graylog2-server by Graylog2.
the class AlarmCallbacksResource method test.
@POST
@Timed
@Path("/{alarmCallbackId}/test")
@ApiOperation(value = "Send a test alert for a given alarm callback")
@ApiResponses(value = { @ApiResponse(code = 404, message = "Alarm callback not found."), @ApiResponse(code = 400, message = "Invalid ObjectId."), @ApiResponse(code = 500, message = "Error while testing alarm callback") })
@NoAuditEvent("only used to test alert notifications")
public Response test(@ApiParam(name = "alarmCallbackId", value = "The alarm callback id to send a test alert for.", required = true) @PathParam("alarmCallbackId") String alarmCallbackId) throws TransportConfigurationException, EmailException, NotFoundException {
final AlarmCallbackConfiguration alarmCallbackConfiguration = alarmCallbackConfigurationService.load(alarmCallbackId);
final String streamId = alarmCallbackConfiguration.getStreamId();
checkPermission(RestPermissions.STREAMS_EDIT, streamId);
final Stream stream = streamService.load(streamId);
final DummyAlertCondition testAlertCondition = new DummyAlertCondition(stream, null, Tools.nowUTC(), getSubject().getPrincipal().toString(), Collections.emptyMap(), "Test Alert");
try {
AbstractAlertCondition.CheckResult checkResult = testAlertCondition.runCheck();
AlarmCallback alarmCallback = alarmCallbackFactory.create(alarmCallbackConfiguration);
alarmCallback.call(stream, checkResult);
} catch (Exception e) {
throw new InternalServerErrorException(e.getMessage(), e);
}
return Response.ok().build();
}
use of org.graylog2.plugin.alarms.transports.TransportConfigurationException 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);
}
}
Aggregations