Search in sources :

Example 96 with StreamRule

use of org.graylog2.plugin.streams.StreamRule in project graylog2-server by Graylog2.

the class StreamRuleResource method delete.

@DELETE
@Path("/{streamRuleId}")
@Timed
@ApiOperation(value = "Delete a stream rule")
@ApiResponses(value = { @ApiResponse(code = 404, message = "Stream rule not found."), @ApiResponse(code = 400, message = "Invalid ObjectId.") })
@AuditEvent(type = AuditEventTypes.STREAM_RULE_DELETE)
public void delete(@ApiParam(name = "streamid", value = "The stream id this new rule belongs to.", required = true) @PathParam("streamid") String streamid, @ApiParam(name = "streamRuleId", required = true) @PathParam("streamRuleId") @NotEmpty String streamRuleId) throws NotFoundException {
    checkPermission(RestPermissions.STREAMS_EDIT, streamid);
    checkNotEditable(streamid, "Cannot delete stream rule on non-editable streams.");
    final StreamRule streamRule = streamRuleService.load(streamRuleId);
    if (streamRule.getStreamId().equals(streamid)) {
        streamRuleService.destroy(streamRule);
    } else {
        throw new NotFoundException("Couldn't delete stream rule " + streamRuleId + "in stream " + streamid);
    }
}
Also used : StreamRule(org.graylog2.plugin.streams.StreamRule) NotFoundException(org.graylog2.database.NotFoundException) Path(javax.ws.rs.Path) DELETE(javax.ws.rs.DELETE) Timed(com.codahale.metrics.annotation.Timed) ApiOperation(io.swagger.annotations.ApiOperation) AuditEvent(org.graylog2.audit.jersey.AuditEvent) ApiResponses(io.swagger.annotations.ApiResponses)

Example 97 with StreamRule

use of org.graylog2.plugin.streams.StreamRule in project graylog2-server by Graylog2.

the class StreamRuleResource method get.

@GET
@Timed
@ApiOperation(value = "Get a list of all stream rules")
@Produces(MediaType.APPLICATION_JSON)
public StreamRuleListResponse get(@ApiParam(name = "streamid", value = "The id of the stream whose stream rules we want.", required = true) @PathParam("streamid") String streamid) throws NotFoundException {
    checkPermission(RestPermissions.STREAMS_READ, streamid);
    final Stream stream = streamService.load(streamid);
    final List<StreamRule> streamRules = streamRuleService.loadForStream(stream);
    return StreamRuleListResponse.create(streamRules.size(), streamRules);
}
Also used : StreamRule(org.graylog2.plugin.streams.StreamRule) Stream(org.graylog2.plugin.streams.Stream) Produces(javax.ws.rs.Produces) Timed(com.codahale.metrics.annotation.Timed) GET(javax.ws.rs.GET) ApiOperation(io.swagger.annotations.ApiOperation)

Example 98 with StreamRule

use of org.graylog2.plugin.streams.StreamRule in project graylog2-server by Graylog2.

the class StreamServiceImpl method loadAll.

