Search in sources :

Example 21 with AlarmCallbackConfiguration

use of org.graylog2.alarmcallbacks.AlarmCallbackConfiguration in project graylog2-server by Graylog2.

the class AlarmCallbackFactory method create.

public AlarmCallback create(AlarmCallbackConfiguration configuration) throws ClassNotFoundException, AlarmCallbackConfigurationException {
    AlarmCallback alarmCallback = create(configuration.getType());
    alarmCallback.initialize(new Configuration(configuration.getConfiguration()));
    return alarmCallback;
}
Also used : Configuration(org.graylog2.plugin.configuration.Configuration) AlarmCallback(org.graylog2.plugin.alarms.callbacks.AlarmCallback)

Example 22 with AlarmCallbackConfiguration

use of org.graylog2.alarmcallbacks.AlarmCallbackConfiguration in project graylog2-server by Graylog2.

the class V20161125142400_EmailAlarmCallbackMigration method migrateStream.

private Optional<String> migrateStream(Stream stream) {
    final Map<String, Object> defaultConfig = this.getDefaultEmailAlarmCallbackConfig();
    LOG.debug("Creating email alarm callback for stream <" + stream.getId() + ">");
    final AlarmCallbackConfiguration alarmCallbackConfiguration = alarmCallbackService.create(stream.getId(), CreateAlarmCallbackRequest.create(EmailAlarmCallback.class.getCanonicalName(), "Email Alert Notification", defaultConfig), "local:admin");
    try {
        final String callbackId = this.alarmCallbackService.save(alarmCallbackConfiguration);
        LOG.debug("Successfully created email alarm callback <" + callbackId + "> for stream <" + stream.getId() + ">.");
        return Optional.of(callbackId);
    } catch (ValidationException e) {
        LOG.error("Unable to create email alarm callback for stream <" + stream.getId() + ">: ", e);
    }
    return Optional.empty();
}
Also used : ValidationException(org.graylog2.plugin.database.ValidationException) AlarmCallbackConfiguration(org.graylog2.alarmcallbacks.AlarmCallbackConfiguration)

Example 23 with AlarmCallbackConfiguration

use of org.graylog2.alarmcallbacks.AlarmCallbackConfiguration in project graylog2-server by Graylog2.

the class V20161125161400_AlertReceiversMigration method migrateStream.

private Optional<String> migrateStream(Stream stream) {
    final List<AlarmCallbackConfiguration> alarmCallbacks = alarmCallbackService.getForStream(stream);
    final List<Optional<String>> updatedConfigurations = alarmCallbacks.stream().filter(callbackConfiguration -> callbackConfiguration.getType().equals(EmailAlarmCallback.class.getCanonicalName())).map(callbackConfiguration -> this.updateConfiguration(stream, callbackConfiguration)).collect(Collectors.toList());
    if (!updatedConfigurations.stream().allMatch(Optional::isPresent)) {
        final long errors = updatedConfigurations.stream().filter(streamId -> !streamId.isPresent()).count();
        LOG.error("Failed moving alert receivers in " + errors + " email alarm callbacks.");
        return Optional.empty();
    }
    this.dbCollection.update(new BasicDBObject("_id", new ObjectId(stream.getId())), new BasicDBObject("$unset", new BasicDBObject(StreamImpl.FIELD_ALERT_RECEIVERS, "")));
    LOG.debug("Successfully removed alert receivers from stream <" + stream.getId() + ">.");
    return Optional.of(updatedConfigurations.stream().map(Optional::get).collect(Collectors.joining(", ")));
}
Also used : JsonProperty(com.fasterxml.jackson.annotation.JsonProperty) ZonedDateTime(java.time.ZonedDateTime) LoggerFactory(org.slf4j.LoggerFactory) WithBeanGetter(org.graylog.autovalue.WithBeanGetter) EmailAlarmCallback(org.graylog2.alarmcallbacks.EmailAlarmCallback) AlarmCallbackConfiguration(org.graylog2.alarmcallbacks.AlarmCallbackConfiguration) Inject(javax.inject.Inject) ClusterConfigService(org.graylog2.plugin.cluster.ClusterConfigService) CollectionName(org.graylog2.database.CollectionName) StreamImpl(org.graylog2.streams.StreamImpl) Map(java.util.Map) Logger(org.slf4j.Logger) Persisted(org.graylog2.plugin.database.Persisted) BasicDBObject(com.mongodb.BasicDBObject) DBCollection(com.mongodb.DBCollection) JsonAutoDetect(com.fasterxml.jackson.annotation.JsonAutoDetect) Collectors(java.util.stream.Collectors) AlarmCallbackConfigurationImpl(org.graylog2.alarmcallbacks.AlarmCallbackConfigurationImpl) AlarmCallbackConfigurationService(org.graylog2.alarmcallbacks.AlarmCallbackConfigurationService) List(java.util.List) Stream(org.graylog2.plugin.streams.Stream) ValidationException(org.graylog2.plugin.database.ValidationException) StreamService(org.graylog2.streams.StreamService) JsonCreator(com.fasterxml.jackson.annotation.JsonCreator) AutoValue(com.google.auto.value.AutoValue) ObjectId(org.bson.types.ObjectId) Optional(java.util.Optional) MongoConnection(org.graylog2.database.MongoConnection) BasicDBObject(com.mongodb.BasicDBObject) Optional(java.util.Optional) ObjectId(org.bson.types.ObjectId) AlarmCallbackConfiguration(org.graylog2.alarmcallbacks.AlarmCallbackConfiguration)

