Search in sources :

Example 1 with DummyAlertCondition

use of org.graylog2.alerts.types.DummyAlertCondition in project graylog2-server by Graylog2.

the class StreamAlertResource method sendDummyAlert.

@POST
@Timed
@Path("sendDummyAlert")
@ApiOperation(value = "Send a test mail for a given stream")
@ApiResponses(value = { @ApiResponse(code = 404, message = "Stream not found."), @ApiResponse(code = 400, message = "Invalid ObjectId."), @ApiResponse(code = 400, message = "Stream has no alarm callbacks") })
@NoAuditEvent("only used to test alert emails")
public void sendDummyAlert(@ApiParam(name = "streamId", value = "The stream id the test alert should be sent for.", required = true) @PathParam("streamId") String streamId) throws TransportConfigurationException, EmailException, NotFoundException {
    checkPermission(RestPermissions.STREAMS_EDIT, streamId);
    final Stream stream = streamService.load(streamId);
    final DummyAlertCondition dummyAlertCondition = new DummyAlertCondition(stream, null, Tools.nowUTC(), getSubject().getPrincipal().toString(), Collections.emptyMap(), "Test Alert");
    try {
        AbstractAlertCondition.CheckResult checkResult = dummyAlertCondition.runCheck();
        List<AlarmCallbackConfiguration> callConfigurations = alarmCallbackConfigurationService.getForStream(stream);
        if (callConfigurations.size() == 0) {
            final String message = "Stream has no alarm callbacks, cannot send test alert.";
            LOG.warn(message);
            throw new BadRequestException(message);
        }
        for (AlarmCallbackConfiguration configuration : callConfigurations) {
            AlarmCallback alarmCallback = alarmCallbackFactory.create(configuration);
            alarmCallback.call(stream, checkResult);
        }
    } catch (AlarmCallbackException | ClassNotFoundException | AlarmCallbackConfigurationException e) {
        throw new InternalServerErrorException(e.getMessage(), e);
    }
}
Also used : AlarmCallbackConfigurationException(org.graylog2.plugin.alarms.callbacks.AlarmCallbackConfigurationException) BadRequestException(javax.ws.rs.BadRequestException) InternalServerErrorException(javax.ws.rs.InternalServerErrorException) Stream(org.graylog2.plugin.streams.Stream) AlarmCallback(org.graylog2.plugin.alarms.callbacks.AlarmCallback) AbstractAlertCondition(org.graylog2.alerts.AbstractAlertCondition) DummyAlertCondition(org.graylog2.alerts.types.DummyAlertCondition) AlarmCallbackException(org.graylog2.plugin.alarms.callbacks.AlarmCallbackException) 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 2 with DummyAlertCondition

use of org.graylog2.alerts.types.DummyAlertCondition in project graylog2-server by Graylog2.

the class HTTPAlarmCallbackTest method callThrowsAlarmCallbackExceptionIfURLIsMalformed.

@Test
public void callThrowsAlarmCallbackExceptionIfURLIsMalformed() throws Exception {
    final Configuration configuration = new Configuration(ImmutableMap.of("url", "!FOOBAR"));
    alarmCallback.initialize(configuration);
    final Stream stream = new StreamMock(Collections.singletonMap("_id", "stream-id"));
    final AlertCondition alertCondition = new DummyAlertCondition(stream, "alert-id", new DateTime(2017, 3, 29, 12, 0, DateTimeZone.UTC), "user", Collections.emptyMap(), "title");
    final AlertCondition.CheckResult checkResult = new AbstractAlertCondition.CheckResult(true, alertCondition, "Result Description", new DateTime(2016, 9, 6, 17, 0, DateTimeZone.UTC), Collections.emptyList());
    expectedException.expect(AlarmCallbackException.class);
    expectedException.expectMessage("Malformed URL: !FOOBAR");
    alarmCallback.call(stream, checkResult);
}
Also used : StreamMock(org.graylog2.streams.StreamMock) Configuration(org.graylog2.plugin.configuration.Configuration) DummyAlertCondition(org.graylog2.alerts.types.DummyAlertCondition) AbstractAlertCondition(org.graylog2.alerts.AbstractAlertCondition) AlertCondition(org.graylog2.plugin.alarms.AlertCondition) Stream(org.graylog2.plugin.streams.Stream) DummyAlertCondition(org.graylog2.alerts.types.DummyAlertCondition) DateTime(org.joda.time.DateTime) Test(org.junit.Test)

