Search in sources :

Example 16 with Notification

use of org.graylog2.notifications.Notification in project graylog2-server by Graylog2.

the class EventDefinitionFacadeTest method createNativeEntity.

@Test
public void createNativeEntity() {
    final EntityV1 entityV1 = createTestEntity();
    final NotificationDto notificationDto = NotificationDto.builder().config(HTTPEventNotificationConfig.builder().url("https://hulud.net").build()).title("Notify me Senpai").description("A notification for senpai").id("dead-beef").build();
    final EntityDescriptor entityDescriptor = EntityDescriptor.create("123123", ModelTypes.NOTIFICATION_V1);
    final ImmutableMap<EntityDescriptor, Object> nativeEntities = ImmutableMap.of(entityDescriptor, notificationDto);
    final JobDefinitionDto jobDefinitionDto = mock(JobDefinitionDto.class);
    final JobTriggerDto jobTriggerDto = mock(JobTriggerDto.class);
    when(jobDefinitionDto.id()).thenReturn("job-123123");
    when(jobSchedulerClock.nowUTC()).thenReturn(DateTime.now(DateTimeZone.UTC));
    when(jobDefinitionService.save(any(JobDefinitionDto.class))).thenReturn(jobDefinitionDto);
    when(jobTriggerService.create(any(JobTriggerDto.class))).thenReturn(jobTriggerDto);
    final UserImpl kmerzUser = new UserImpl(mock(PasswordAlgorithmFactory.class), new Permissions(ImmutableSet.of()), ImmutableMap.of("username", "kmerz"));
    when(userService.load("kmerz")).thenReturn(kmerzUser);
    final NativeEntity<EventDefinitionDto> nativeEntity = facade.createNativeEntity(entityV1, ImmutableMap.of(), nativeEntities, "kmerz");
    assertThat(nativeEntity).isNotNull();
    final EventDefinitionDto eventDefinitionDto = nativeEntity.entity();
    assertThat(eventDefinitionDto.title()).isEqualTo("title");
    assertThat(eventDefinitionDto.description()).isEqualTo("description");
    assertThat(eventDefinitionDto.config().type()).isEqualTo("aggregation-v1");
    // verify that ownership was registered for this entity
    verify(entityOwnershipService, times(1)).registerNewEventDefinition(nativeEntity.entity().id(), kmerzUser);
}
Also used : EntityV1(org.graylog2.contentpacks.model.entities.EntityV1) EntityDescriptor(org.graylog2.contentpacks.model.entities.EntityDescriptor) NativeEntityDescriptor(org.graylog2.contentpacks.model.entities.NativeEntityDescriptor) PasswordAlgorithmFactory(org.graylog2.security.PasswordAlgorithmFactory) NotificationDto(org.graylog.events.notifications.NotificationDto) EventDefinitionDto(org.graylog.events.processor.EventDefinitionDto) JobDefinitionDto(org.graylog.scheduler.JobDefinitionDto) UserImpl(org.graylog2.users.UserImpl) Permissions(org.graylog2.shared.security.Permissions) JobTriggerDto(org.graylog.scheduler.JobTriggerDto) Test(org.junit.Test)

Example 17 with Notification

use of org.graylog2.notifications.Notification 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)

Example 18 with Notification

use of org.graylog2.notifications.Notification in project graylog2-server by Graylog2.

the class V20161125161400_AlertReceiversMigrationTest method doMigrateSingleQualifyingStream.

