Search in sources :

Example 46 with AlertCondition

use of org.graylog2.plugin.alarms.AlertCondition in project graylog2-server by Graylog2.

the class StreamResource method cloneStream.

@POST
@Path("/{streamId}/clone")
@Timed
@ApiOperation(value = "Clone a stream")
@ApiResponses(value = { @ApiResponse(code = 404, message = "Stream not found."), @ApiResponse(code = 400, message = "Invalid or missing Stream id.") })
@Consumes(MediaType.APPLICATION_JSON)
@Produces(MediaType.APPLICATION_JSON)
@AuditEvent(type = AuditEventTypes.STREAM_CREATE)
public Response cloneStream(@ApiParam(name = "streamId", required = true) @PathParam("streamId") String streamId, @ApiParam(name = "JSON body", required = true) @Valid @NotNull CloneStreamRequest cr, @Context UserContext userContext) throws ValidationException, NotFoundException {
    checkPermission(RestPermissions.STREAMS_CREATE);
    checkPermission(RestPermissions.STREAMS_READ, streamId);
    checkNotEditableStream(streamId, "The stream cannot be cloned.");
    final Stream sourceStream = streamService.load(streamId);
    final String creatorUser = getCurrentUser().getName();
    final List<StreamRule> sourceStreamRules = streamRuleService.loadForStream(sourceStream);
    final ImmutableSet.Builder<StreamRule> newStreamRules = ImmutableSet.builderWithExpectedSize(sourceStreamRules.size());
    for (StreamRule streamRule : sourceStreamRules) {
        final Map<String, Object> streamRuleData = Maps.newHashMapWithExpectedSize(6);
        streamRuleData.put(StreamRuleImpl.FIELD_TYPE, streamRule.getType().toInteger());
        streamRuleData.put(StreamRuleImpl.FIELD_FIELD, streamRule.getField());
        streamRuleData.put(StreamRuleImpl.FIELD_VALUE, streamRule.getValue());
        streamRuleData.put(StreamRuleImpl.FIELD_INVERTED, streamRule.getInverted());
        streamRuleData.put(StreamRuleImpl.FIELD_DESCRIPTION, streamRule.getDescription());
        final StreamRule newStreamRule = streamRuleService.create(streamRuleData);
        newStreamRules.add(newStreamRule);
    }
    final Map<String, Object> streamData = Maps.newHashMap();
    streamData.put(StreamImpl.FIELD_TITLE, cr.title());
    streamData.put(StreamImpl.FIELD_DESCRIPTION, cr.description());
    streamData.put(StreamImpl.FIELD_CREATOR_USER_ID, creatorUser);
    streamData.put(StreamImpl.FIELD_CREATED_AT, Tools.nowUTC());
    streamData.put(StreamImpl.FIELD_MATCHING_TYPE, sourceStream.getMatchingType().toString());
    streamData.put(StreamImpl.FIELD_REMOVE_MATCHES_FROM_DEFAULT_STREAM, cr.removeMatchesFromDefaultStream());
    streamData.put(StreamImpl.FIELD_DISABLED, true);
    streamData.put(StreamImpl.FIELD_INDEX_SET_ID, cr.indexSetId());
    final Stream stream = streamService.create(streamData);
    final String savedStreamId = streamService.saveWithRulesAndOwnership(stream, newStreamRules.build(), userContext.getUser());
    final ObjectId savedStreamObjectId = new ObjectId(savedStreamId);
    for (AlertCondition alertCondition : streamService.getAlertConditions(sourceStream)) {
        try {
            final AlertCondition clonedAlertCondition = alertService.fromRequest(CreateConditionRequest.create(alertCondition.getType(), alertCondition.getTitle(), alertCondition.getParameters()), stream, creatorUser);
            streamService.addAlertCondition(stream, clonedAlertCondition);
        } catch (ConfigurationException e) {
            LOG.warn("Unable to clone alert condition <" + alertCondition + "> - skipping: ", e);
        }
    }
    for (AlarmCallbackConfiguration alarmCallbackConfiguration : alarmCallbackConfigurationService.getForStream(sourceStream)) {
        final CreateAlarmCallbackRequest request = CreateAlarmCallbackRequest.create(alarmCallbackConfiguration);
        final AlarmCallbackConfiguration alarmCallback = alarmCallbackConfigurationService.create(stream.getId(), request, getCurrentUser().getName());
        alarmCallbackConfigurationService.save(alarmCallback);
    }
    final Set<ObjectId> outputIds = sourceStream.getOutputs().stream().map(Output::getId).map(ObjectId::new).collect(Collectors.toSet());
    streamService.addOutputs(savedStreamObjectId, outputIds);
    final Map<String, String> result = ImmutableMap.of("stream_id", savedStreamId);
    final URI streamUri = getUriBuilderToSelf().path(StreamResource.class).path("{streamId}").build(savedStreamId);
    return Response.created(streamUri).entity(result).build();
}
Also used : ObjectId(org.bson.types.ObjectId) StreamRule(org.graylog2.plugin.streams.StreamRule) URI(java.net.URI) CreateAlarmCallbackRequest(org.graylog2.rest.models.alarmcallbacks.requests.CreateAlarmCallbackRequest) ImmutableSet(com.google.common.collect.ImmutableSet) ConfigurationException(org.graylog2.plugin.configuration.ConfigurationException) Output(org.graylog2.plugin.streams.Output) AlertCondition(org.graylog2.plugin.alarms.AlertCondition) Stream(org.graylog2.plugin.streams.Stream) AlarmCallbackConfiguration(org.graylog2.alarmcallbacks.AlarmCallbackConfiguration) Path(javax.ws.rs.Path) POST(javax.ws.rs.POST) Consumes(javax.ws.rs.Consumes) Produces(javax.ws.rs.Produces) Timed(com.codahale.metrics.annotation.Timed) ApiOperation(io.swagger.annotations.ApiOperation) NoAuditEvent(org.graylog2.audit.jersey.NoAuditEvent) AuditEvent(org.graylog2.audit.jersey.AuditEvent) ApiResponses(io.swagger.annotations.ApiResponses)

