Search in sources :

Example 6 with AlarmCallbackConfiguration

use of org.graylog2.alarmcallbacks.AlarmCallbackConfiguration in project graylog2-server by Graylog2.

the class StreamAlarmCallbackResource method update.

@PUT
@Path("/{alarmCallbackId}")
@Timed
@ApiOperation(value = "Update an alarm callback")
@Consumes(MediaType.APPLICATION_JSON)
@Produces(MediaType.APPLICATION_JSON)
@AuditEvent(type = AuditEventTypes.ALARM_CALLBACK_UPDATE)
public void update(@ApiParam(name = "streamid", value = "The stream id this alarm callback belongs to.", required = true) @PathParam("streamid") String streamid, @ApiParam(name = "alarmCallbackId", required = true) @PathParam("alarmCallbackId") String alarmCallbackId, @ApiParam(name = "JSON body", required = true) CreateAlarmCallbackRequest alarmCallbackRequest) throws NotFoundException {
    checkPermission(RestPermissions.STREAMS_EDIT, streamid);
    final AlarmCallbackConfiguration callbackConfiguration = alarmCallbackConfigurationService.load(alarmCallbackId);
    if (callbackConfiguration == null) {
        throw new NotFoundException("Unable to find alarm callback configuration " + alarmCallbackId);
    }
    final Map<String, Object> configuration = convertConfigurationValues(alarmCallbackRequest);
    final AlarmCallbackConfiguration updatedConfig = ((AlarmCallbackConfigurationImpl) callbackConfiguration).toBuilder().setTitle(alarmCallbackRequest.title()).setConfiguration(configuration).build();
    try {
        alarmCallbackFactory.create(updatedConfig).checkConfiguration();
        alarmCallbackConfigurationService.save(updatedConfig);
    } catch (ValidationException | AlarmCallbackConfigurationException | ConfigurationException e) {
        LOG.error("Invalid alarm callback configuration.", e);
        throw new BadRequestException(e.getMessage(), e);
    } catch (ClassNotFoundException e) {
        LOG.error("Invalid alarm callback type.", e);
        throw new BadRequestException("Invalid alarm callback type.", e);
    }
}
Also used : ValidationException(org.graylog2.plugin.database.ValidationException) AlarmCallbackConfigurationException(org.graylog2.plugin.alarms.callbacks.AlarmCallbackConfigurationException) ConfigurationException(org.graylog2.plugin.configuration.ConfigurationException) NotFoundException(org.graylog2.database.NotFoundException) BadRequestException(javax.ws.rs.BadRequestException) AlarmCallbackConfiguration(org.graylog2.alarmcallbacks.AlarmCallbackConfiguration) AlarmCallbackConfigurationException(org.graylog2.plugin.alarms.callbacks.AlarmCallbackConfigurationException) Path(javax.ws.rs.Path) Consumes(javax.ws.rs.Consumes) Produces(javax.ws.rs.Produces) Timed(com.codahale.metrics.annotation.Timed) ApiOperation(io.swagger.annotations.ApiOperation) AuditEvent(org.graylog2.audit.jersey.AuditEvent) PUT(javax.ws.rs.PUT)

Example 7 with AlarmCallbackConfiguration

use of org.graylog2.alarmcallbacks.AlarmCallbackConfiguration in project graylog2-server by Graylog2.

the class StreamAlarmCallbackResource method delete.

