Search in sources :

Example 26 with AlertCondition

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

the class AlarmCallbackHistoryServiceImplTest method testSaveForDummySuccess.

@Test
public void testSaveForDummySuccess() throws Exception {
    final Date createdAt = DateTime.parse(CFG_CREATED_AT).toDate();
    final AlarmCallbackConfiguration alarmCallbackConfiguration = mockAlarmCallbackConfiguration(createdAt);
    final Alert alert = mockAlert();
    final AlertCondition alertCondition = mockAlertCondition();
    final AlarmCallbackHistory success = this.alarmCallbackHistoryService.success(alarmCallbackConfiguration, alert, alertCondition);
    this.alarmCallbackHistoryService.save(success);
    MongoCollection<Document> collection = mongodb.mongoConnection().getMongoDatabase().getCollection(collectionName);
    Document document = collection.find().first();
    Document configuration = document.get("alarmcallbackconfiguration", Document.class);
    Document result = document.get("result", Document.class);
    assertThat(document.get("alert_id")).isEqualTo(ALERT_ID);
    assertThat(document.get("alertcondition_id")).isEqualTo(ALERT_CONDITION_ID);
    assertThat(configuration.get("id")).isEqualTo(CFG_ID);
    assertThat(configuration.get("type")).isEqualTo(CFG_TYPE);
    assertThat(configuration.get("stream_id")).isEqualTo(CFG_STREAM_ID);
    assertThat(configuration.get("creator_user_id")).isEqualTo(CFG_USER);
    assertThat(result.get("type")).isEqualTo("success");
}
Also used : AlertCondition(org.graylog2.plugin.alarms.AlertCondition) Alert(org.graylog2.alerts.Alert) Document(org.bson.Document) Date(java.util.Date) MongoDBServiceTest(org.graylog2.database.MongoDBServiceTest) Test(org.junit.Test)

Example 27 with AlertCondition

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

the class AlertNotificationsSenderTest method executeStreamWithNotifications.

@Test
public void executeStreamWithNotifications() throws Exception {
    final Stream stream = mock(Stream.class);
    final Alert alert = mock(Alert.class);
    final AlertCondition alertCondition = mock(AlertCondition.class);
    final AlertCondition.CheckResult positiveCheckResult = new AbstractAlertCondition.CheckResult(true, alertCondition, "Mocked positive CheckResult", Tools.nowUTC(), Collections.emptyList());
    final AlarmCallbackConfiguration alarmCallbackConfiguration = mock(AlarmCallbackConfiguration.class);
    when(alarmCallbackConfigurationService.getForStream(eq(stream))).thenReturn(ImmutableList.of(alarmCallbackConfiguration));
    final AlarmCallback alarmCallback = mock(AlarmCallback.class);
    when(alarmCallbackFactory.create(eq(alarmCallbackConfiguration))).thenReturn(alarmCallback);
    alertNotificationsSender.send(positiveCheckResult, stream, alert, alertCondition);
    final ArgumentCaptor<Stream> streamCaptor = ArgumentCaptor.forClass(Stream.class);
    final ArgumentCaptor<AlertCondition.CheckResult> checkResultCaptor = ArgumentCaptor.forClass(AlertCondition.CheckResult.class);
    verify(alarmCallback, times(1)).call(streamCaptor.capture(), checkResultCaptor.capture());
    assertThat(streamCaptor.getValue()).isEqualTo(stream);
    assertThat(checkResultCaptor.getValue()).isEqualTo(positiveCheckResult);
    final ArgumentCaptor<AlarmCallbackConfiguration> alarmCallbackConfigurationCaptor = ArgumentCaptor.forClass(AlarmCallbackConfiguration.class);
    final ArgumentCaptor<Alert> alertCaptor = ArgumentCaptor.forClass(Alert.class);
    final ArgumentCaptor<AlertCondition> alertConditionCaptor = ArgumentCaptor.forClass(AlertCondition.class);
    verify(alarmCallbackHistoryService, times(1)).success(alarmCallbackConfigurationCaptor.capture(), alertCaptor.capture(), alertConditionCaptor.capture());
    assertThat(alarmCallbackConfigurationCaptor.getValue()).isEqualTo(alarmCallbackConfiguration);
    assertThat(alertCaptor.getValue()).isEqualTo(alert);
    assertThat(alertConditionCaptor.getValue()).isEqualTo(alertCondition);
}
Also used : AlertCondition(org.graylog2.plugin.alarms.AlertCondition) Stream(org.graylog2.plugin.streams.Stream) AlarmCallback(org.graylog2.plugin.alarms.callbacks.AlarmCallback) AlarmCallbackConfiguration(org.graylog2.alarmcallbacks.AlarmCallbackConfiguration) Test(org.junit.Test)

