Search in sources :

Example 1 with AlarmCallbackConfigurationImpl

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

the class V20161125161400_AlertReceiversMigration method updateConfiguration.

private Optional<String> updateConfiguration(Stream stream, AlarmCallbackConfiguration callbackConfiguration) {
    final Map<String, List<String>> alertReceivers = stream.getAlertReceivers();
    final List<String> usernames = alertReceivers.get("users");
    final List<String> emails = alertReceivers.get("emails");
    final Map<String, Object> configuration = callbackConfiguration.getConfiguration();
    if (usernames != null && !usernames.isEmpty()) {
        LOG.debug("Moving users alert receivers from stream <" + stream.getId() + ">");
        configuration.put(EmailAlarmCallback.CK_USER_RECEIVERS, usernames);
    }
    if (emails != null && !emails.isEmpty()) {
        LOG.debug("Moving emails alert receivers from stream <" + stream.getId() + ">");
        configuration.put(EmailAlarmCallback.CK_EMAIL_RECEIVERS, emails);
    }
    final AlarmCallbackConfigurationImpl updatedConfiguration = ((AlarmCallbackConfigurationImpl) callbackConfiguration).toBuilder().setConfiguration(configuration).build();
    try {
        final String callbackId = this.alarmCallbackService.save(updatedConfiguration);
        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) List(java.util.List) BasicDBObject(com.mongodb.BasicDBObject) AlarmCallbackConfigurationImpl(org.graylog2.alarmcallbacks.AlarmCallbackConfigurationImpl)

Example 2 with AlarmCallbackConfigurationImpl

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

the class StreamAlarmCallbackResource method update.

@PUT
@Path("/{alarmCallbackId}")
@Timed
@ApiOperation(value = "Update an alarm callback")
@Consumes(MediaType.APPLICATION_JSON)
@Produces(MediaType.APPLICATION_JSON)
@AuditEvent(type = AuditEventTypes.ALARM_CALLBACK_UPDATE)
public void update(@ApiParam(name = "streamid", value = "The stream id this alarm callback belongs to.", required = true) @PathParam("streamid") String streamid, @ApiParam(name = "alarmCallbackId", required = true) @PathParam("alarmCallbackId") String alarmCallbackId, @ApiParam(name = "JSON body", required = true) CreateAlarmCallbackRequest alarmCallbackRequest) throws NotFoundException {
    checkPermission(RestPermissions.STREAMS_EDIT, streamid);
    final AlarmCallbackConfiguration callbackConfiguration = alarmCallbackConfigurationService.load(alarmCallbackId);
    if (callbackConfiguration == null) {
        throw new NotFoundException("Unable to find alarm callback configuration " + alarmCallbackId);
    }
    final Map<String, Object> configuration = convertConfigurationValues(alarmCallbackRequest);
    final AlarmCallbackConfiguration updatedConfig = ((AlarmCallbackConfigurationImpl) callbackConfiguration).toBuilder().setTitle(alarmCallbackRequest.title()).setConfiguration(configuration).build();
    try {
        alarmCallbackFactory.create(updatedConfig).checkConfiguration();
        alarmCallbackConfigurationService.save(updatedConfig);
    } catch (ValidationException | AlarmCallbackConfigurationException | ConfigurationException e) {
        LOG.error("Invalid alarm callback configuration.", e);
        throw new BadRequestException(e.getMessage(), e);
    } catch (ClassNotFoundException e) {
        LOG.error("Invalid alarm callback type.", e);
        throw new BadRequestException("Invalid alarm callback type.", e);
    }
}
Also used : ValidationException(org.graylog2.plugin.database.ValidationException) AlarmCallbackConfigurationException(org.graylog2.plugin.alarms.callbacks.AlarmCallbackConfigurationException) ConfigurationException(org.graylog2.plugin.configuration.ConfigurationException) NotFoundException(org.graylog2.database.NotFoundException) BadRequestException(javax.ws.rs.BadRequestException) AlarmCallbackConfiguration(org.graylog2.alarmcallbacks.AlarmCallbackConfiguration) AlarmCallbackConfigurationException(org.graylog2.plugin.alarms.callbacks.AlarmCallbackConfigurationException) Path(javax.ws.rs.Path) Consumes(javax.ws.rs.Consumes) Produces(javax.ws.rs.Produces) Timed(com.codahale.metrics.annotation.Timed) ApiOperation(io.swagger.annotations.ApiOperation) AuditEvent(org.graylog2.audit.jersey.AuditEvent) PUT(javax.ws.rs.PUT)

Example 3 with AlarmCallbackConfigurationImpl

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

the class StreamServiceImpl method updateCallbackConfiguration.

// I tried to be sorry, really. https://www.youtube.com/watch?v=3KVyRqloGmk
private void updateCallbackConfiguration(String action, String type, String entity, List<AlarmCallbackConfiguration> streamCallbacks) {
    final AtomicBoolean ran = new AtomicBoolean(false);
    streamCallbacks.stream().filter(callback -> callback.getType().equals(EmailAlarmCallback.class.getCanonicalName())).forEach(callback -> {
        ran.set(true);
        final Map<String, Object> configuration = callback.getConfiguration();
        String key;
        if ("users".equals(type)) {
            key = EmailAlarmCallback.CK_USER_RECEIVERS;
        } else {
            key = EmailAlarmCallback.CK_EMAIL_RECEIVERS;
        }
        @SuppressWarnings("unchecked") final List<String> recipients = (List<String>) configuration.get(key);
        if ("add".equals(action)) {
            if (!recipients.contains(entity)) {
                recipients.add(entity);
            }
        } else {
            if (recipients.contains(entity)) {
                recipients.remove(entity);
            }
        }
        configuration.put(key, recipients);
        final AlarmCallbackConfiguration updatedConfig = ((AlarmCallbackConfigurationImpl) callback).toBuilder().setConfiguration(configuration).build();
        try {
            alarmCallbackConfigurationService.save(updatedConfig);
        } catch (ValidationException e) {
            throw new BadRequestException("Unable to save alarm callback configuration", e);
        }
    });
    if (!ran.get()) {
        throw new BadRequestException("Unable to " + action + " receiver: Stream has no email alarm callback.");
    }
}
Also used : QueryBuilder(com.mongodb.QueryBuilder) IndexSetConfig(org.graylog2.indexer.indexset.IndexSetConfig) Tools(org.graylog2.plugin.Tools) LoggerFactory(org.slf4j.LoggerFactory) AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) Strings.isNullOrEmpty(com.google.common.base.Strings.isNullOrEmpty) EmailAlarmCallback(org.graylog2.alarmcallbacks.EmailAlarmCallback) HashMap(java.util.HashMap) AlarmCallbackConfiguration(org.graylog2.alarmcallbacks.AlarmCallbackConfiguration) AlertService(org.graylog2.alerts.AlertService) StreamRule(org.graylog2.plugin.streams.StreamRule) Inject(javax.inject.Inject) HashSet(java.util.HashSet) Lists(com.google.common.collect.Lists) ImmutableList(com.google.common.collect.ImmutableList) DBObject(com.mongodb.DBObject) Map(java.util.Map) MongoIndexSet(org.graylog2.indexer.MongoIndexSet) EmbeddedPersistable(org.graylog2.plugin.database.EmbeddedPersistable) BadRequestException(javax.ws.rs.BadRequestException) IndexSet(org.graylog2.indexer.IndexSet) Alert(org.graylog2.alerts.Alert) PersistedServiceImpl(org.graylog2.database.PersistedServiceImpl) NotFoundException(org.graylog2.database.NotFoundException) Nullable(javax.annotation.Nullable) Notification(org.graylog2.notifications.Notification) Logger(org.slf4j.Logger) NotificationService(org.graylog2.notifications.NotificationService) BasicDBObject(com.mongodb.BasicDBObject) Set(java.util.Set) Maps(com.google.common.collect.Maps) Collectors(java.util.stream.Collectors) AlarmCallbackConfigurationImpl(org.graylog2.alarmcallbacks.AlarmCallbackConfigurationImpl) AlarmCallbackConfigurationService(org.graylog2.alarmcallbacks.AlarmCallbackConfigurationService) CreateStreamRequest(org.graylog2.rest.resources.streams.requests.CreateStreamRequest) List(java.util.List) IndexSetService(org.graylog2.indexer.indexset.IndexSetService) Output(org.graylog2.plugin.streams.Output) Stream(org.graylog2.plugin.streams.Stream) ValidationException(org.graylog2.plugin.database.ValidationException) AlertCondition(org.graylog2.plugin.alarms.AlertCondition) ObjectId(org.bson.types.ObjectId) Optional(java.util.Optional) MongoConnection(org.graylog2.database.MongoConnection) Collections(java.util.Collections) ValidationException(org.graylog2.plugin.database.ValidationException) AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) BadRequestException(javax.ws.rs.BadRequestException) DBObject(com.mongodb.DBObject) BasicDBObject(com.mongodb.BasicDBObject) ImmutableList(com.google.common.collect.ImmutableList) List(java.util.List) EmailAlarmCallback(org.graylog2.alarmcallbacks.EmailAlarmCallback) AlarmCallbackConfigurationImpl(org.graylog2.alarmcallbacks.AlarmCallbackConfigurationImpl) AlarmCallbackConfiguration(org.graylog2.alarmcallbacks.AlarmCallbackConfiguration)

Aggregations

ValidationException (org.graylog2.plugin.database.ValidationException)3 BasicDBObject (com.mongodb.BasicDBObject)2 List (java.util.List)2 BadRequestException (javax.ws.rs.BadRequestException)2 AlarmCallbackConfiguration (org.graylog2.alarmcallbacks.AlarmCallbackConfiguration)2 AlarmCallbackConfigurationImpl (org.graylog2.alarmcallbacks.AlarmCallbackConfigurationImpl)2 NotFoundException (org.graylog2.database.NotFoundException)2 Timed (com.codahale.metrics.annotation.Timed)1 Strings.isNullOrEmpty (com.google.common.base.Strings.isNullOrEmpty)1 ImmutableList (com.google.common.collect.ImmutableList)1 Lists (com.google.common.collect.Lists)1 Maps (com.google.common.collect.Maps)1 DBObject (com.mongodb.DBObject)1 QueryBuilder (com.mongodb.QueryBuilder)1 ApiOperation (io.swagger.annotations.ApiOperation)1 Collections (java.util.Collections)1 HashMap (java.util.HashMap)1 HashSet (java.util.HashSet)1 Map (java.util.Map)1 Optional (java.util.Optional)1