Example 3 with DummyAlertCondition

use of org.graylog2.alerts.types.DummyAlertCondition in project graylog2-server by Graylog2.

the class HTTPAlarmCallbackTest method callThrowsAlarmCallbackExceptionIfRemoteServerReturnsError.

@Test
public void callThrowsAlarmCallbackExceptionIfRemoteServerReturnsError() throws Exception {
    when(whitelistService.isWhitelisted(anyString())).thenReturn(true);
    server.enqueue(new MockResponse().setResponseCode(500));
    server.start();
    final Configuration configuration = new Configuration(ImmutableMap.of("url", server.url("/").toString()));
    alarmCallback.initialize(configuration);
    alarmCallback.checkConfiguration();
    final Stream stream = new StreamMock(Collections.singletonMap("_id", "stream-id"));
    final AlertCondition alertCondition = new DummyAlertCondition(stream, "alert-id", new DateTime(2017, 3, 29, 12, 0, DateTimeZone.UTC), "user", Collections.emptyMap(), "title");
    final AlertCondition.CheckResult checkResult = new AbstractAlertCondition.CheckResult(true, alertCondition, "Result Description", new DateTime(2016, 9, 6, 17, 0, DateTimeZone.UTC), Collections.emptyList());
    expectedException.expect(AlarmCallbackException.class);
    expectedException.expectMessage("Expected successful HTTP response [2xx] but got [500].");
    alarmCallback.call(stream, checkResult);
    final RecordedRequest request = server.takeRequest();
    assertThat(request.getPath()).isEqualTo("/");
    assertThat(request.getHeader("Content-Type")).isEqualTo("application/json");
    assertThat(request.getBodySize()).isPositive();
}
Also used : RecordedRequest(okhttp3.mockwebserver.RecordedRequest) MockResponse(okhttp3.mockwebserver.MockResponse) StreamMock(org.graylog2.streams.StreamMock) Configuration(org.graylog2.plugin.configuration.Configuration) DummyAlertCondition(org.graylog2.alerts.types.DummyAlertCondition) AbstractAlertCondition(org.graylog2.alerts.AbstractAlertCondition) AlertCondition(org.graylog2.plugin.alarms.AlertCondition) Stream(org.graylog2.plugin.streams.Stream) DummyAlertCondition(org.graylog2.alerts.types.DummyAlertCondition) DateTime(org.joda.time.DateTime) Test(org.junit.Test)

Example 4 with DummyAlertCondition

use of org.graylog2.alerts.types.DummyAlertCondition in project graylog2-server by Graylog2.

the class HTTPAlarmCallbackTest method callSucceedsIfRemoteRequestSucceeds.