Example 28 with AlertCondition

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

the class AlertServiceImplTest method shouldNotRepeatNotificationsWhenOptionIsDisabled.

@Test
@MongoDBFixtures("unresolved-alert.json")
public void shouldNotRepeatNotificationsWhenOptionIsDisabled() throws Exception {
    final AlertCondition alertCondition = mock(AlertCondition.class);
    when(alertCondition.shouldRepeatNotifications()).thenReturn(false);
    final Alert alert = alertService.load(ALERT_ID, STREAM_ID);
    assertThat(alertService.shouldRepeatNotifications(alertCondition, alert)).isFalse();
}
Also used : AlertCondition(org.graylog2.plugin.alarms.AlertCondition) MongoDBFixtures(org.graylog.testing.mongodb.MongoDBFixtures) MongoDBServiceTest(org.graylog2.database.MongoDBServiceTest) Test(org.junit.Test)

Example 29 with AlertCondition

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

the class AlertServiceImplTest method repeatNotificationsOptionShouldComplyWithGracePeriod.

@Test
@MongoDBFixtures("unresolved-alert.json")
public void repeatNotificationsOptionShouldComplyWithGracePeriod() throws Exception {
    final AlertCondition alertCondition = mock(AlertCondition.class);
    when(alertCondition.getGrace()).thenReturn(15);
    when(alertCondition.shouldRepeatNotifications()).thenReturn(true);
    final Alert alert = alertService.load(ALERT_ID, STREAM_ID);
    // Should repeat notification when there was no previous alert
    assertThat(alertService.shouldRepeatNotifications(alertCondition, alert)).isTrue();
    final AlarmCallbackHistory alarmCallbackHistory = mock(AlarmCallbackHistory.class);
    when(alarmCallbackHistory.createdAt()).thenReturn(DateTime.now(DateTimeZone.UTC).minusMinutes(14));
    when(alarmCallbackHistoryService.getForAlertId(ALERT_ID)).thenReturn(ImmutableList.of(alarmCallbackHistory));
    // Should not repeat notification if a notification was sent during grace period
    assertThat(alertService.shouldRepeatNotifications(alertCondition, alert)).isFalse();
    when(alarmCallbackHistory.createdAt()).thenReturn(DateTime.now(DateTimeZone.UTC).minusMinutes(15));
    when(alarmCallbackHistoryService.getForAlertId(ALERT_ID)).thenReturn(ImmutableList.of(alarmCallbackHistory));
    // Should repeat notification after grace period passed
    assertThat(alertService.shouldRepeatNotifications(alertCondition, alert)).isTrue();
}
Also used : AlarmCallbackHistory(org.graylog2.alarmcallbacks.AlarmCallbackHistory) AlertCondition(org.graylog2.plugin.alarms.AlertCondition) MongoDBFixtures(org.graylog.testing.mongodb.MongoDBFixtures) MongoDBServiceTest(org.graylog2.database.MongoDBServiceTest) Test(org.junit.Test)

Example 30 with AlertCondition

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

the class AlertScannerTest method testCheckTriggersAlertIfPreviousIsResolved.

@Test
public void testCheckTriggersAlertIfPreviousIsResolved() throws Exception {
    final Stream stream = mock(Stream.class);
    final AlertCondition alertCondition = mock(AlertCondition.class);
    when(alertService.inGracePeriod(eq(alertCondition))).thenReturn(false);
    final AlertCondition.CheckResult positiveCheckResult = new AbstractAlertCondition.CheckResult(true, alertCondition, "Mocked positive CheckResult", Tools.nowUTC(), Collections.emptyList());
    when(alertCondition.runCheck()).thenReturn(positiveCheckResult);
    final Alert alert = mock(Alert.class);
    final Alert previousAlert = mock(Alert.class);
    when(alertService.getLastTriggeredAlert(stream.getId(), alertCondition.getId())).thenReturn(Optional.of(previousAlert));
    when(alertService.isResolved(previousAlert)).thenReturn(true);
    when(alertService.factory(eq(positiveCheckResult))).thenReturn(alert);
    assertThat(this.alertScanner.checkAlertCondition(stream, alertCondition)).isTrue();
    verify(alertCondition, times(1)).runCheck();
    verify(alertNotificationsSender, times(1)).send(positiveCheckResult, stream, alert, alertCondition);
    verify(alertService, never()).resolveAlert(alert);
}
Also used : AlertCondition(org.graylog2.plugin.alarms.AlertCondition) Stream(org.graylog2.plugin.streams.Stream) Test(org.junit.Test)

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