Search in sources :

Example 1 with EventDto

use of org.graylog.events.event.EventDto in project graylog2-server by Graylog2.

the class EventNotificationExecutionJob method execute.

@Override
public JobTriggerUpdate execute(JobExecutionContext ctx) throws JobExecutionException {
    Optional<EventDefinitionDto> optionalEventDefinition;
    long gracePeriodInMS = 0;
    final JobTriggerDto trigger = ctx.trigger();
    final Optional<Data> optionalData = trigger.data().map(d -> (Data) d);
    if (!optionalData.isPresent()) {
        throw new JobExecutionException("Missing notification job data for notification <" + jobConfig.notificationId() + ">, unable to execute notification: " + ctx.definition().title(), trigger, JobTriggerUpdate.withoutNextTime());
    }
    final Data data = optionalData.get();
    final EventDto eventDto = data.eventDto();
    final NotificationDto notification = notificationService.get(jobConfig.notificationId()).orElseThrow(() -> new JobExecutionException("Couldn't find notification <" + jobConfig.notificationId() + ">", trigger, JobTriggerUpdate.withError(trigger)));
    final EventNotification.Factory eventNotificationFactory = eventNotificationFactories.get(notification.config().type());
    if (eventNotificationFactory == null) {
        throw new JobExecutionException("Couldn't find factory for notification type <" + notification.config().type() + ">", trigger, ctx.jobTriggerUpdates().scheduleNextExecution());
    }
    final EventNotification eventNotification = eventNotificationFactory.create();
    metrics.registerEventNotification(eventNotification, notification);
    try {
        optionalEventDefinition = Optional.ofNullable(getEventDefinition(eventDto));
        if (optionalEventDefinition.isPresent()) {
            gracePeriodInMS = optionalEventDefinition.get().notificationSettings().gracePeriodMs();
        }
    } catch (NotFoundException e) {
        LOG.error("Couldn't find event definition with ID <{}>.", eventDto.eventDefinitionId());
        optionalEventDefinition = Optional.empty();
    }
    EventNotificationContext notificationContext = EventNotificationContext.builder().notificationId(notification.id()).notificationConfig(notification.config()).event(eventDto).eventDefinition(optionalEventDefinition.get()).jobTrigger(trigger).build();
    updateTriggerStatus(eventDto, gracePeriodInMS);
    if (inGrace(eventDto, gracePeriodInMS)) {
        LOG.debug("Notification <{}> triggered but it's in grace period.", jobConfig.notificationId());
        metrics.markInGrace(eventNotification, notification);
        return ctx.jobTriggerUpdates().scheduleNextExecution();
    }
    try {
        metrics.markExecution(eventNotification, notification);
        eventNotification.execute(notificationContext);
        metrics.markSuccess(eventNotification, notification);
    } catch (TemporaryEventNotificationException e) {
        metrics.markFailedTemporarily(eventNotification, notification);
        final long retryPeriod = configurationProvider.get().eventNotificationsRetry();
        throw new JobExecutionException(String.format(Locale.ROOT, "Failed to execute notification, retrying in %d minutes - <%s/%s/%s>", TimeUnit.MILLISECONDS.toMinutes(retryPeriod), notification.id(), notification.title(), notification.config().type()), trigger, ctx.jobTriggerUpdates().retryIn(retryPeriod, TimeUnit.MILLISECONDS), e);
    } catch (PermanentEventNotificationException e) {
        metrics.markFailedPermanently(eventNotification, notification);
        throw new JobExecutionException(String.format(Locale.ROOT, "Failed permanently to execute notification, giving up - <%s/%s/%s>", notification.id(), notification.title(), notification.config().type()), trigger, ctx.jobTriggerUpdates().scheduleNextExecution(), e);
    } catch (EventNotificationException e) {
        metrics.markFailed(eventNotification, notification);
        throw new JobExecutionException(String.format(Locale.ROOT, "Notification failed to execute - <%s/%s/%s>", notification.id(), notification.title(), notification.config().type()), trigger, ctx.jobTriggerUpdates().scheduleNextExecution(), e);
    }
    updateNotifiedStatus(eventDto, gracePeriodInMS);
    return ctx.jobTriggerUpdates().scheduleNextExecution();
}
Also used : EventDto(org.graylog.events.event.EventDto) NotFoundException(org.graylog2.database.NotFoundException) JobTriggerData(org.graylog.scheduler.JobTriggerData) JobTriggerDto(org.graylog.scheduler.JobTriggerDto) JobExecutionException(org.graylog.scheduler.JobExecutionException) EventDefinitionDto(org.graylog.events.processor.EventDefinitionDto)

