1 /* Icinga 2 | (c) 2012 Icinga GmbH | GPLv2+ */
3 #include "icinga/checkable.hpp"
4 #include "icinga/icingaapplication.hpp"
5 #include "base/objectlock.hpp"
6 #include "base/logger.hpp"
7 #include "base/exception.hpp"
8 #include "base/context.hpp"
9 #include "base/convert.hpp"
11 using namespace icinga;
13 boost::signals2::signal<void (const Notification::Ptr&, const Checkable::Ptr&, const std::set<User::Ptr>&,
14 const NotificationType&, const CheckResult::Ptr&, const String&, const String&,
15 const MessageOrigin::Ptr&)> Checkable::OnNotificationSentToAllUsers;
16 boost::signals2::signal<void (const Notification::Ptr&, const Checkable::Ptr&, const User::Ptr&,
17 const NotificationType&, const CheckResult::Ptr&, const NotificationResult::Ptr&, const String&,
18 const String&, const String&, const MessageOrigin::Ptr&)> Checkable::OnNotificationSentToUser;
20 void Checkable::ResetNotificationNumbers()
22 for (const Notification::Ptr& notification : GetNotifications()) {
23 ObjectLock olock(notification);
24 notification->ResetNotificationNumber();
28 void Checkable::SendNotifications(NotificationType type, const CheckResult::Ptr& cr, const String& author, const String& text)
30 String checkableName = GetName();
32 CONTEXT("Sending notifications for object '" + checkableName + "'");
34 bool force = GetForceNextNotification();
36 SetForceNextNotification(false);
38 if (!IcingaApplication::GetInstance()->GetEnableNotifications() || !GetEnableNotifications()) {
40 Log(LogInformation, "Checkable")
41 << "Notifications are disabled for checkable '" << checkableName << "'.";
46 std::set<Notification::Ptr> notifications = GetNotifications();
48 Log(LogInformation, "Checkable")
49 << "Checkable '" << checkableName << "' has " << notifications.size() << " notification(s). Proceeding with filters, successful sends will be logged.";
51 if (notifications.empty())
54 for (const Notification::Ptr& notification : notifications) {
56 if (!notification->IsPaused()) {
57 notification->BeginExecuteNotification(type, cr, force, false, author, text);
59 Log(LogNotice, "Notification")
60 << "Notification '" << notification->GetName() << "': HA cluster active, this endpoint does not have the authority (paused=true). Skipping.";
62 } catch (const std::exception& ex) {
63 Log(LogWarning, "Checkable")
64 << "Exception occurred during notification '" << notification->GetName() << "' for checkable '"
65 << GetName() << "': " << DiagnosticInformation(ex, false);
70 std::set<Notification::Ptr> Checkable::GetNotifications() const
72 boost::mutex::scoped_lock lock(m_NotificationMutex);
73 return m_Notifications;
76 void Checkable::RegisterNotification(const Notification::Ptr& notification)
78 boost::mutex::scoped_lock lock(m_NotificationMutex);
79 m_Notifications.insert(notification);
82 void Checkable::UnregisterNotification(const Notification::Ptr& notification)
84 boost::mutex::scoped_lock lock(m_NotificationMutex);
85 m_Notifications.erase(notification);