Example 47 with AlertCondition

use of org.graylog2.plugin.alarms.AlertCondition in project graylog2-server by Graylog2.

the class StreamAlertConditionResource method create.

@POST
@Timed
@ApiOperation(value = "Create an alert condition")
@ApiResponses(value = { @ApiResponse(code = 404, message = "Stream not found."), @ApiResponse(code = 400, message = "Invalid ObjectId.") })
@AuditEvent(type = AuditEventTypes.ALERT_CONDITION_CREATE)
public Response create(@ApiParam(name = "streamId", value = "The stream id this new alert condition belongs to.", required = true) @PathParam("streamId") String streamid, @ApiParam(name = "JSON body", required = true) @Valid @NotNull CreateConditionRequest ccr) throws NotFoundException, ValidationException {
    checkPermission(RestPermissions.STREAMS_EDIT, streamid);
    final Stream stream = streamService.load(streamid);
    try {
        final AlertCondition alertCondition = alertService.fromRequest(convertConfigurationInRequest(ccr), stream, getCurrentUser().getName());
        streamService.addAlertCondition(stream, alertCondition);
        final Map<String, String> result = ImmutableMap.of("alert_condition_id", alertCondition.getId());
        final URI alertConditionUri = getUriBuilderToSelf().path(StreamAlertConditionResource.class).path("{conditionId}").build(stream.getId(), alertCondition.getId());
        return Response.created(alertConditionUri).entity(result).build();
    } catch (ConfigurationException e) {
        throw new BadRequestException("Invalid alert condition parameters", e);
    }
}
Also used : ConfigurationException(org.graylog2.plugin.configuration.ConfigurationException) AlertCondition(org.graylog2.plugin.alarms.AlertCondition) BadRequestException(javax.ws.rs.BadRequestException) Stream(org.graylog2.plugin.streams.Stream) URI(java.net.URI) POST(javax.ws.rs.POST) Timed(com.codahale.metrics.annotation.Timed) ApiOperation(io.swagger.annotations.ApiOperation) AuditEvent(org.graylog2.audit.jersey.AuditEvent) NoAuditEvent(org.graylog2.audit.jersey.NoAuditEvent) ApiResponses(io.swagger.annotations.ApiResponses)

Example 48 with AlertCondition

use of org.graylog2.plugin.alarms.AlertCondition in project graylog2-server by Graylog2.

the class StreamAlertConditionResource method testNew.