Example 2 with EventDto

use of org.graylog.events.event.EventDto in project graylog2-server by Graylog2.

the class NotificationTestData method getDummyContext.

public static EventNotificationContext getDummyContext(NotificationDto notificationDto, String userName) {
    final EventDto eventDto = EventDto.builder().alert(true).eventDefinitionId("EventDefinitionTestId").eventDefinitionType("notification-test-v1").eventTimestamp(Tools.nowUTC()).processingTimestamp(Tools.nowUTC()).id("TEST_NOTIFICATION_ID").streams(ImmutableSet.of(Stream.DEFAULT_EVENTS_STREAM_ID)).message("Notification test message triggered from user <" + userName + ">").source(Stream.DEFAULT_STREAM_ID).keyTuple(ImmutableList.of("testkey")).key("testkey").originContext(EventOriginContext.elasticsearchMessage("testIndex_42", "b5e53442-12bb-4374-90ed-0deadbeefbaz")).priority(2).fields(ImmutableMap.of("field1", "value1", "field2", "value2")).build();
    final EventDefinitionDto eventDefinitionDto = EventDefinitionDto.builder().alert(true).id(TEST_NOTIFICATION_ID).title("Event Definition Test Title").description("Event Definition Test Description").config(new EventProcessorConfig() {

        @Override
        public String type() {
            return "test-dummy-v1";
        }

        @Override
        public ValidationResult validate() {
            return null;
        }

        @Override
        public EventProcessorConfigEntity toContentPackEntity(EntityDescriptorIds entityDescriptorIds) {
            return null;
        }
    }).fieldSpec(ImmutableMap.of()).priority(2).keySpec(ImmutableList.of()).notificationSettings(new EventNotificationSettings() {

        @Override
        public long gracePeriodMs() {
            return 0;
        }

        @Override
        public // disable to avoid errors in getBacklogForEvent()
        long backlogSize() {
            return 0;
        }

        @Override
        public Builder toBuilder() {
            return null;
        }
    }).build();
    return EventNotificationContext.builder().notificationId(TEST_NOTIFICATION_ID).notificationConfig(notificationDto.config()).event(eventDto).eventDefinition(eventDefinitionDto).build();
}
Also used : EventDefinitionDto(org.graylog.events.processor.EventDefinitionDto) EntityDescriptorIds(org.graylog2.contentpacks.EntityDescriptorIds) EventDto(org.graylog.events.event.EventDto) EventProcessorConfig(org.graylog.events.processor.EventProcessorConfig)

Aggregations

EventDto (org.graylog.events.event.EventDto)2 EventDefinitionDto (org.graylog.events.processor.EventDefinitionDto)2 EventProcessorConfig (org.graylog.events.processor.EventProcessorConfig)1 JobExecutionException (org.graylog.scheduler.JobExecutionException)1 JobTriggerData (org.graylog.scheduler.JobTriggerData)1 JobTriggerDto (org.graylog.scheduler.JobTriggerDto)1 EntityDescriptorIds (org.graylog2.contentpacks.EntityDescriptorIds)1 NotFoundException (org.graylog2.database.NotFoundException)1