use of org.graylog2.plugin.alarms.callbacks.AlarmCallbackException 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);
}
}
use of org.graylog2.plugin.alarms.callbacks.AlarmCallbackException 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);
}
}
use of org.graylog2.plugin.alarms.callbacks.AlarmCallbackException in project graylog2-server by Graylog2.
the class HTTPAlarmCallback method call.
@Override
public void call(final Stream stream, final AlertCondition.CheckResult result) throws AlarmCallbackException {
final Map<String, Object> event = Maps.newHashMap();
event.put("stream", stream);
event.put("check_result", result);
final byte[] body;
try {
body = objectMapper.writeValueAsBytes(event);
} catch (JsonProcessingException e) {
throw new AlarmCallbackException("Unable to serialize alarm", e);
}
final String url = configuration.getString(CK_URL);
final HttpUrl httpUrl = HttpUrl.parse(url);
if (httpUrl == null) {
throw new AlarmCallbackException("Malformed URL: " + url);
}
final Request request = new Request.Builder().url(httpUrl).post(RequestBody.create(CONTENT_TYPE, body)).build();
try (final Response r = httpClient.newCall(request).execute()) {
if (!r.isSuccessful()) {
throw new AlarmCallbackException("Expected successful HTTP response [2xx] but got [" + r.code() + "].");
}
} catch (IOException e) {
throw new AlarmCallbackException(e.getMessage(), e);
}
}
Aggregations