@Test
public void doMigrateSingleQualifyingStream() throws Exception {
    final String matchingStreamId = 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(matchingStreamId);
    when(this.streamService.loadAll()).thenReturn(ImmutableList.of(stream1, stream2));
    final AlertCondition alertCondition = mock(AlertCondition.class);
    when(this.streamService.getAlertConditions(eq(stream2))).thenReturn(ImmutableList.of(alertCondition));
    final String alarmCallbackId = new ObjectId().toHexString();
    final AlarmCallbackConfiguration alarmCallback = AlarmCallbackConfigurationImpl.create(alarmCallbackId, matchingStreamId, EmailAlarmCallback.class.getCanonicalName(), "Email Alert Notification", new HashMap<>(), new Date(), "admin");
    when(alarmCallbackConfigurationService.getForStream(eq(stream2))).thenReturn(ImmutableList.of(alarmCallback));
    when(alarmCallbackConfigurationService.save(eq(alarmCallback))).thenReturn(alarmCallbackId);
    when(this.dbCollection.update(any(BasicDBObject.class), any(BasicDBObject.class))).thenReturn(new WriteResult(1, true, matchingStreamId));
    this.alertReceiversMigration.upgrade();
    final ArgumentCaptor<AlarmCallbackConfiguration> configurationArgumentCaptor = ArgumentCaptor.forClass(AlarmCallbackConfiguration.class);
    verify(this.alarmCallbackConfigurationService, times(1)).save(configurationArgumentCaptor.capture());
    final AlarmCallbackConfiguration updatedConfiguration = configurationArgumentCaptor.getValue();
    assertThat(updatedConfiguration).isEqualTo(alarmCallback);
    assertThat(updatedConfiguration.getType()).isEqualTo(EmailAlarmCallback.class.getCanonicalName());
    assertThat(((List) updatedConfiguration.getConfiguration().get(EmailAlarmCallback.CK_EMAIL_RECEIVERS)).size()).isEqualTo(1);
    assertThat(((List) updatedConfiguration.getConfiguration().get(EmailAlarmCallback.CK_EMAIL_RECEIVERS)).get(0)).isEqualTo("foo@bar.com");
    assertThat(((List) updatedConfiguration.getConfiguration().get(EmailAlarmCallback.CK_USER_RECEIVERS)).size()).isEqualTo(1);
    assertThat(((List) updatedConfiguration.getConfiguration().get(EmailAlarmCallback.CK_USER_RECEIVERS)).get(0)).isEqualTo("foouser");
    final ArgumentCaptor<BasicDBObject> queryCaptor = ArgumentCaptor.forClass(BasicDBObject.class);
    final ArgumentCaptor<BasicDBObject> updateCaptor = ArgumentCaptor.forClass(BasicDBObject.class);
    verify(this.dbCollection, times(1)).update(queryCaptor.capture(), updateCaptor.capture());
    assertThat(queryCaptor.getValue().toJson()).isEqualTo("{\"_id\": {\"$oid\": \"" + matchingStreamId + "\"}}");
    assertThat(updateCaptor.getValue().toJson()).isEqualTo("{\"$unset\": {\"" + StreamImpl.FIELD_ALERT_RECEIVERS + "\": \"\"}}");
    verifyMigrationCompletedWasPosted(ImmutableMap.of(matchingStreamId, Optional.of(alarmCallbackId)));
}
Also used : ObjectId(org.bson.types.ObjectId) Date(java.util.Date) BasicDBObject(com.mongodb.BasicDBObject) 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)

Example 19 with Notification

use of org.graylog2.notifications.Notification in project graylog2-server by Graylog2.

the class IndexerSetupService method startUp.