@Test
public void callSucceedsIfRemoteRequestSucceeds() throws Exception {
    when(whitelistService.isWhitelisted(anyString())).thenReturn(true);
    server.enqueue(new MockResponse().setResponseCode(200));
    server.start();
    final Configuration configuration = new Configuration(ImmutableMap.of("url", server.url("/").toString()));
    alarmCallback.initialize(configuration);
    alarmCallback.checkConfiguration();
    final Stream stream = new StreamMock(ImmutableMap.of("_id", "stream-id", "title", "Stream Title", "description", "Stream Description"), ImmutableList.of());
    final AlertCondition alertCondition = new DummyAlertCondition(stream, "condition-id", new DateTime(2016, 9, 6, 17, 0, DateTimeZone.UTC), "user", ImmutableMap.of(), "Alert Condition Title");
    final List<MessageSummary> messageSummaries = ImmutableList.of(new MessageSummary("graylog_1", new Message("Test message 1", "source1", new DateTime(2016, 9, 6, 17, 0, DateTimeZone.UTC))), new MessageSummary("graylog_2", new Message("Test message 2", "source2", new DateTime(2016, 9, 6, 17, 0, DateTimeZone.UTC))));
    final AlertCondition.CheckResult checkResult = new AbstractAlertCondition.CheckResult(true, alertCondition, "Result Description", new DateTime(2016, 9, 6, 17, 0, DateTimeZone.UTC), messageSummaries);
    alarmCallback.call(stream, checkResult);
    final RecordedRequest request = server.takeRequest();
    assertThat(request.getPath()).isEqualTo("/");
    assertThat(request.getHeader("Content-Type")).isEqualTo("application/json");
    assertThat(request.getBodySize()).isPositive();
    final String requestBody = request.getBody().readUtf8();
    final JsonNode jsonNode = objectMapper.readTree(requestBody);
    assertThat(jsonNode.get("check_result").get("matching_messages").size()).isEqualTo(2);
    assertThat(jsonNode.get("check_result").get("triggered").asBoolean()).isTrue();
    assertThat(jsonNode.get("check_result").get("triggered_at").asText()).isEqualTo("2016-09-06T17:00:00.000Z");
    assertThat(jsonNode.get("stream").get("id").asText()).isEqualTo("stream-id");
}
Also used : RecordedRequest(okhttp3.mockwebserver.RecordedRequest) MockResponse(okhttp3.mockwebserver.MockResponse) Configuration(org.graylog2.plugin.configuration.Configuration) Message(org.graylog2.plugin.Message) JsonNode(com.fasterxml.jackson.databind.JsonNode) ArgumentMatchers.anyString(org.mockito.ArgumentMatchers.anyString) DateTime(org.joda.time.DateTime) StreamMock(org.graylog2.streams.StreamMock) DummyAlertCondition(org.graylog2.alerts.types.DummyAlertCondition) AbstractAlertCondition(org.graylog2.alerts.AbstractAlertCondition) AlertCondition(org.graylog2.plugin.alarms.AlertCondition) Stream(org.graylog2.plugin.streams.Stream) MessageSummary(org.graylog2.plugin.MessageSummary) DummyAlertCondition(org.graylog2.alerts.types.DummyAlertCondition) Test(org.junit.Test)

Example 5 with DummyAlertCondition

use of org.graylog2.alerts.types.DummyAlertCondition 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)

Aggregations

AbstractAlertCondition (org.graylog2.alerts.AbstractAlertCondition)5 DummyAlertCondition (org.graylog2.alerts.types.DummyAlertCondition)5 Stream (org.graylog2.plugin.streams.Stream)5 AlertCondition (org.graylog2.plugin.alarms.AlertCondition)3 Configuration (org.graylog2.plugin.configuration.Configuration)3 StreamMock (org.graylog2.streams.StreamMock)3 DateTime (org.joda.time.DateTime)3 Test (org.junit.Test)3 Timed (com.codahale.metrics.annotation.Timed)2 ApiOperation (io.swagger.annotations.ApiOperation)2 ApiResponses (io.swagger.annotations.ApiResponses)2 InternalServerErrorException (javax.ws.rs.InternalServerErrorException)2 POST (javax.ws.rs.POST)2 Path (javax.ws.rs.Path)2 MockResponse (okhttp3.mockwebserver.MockResponse)2 RecordedRequest (okhttp3.mockwebserver.RecordedRequest)2 AlarmCallbackConfiguration (org.graylog2.alarmcallbacks.AlarmCallbackConfiguration)2 NoAuditEvent (org.graylog2.audit.jersey.NoAuditEvent)2 AlarmCallback (org.graylog2.plugin.alarms.callbacks.AlarmCallback)2 JsonNode (com.fasterxml.jackson.databind.JsonNode)1