2019-02-25 08:48:22 -05:00
|
|
|
/* Icinga 2 | (c) 2012 Icinga GmbH | GPLv2+ */
|
2013-02-09 09:20:10 -05:00
|
|
|
|
2014-05-25 10:23:35 -04:00
|
|
|
#include "icinga/checkable.hpp"
|
|
|
|
|
#include "icinga/icingaapplication.hpp"
|
|
|
|
|
#include "base/objectlock.hpp"
|
2014-10-19 08:21:12 -04:00
|
|
|
#include "base/logger.hpp"
|
2014-05-25 10:23:35 -04:00
|
|
|
#include "base/exception.hpp"
|
|
|
|
|
#include "base/context.hpp"
|
|
|
|
|
#include "base/convert.hpp"
|
2013-02-09 09:20:10 -05:00
|
|
|
|
|
|
|
|
using namespace icinga;
|
|
|
|
|
|
2014-04-03 09:36:13 -04:00
|
|
|
boost::signals2::signal<void (const Notification::Ptr&, const Checkable::Ptr&, const std::set<User::Ptr>&,
|
2017-12-19 09:50:05 -05:00
|
|
|
const NotificationType&, const CheckResult::Ptr&, const String&, const String&,
|
|
|
|
|
const MessageOrigin::Ptr&)> Checkable::OnNotificationSentToAllUsers;
|
2014-04-03 09:36:13 -04:00
|
|
|
boost::signals2::signal<void (const Notification::Ptr&, const Checkable::Ptr&, const User::Ptr&,
|
2019-03-28 06:42:28 -04:00
|
|
|
const NotificationType&, const CheckResult::Ptr&, const NotificationResult::Ptr&, const String&,
|
|
|
|
|
const String&, const String&, const MessageOrigin::Ptr&)> Checkable::OnNotificationSentToUser;
|
2014-04-03 09:36:13 -04:00
|
|
|
|
2018-01-03 22:25:35 -05:00
|
|
|
void Checkable::ResetNotificationNumbers()
|
2013-07-18 11:04:09 -04:00
|
|
|
{
|
2016-08-25 00:19:44 -04:00
|
|
|
for (const Notification::Ptr& notification : GetNotifications()) {
|
2013-07-18 11:04:09 -04:00
|
|
|
ObjectLock olock(notification);
|
|
|
|
|
notification->ResetNotificationNumber();
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2014-04-03 09:36:13 -04:00
|
|
|
void Checkable::SendNotifications(NotificationType type, const CheckResult::Ptr& cr, const String& author, const String& text)
|
2013-02-09 09:20:10 -05:00
|
|
|
{
|
2019-02-07 09:42:09 -05:00
|
|
|
String checkableName = GetName();
|
|
|
|
|
|
|
|
|
|
CONTEXT("Sending notifications for object '" + checkableName + "'");
|
2013-11-19 01:49:41 -05:00
|
|
|
|
2013-08-30 09:56:03 -04:00
|
|
|
bool force = GetForceNextNotification();
|
2013-03-21 08:42:46 -04:00
|
|
|
|
2016-11-17 05:46:40 -05:00
|
|
|
SetForceNextNotification(false);
|
|
|
|
|
|
2013-10-08 05:57:35 -04:00
|
|
|
if (!IcingaApplication::GetInstance()->GetEnableNotifications() || !GetEnableNotifications()) {
|
2013-08-30 09:56:03 -04:00
|
|
|
if (!force) {
|
2014-10-19 11:52:17 -04:00
|
|
|
Log(LogInformation, "Checkable")
|
2019-02-07 09:42:09 -05:00
|
|
|
<< "Notifications are disabled for checkable '" << checkableName << "'.";
|
2013-03-21 08:22:26 -04:00
|
|
|
return;
|
|
|
|
|
}
|
2013-02-26 06:37:25 -05:00
|
|
|
}
|
|
|
|
|
|
2013-03-16 16:18:53 -04:00
|
|
|
std::set<Notification::Ptr> notifications = GetNotifications();
|
2013-02-09 09:20:10 -05:00
|
|
|
|
2019-02-07 09:42:09 -05:00
|
|
|
Log(LogInformation, "Checkable")
|
|
|
|
|
<< "Checkable '" << checkableName << "' has " << notifications.size() << " notification(s). Proceeding with filters, successful sends will be logged.";
|
2013-02-09 09:20:10 -05:00
|
|
|
|
2019-02-07 09:42:09 -05:00
|
|
|
if (notifications.empty())
|
|
|
|
|
return;
|
2014-01-27 11:22:48 -05:00
|
|
|
|
2016-08-25 00:19:44 -04:00
|
|
|
for (const Notification::Ptr& notification : notifications) {
|
2013-02-24 02:26:10 -05:00
|
|
|
try {
|
2019-02-07 09:42:09 -05:00
|
|
|
if (!notification->IsPaused()) {
|
2016-08-12 08:49:29 -04:00
|
|
|
notification->BeginExecuteNotification(type, cr, force, false, author, text);
|
2019-02-07 09:42:09 -05:00
|
|
|
} else {
|
|
|
|
|
Log(LogNotice, "Notification")
|
|
|
|
|
<< "Notification '" << notification->GetName() << "': HA cluster active, this endpoint does not have the authority (paused=true). Skipping.";
|
|
|
|
|
}
|
2013-03-16 16:18:53 -04:00
|
|
|
} catch (const std::exception& ex) {
|
2014-10-19 11:52:17 -04:00
|
|
|
Log(LogWarning, "Checkable")
|
2019-02-07 09:42:09 -05:00
|
|
|
<< "Exception occurred during notification '" << notification->GetName() << "' for checkable '"
|
|
|
|
|
<< GetName() << "': " << DiagnosticInformation(ex, false);
|
2013-02-24 02:26:10 -05:00
|
|
|
}
|
2013-02-09 09:20:10 -05:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2018-01-03 22:25:35 -05:00
|
|
|
std::set<Notification::Ptr> Checkable::GetNotifications() const
|
2013-02-27 09:23:25 -05:00
|
|
|
{
|
2015-01-29 07:18:46 -05:00
|
|
|
boost::mutex::scoped_lock lock(m_NotificationMutex);
|
2013-08-20 05:06:04 -04:00
|
|
|
return m_Notifications;
|
2013-02-27 09:23:25 -05:00
|
|
|
}
|
|
|
|
|
|
2015-08-20 11:18:48 -04:00
|
|
|
void Checkable::RegisterNotification(const Notification::Ptr& notification)
|
2013-02-09 09:20:10 -05:00
|
|
|
{
|
2015-01-29 07:18:46 -05:00
|
|
|
boost::mutex::scoped_lock lock(m_NotificationMutex);
|
2013-08-20 05:06:04 -04:00
|
|
|
m_Notifications.insert(notification);
|
2013-02-09 09:20:10 -05:00
|
|
|
}
|
|
|
|
|
|
2015-08-20 11:18:48 -04:00
|
|
|
void Checkable::UnregisterNotification(const Notification::Ptr& notification)
|
2013-02-09 09:20:10 -05:00
|
|
|
{
|
2015-01-29 07:18:46 -05:00
|
|
|
boost::mutex::scoped_lock lock(m_NotificationMutex);
|
2013-08-20 05:06:04 -04:00
|
|
|
m_Notifications.erase(notification);
|
2013-02-09 09:20:10 -05:00
|
|
|
}
|