From 9a7aea0b0c1146605480877b2de5646f66f45aeb Mon Sep 17 00:00:00 2001 From: rem31 <73845199+rem31@users.noreply.github.com> Date: Tue, 2 Feb 2021 15:04:07 +0100 Subject: [PATCH] Mon 6538 user time period not respected 19.10 (#442) * Mon 6538 Engine doesn't respect user's defined notification periods * fix(notification) the timeperiod are now filtered for the contact REFS: MON-6538 --- doc/en/release_notes/engine19.10.rst | 9 +++++ src/contact.cc | 36 ++++++++++--------- src/notification.cc | 3 +- src/notifier.cc | 6 ++-- src/timeperiod.cc | 8 ++--- .../get_next_valid_time/calendar_date.cc | 13 ++++++- 6 files changed, 48 insertions(+), 27 deletions(-) diff --git a/doc/en/release_notes/engine19.10.rst b/doc/en/release_notes/engine19.10.rst index 30551c771..be6db9376 100644 --- a/doc/en/release_notes/engine19.10.rst +++ b/doc/en/release_notes/engine19.10.rst @@ -28,6 +28,15 @@ last service notification Last Service Notification set when state is HARD without notification. This new version fixes this point. +Macros +============== +fix macro $TIMET$ now return time in a unix epoch format. + +Notification Period +=================== +the timeperiod are now filtered for the contact +and now don't push the notification with time period empty. + ======================== Centreon Engine 19.10.15 ======================== diff --git a/src/contact.cc b/src/contact.cc index 8828bbc50..5fa233afd 100644 --- a/src/contact.cc +++ b/src/contact.cc @@ -841,33 +841,37 @@ bool contact::should_be_notified(notifier::notification_category cat, logger(dbg_functions, basic) << "contact::should_be_notified()"; /* Are notifications enabled? */ switch (notif.get_notifier_type()) { - case notifier::service_notification: + case notifier::service_notification: { if (!_service_notifications_enabled) { logger(dbg_notifications, most) << "This contact shouldn't be notified from services."; return false; } - break; - case notifier::host_notification: + // See if the contact can be notified at this time for the host. + timezone_locker lock(get_timezone()); + if (!check_time_against_period(std::time(nullptr), + get_service_notification_period_ptr())) { + logger(dbg_notifications, most) + << "This contact shouldn't be notified at this time."; + return false; + } + } break; + case notifier::host_notification: { if (!_host_notifications_enabled) { logger(dbg_notifications, most) << "This contact shouldn't be notified from hosts."; return false; } - break; - } - - // See if the contact can be notified at this time. - { - timezone_locker lock(get_timezone()); - if (!check_time_against_period(std::time(nullptr), - get_service_notification_period_ptr())) { - logger(dbg_notifications, most) - << "This contact shouldn't be notified at this time."; - return false; - } + // See if the contact can be notified at this time for the service. + timezone_locker lock(get_timezone()); + if (!check_time_against_period(std::time(nullptr), + get_host_notification_period_ptr())) { + logger(dbg_notifications, most) + << "This contact shouldn't be notified at this time."; + return false; + } + } break; } - return (this->*(_to_notify[cat]))(type, notif); } diff --git a/src/notification.cc b/src/notification.cc index 17618b2ac..24da25449 100644 --- a/src/notification.cc +++ b/src/notification.cc @@ -174,7 +174,7 @@ int notification::execute(std::unordered_set const& to_notify) { /* check viability of notifying the user */ notifier::notification_category cat{notifier::get_category(_type)}; - if (ctc->should_be_notified(cat, _type, *_parent)) { + /* notify this contact */ if (_parent->notify_contact(&mac, ctc, _type, _author.c_str(), _message.c_str(), _options, @@ -183,7 +183,6 @@ int notification::execute(std::unordered_set const& to_notify) { contacts_notified++; _notified_contact.insert(ctc->get_name()); } - } } /* get the time we finished */ diff --git a/src/notifier.cc b/src/notifier.cc index 1f75b3aed..0b220fcb7 100644 --- a/src/notifier.cc +++ b/src/notifier.cc @@ -695,7 +695,8 @@ std::unordered_set notifier::get_contacts_to_notify( end{get_contacts().end()}; it != end; ++it) { assert(it->second); - retval.insert(it->second); + if (it->second->should_be_notified(cat, type, *this)) + retval.insert(it->second); } /* For each contact group, we also add its contacts. */ @@ -708,7 +709,8 @@ std::unordered_set notifier::get_contacts_to_notify( cend{it->second->get_members().end()}; cit != cend; ++cit) { assert(cit->second); - retval.insert(cit->second); + if (cit->second->should_be_notified(cat, type, *this)) + retval.insert(cit->second); } } } diff --git a/src/timeperiod.cc b/src/timeperiod.cc index 8ea6e2b91..772da90d1 100644 --- a/src/timeperiod.cc +++ b/src/timeperiod.cc @@ -1198,12 +1198,8 @@ void timeperiod::get_next_valid_time_per_timeperiod( 24 * 60 * 60); } - // If we couldn't find a time period there must be none defined. - if (earliest_time == (time_t)-1) - *valid_time = original_preferred_time; - // Else use the calculated time. - else - *valid_time = earliest_time; + // If we couldn't find a time period there must be none defined and return -1. + *valid_time = earliest_time; } /** diff --git a/tests/timeperiod/get_next_valid_time/calendar_date.cc b/tests/timeperiod/get_next_valid_time/calendar_date.cc index d6df1f805..ee11bc93a 100644 --- a/tests/timeperiod/get_next_valid_time/calendar_date.cc +++ b/tests/timeperiod/get_next_valid_time/calendar_date.cc @@ -101,10 +101,21 @@ TEST_F(GetNextValidTimeCalendarDateTest, WithinCalendarDate) { // When get_next_valid_time() is called // Then the next valid time is now TEST_F(GetNextValidTimeCalendarDateTest, AfterCalendarDates) { + std::unique_ptr tiperiod{ + new engine::timeperiod("tperiod", "alias")}; + + for (int i = 0; i < 7; ++i) { + timerange_list list_time; + // list_time.push_back(std::make_shared(1000, 15000)); + list_time.push_back(std::make_shared(8000, 85000)); + tiperiod->days[i] = list_time; + } + default_data_set(); + time_t now(strtotimet("2016-10-30 12:00:00")); set_time(now); time_t computed((time_t)-1); - get_next_valid_time(now, &computed, _creator.get_timeperiods()); + get_next_valid_time(now, &computed, tiperiod.get()); ASSERT_EQ(computed, now); }