@DELETE
@Path("/{alarmCallbackId}")
@Timed
@ApiOperation(value = "Delete an alarm callback")
@ApiResponses(value = { @ApiResponse(code = 404, message = "Alarm callback not found."), @ApiResponse(code = 400, message = "Invalid ObjectId.") })
@AuditEvent(type = AuditEventTypes.ALARM_CALLBACK_DELETE)
public void delete(@ApiParam(name = "streamid", value = "The stream id this alarm callback belongs to.", required = true) @PathParam("streamid") String streamid, @ApiParam(name = "alarmCallbackId", required = true) @PathParam("alarmCallbackId") String alarmCallbackId) throws NotFoundException {
    checkPermission(RestPermissions.STREAMS_EDIT, streamid);
    final Stream stream = streamService.load(streamid);
    final AlarmCallbackConfiguration result = alarmCallbackConfigurationService.load(alarmCallbackId);
    if (result == null || !result.getStreamId().equals(stream.getId())) {
        throw new javax.ws.rs.NotFoundException("Couldn't find alarm callback " + alarmCallbackId + " in for steam " + streamid);
    }
    if (alarmCallbackConfigurationService.destroy(result) == 0) {
        final String msg = "Couldn't remove alarm callback with ID " + result.getId();
        LOG.error(msg);
        throw new InternalServerErrorException(msg);
    }
}
Also used : NotFoundException(org.graylog2.database.NotFoundException) InternalServerErrorException(javax.ws.rs.InternalServerErrorException) Stream(org.graylog2.plugin.streams.Stream) AlarmCallbackConfiguration(org.graylog2.alarmcallbacks.AlarmCallbackConfiguration) Path(javax.ws.rs.Path) DELETE(javax.ws.rs.DELETE) Timed(com.codahale.metrics.annotation.Timed) ApiOperation(io.swagger.annotations.ApiOperation) AuditEvent(org.graylog2.audit.jersey.AuditEvent) ApiResponses(io.swagger.annotations.ApiResponses)

Example 8 with AlarmCallbackConfiguration

use of org.graylog2.alarmcallbacks.AlarmCallbackConfiguration in project graylog2-server by Graylog2.

the class AlarmCallbackConfigurationServiceImplTest method testGetForStreamSingleDocument.

@Test
@MongoDBFixtures("alarmCallbackConfigurationsSingleDocument.json")
public void testGetForStreamSingleDocument() throws Exception {
    final Stream stream = mock(StreamImpl.class);
    final String streamId = "5400deadbeefdeadbeefaffe";
    when(stream.getId()).thenReturn(streamId);
    final List<AlarmCallbackConfiguration> configs = alarmCallbackConfigurationService.getForStream(stream);
    final AlarmCallbackConfiguration alarmCallback = configs.get(0);
    assertNotNull("Returned list should not be null", configs);
    assertEquals("Returned list should contain a single document", 1, configs.size());
    assertNotNull("Returned Alarm Callback should not be null", alarmCallback);
}
Also used : Stream(org.graylog2.plugin.streams.Stream) MongoDBFixtures(org.graylog.testing.mongodb.MongoDBFixtures) MongoDBServiceTest(org.graylog2.database.MongoDBServiceTest) Test(org.junit.Test)

Example 9 with AlarmCallbackConfiguration

use of org.graylog2.alarmcallbacks.AlarmCallbackConfiguration in project graylog2-server by Graylog2.

the class AlarmCallbackHistoryServiceImplTest method testSuccess.

@Test
public void testSuccess() throws Exception {
    final AlarmCallbackConfiguration alarmCallbackConfiguration = mockAlarmCallbackConfiguration(new Date());
    final Alert alert = mockAlert();
    final AlertCondition alertCondition = mockAlertCondition();
    final AlarmCallbackHistory alarmCallbackHistory = this.alarmCallbackHistoryService.success(alarmCallbackConfiguration, alert, alertCondition);
    verifyAlarmCallbackHistory(alarmCallbackHistory, alert, alertCondition);
    assertThat(alarmCallbackHistory.result()).isNotNull().isInstanceOf(AlarmCallbackSuccess.class);
    assertThat(alarmCallbackHistory.result().type()).isEqualTo("success");
}
Also used : AlertCondition(org.graylog2.plugin.alarms.AlertCondition) Alert(org.graylog2.alerts.Alert) Date(java.util.Date) MongoDBServiceTest(org.graylog2.database.MongoDBServiceTest) Test(org.junit.Test)

Example 10 with AlarmCallbackConfiguration

use of org.graylog2.alarmcallbacks.AlarmCallbackConfiguration in project graylog2-server by Graylog2.

the class V20161125161400_AlertReceiversMigrationTest method doMigrateMultipleQualifyingStreams.

