Search in sources :

Example 1 with Output

use of org.graylog2.plugin.streams.Output 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) throws ValidationException, NotFoundException {
    checkPermission(RestPermissions.STREAMS_CREATE);
    checkPermission(RestPermissions.STREAMS_READ, streamId);
    checkNotDefaultStream(streamId, "The default stream cannot be cloned.");
    final Stream sourceStream = streamService.load(streamId);
    final String creatorUser = getCurrentUser().getName();
    // Create stream.
    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_INDEX_SET_ID, cr.indexSetId());
    final Stream stream = streamService.create(streamData);
    streamService.pause(stream);
    final String id = streamService.save(stream);
    final List<StreamRule> sourceStreamRules = streamRuleService.loadForStream(sourceStream);
    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_STREAM_ID, new ObjectId(id));
        streamRuleData.put(StreamRuleImpl.FIELD_DESCRIPTION, streamRule.getDescription());
        final StreamRule newStreamRule = streamRuleService.create(streamRuleData);
        streamRuleService.save(newStreamRule);
    }
    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);
    }
    for (Output output : sourceStream.getOutputs()) {
        streamService.addOutput(stream, output);
    }
    clusterEventBus.post(StreamsChangedEvent.create(stream.getId()));
    final Map<String, String> result = ImmutableMap.of("stream_id", id);
    final URI streamUri = getUriBuilderToSelf().path(StreamResource.class).path("{streamId}").build(id);
    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) 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 2 with Output

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

the class StreamOutputResource method get.

@GET
@Timed
@ApiOperation(value = "Get a list of all outputs for a stream")
@RequiresPermissions(RestPermissions.STREAM_OUTPUTS_CREATE)
@Produces(MediaType.APPLICATION_JSON)
@ApiResponses(value = { @ApiResponse(code = 404, message = "No such stream on this node.") })
public OutputListResponse get(@ApiParam(name = "streamid", value = "The id of the stream whose outputs we want.", required = true) @PathParam("streamid") String streamid) throws NotFoundException {
    checkPermission(RestPermissions.STREAMS_READ, streamid);
    checkPermission(RestPermissions.STREAM_OUTPUTS_READ);
    final Stream stream = streamService.load(streamid);
    final Set<OutputSummary> outputs = new HashSet<>();
    for (Output output : stream.getOutputs()) outputs.add(OutputSummary.create(output.getId(), output.getTitle(), output.getType(), output.getCreatorUserId(), new DateTime(output.getCreatedAt()), new HashMap<>(output.getConfiguration()), output.getContentPack()));
    return OutputListResponse.create(outputs);
}
Also used : OutputSummary(org.graylog2.rest.models.system.outputs.responses.OutputSummary) Output(org.graylog2.plugin.streams.Output) Stream(org.graylog2.plugin.streams.Stream) DateTime(org.joda.time.DateTime) HashSet(java.util.HashSet) RequiresPermissions(org.apache.shiro.authz.annotation.RequiresPermissions) Produces(javax.ws.rs.Produces) Timed(com.codahale.metrics.annotation.Timed) GET(javax.ws.rs.GET) ApiOperation(io.swagger.annotations.ApiOperation) ApiResponses(io.swagger.annotations.ApiResponses)

Example 3 with Output

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

the class MessageOutputFactoryTest method testNonExistentOutputType.

@Test(expected = IllegalArgumentException.class)
public void testNonExistentOutputType() throws MessageOutputConfigurationException {
    final String outputType = "non.existent";
    final Output output = mock(Output.class);
    when(output.getType()).thenReturn(outputType);
    final Stream stream = mock(Stream.class);
    final Configuration configuration = mock(Configuration.class);
    messageOutputFactory.fromStreamOutput(output, stream, configuration);
}
Also used : Configuration(org.graylog2.plugin.configuration.Configuration) MessageOutput(org.graylog2.plugin.outputs.MessageOutput) Output(org.graylog2.plugin.streams.Output) Stream(org.graylog2.plugin.streams.Stream) Test(org.junit.Test)

Example 4 with Output

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

the class OutputRegistryTest method testInvalidOutputConfiguration.

@Test
public void testInvalidOutputConfiguration() throws Exception {
    final String outputId = "foobar";
    final Stream stream = mock(Stream.class);
    when(messageOutputFactory.fromStreamOutput(eq(output), any(Stream.class), any(Configuration.class))).thenThrow(new MessageOutputConfigurationException());
    when(outputService.load(eq(outputId))).thenReturn(output);
    final OutputRegistry outputRegistry = new OutputRegistry(null, outputService, messageOutputFactory, null, null, FAULT_COUNT_THRESHOLD, FAULT_PENALTY_SECONDS);
    assertEquals(outputRegistry.getRunningMessageOutputs().size(), 0);
    MessageOutput result = outputRegistry.getOutputForIdAndStream(outputId, stream);
    assertNull(result);
    assertEquals(outputRegistry.getRunningMessageOutputs().size(), 0);
}
Also used : MessageOutput(org.graylog2.plugin.outputs.MessageOutput) Configuration(org.graylog2.plugin.configuration.Configuration) MessageOutputConfigurationException(org.graylog2.plugin.outputs.MessageOutputConfigurationException) Stream(org.graylog2.plugin.streams.Stream) Test(org.junit.Test)

Example 5 with Output

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

the class StreamListFingerprint method buildFingerprint.

private String buildFingerprint(List<Stream> streams) {
    final MessageDigest sha1Digest = DigestUtils.getSha1Digest();
    final StringBuilder sb = new StringBuilder();
    for (Stream stream : Ordering.from(getStreamComparator()).sortedCopy(streams)) {
        sb.append(stream.hashCode());
        for (StreamRule rule : Ordering.from(getStreamRuleComparator()).sortedCopy(stream.getStreamRules())) {
            sb.append(rule.hashCode());
        }
        for (Output output : Ordering.from(getOutputComparator()).sortedCopy(stream.getOutputs())) {
            sb.append(output.hashCode());
        }
    }
    return String.valueOf(Hex.encodeHex(sha1Digest.digest(sb.toString().getBytes(StandardCharsets.US_ASCII))));
}
Also used : StreamRule(org.graylog2.plugin.streams.StreamRule) Output(org.graylog2.plugin.streams.Output) Stream(org.graylog2.plugin.streams.Stream) MessageDigest(java.security.MessageDigest)

Aggregations

Output (org.graylog2.plugin.streams.Output)15 Stream (org.graylog2.plugin.streams.Stream)11 ApiOperation (io.swagger.annotations.ApiOperation)10 Timed (com.codahale.metrics.annotation.Timed)9 Produces (javax.ws.rs.Produces)9 MessageOutput (org.graylog2.plugin.outputs.MessageOutput)9 ApiResponses (io.swagger.annotations.ApiResponses)8 RequiresPermissions (org.apache.shiro.authz.annotation.RequiresPermissions)8 AuditEvent (org.graylog2.audit.jersey.AuditEvent)7 Path (javax.ws.rs.Path)6 Test (org.junit.Test)6 Consumes (javax.ws.rs.Consumes)4 DateTime (org.joda.time.DateTime)4 HashSet (java.util.HashSet)3 GET (javax.ws.rs.GET)3 POST (javax.ws.rs.POST)3 ObjectId (org.bson.types.ObjectId)3 ValidationException (org.graylog2.plugin.database.ValidationException)3 StreamRule (org.graylog2.plugin.streams.StreamRule)3 AvailableOutputSummary (org.graylog2.rest.resources.streams.outputs.AvailableOutputSummary)3