@Override
protected void startUp() throws Exception {
    Tools.silenceUncaughtExceptionsInThisThread();
    LOG.debug("Starting indexer");
    node.start();
    final Client client = node.client();
    try {
        /* try to determine the cluster health. if this times out we could not connect and try to determine if there's
                   anything listening at all. if that happens this usually has these reasons:
                    1. cluster.name is different
                    2. network.publish_host is not reachable
                    3. wrong address configured
                    4. multicast in use but broken in this environment
                   we handle a red cluster state differently because if we can get that result it means the cluster itself
                   is reachable, which is a completely different problem from not being able to join at all.
                 */
        final ClusterHealthRequest atLeastRed = client.admin().cluster().prepareHealth().setWaitForStatus(ClusterHealthStatus.RED).request();
        final ClusterHealthResponse health = client.admin().cluster().health(atLeastRed).actionGet(configuration.getClusterDiscoveryTimeout(), MILLISECONDS);
        // we don't get here if we couldn't join the cluster. just check for red cluster state
        if (ClusterHealthStatus.RED.equals(health.getStatus())) {
            final Notification notification = notificationService.buildNow().addSeverity(Notification.Severity.URGENT).addType(Notification.Type.ES_CLUSTER_RED);
            notificationService.publishIfFirst(notification);
            LOG.warn("The Elasticsearch cluster state is RED which means shards are unassigned.");
            LOG.info("This usually indicates a crashed and corrupt cluster and needs to be investigated. Graylog will write into the local disk journal.");
            LOG.info("See {} for details.", DocsHelper.PAGE_ES_CONFIGURATION);
        }
        if (ClusterHealthStatus.GREEN.equals(health.getStatus())) {
            notificationService.fixed(Notification.Type.ES_CLUSTER_RED);
        }
        notificationService.fixed(Notification.Type.ES_UNAVAILABLE);
    } catch (ElasticsearchTimeoutException e) {
        final String hosts = node.settings().get("discovery.zen.ping.unicast.hosts");
        if (!isNullOrEmpty(hosts)) {
            final Iterable<String> hostList = Splitter.on(',').omitEmptyStrings().trimResults().split(hosts);
            for (String host : hostList) {
                final URI esUri = URI.create("http://" + HostAndPort.fromString(host).getHostText() + ":9200/");
                LOG.info("Checking Elasticsearch HTTP API at {}", esUri);
                // Try the HTTP API endpoint
                final Request request = new Request.Builder().get().url(esUri.resolve("/_nodes").toString()).build();
                try (final Response response = httpClient.newCall(request).execute()) {
                    if (response.isSuccessful()) {
                        final JsonNode resultTree = objectMapper.readTree(response.body().byteStream());
                        final JsonNode nodesList = resultTree.get("nodes");
                        if (!configuration.isDisableVersionCheck()) {
                            final Iterator<String> nodes = nodesList.fieldNames();
                            while (nodes.hasNext()) {
                                final String id = nodes.next();
                                final Version clusterVersion = Version.fromString(nodesList.get(id).get("version").textValue());
                                checkClusterVersion(clusterVersion);
                            }
                        }
                        final String clusterName = resultTree.get("cluster_name").textValue();
                        checkClusterName(clusterName);
                    } else {
                        LOG.error("Could not connect to Elasticsearch at " + esUri + ". Is it running?");
                    }
                } catch (IOException ioException) {
                    LOG.error("Could not connect to Elasticsearch: {}", ioException.getMessage());
                }
            }
        }
        final Notification notification = notificationService.buildNow().addSeverity(Notification.Severity.URGENT).addType(Notification.Type.ES_UNAVAILABLE);
        notificationService.publishIfFirst(notification);
        LOG.warn("Could not connect to Elasticsearch");
        LOG.info("If you're using multicast, check that it is working in your network and that Elasticsearch is accessible. Also check that the cluster name setting is correct.");
        LOG.info("See {} for details.", DocsHelper.PAGE_ES_CONFIGURATION);
    }
}
Also used : ClusterHealthResponse(org.elasticsearch.action.admin.cluster.health.ClusterHealthResponse) ClusterHealthRequest(org.elasticsearch.action.admin.cluster.health.ClusterHealthRequest) ThreadFactoryBuilder(com.google.common.util.concurrent.ThreadFactoryBuilder) Request(okhttp3.Request) ClusterHealthRequest(org.elasticsearch.action.admin.cluster.health.ClusterHealthRequest) JsonNode(com.fasterxml.jackson.databind.JsonNode) IOException(java.io.IOException) URI(java.net.URI) Notification(org.graylog2.notifications.Notification) Response(okhttp3.Response) ClusterHealthResponse(org.elasticsearch.action.admin.cluster.health.ClusterHealthResponse) ElasticsearchTimeoutException(org.elasticsearch.ElasticsearchTimeoutException) Version(org.elasticsearch.Version) Iterator(java.util.Iterator) Client(org.elasticsearch.client.Client) OkHttpClient(okhttp3.OkHttpClient)

Example 20 with Notification

use of org.graylog2.notifications.Notification in project graylog2-server by Graylog2.

the class IndexRangesMigrationPeriodical method doRun.