@POST
@Path("test")
@Timed
@ApiOperation("Test new alert condition")
@NoAuditEvent("resource doesn't modify any data")
public Response testNew(@ApiParam(name = "streamId", value = "The stream ID this alert condition belongs to.", required = true) @PathParam("streamId") String streamId, @ApiParam(name = "Alert condition parameters", required = true) @Valid @NotNull CreateConditionRequest ccr) throws NotFoundException {
    checkPermission(RestPermissions.STREAMS_EDIT, streamId);
    final Stream stream = streamService.load(streamId);
    try {
        final AlertCondition alertCondition = alertService.fromRequest(convertConfigurationInRequest(ccr), stream, getCurrentUser().getName());
        return Response.ok(testAlertCondition(alertCondition)).build();
    } catch (ConfigurationException e) {
        throw new BadRequestException("Invalid alert condition parameters", e);
    }
}
Also used : ConfigurationException(org.graylog2.plugin.configuration.ConfigurationException) AlertCondition(org.graylog2.plugin.alarms.AlertCondition) BadRequestException(javax.ws.rs.BadRequestException) Stream(org.graylog2.plugin.streams.Stream) Path(javax.ws.rs.Path) POST(javax.ws.rs.POST) Timed(com.codahale.metrics.annotation.Timed) ApiOperation(io.swagger.annotations.ApiOperation) NoAuditEvent(org.graylog2.audit.jersey.NoAuditEvent)

Example 49 with AlertCondition

use of org.graylog2.plugin.alarms.AlertCondition in project graylog2-server by Graylog2.

the class StreamAlertConditionResource method list.

@GET
@Timed
@ApiOperation(value = "Get all alert conditions of this stream")
@ApiResponses(value = { @ApiResponse(code = 404, message = "Stream not found."), @ApiResponse(code = 400, message = "Invalid ObjectId.") })
public AlertConditionListSummary list(@ApiParam(name = "streamId", value = "The id of the stream whose alert conditions we want.", required = true) @PathParam("streamId") String streamid) throws NotFoundException {
    checkPermission(RestPermissions.STREAMS_READ, streamid);
    final Stream stream = streamService.load(streamid);
    final List<AlertCondition> alertConditions = streamService.getAlertConditions(stream);
    final List<AlertConditionSummary> conditionSummaries = alertConditions.stream().map((condition) -> AlertConditionSummary.create(condition.getId(), condition.getType(), condition.getCreatorUserId(), condition.getCreatedAt().toDate(), condition.getParameters(), alertService.inGracePeriod(condition), condition.getTitle())).collect(Collectors.toList());
    return AlertConditionListSummary.create(conditionSummaries);
}
Also used : PathParam(javax.ws.rs.PathParam) Produces(javax.ws.rs.Produces) GET(javax.ws.rs.GET) Path(javax.ws.rs.Path) ApiParam(io.swagger.annotations.ApiParam) ApiResponses(io.swagger.annotations.ApiResponses) AlertService(org.graylog2.alerts.AlertService) Inject(javax.inject.Inject) Valid(javax.validation.Valid) ApiOperation(io.swagger.annotations.ApiOperation) MediaType(javax.ws.rs.core.MediaType) Consumes(javax.ws.rs.Consumes) AlertConditionSummary(org.graylog2.rest.models.streams.alerts.AlertConditionSummary) ConfigurationException(org.graylog2.plugin.configuration.ConfigurationException) Map(java.util.Map) AuditEvent(org.graylog2.audit.jersey.AuditEvent) BadRequestException(javax.ws.rs.BadRequestException) Api(io.swagger.annotations.Api) URI(java.net.URI) NotFoundException(org.graylog2.database.NotFoundException) AlertConditionTestResponse(org.graylog2.rest.resources.streams.responses.AlertConditionTestResponse) DELETE(javax.ws.rs.DELETE) NoAuditEvent(org.graylog2.audit.jersey.NoAuditEvent) ConfigurationRequest(org.graylog2.plugin.configuration.ConfigurationRequest) POST(javax.ws.rs.POST) ConfigurationMapConverter(org.graylog2.utilities.ConfigurationMapConverter) ImmutableMap(com.google.common.collect.ImmutableMap) AlertConditionListSummary(org.graylog2.rest.models.streams.alerts.AlertConditionListSummary) RestResource(org.graylog2.shared.rest.resources.RestResource) NotNull(javax.validation.constraints.NotNull) Collectors(java.util.stream.Collectors) Timed(com.codahale.metrics.annotation.Timed) List(java.util.List) Response(javax.ws.rs.core.Response) Stream(org.graylog2.plugin.streams.Stream) ApiResponse(io.swagger.annotations.ApiResponse) AuditEventTypes(org.graylog2.audit.AuditEventTypes) ValidationException(org.graylog2.plugin.database.ValidationException) RestPermissions(org.graylog2.shared.security.RestPermissions) StreamService(org.graylog2.streams.StreamService) AlertCondition(org.graylog2.plugin.alarms.AlertCondition) CreateConditionRequest(org.graylog2.rest.models.streams.alerts.requests.CreateConditionRequest) PUT(javax.ws.rs.PUT) RequiresAuthentication(org.apache.shiro.authz.annotation.RequiresAuthentication) AlertCondition(org.graylog2.plugin.alarms.AlertCondition) Stream(org.graylog2.plugin.streams.Stream) AlertConditionSummary(org.graylog2.rest.models.streams.alerts.AlertConditionSummary) Timed(com.codahale.metrics.annotation.Timed) GET(javax.ws.rs.GET) ApiOperation(io.swagger.annotations.ApiOperation) ApiResponses(io.swagger.annotations.ApiResponses)