private List<Stream> loadAll(DBObject query) {
    final List<DBObject> results = query(StreamImpl.class, query);
    final List<String> streamIds = results.stream().map(o -> o.get("_id").toString()).collect(Collectors.toList());
    final Map<String, List<StreamRule>> allStreamRules = streamRuleService.loadForStreamIds(streamIds);
    final ImmutableList.Builder<Stream> streams = ImmutableList.builder();
    final Map<String, IndexSet> indexSets = indexSetsForStreams(results);
    final Set<String> outputIds = results.stream().map(this::outputIdsForRawStream).flatMap(outputs -> outputs.stream().map(ObjectId::toHexString)).collect(Collectors.toSet());
    final Map<String, Output> outputsById = outputService.loadByIds(outputIds).stream().collect(Collectors.toMap(Output::getId, Function.identity()));
    for (DBObject o : results) {
        final ObjectId objectId = (ObjectId) o.get("_id");
        final String id = objectId.toHexString();
        final List<StreamRule> streamRules = allStreamRules.getOrDefault(id, Collections.emptyList());
        LOG.debug("Found {} rules for stream <{}>", streamRules.size(), id);
        final Set<Output> outputs = outputIdsForRawStream(o).stream().map(ObjectId::toHexString).map(outputId -> {
            final Output output = outputsById.get(outputId);
            if (output == null) {
                final String streamTitle = Strings.nullToEmpty((String) o.get(StreamImpl.FIELD_TITLE));
                LOG.warn("Stream \"" + streamTitle + "\" <" + id + "> references missing output <" + outputId + "> - ignoring output.");
            }
            return output;
        }).filter(Objects::nonNull).collect(Collectors.toSet());
        @SuppressWarnings("unchecked") final Map<String, Object> fields = o.toMap();
        final String indexSetId = (String) fields.get(StreamImpl.FIELD_INDEX_SET_ID);
        streams.add(new StreamImpl(objectId, fields, streamRules, outputs, indexSets.get(indexSetId)));
    }
    return streams.build();
}
Also used : IndexSetConfig(org.graylog2.indexer.indexset.IndexSetConfig) Tools(org.graylog2.plugin.Tools) LoggerFactory(org.slf4j.LoggerFactory) AlarmCallbackConfiguration(org.graylog2.alarmcallbacks.AlarmCallbackConfiguration) AlertService(org.graylog2.alerts.AlertService) StreamRule(org.graylog2.plugin.streams.StreamRule) BasicDBList(com.mongodb.BasicDBList) DBObject(com.mongodb.DBObject) Map(java.util.Map) EmbeddedPersistable(org.graylog2.plugin.database.EmbeddedPersistable) BadRequestException(javax.ws.rs.BadRequestException) IndexSet(org.graylog2.indexer.IndexSet) StreamsChangedEvent(org.graylog2.streams.events.StreamsChangedEvent) PersistedServiceImpl(org.graylog2.database.PersistedServiceImpl) ImmutableSet(com.google.common.collect.ImmutableSet) NotificationService(org.graylog2.notifications.NotificationService) Collection(java.util.Collection) Set(java.util.Set) Collectors(java.util.stream.Collectors) Objects(java.util.Objects) CreateStreamRequest(org.graylog2.rest.resources.streams.requests.CreateStreamRequest) List(java.util.List) ClusterEventBus(org.graylog2.events.ClusterEventBus) IndexSetService(org.graylog2.indexer.indexset.IndexSetService) Stream(org.graylog2.plugin.streams.Stream) AlertCondition(org.graylog2.plugin.alarms.AlertCondition) Optional(java.util.Optional) MongoConnection(org.graylog2.database.MongoConnection) EntityOwnershipService(org.graylog.security.entities.EntityOwnershipService) QueryBuilder(com.mongodb.QueryBuilder) AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) Strings.isNullOrEmpty(com.google.common.base.Strings.isNullOrEmpty) EmailAlarmCallback(org.graylog2.alarmcallbacks.EmailAlarmCallback) HashMap(java.util.HashMap) Function(java.util.function.Function) DBProjection(org.mongojack.DBProjection) Inject(javax.inject.Inject) HashSet(java.util.HashSet) Strings(com.google.common.base.Strings) Lists(com.google.common.collect.Lists) ImmutableList(com.google.common.collect.ImmutableList) MongoIndexSet(org.graylog2.indexer.MongoIndexSet) StreamSupport(java.util.stream.StreamSupport) Alert(org.graylog2.alerts.Alert) NotFoundException(org.graylog2.database.NotFoundException) Nullable(javax.annotation.Nullable) Notification(org.graylog2.notifications.Notification) Logger(org.slf4j.Logger) BasicDBObject(com.mongodb.BasicDBObject) StreamDeletedEvent(org.graylog2.streams.events.StreamDeletedEvent) Maps(com.google.common.collect.Maps) AlarmCallbackConfigurationImpl(org.graylog2.alarmcallbacks.AlarmCallbackConfigurationImpl) AlarmCallbackConfigurationService(org.graylog2.alarmcallbacks.AlarmCallbackConfigurationService) DBCursor(com.mongodb.DBCursor) Output(org.graylog2.plugin.streams.Output) ValidationException(org.graylog2.plugin.database.ValidationException) ObjectId(org.bson.types.ObjectId) User(org.graylog2.plugin.database.users.User) Collections(java.util.Collections) ObjectId(org.bson.types.ObjectId) ImmutableList(com.google.common.collect.ImmutableList) StreamRule(org.graylog2.plugin.streams.StreamRule) DBObject(com.mongodb.DBObject) BasicDBObject(com.mongodb.BasicDBObject) Output(org.graylog2.plugin.streams.Output) BasicDBList(com.mongodb.BasicDBList) List(java.util.List) ImmutableList(com.google.common.collect.ImmutableList) Stream(org.graylog2.plugin.streams.Stream) DBObject(com.mongodb.DBObject) BasicDBObject(com.mongodb.BasicDBObject) IndexSet(org.graylog2.indexer.IndexSet) MongoIndexSet(org.graylog2.indexer.MongoIndexSet)

