Search in sources :

Example 11 with MessageSummary

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

the class EmailSender method sendEmails.

// TODO: move EmailRecipients class to events code
void sendEmails(EmailEventNotificationConfig notificationConfig, EventNotificationContext ctx, ImmutableList<MessageSummary> backlog) throws TransportConfigurationException, EmailException, ConfigurationError {
    if (!emailFactory.isEmailTransportEnabled()) {
        throw new TransportConfigurationException("Email transport is not enabled in server configuration file!");
    }
    final EmailRecipients emailRecipients = emailRecipientsFactory.create(new ArrayList<>(notificationConfig.userRecipients()), new ArrayList<>(notificationConfig.emailRecipients()));
    if (emailRecipients.isEmpty()) {
        LOG.debug("Cannot send emails: empty recipient list.");
        return;
    }
    final Set<String> recipientsSet = emailRecipients.getEmailRecipients();
    if (recipientsSet.size() == 0) {
        final Notification notification = notificationService.buildNow().addNode(nodeId.toString()).addType(Notification.Type.GENERIC).addSeverity(Notification.Severity.NORMAL).addDetail("title", "No recipients have been defined!").addDetail("description", "To fix this, go to the notification configuration and add at least one alert recipient.");
        notificationService.publishIfFirst(notification);
    }
    final Map<String, Object> model = getModel(ctx, backlog, notificationConfig.timeZone());
    for (String email : recipientsSet) {
        sendEmail(notificationConfig, email, model);
    }
}
Also used : TransportConfigurationException(org.graylog2.plugin.alarms.transports.TransportConfigurationException) EmailRecipients(org.graylog2.alerts.EmailRecipients) Notification(org.graylog2.notifications.Notification)

Example 12 with MessageSummary

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

the class EventBacklogService method getMessagesForEvent.

public ImmutableList<MessageSummary> getMessagesForEvent(EventDto eventDto, long backlogSize) throws NotFoundException {
    if (backlogSize <= 0) {
        return ImmutableList.of();
    }
    final EventProcessor.Factory factory = eventProcessorFactories.get(eventDto.eventDefinitionType());
    if (factory == null) {
        throw new NotFoundException("Couldn't find event processor factory for type " + eventDto.eventDefinitionType());
    }
    final EventDefinition eventDefinition = eventDefinitionService.get(eventDto.eventDefinitionId()).orElseThrow(() -> new NotFoundException("Could not find event definintion <" + eventDto.eventDefinitionId() + ">"));
    final EventProcessor eventProcessor = factory.create(eventDefinition);
    final ImmutableList.Builder<MessageSummary> backlogBuilder = ImmutableList.builder();
    try {
        eventProcessor.sourceMessagesForEvent(Event.fromDto(eventDto), backlogBuilder::addAll, backlogSize);
    } catch (EventProcessorException e) {
        // TODO return this error, so it can be included in the notification message?
        LOG.error("Failed to query backlog messages for Event {}", eventDto.id(), e);
    }
    return backlogBuilder.build();
}
Also used : EventProcessorException(org.graylog.events.processor.EventProcessorException) ImmutableList(com.google.common.collect.ImmutableList) EventProcessor(org.graylog.events.processor.EventProcessor) NotFoundException(org.graylog2.database.NotFoundException) EventDefinition(org.graylog.events.processor.EventDefinition) MessageSummary(org.graylog2.plugin.MessageSummary)

Example 13 with MessageSummary

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

the class HTTPEventNotification method execute.

@Override
public void execute(EventNotificationContext ctx) throws TemporaryEventNotificationException, PermanentEventNotificationException {
    final HTTPEventNotificationConfig config = (HTTPEventNotificationConfig) ctx.notificationConfig();
    final HttpUrl httpUrl = HttpUrl.parse(config.url());
    if (httpUrl == null) {
        throw new TemporaryEventNotificationException("Malformed URL: <" + config.url() + "> in notification <" + ctx.notificationId() + ">");
    }
    ImmutableList<MessageSummary> backlog = notificationCallbackService.getBacklogForEvent(ctx);
    final EventNotificationModelData model = EventNotificationModelData.of(ctx, backlog);
    if (!whitelistService.isWhitelisted(config.url())) {
        if (!NotificationTestData.TEST_NOTIFICATION_ID.equals(ctx.notificationId())) {
            publishSystemNotificationForWhitelistFailure(config.url(), model.eventDefinitionTitle());
        }
        throw new TemporaryEventNotificationException("URL <" + config.url() + "> is not whitelisted.");
    }
    final byte[] body;
    try {
        body = objectMapper.writeValueAsBytes(model);
    } catch (JsonProcessingException e) {
        throw new PermanentEventNotificationException("Unable to serialize notification", e);
    }
    final Request request = new Request.Builder().url(httpUrl).post(RequestBody.create(CONTENT_TYPE, body)).build();
    LOG.debug("Requesting HTTP endpoint at <{}> in notification <{}>", config.url(), ctx.notificationId());
    try (final Response r = httpClient.newCall(request).execute()) {
        if (!r.isSuccessful()) {
            throw new PermanentEventNotificationException("Expected successful HTTP response [2xx] but got [" + r.code() + "]. " + config.url());
        }
    } catch (IOException e) {
        throw new PermanentEventNotificationException(e.getMessage());
    }
}
Also used : Request(okhttp3.Request) IOException(java.io.IOException) HttpUrl(okhttp3.HttpUrl) Response(okhttp3.Response) EventNotificationModelData(org.graylog.events.notifications.EventNotificationModelData) PermanentEventNotificationException(org.graylog.events.notifications.PermanentEventNotificationException) MessageSummary(org.graylog2.plugin.MessageSummary) TemporaryEventNotificationException(org.graylog.events.notifications.TemporaryEventNotificationException) JsonProcessingException(com.fasterxml.jackson.core.JsonProcessingException)

Aggregations

MessageSummary (org.graylog2.plugin.MessageSummary)11 Message (org.graylog2.plugin.Message)6 ResultMessage (org.graylog2.indexer.results.ResultMessage)4 Stream (org.graylog2.plugin.streams.Stream)4 PermanentEventNotificationException (org.graylog.events.notifications.PermanentEventNotificationException)3 AbstractAlertCondition (org.graylog2.alerts.AbstractAlertCondition)3 AlertCondition (org.graylog2.plugin.alarms.AlertCondition)3 InvalidRangeParametersException (org.graylog2.plugin.indexer.searches.timeranges.InvalidRangeParametersException)3 DateTime (org.joda.time.DateTime)3 ImmutableList (com.google.common.collect.ImmutableList)2 IOException (java.io.IOException)2 DummyAlertCondition (org.graylog2.alerts.types.DummyAlertCondition)2 SearchResult (org.graylog2.indexer.results.SearchResult)2 Sorting (org.graylog2.indexer.searches.Sorting)2 Configuration (org.graylog2.plugin.configuration.Configuration)2 AbsoluteRange (org.graylog2.plugin.indexer.searches.timeranges.AbsoluteRange)2 Test (org.junit.Test)2 JsonProcessingException (com.fasterxml.jackson.core.JsonProcessingException)1 JsonNode (com.fasterxml.jackson.databind.JsonNode)1 VisibleForTesting (com.google.common.annotations.VisibleForTesting)1