use of com.codahale.metrics.annotation.Timed in project graylog2-server by Graylog2.
the class StreamAlertConditionResource method delete.
@DELETE
@Timed
@Path("{conditionId}")
@ApiOperation(value = "Delete an alert condition")
@ApiResponses(value = { @ApiResponse(code = 404, message = "Stream not found."), @ApiResponse(code = 400, message = "Invalid ObjectId.") })
@AuditEvent(type = AuditEventTypes.ALERT_CONDITION_DELETE)
public void delete(@ApiParam(name = "streamId", value = "The stream id this alert condition belongs to.", required = true) @PathParam("streamId") String streamid, @ApiParam(name = "conditionId", value = "The alert condition id to be deleted", required = true) @PathParam("conditionId") String conditionId) throws NotFoundException {
checkPermission(RestPermissions.STREAMS_READ, streamid);
final Stream stream = streamService.load(streamid);
streamService.removeAlertCondition(stream, conditionId);
}
use of com.codahale.metrics.annotation.Timed in project graylog2-server by Graylog2.
the class StreamAlertResource method removeReceiver.
@DELETE
@Timed
@Path("receivers")
@ApiOperation(value = "Remove an alert receiver")
@ApiResponses(value = { @ApiResponse(code = 404, message = "Stream not found."), @ApiResponse(code = 400, message = "Invalid ObjectId."), @ApiResponse(code = 400, message = "Stream has no email alarm callbacks.") })
@AuditEvent(type = AuditEventTypes.ALERT_RECEIVER_DELETE)
@Deprecated
public void removeReceiver(@ApiParam(name = "streamId", value = "The stream id this new alert condition belongs to.", required = true) @PathParam("streamId") String streamId, @ApiParam(name = "entity", value = "Name/ID of user or email address to remove from alert receivers.", required = true) @QueryParam("entity") String entity, @ApiParam(name = "type", value = "Type: users or emails", required = true) @QueryParam("type") String type) throws NotFoundException {
checkPermission(RestPermissions.STREAMS_EDIT, streamId);
if (!"users".equals(type) && !"emails".equals(type)) {
final String msg = "No such type: [" + type + "]";
LOG.warn(msg);
throw new BadRequestException(msg);
}
final Stream stream = streamService.load(streamId);
streamService.removeAlertReceiver(stream, type, entity);
}
use of com.codahale.metrics.annotation.Timed in project graylog2-server by Graylog2.
the class StreamAlertResource method checkConditions.
@GET
@Timed
@Path("check")
@ApiOperation(value = "Check for triggered alert conditions of this streams. Results cached for " + REST_CHECK_CACHE_SECONDS + " seconds.")
@Produces(MediaType.APPLICATION_JSON)
@ApiResponses(value = { @ApiResponse(code = 404, message = "Stream not found."), @ApiResponse(code = 400, message = "Invalid ObjectId.") })
public Map<String, Object> checkConditions(@ApiParam(name = "streamId", value = "The ID of the stream to check.", required = true) @PathParam("streamId") String streamId) throws NotFoundException {
checkPermission(RestPermissions.STREAMS_READ, streamId);
final Stream stream = streamService.load(streamId);
final Map<String, Object> result;
try {
result = CACHE.get(CACHE_KEY_BASE + stream.getId(), () -> {
final List<AlertCondition> alertConditions = streamService.getAlertConditions(stream);
int triggered = 0;
final List<Map<String, Object>> results = new ArrayList<>(alertConditions.size());
for (AlertCondition alertCondition : alertConditions) {
final Map<String, Object> conditionResult = new HashMap<>();
conditionResult.put("condition", alertCondition);
final AlertCondition.CheckResult checkResult = alertCondition.runCheck();
conditionResult.put("triggered", checkResult.isTriggered());
if (checkResult.isTriggered()) {
triggered++;
conditionResult.put("alert_description", checkResult.getResultDescription());
}
results.add(conditionResult);
}
return ImmutableMap.of("results", results, "calculated_at", Tools.getISO8601String(Tools.nowUTC()), "total_triggered", triggered);
});
} catch (ExecutionException e) {
final Throwable rootCause = Throwables.getRootCause(e);
LOG.error("Could not check for alerts.", rootCause);
throw new InternalServerErrorException(rootCause);
}
return result;
}
use of com.codahale.metrics.annotation.Timed 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 com.codahale.metrics.annotation.Timed in project graylog2-server by Graylog2.
the class StreamAlertResource method list.
@GET
@Timed
@ApiOperation(value = "Get the most recent alarms of this stream.")
@Produces(MediaType.APPLICATION_JSON)
@ApiResponses(value = { @ApiResponse(code = 404, message = "Stream not found."), @ApiResponse(code = 400, message = "Invalid ObjectId.") })
public AlertListSummary list(@ApiParam(name = "streamId", value = "The stream id this new alert condition belongs to.", required = true) @PathParam("streamId") String streamId, @ApiParam(name = "since", value = "Optional parameter to define a lower date boundary. (UNIX timestamp)") @QueryParam("since") @DefaultValue("0") @Min(0) int sinceTs, @ApiParam(name = "limit", value = "Maximum number of alerts to return.") @QueryParam("limit") @DefaultValue("300") @Min(1) int limit) throws NotFoundException {
checkPermission(RestPermissions.STREAMS_READ, streamId);
final DateTime since = new DateTime(sinceTs * 1000L, DateTimeZone.UTC);
final Stream stream = streamService.load(streamId);
final List<AlertSummary> conditions = toSummaryList(alertService.loadRecentOfStream(stream.getId(), since, limit));
return AlertListSummary.create(alertService.totalCountForStream(streamId), conditions);
}
Aggregations