icinga2/lib/icinga/checkable-notification.cpp

203 lines
6.5 KiB
C++
Raw Normal View History

/* Icinga 2 | (c) 2012 Icinga GmbH | GPLv2+ */
2014-05-25 16:23:35 +02:00
#include "icinga/checkable.hpp"
#include "icinga/host.hpp"
2014-05-25 16:23:35 +02:00
#include "icinga/icingaapplication.hpp"
#include "icinga/service.hpp"
2014-05-25 16:23:35 +02:00
#include "base/objectlock.hpp"
2014-10-19 14:21:12 +02:00
#include "base/logger.hpp"
2014-05-25 16:23:35 +02:00
#include "base/exception.hpp"
#include "base/context.hpp"
#include "base/convert.hpp"
using namespace icinga;
2014-04-03 15:36:13 +02:00
boost::signals2::signal<void (const Notification::Ptr&, const Checkable::Ptr&, const std::set<User::Ptr>&,
const NotificationType&, const CheckResult::Ptr&, const String&, const String&,
const MessageOrigin::Ptr&)> Checkable::OnNotificationSentToAllUsers;
2014-04-03 15:36:13 +02:00
boost::signals2::signal<void (const Notification::Ptr&, const Checkable::Ptr&, const User::Ptr&,
const NotificationType&, const CheckResult::Ptr&, const NotificationResult::Ptr&, const String&,
const String&, const String&, const MessageOrigin::Ptr&)> Checkable::OnNotificationSentToUser;
2014-04-03 15:36:13 +02:00
void Checkable::ResetNotificationNumbers()
2013-07-18 17:04:09 +02:00
{
for (const Notification::Ptr& notification : GetNotifications()) {
2013-07-18 17:04:09 +02:00
ObjectLock olock(notification);
notification->ResetNotificationNumber();
}
}
2014-04-03 15:36:13 +02:00
void Checkable::SendNotifications(NotificationType type, const CheckResult::Ptr& cr, const String& author, const String& text)
{
String checkableName = GetName();
CONTEXT("Sending notifications for object '" + checkableName + "'");
bool force = GetForceNextNotification();
SetForceNextNotification(false);
2013-10-08 11:57:35 +02:00
if (!IcingaApplication::GetInstance()->GetEnableNotifications() || !GetEnableNotifications()) {
if (!force) {
2014-10-19 17:52:17 +02:00
Log(LogInformation, "Checkable")
<< "Notifications are disabled for checkable '" << checkableName << "'.";
return;
}
}
2013-03-16 21:18:53 +01:00
std::set<Notification::Ptr> notifications = GetNotifications();
Log(LogInformation, "Checkable")
<< "Checkable '" << checkableName << "' has " << notifications.size() << " notification(s). Proceeding with filters, successful sends will be logged.";
if (notifications.empty())
return;
for (const Notification::Ptr& notification : notifications) {
2013-02-24 08:26:10 +01:00
try {
if (!notification->IsPaused()) {
notification->BeginExecuteNotification(type, cr, force, false, author, text);
} else {
Log(LogNotice, "Notification")
<< "Notification '" << notification->GetName() << "': HA cluster active, this endpoint does not have the authority (paused=true). Skipping.";
}
2013-03-16 21:18:53 +01:00
} catch (const std::exception& ex) {
2014-10-19 17:52:17 +02:00
Log(LogWarning, "Checkable")
<< "Exception occurred during notification '" << notification->GetName() << "' for checkable '"
<< GetName() << "': " << DiagnosticInformation(ex, false);
2013-02-24 08:26:10 +01:00
}
}
}
std::set<Notification::Ptr> Checkable::GetNotifications() const
2013-02-27 15:23:25 +01:00
{
boost::mutex::scoped_lock lock(m_NotificationMutex);
return m_Notifications;
2013-02-27 15:23:25 +01:00
}
void Checkable::RegisterNotification(const Notification::Ptr& notification)
{
boost::mutex::scoped_lock lock(m_NotificationMutex);
m_Notifications.insert(notification);
}
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) {
2019-07-10 11:51:58 +02:00
case NotificationProblem:
still_applies = cr && !checkable->IsStateOK(cr->GetState()) && checkable->GetStateType() == StateTypeHard;
break;
case NotificationRecovery:
still_applies = cr && checkable->IsStateOK(cr->GetState());
break;
case NotificationFlappingStart:
still_applies = checkable->IsFlapping();
break;
case NotificationFlappingEnd:
still_applies = !checkable->IsFlapping();
break;
default:
break;
}
if (still_applies) {
bool still_suppressed;
switch (type) {
2019-07-10 11:51:58 +02:00
case NotificationProblem:
/* Fall through. */
case NotificationRecovery:
still_suppressed = !checkable->IsReachable(DependencyNotification) || checkable->IsInDowntime() || checkable->IsAcknowledged();
break;
case NotificationFlappingStart:
/* Fall through. */
case NotificationFlappingEnd:
still_suppressed = checkable->IsInDowntime();
break;
default:
break;
}
if (!still_suppressed && checkable->GetEnableActiveChecks()) {
/* If e.g. the downtime just ended, but the service is still not ok, we would re-send the stashed problem notification.
* But if the next check result recovers the service soon, we would send a recovery notification soon after the problem one.
* This is not desired, especially for lots of services at once.
* Because of that if there's likely to be a check result soon,
* we delay the re-sending of the stashed notification until the next check.
* That check either doesn't change anything and we finally re-send the stashed problem notification
* or recovers the service and we drop the stashed notification. */
/* One minute unless the check interval is too short so the next check will always run during the next minute. */
auto threshold (checkable->GetCheckInterval() - 10);
if (threshold > 60)
threshold = 60;
else if (threshold < 0)
threshold = 0;
still_suppressed = checkable->GetNextCheck() <= Utility::GetTime() + threshold;
}
if (!still_suppressed) {
Checkable::OnNotificationsRequested(checkable, type, cr, "", "", nullptr);
subtract |= type;
}
} else {
subtract |= type;
}
}
}
if (subtract) {
ObjectLock olock (checkable);
2019-07-10 11:51:58 +02:00
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);
}
}
}
/**
* Re-sends all notifications previously suppressed by e.g. downtimes if the notification reason still applies.
*/
void Checkable::FireSuppressedNotifications(const Timer * const&)
{
for (auto& host : ConfigType::GetObjectsByType<Host>()) {
::FireSuppressedNotifications(host.get());
}
for (auto& service : ConfigType::GetObjectsByType<Service>()) {
::FireSuppressedNotifications(service.get());
}
}