@Override
public void doRun() {
    final MongoIndexRangesMigrationComplete migrationComplete = clusterConfigService.get(MongoIndexRangesMigrationComplete.class);
    if (migrationComplete != null && migrationComplete.complete) {
        LOG.debug("Migration of index ranges (pre Graylog 1.2.2) already complete. Skipping migration process.");
        return;
    }
    while (!cluster.isConnected() || !cluster.isHealthy()) {
        Uninterruptibles.sleepUninterruptibly(5, TimeUnit.SECONDS);
    }
    final Set<String> indexNames = ImmutableSet.copyOf(indexSetRegistry.getManagedIndices());
    // Migrate old MongoDB index ranges
    final SortedSet<IndexRange> mongoIndexRanges = legacyMongoIndexRangeService.findAll();
    for (IndexRange indexRange : mongoIndexRanges) {
        if (indexNames.contains(indexRange.indexName())) {
            LOG.info("Migrating index range from MongoDB: {}", indexRange);
            indexRangeService.save(indexRange);
        } else {
            LOG.info("Removing stale index range from MongoDB: {}", indexRange);
        }
        legacyMongoIndexRangeService.delete(indexRange.indexName());
    }
    // Check whether all index ranges have been migrated
    final int numberOfIndices = indexNames.size();
    final SortedSet<IndexRange> allIndexRanges = indexRangeService.findAll();
    final int numberOfIndexRanges = allIndexRanges.size();
    if (numberOfIndices > numberOfIndexRanges) {
        LOG.info("There are more indices ({}) than there are index ranges ({}). Notifying administrator.", numberOfIndices, numberOfIndexRanges);
        // remove all present index names so we can display the index sets that need manual fixing
        final Set<String> extraIndices = Sets.newHashSet(indexNames);
        allIndexRanges.forEach(indexRange -> extraIndices.remove(indexRange.indexName()));
        final Set<String> affectedIndexSetNames = extraIndices.stream().map(indexSetRegistry::getForIndex).filter(Optional::isPresent).map(Optional::get).map(IndexSet::getConfig).map(IndexSetConfig::title).collect(Collectors.toSet());
        final Notification notification = notificationService.buildNow().addSeverity(Notification.Severity.URGENT).addType(Notification.Type.INDEX_RANGES_RECALCULATION).addDetail("indices", numberOfIndices).addDetail("index_ranges", numberOfIndexRanges).addDetail("index_sets", affectedIndexSetNames.isEmpty() ? null : Joiner.on(", ").join(affectedIndexSetNames));
        notificationService.publishIfFirst(notification);
    }
    clusterConfigService.write(new MongoIndexRangesMigrationComplete(true));
}
Also used : IndexRange(org.graylog2.indexer.ranges.IndexRange) Optional(java.util.Optional) IndexSet(org.graylog2.indexer.IndexSet) Notification(org.graylog2.notifications.Notification)

Aggregations

Notification (org.graylog2.notifications.Notification)28 Test (org.junit.Test)7 ImmutableList (com.google.common.collect.ImmutableList)6 ApiOperation (io.swagger.annotations.ApiOperation)6 NotificationDto (org.graylog.events.notifications.NotificationDto)6 Map (java.util.Map)5 EventDefinitionDto (org.graylog.events.processor.EventDefinitionDto)5 AlertCondition (org.graylog2.plugin.alarms.AlertCondition)5 Activity (org.graylog2.shared.system.activities.Activity)5 Timed (com.codahale.metrics.annotation.Timed)4 List (java.util.List)4 Path (javax.ws.rs.Path)4 JobDefinitionDto (org.graylog.scheduler.JobDefinitionDto)4 EntityV1 (org.graylog2.contentpacks.model.entities.EntityV1)4 NotFoundException (org.graylog2.database.NotFoundException)4 MessageSummary (org.graylog2.plugin.MessageSummary)4 TransportConfigurationException (org.graylog2.plugin.alarms.transports.TransportConfigurationException)4 JsonNode (com.fasterxml.jackson.databind.JsonNode)3 VisibleForTesting (com.google.common.annotations.VisibleForTesting)3 ApiResponses (io.swagger.annotations.ApiResponses)3