Search in sources :

Example 11 with AlarmCallback

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

the class AlarmCallbackFactoryTest method testCreateByAlarmCallbackConfiguration.

@Test
public void testCreateByAlarmCallbackConfiguration() throws Exception {
    AlarmCallbackConfiguration configuration = mock(AlarmCallbackConfiguration.class);
    when(configuration.getType()).thenReturn(DummyAlarmCallback.class.getCanonicalName());
    AlarmCallback alarmCallback = alarmCallbackFactory.create(configuration);
    assertNotNull(alarmCallback);
    assertTrue(alarmCallback instanceof DummyAlarmCallback);
    assertEquals(dummyAlarmCallback, alarmCallback);
}
Also used : AlarmCallback(org.graylog2.plugin.alarms.callbacks.AlarmCallback) Test(org.junit.Test)

Example 12 with AlarmCallback

use of org.graylog2.plugin.alarms.callbacks.AlarmCallback 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 13 with AlarmCallback

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

the class AlertNotificationsSender method send.

public void send(AlertCondition.CheckResult result, Stream stream, Alert alert, AlertCondition alertCondition) {
    final List<AlarmCallbackConfiguration> callConfigurations = alarmCallbackConfigurationService.getForStream(stream);
    // Checking if alarm callbacks have been defined
    for (AlarmCallbackConfiguration configuration : callConfigurations) {
        AlarmCallbackHistory alarmCallbackHistory;
        AlarmCallback alarmCallback = null;
        try {
            alarmCallback = alarmCallbackFactory.create(configuration);
            alarmCallback.call(stream, result);
            alarmCallbackHistory = alarmCallbackHistoryService.success(configuration, alert, alertCondition);
        } catch (Exception e) {
            if (alarmCallback != null) {
                LOG.warn("Alarm callback <" + alarmCallback.getName() + "> failed. Skipping.", e);
            } else {
                LOG.warn("Alarm callback with id " + configuration.getId() + " failed. Skipping.", e);
            }
            alarmCallbackHistory = alarmCallbackHistoryService.error(configuration, alert, alertCondition, e.getMessage());
        }
        try {
            alarmCallbackHistoryService.save(alarmCallbackHistory);
        } catch (Exception e) {
            LOG.warn("Unable to save history of alarm callback run: ", e);
        }
    }
}
Also used : AlarmCallbackHistory(org.graylog2.alarmcallbacks.AlarmCallbackHistory) AlarmCallback(org.graylog2.plugin.alarms.callbacks.AlarmCallback) AlarmCallbackConfiguration(org.graylog2.alarmcallbacks.AlarmCallbackConfiguration)

Example 14 with AlarmCallback

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

the class AlarmCallbackFactory method create.

public AlarmCallback create(AlarmCallbackConfiguration configuration) throws ClassNotFoundException, AlarmCallbackConfigurationException {
    AlarmCallback alarmCallback = create(configuration.getType());
    alarmCallback.initialize(new Configuration(configuration.getConfiguration()));
    return alarmCallback;
}
Also used : Configuration(org.graylog2.plugin.configuration.Configuration) AlarmCallback(org.graylog2.plugin.alarms.callbacks.AlarmCallback)

Aggregations

AlarmCallback (org.graylog2.plugin.alarms.callbacks.AlarmCallback)10 AlarmCallbackConfiguration (org.graylog2.alarmcallbacks.AlarmCallbackConfiguration)6 Stream (org.graylog2.plugin.streams.Stream)6 Test (org.junit.Test)6 Timed (com.codahale.metrics.annotation.Timed)4 ApiOperation (io.swagger.annotations.ApiOperation)4 Path (javax.ws.rs.Path)4 ApiResponses (io.swagger.annotations.ApiResponses)3 POST (javax.ws.rs.POST)3 EmailAlarmCallback (org.graylog2.alarmcallbacks.EmailAlarmCallback)3 NoAuditEvent (org.graylog2.audit.jersey.NoAuditEvent)3 AlertCondition (org.graylog2.plugin.alarms.AlertCondition)3 BadRequestException (javax.ws.rs.BadRequestException)2 InternalServerErrorException (javax.ws.rs.InternalServerErrorException)2 ObjectId (org.bson.types.ObjectId)2 AbstractAlertCondition (org.graylog2.alerts.AbstractAlertCondition)2 DummyAlertCondition (org.graylog2.alerts.types.DummyAlertCondition)2 ImmutableList (com.google.common.collect.ImmutableList)1 UsingDataSet (com.lordofthejars.nosqlunit.annotation.UsingDataSet)1 BasicDBObject (com.mongodb.BasicDBObject)1