Example 24 with AlarmCallbackConfiguration

use of org.graylog2.alarmcallbacks.AlarmCallbackConfiguration 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();
}
Also used : InternalServerErrorException(javax.ws.rs.InternalServerErrorException) Stream(org.graylog2.plugin.streams.Stream) EmailAlarmCallback(org.graylog2.alarmcallbacks.EmailAlarmCallback) AlarmCallback(org.graylog2.plugin.alarms.callbacks.AlarmCallback) AbstractAlertCondition(org.graylog2.alerts.AbstractAlertCondition) DummyAlertCondition(org.graylog2.alerts.types.DummyAlertCondition) TransportConfigurationException(org.graylog2.plugin.alarms.transports.TransportConfigurationException) NotFoundException(org.graylog2.database.NotFoundException) InternalServerErrorException(javax.ws.rs.InternalServerErrorException) EmailException(org.apache.commons.mail.EmailException) 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 25 with AlarmCallbackConfiguration

use of org.graylog2.alarmcallbacks.AlarmCallbackConfiguration in project graylog2-server by Graylog2.

the class StreamAlarmCallbackResource method get.

@GET
@Timed
@ApiOperation(value = "Get a list of all alarm callbacks for this stream")
@Produces(MediaType.APPLICATION_JSON)
public AlarmCallbackListSummary get(@ApiParam(name = "streamid", value = "The id of the stream whose alarm callbacks we want.", required = true) @PathParam("streamid") String streamid) throws NotFoundException {
    checkPermission(RestPermissions.STREAMS_READ, streamid);
    final Stream stream = streamService.load(streamid);
    final List<AlarmCallbackSummary> alarmCallbacks = Lists.newArrayList();
    for (AlarmCallbackConfiguration callback : alarmCallbackConfigurationService.getForStream(stream)) {
        alarmCallbacks.add(AlarmCallbackSummary.create(callback.getId(), callback.getStreamId(), callback.getType(), callback.getTitle(), callback.getConfiguration(), callback.getCreatedAt(), callback.getCreatorUserId()));
    }
    return AlarmCallbackListSummary.create(alarmCallbacks);
}
Also used : AlarmCallbackSummary(org.graylog2.rest.models.alarmcallbacks.AlarmCallbackSummary) Stream(org.graylog2.plugin.streams.Stream) AlarmCallbackConfiguration(org.graylog2.alarmcallbacks.AlarmCallbackConfiguration) Produces(javax.ws.rs.Produces) Timed(com.codahale.metrics.annotation.Timed) GET(javax.ws.rs.GET) ApiOperation(io.swagger.annotations.ApiOperation)

Aggregations

AlarmCallbackConfiguration (org.graylog2.alarmcallbacks.AlarmCallbackConfiguration)18 Stream (org.graylog2.plugin.streams.Stream)15 AlertCondition (org.graylog2.plugin.alarms.AlertCondition)13 Test (org.junit.Test)12 Timed (com.codahale.metrics.annotation.Timed)8 ApiOperation (io.swagger.annotations.ApiOperation)8 ObjectId (org.bson.types.ObjectId)7 ValidationException (org.graylog2.plugin.database.ValidationException)7 Date (java.util.Date)6 List (java.util.List)6 Path (javax.ws.rs.Path)6 EmailAlarmCallback (org.graylog2.alarmcallbacks.EmailAlarmCallback)6 MongoDBServiceTest (org.graylog2.database.MongoDBServiceTest)6 BasicDBObject (com.mongodb.BasicDBObject)5 ApiResponses (io.swagger.annotations.ApiResponses)5 POST (javax.ws.rs.POST)5 Produces (javax.ws.rs.Produces)5 Alert (org.graylog2.alerts.Alert)5 AuditEvent (org.graylog2.audit.jersey.AuditEvent)5 AlarmCallback (org.graylog2.plugin.alarms.callbacks.AlarmCallback)5