Skip to content

Commit

Permalink
Re-send suppressed notifications
Browse files Browse the repository at this point in the history
refs #5919
  • Loading branch information
Al2Klimov committed Jul 3, 2019
1 parent 58cb1e0 commit bfcd278
Show file tree
Hide file tree
Showing 5 changed files with 149 additions and 16 deletions.
64 changes: 48 additions & 16 deletions lib/icinga/checkable-check.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -309,15 +309,14 @@ void Checkable::ProcessCheckResult(const CheckResult::Ptr& cr, const MessageOrig
bool in_downtime = IsInDowntime();

bool send_notification = false;
bool suppress_notification = !notification_reachable || in_downtime || IsAcknowledged();

if (notification_reachable && !in_downtime && !IsAcknowledged()) {
/* Send notifications whether when a hard state change occurred. */
if (hardChange && !(old_stateType == StateTypeSoft && IsStateOK(new_state)))
send_notification = true;
/* Or if the checkable is volatile and in a HARD state. */
else if (is_volatile && GetStateType() == StateTypeHard)
send_notification = true;
}
/* Send notifications whether when a hard state change occurred. */
if (hardChange && !(old_stateType == StateTypeSoft && IsStateOK(new_state)))
send_notification = true;
/* Or if the checkable is volatile and in a HARD state. */
else if (is_volatile && GetStateType() == StateTypeHard)
send_notification = true;

if (IsStateOK(old_state) && old_stateType == StateTypeSoft)
send_notification = false; /* Don't send notifications for SOFT-OK -> HARD-OK. */
Expand Down Expand Up @@ -405,21 +404,33 @@ void Checkable::ProcessCheckResult(const CheckResult::Ptr& cr, const MessageOrig
(is_volatile && !(IsStateOK(old_state) && IsStateOK(new_state))))
ExecuteEventHandler();

int suppressed_types = 0;

/* Flapping start/end notifications */
if (!in_downtime && !was_flapping && is_flapping) {
if (!was_flapping && is_flapping) {
/* FlappingStart notifications happen on state changes, not in downtimes */
if (!IsPaused())
OnNotificationsRequested(this, NotificationFlappingStart, cr, "", "", nullptr);
if (!IsPaused()) {
if (in_downtime) {
suppressed_types |= NotificationFlappingStart;
} else {
OnNotificationsRequested(this, NotificationFlappingStart, cr, "", "", nullptr);
}
}

Log(LogNotice, "Checkable")
<< "Flapping Start: Checkable '" << GetName() << "' started flapping (Current flapping value "
<< GetFlappingCurrent() << "% > high threshold " << GetFlappingThresholdHigh() << "%).";

NotifyFlapping(origin);
} else if (!in_downtime && was_flapping && !is_flapping) {
} else if (was_flapping && !is_flapping) {
/* FlappingEnd notifications are independent from state changes, must not happen in downtine */
if (!IsPaused())
OnNotificationsRequested(this, NotificationFlappingEnd, cr, "", "", nullptr);
if (!IsPaused()) {
if (in_downtime) {
suppressed_types |= NotificationFlappingEnd;
} else {
OnNotificationsRequested(this, NotificationFlappingEnd, cr, "", "", nullptr);
}
}

Log(LogNotice, "Checkable")
<< "Flapping Stop: Checkable '" << GetName() << "' stopped flapping (Current flapping value "
Expand All @@ -429,8 +440,29 @@ void Checkable::ProcessCheckResult(const CheckResult::Ptr& cr, const MessageOrig
}

if (send_notification && !is_flapping) {
if (!IsPaused())
OnNotificationsRequested(this, recovery ? NotificationRecovery : NotificationProblem, cr, "", "", nullptr);
if (!IsPaused()) {
if (suppress_notification) {
suppressed_types |= (recovery ? NotificationRecovery : NotificationProblem);
} else {
OnNotificationsRequested(this, recovery ? NotificationRecovery : NotificationProblem, cr, "", "", nullptr);
}
}
}

if (suppressed_types) {
ObjectLock olock (this);
int suppressed_types_before (GetSuppressedNotifications());
int suppressed_types_after (suppressed_types_before | suppressed_types);

for (int conflict : {NotificationProblem | NotificationRecovery, NotificationFlappingStart | NotificationFlappingEnd}) {
if (suppressed_types_after & conflict == conflict) {
suppressed_types_after &= ~conflict;
}
}

if (suppressed_types_after != suppressed_types_before) {
SetSuppressedNotifications(suppressed_types_after);
}
}
}

Expand Down
82 changes: 82 additions & 0 deletions lib/icinga/checkable-notification.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -84,3 +84,85 @@ void Checkable::UnregisterNotification(const Notification::Ptr& notification)
boost::mutex::scoped_lock lock(m_NotificationMutex);
m_Notifications.erase(notification);
}

static void FireSuppressedNotifications(Checkable* checkable)
{
if (!checkable->IsActive())
return;

if (checkable->IsPaused())
return;

if (!checkable->GetEnableNotifications())
return;

int suppressed_types (checkable->GetSuppressedNotifications());
if (!suppressed_types)
return;

int subtract = 0;

for (auto type : {NotificationProblem, NotificationRecovery, NotificationFlappingStart, NotificationFlappingEnd}) {
if (suppressed_types & type) {
bool still_applies;
auto cr (checkable->GetLastCheckResult());

switch (type) {
case NotificationProblem:
still_applies = !checkable->IsStateOK(cr->GetState()) && checkable->GetStateType() == StateTypeHard;
break;
case NotificationRecovery:
still_applies = checkable->IsStateOK(cr->GetState());
break;
case NotificationFlappingStart:
still_applies = checkable->IsFlapping();
break;
case NotificationFlappingEnd:
still_applies = !checkable->IsFlapping();
}

if (still_applies) {
bool still_suppressed;

switch (type) {
case NotificationProblem:
case NotificationRecovery:
still_suppressed = !checkable->IsReachable(DependencyNotification) || checkable->IsInDowntime() || checkable->IsAcknowledged();
break;
case NotificationFlappingStart:
case NotificationFlappingEnd:
still_suppressed = checkable->IsInDowntime();
}

if (!still_suppressed) {
Checkable::OnNotificationsRequested(checkable, type, cr, "", "", nullptr);

subtract |= type;
}
} else {
subtract |= type;
}
}
}

if (subtract) {
ObjectLock olock (checkable);
int suppressed_types_before (checkable->GetSuppressedNotifications());
int suppressed_types_after (suppressed_types_before & ~subtract);

if (suppressed_types_after != suppressed_types_before) {
checkable->SetSuppressedNotifications(suppressed_types_after);
}
}
}

void Checkable::FireSuppressedNotifications(const Timer * const&)
{
for (auto& host : ConfigType::GetObjectsByType<Host>()) {
::FireSuppressedNotifications(host.get());
}

for (auto& service : ConfigType::GetObjectsByType<Service>()) {
::FireSuppressedNotifications(service.get());
}
}
13 changes: 13 additions & 0 deletions lib/icinga/checkable.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@
#include "base/objectlock.hpp"
#include "base/utility.hpp"
#include "base/exception.hpp"
#include "base/timer.hpp"
#include <boost/thread/once.hpp>

using namespace icinga;

Expand All @@ -16,6 +18,8 @@ INITIALIZE_ONCE(&Checkable::StaticInitialize);
boost::signals2::signal<void (const Checkable::Ptr&, const String&, const String&, AcknowledgementType, bool, bool, double, const MessageOrigin::Ptr&)> Checkable::OnAcknowledgementSet;
boost::signals2::signal<void (const Checkable::Ptr&, const MessageOrigin::Ptr&)> Checkable::OnAcknowledgementCleared;

static Timer::Ptr l_CheckablesFireSuppressedNotifications;

void Checkable::StaticInitialize()
{
/* fixed downtime start */
Expand Down Expand Up @@ -65,6 +69,15 @@ void Checkable::Start(bool runtimeCreated)
}

ObjectImpl<Checkable>::Start(runtimeCreated);

static boost::once_flag once = BOOST_ONCE_INIT;

boost::call_once(once, []() {
l_CheckablesFireSuppressedNotifications = new Timer();
l_CheckablesFireSuppressedNotifications->SetInterval(5);
l_CheckablesFireSuppressedNotifications->OnTimerExpired.connect(&Checkable::FireSuppressedNotifications);
l_CheckablesFireSuppressedNotifications->Start();
});
}