Example 99 with StreamRule

use of org.graylog2.plugin.streams.StreamRule in project graylog2-server by Graylog2.

the class StreamServiceImpl method destroy.

@Override
public void destroy(Stream stream) throws NotFoundException {
    for (StreamRule streamRule : streamRuleService.loadForStream(stream)) {
        super.destroy(streamRule);
    }
    final String streamId = stream.getId();
    for (Notification notification : notificationService.all()) {
        Object rawValue = notification.getDetail("stream_id");
        if (rawValue != null && rawValue.toString().equals(streamId)) {
            LOG.debug("Removing notification that references stream: {}", notification);
            notificationService.destroy(notification);
        }
    }
    super.destroy(stream);
    clusterEventBus.post(StreamsChangedEvent.create(streamId));
    clusterEventBus.post(StreamDeletedEvent.create(streamId));
    entityOwnershipService.unregisterStream(streamId);
}
Also used : StreamRule(org.graylog2.plugin.streams.StreamRule) DBObject(com.mongodb.DBObject) BasicDBObject(com.mongodb.BasicDBObject) Notification(org.graylog2.notifications.Notification)

Example 100 with StreamRule

use of org.graylog2.plugin.streams.StreamRule in project graylog2-server by Graylog2.

the class SmallerMatcher method match.

@Override
public boolean match(Message msg, StreamRule rule) {
    Double msgVal = getDouble(msg.getField(rule.getField()));
    if (msgVal == null) {
        return false;
    }
    Double ruleVal = getDouble(rule.getValue());
    if (ruleVal == null) {
        return false;
    }
    return rule.getInverted() ^ (msgVal < ruleVal);
}
Also used : Tools.getDouble(org.graylog2.plugin.Tools.getDouble)

Aggregations

StreamRule (org.graylog2.plugin.streams.StreamRule)98 Message (org.graylog2.plugin.Message)73 Test (org.junit.Test)71 Stream (org.graylog2.plugin.streams.Stream)16 ObjectId (org.bson.types.ObjectId)11 Timed (com.codahale.metrics.annotation.Timed)10 ApiOperation (io.swagger.annotations.ApiOperation)10 Output (org.graylog2.plugin.streams.Output)9 Produces (javax.ws.rs.Produces)8 AuditEvent (org.graylog2.audit.jersey.AuditEvent)8 ApiResponses (io.swagger.annotations.ApiResponses)7 Consumes (javax.ws.rs.Consumes)7 POST (javax.ws.rs.POST)7 Path (javax.ws.rs.Path)7 NotFoundException (org.graylog2.database.NotFoundException)7 URI (java.net.URI)6 Map (java.util.Map)6 AlarmCallbackConfiguration (org.graylog2.alarmcallbacks.AlarmCallbackConfiguration)6 NoAuditEvent (org.graylog2.audit.jersey.NoAuditEvent)6 AlertCondition (org.graylog2.plugin.alarms.AlertCondition)6