@Test
public void doMigrateMultipleQualifyingStreams() throws Exception {
    final String matchingStreamId1 = new ObjectId().toHexString();
    final String matchingStreamId2 = new ObjectId().toHexString();
    final Stream stream1 = mock(Stream.class);
    when(stream1.getAlertReceivers()).thenReturn(Collections.emptyMap());
    final Stream stream2 = mock(Stream.class);
    when(stream2.getAlertReceivers()).thenReturn(ImmutableMap.of("users", ImmutableList.of("foouser"), "emails", ImmutableList.of("foo@bar.com")));
    when(stream2.getId()).thenReturn(matchingStreamId1);
    final Stream stream3 = mock(Stream.class);
    when(stream3.getAlertReceivers()).thenReturn(ImmutableMap.of("users", ImmutableList.of("foouser2")));
    when(stream3.getId()).thenReturn(matchingStreamId2);
    when(this.streamService.loadAll()).thenReturn(ImmutableList.of(stream1, stream2, stream3));
    final AlertCondition alertCondition1 = mock(AlertCondition.class);
    final AlertCondition alertCondition2 = mock(AlertCondition.class);
    when(this.streamService.getAlertConditions(eq(stream2))).thenReturn(ImmutableList.of(alertCondition1));
    when(this.streamService.getAlertConditions(eq(stream3))).thenReturn(ImmutableList.of(alertCondition2));
    final String alarmCallbackId1 = new ObjectId().toHexString();
    final AlarmCallbackConfiguration alarmCallback1 = AlarmCallbackConfigurationImpl.create(alarmCallbackId1, matchingStreamId1, EmailAlarmCallback.class.getCanonicalName(), "Email Alert Notification", new HashMap<>(), new Date(), "admin");
    final String alarmCallbackId2 = new ObjectId().toHexString();
    final AlarmCallbackConfiguration alarmCallback2 = AlarmCallbackConfigurationImpl.create(alarmCallbackId2, matchingStreamId2, EmailAlarmCallback.class.getCanonicalName(), "Email Alert Notification", new HashMap<>(), new Date(), "admin");
    final String alarmCallbackId3 = new ObjectId().toHexString();
    final AlarmCallbackConfiguration alarmCallback3 = AlarmCallbackConfigurationImpl.create(alarmCallbackId3, matchingStreamId2, EmailAlarmCallback.class.getCanonicalName(), "Email Alert Notification", new HashMap<>(), new Date(), "admin");
    final String alarmCallbackId4 = new ObjectId().toHexString();
    final AlarmCallbackConfiguration alarmCallback4 = AlarmCallbackConfigurationImpl.create(alarmCallbackId4, matchingStreamId2, HTTPAlarmCallback.class.getCanonicalName(), "Email Alert Notification", new HashMap<>(), new Date(), "admin");
    when(alarmCallbackConfigurationService.getForStream(eq(stream2))).thenReturn(ImmutableList.of(alarmCallback1));
    when(alarmCallbackConfigurationService.getForStream(eq(stream3))).thenReturn(ImmutableList.of(alarmCallback2, alarmCallback3, alarmCallback4));
    when(alarmCallbackConfigurationService.save(eq(alarmCallback1))).thenReturn(alarmCallbackId1);
    when(alarmCallbackConfigurationService.save(eq(alarmCallback2))).thenReturn(alarmCallbackId2);
    when(alarmCallbackConfigurationService.save(eq(alarmCallback3))).thenReturn(alarmCallbackId3);
    when(this.dbCollection.update(any(BasicDBObject.class), any(BasicDBObject.class))).thenReturn(new WriteResult(1, true, matchingStreamId1));
    when(this.dbCollection.update(any(BasicDBObject.class), any(BasicDBObject.class))).thenReturn(new WriteResult(1, true, matchingStreamId2));
    this.alertReceiversMigration.upgrade();
    final ArgumentCaptor<AlarmCallbackConfiguration> configurationArgumentCaptor = ArgumentCaptor.forClass(AlarmCallbackConfiguration.class);
    verify(this.alarmCallbackConfigurationService, times(3)).save(configurationArgumentCaptor.capture());
    final List<AlarmCallbackConfiguration> configurationValues = configurationArgumentCaptor.getAllValues();
    assertThat(configurationValues).isNotNull().isNotEmpty().hasSize(3).contains(alarmCallback1).contains(alarmCallback2).contains(alarmCallback3);
    for (AlarmCallbackConfiguration configurationValue : configurationValues) {
        if (configurationValue.getStreamId().equals(matchingStreamId1)) {
            assertThat(((List) configurationValue.getConfiguration().get(EmailAlarmCallback.CK_EMAIL_RECEIVERS)).size()).isEqualTo(1);
            assertThat(((List) configurationValue.getConfiguration().get(EmailAlarmCallback.CK_EMAIL_RECEIVERS)).get(0)).isEqualTo("foo@bar.com");
            assertThat(((List) configurationValue.getConfiguration().get(EmailAlarmCallback.CK_USER_RECEIVERS)).size()).isEqualTo(1);
            assertThat(((List) configurationValue.getConfiguration().get(EmailAlarmCallback.CK_USER_RECEIVERS)).get(0)).isEqualTo("foouser");
        }
        if (configurationValue.getStreamId().equals(matchingStreamId2)) {
            assertThat(configurationValue.getConfiguration().get(EmailAlarmCallback.CK_EMAIL_RECEIVERS)).isNull();
            assertThat(((List) configurationValue.getConfiguration().get(EmailAlarmCallback.CK_USER_RECEIVERS)).size()).isEqualTo(1);
            assertThat(((List) configurationValue.getConfiguration().get(EmailAlarmCallback.CK_USER_RECEIVERS)).get(0)).isEqualTo("foouser2");
        }
    }
    final ArgumentCaptor<BasicDBObject> queryCaptor = ArgumentCaptor.forClass(BasicDBObject.class);
    final ArgumentCaptor<BasicDBObject> updateCaptor = ArgumentCaptor.forClass(BasicDBObject.class);
    verify(this.dbCollection, times(2)).update(queryCaptor.capture(), updateCaptor.capture());
    final List<BasicDBObject> queries = queryCaptor.getAllValues();
    for (BasicDBObject query : queries) {
        final String streamId = (queries.indexOf(query) == 0 ? matchingStreamId1 : matchingStreamId2);
        assertThat(query.toJson()).isEqualTo("{\"_id\": {\"$oid\": \"" + streamId + "\"}}");
    }
    updateCaptor.getAllValues().forEach(update -> assertThat(update.toJson()).isEqualTo("{\"$unset\": {\"" + StreamImpl.FIELD_ALERT_RECEIVERS + "\": \"\"}}"));
    verifyMigrationCompletedWasPosted(ImmutableMap.of(matchingStreamId1, Optional.of(alarmCallbackId1), matchingStreamId2, Optional.of(alarmCallbackId2 + ", " + alarmCallbackId3)));
}
Also used : ObjectId(org.bson.types.ObjectId) Date(java.util.Date) BasicDBObject(com.mongodb.BasicDBObject) HTTPAlarmCallback(org.graylog2.alarmcallbacks.HTTPAlarmCallback) WriteResult(com.mongodb.WriteResult) AlertCondition(org.graylog2.plugin.alarms.AlertCondition) Stream(org.graylog2.plugin.streams.Stream) ImmutableList(com.google.common.collect.ImmutableList) List(java.util.List) EmailAlarmCallback(org.graylog2.alarmcallbacks.EmailAlarmCallback) AlarmCallbackConfiguration(org.graylog2.alarmcallbacks.AlarmCallbackConfiguration) Test(org.junit.Test)

Aggregations

AlarmCallbackConfiguration (org.graylog2.alarmcallbacks.AlarmCallbackConfiguration)18 Stream (org.graylog2.plugin.streams.Stream)15 AlertCondition (org.graylog2.plugin.alarms.AlertCondition)13 Test (org.junit.Test)12 Timed (com.codahale.metrics.annotation.Timed)8 ApiOperation (io.swagger.annotations.ApiOperation)8 ObjectId (org.bson.types.ObjectId)7 ValidationException (org.graylog2.plugin.database.ValidationException)7 Date (java.util.Date)6 List (java.util.List)6 Path (javax.ws.rs.Path)6 EmailAlarmCallback (org.graylog2.alarmcallbacks.EmailAlarmCallback)6 MongoDBServiceTest (org.graylog2.database.MongoDBServiceTest)6 BasicDBObject (com.mongodb.BasicDBObject)5 ApiResponses (io.swagger.annotations.ApiResponses)5 POST (javax.ws.rs.POST)5 Produces (javax.ws.rs.Produces)5 Alert (org.graylog2.alerts.Alert)5 AuditEvent (org.graylog2.audit.jersey.AuditEvent)5 AlarmCallback (org.graylog2.plugin.alarms.callbacks.AlarmCallback)5