void Checkable::AddGroup(const String& name)
Expand Down
3 changes: 3 additions & 0 deletions lib/icinga/checkable.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
#ifndef CHECKABLE_H
#define CHECKABLE_H

#include "base/timer.hpp"
#include "icinga/i2-icinga.hpp"
#include "icinga/checkable-ti.hpp"
#include "icinga/timeperiod.hpp"
Expand Down Expand Up @@ -211,6 +212,8 @@ class Checkable : public ObjectImpl<Checkable>

static void NotifyDowntimeEnd(const Downtime::Ptr& downtime);

static void FireSuppressedNotifications(const Timer * const&);

/* Comments */
std::set<Comment::Ptr> m_Comments;
mutable boost::mutex m_CommentMutex;
Expand Down
3 changes: 3 additions & 0 deletions lib/icinga/checkable.ti
Original file line number Diff line number Diff line change
Expand Up @@ -154,6 +154,9 @@ abstract class Checkable : CustomVarObject
[state, no_user_view, no_user_modify] int flapping_buffer;
[state, no_user_view, no_user_modify] int flapping_index;
[state, protected] bool flapping;
[state, no_user_view, no_user_modify] int suppressed_notifications {
default {{{ return 0; }}}
};

[config, navigation] name(Endpoint) command_endpoint (CommandEndpointRaw) {
navigate {{{
Expand Down

0 comments on commit bfcd278

Please sign in to comment.