Skip to content
This repository has been archived by the owner on Oct 6, 2023. It is now read-only.

Commit

Permalink
Mon 6538 user time period not respected 19.10 (#442)
Browse files Browse the repository at this point in the history
* Mon 6538 Engine doesn't respect user's defined notification periods

    * fix(notification) the timeperiod are now filtered for the contact

    REFS: MON-6538
  • Loading branch information
rem31 committed Mar 9, 2021
1 parent 257b11d commit 9a7aea0
Show file tree
Hide file tree
Showing 6 changed files with 48 additions and 27 deletions.
9 changes: 9 additions & 0 deletions doc/en/release_notes/engine19.10.rst
Original file line number Diff line number Diff line change
Expand Up @@ -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
========================
Expand Down
36 changes: 20 additions & 16 deletions src/contact.cc
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}

Expand Down
3 changes: 1 addition & 2 deletions src/notification.cc
Original file line number Diff line number Diff line change
Expand Up @@ -174,7 +174,7 @@ int notification::execute(std::unordered_set<contact*> 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,
Expand All @@ -183,7 +183,6 @@ int notification::execute(std::unordered_set<contact*> const& to_notify) {
contacts_notified++;
_notified_contact.insert(ctc->get_name());
}
}
}

/* get the time we finished */
Expand Down
6 changes: 4 additions & 2 deletions src/notifier.cc
Original file line number Diff line number Diff line change
Expand Up @@ -695,7 +695,8 @@ std::unordered_set<contact*> 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. */
Expand All @@ -708,7 +709,8 @@ std::unordered_set<contact*> 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);
}
}
}
Expand Down
8 changes: 2 additions & 6 deletions src/timeperiod.cc
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}

/**
Expand Down
13 changes: 12 additions & 1 deletion tests/timeperiod/get_next_valid_time/calendar_date.cc
Original file line number Diff line number Diff line change
Expand Up @@ -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<engine::timeperiod> tiperiod{
new engine::timeperiod("tperiod", "alias")};

for (int i = 0; i < 7; ++i) {
timerange_list list_time;
// list_time.push_back(std::make_shared<engine::timerange>(1000, 15000));
list_time.push_back(std::make_shared<engine::timerange>(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);
}

0 comments on commit 9a7aea0

Please sign in to comment.