Example 50 with AlertCondition

use of org.graylog2.plugin.alarms.AlertCondition in project graylog2-server by Graylog2.

the class StreamAlertConditionResource method testExisting.

@POST
@Path("{conditionId}/test")
@Timed
@ApiOperation("Test existing alert condition")
@NoAuditEvent("resource doesn't modify any data")
public Response testExisting(@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 fetched", required = true) @PathParam("conditionId") String conditionId) throws NotFoundException {
    checkPermission(RestPermissions.STREAMS_EDIT, streamId);
    final Stream stream = streamService.load(streamId);
    final AlertCondition alertCondition = streamService.getAlertCondition(stream, conditionId);
    final AlertConditionTestResponse testResultResponse = testAlertCondition(alertCondition);
    if (testResultResponse.error()) {
        return Response.status(400).entity(testResultResponse).build();
    } else {
        return Response.ok(testResultResponse).build();
    }
}
Also used : AlertConditionTestResponse(org.graylog2.rest.resources.streams.responses.AlertConditionTestResponse) AlertCondition(org.graylog2.plugin.alarms.AlertCondition) Stream(org.graylog2.plugin.streams.Stream) Path(javax.ws.rs.Path) POST(javax.ws.rs.POST) Timed(com.codahale.metrics.annotation.Timed) ApiOperation(io.swagger.annotations.ApiOperation) NoAuditEvent(org.graylog2.audit.jersey.NoAuditEvent)

Aggregations

AlertCondition (org.graylog2.plugin.alarms.AlertCondition)45 Stream (org.graylog2.plugin.streams.Stream)35 Test (org.junit.Test)32 ConfigurationException (org.graylog2.plugin.configuration.ConfigurationException)10 DateTime (org.joda.time.DateTime)10 Timed (com.codahale.metrics.annotation.Timed)9 ApiOperation (io.swagger.annotations.ApiOperation)9 AlarmCallbackConfiguration (org.graylog2.alarmcallbacks.AlarmCallbackConfiguration)9 Path (javax.ws.rs.Path)8 NoAuditEvent (org.graylog2.audit.jersey.NoAuditEvent)8 MongoDBServiceTest (org.graylog2.database.MongoDBServiceTest)8 ApiResponses (io.swagger.annotations.ApiResponses)7 AbstractAlertCondition (org.graylog2.alerts.AbstractAlertCondition)7 Date (java.util.Date)6 AuditEvent (org.graylog2.audit.jersey.AuditEvent)6 List (java.util.List)5 POST (javax.ws.rs.POST)5 DummyAlertCondition (org.graylog2.alerts.types.DummyAlertCondition)5 EmailConfiguration (org.graylog2.configuration.EmailConfiguration)5 CreateAlarmCallbackRequest (org.graylog2.rest.models.alarmcallbacks.requests.CreateAlarmCallbackRequest)5