]> granicus.if.org Git - icinga2/blob - lib/icinga/checkable-notification.cpp
Merge pull request #6727 from Icinga/feature/cluster-config-sync-stage
[icinga2] / lib / icinga / checkable-notification.cpp
1 /* Icinga 2 | (c) 2012 Icinga GmbH | GPLv2+ */
2
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"
10
11 using namespace icinga;
12
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;
19
20 void Checkable::ResetNotificationNumbers()
21 {
22         for (const Notification::Ptr& notification : GetNotifications()) {
23                 ObjectLock olock(notification);
24                 notification->ResetNotificationNumber();
25         }
26 }
27
28 void Checkable::SendNotifications(NotificationType type, const CheckResult::Ptr& cr, const String& author, const String& text)
29 {
30         String checkableName = GetName();
31
32         CONTEXT("Sending notifications for object '" + checkableName + "'");
33
34         bool force = GetForceNextNotification();
35
36         SetForceNextNotification(false);
37
38         if (!IcingaApplication::GetInstance()->GetEnableNotifications() || !GetEnableNotifications()) {
39                 if (!force) {
40                         Log(LogInformation, "Checkable")
41                                 << "Notifications are disabled for checkable '" << checkableName << "'.";
42                         return;
43                 }
44         }
45
46         std::set<Notification::Ptr> notifications = GetNotifications();
47
48         Log(LogInformation, "Checkable")
49                 << "Checkable '" << checkableName << "' has " << notifications.size() << " notification(s). Proceeding with filters, successful sends will be logged.";
50
51         if (notifications.empty())
52                 return;
53
54         for (const Notification::Ptr& notification : notifications) {
55                 try {
56                         if (!notification->IsPaused()) {
57                                 notification->BeginExecuteNotification(type, cr, force, false, author, text);
58                         } else {
59                                 Log(LogNotice, "Notification")
60                                         << "Notification '" << notification->GetName() << "': HA cluster active, this endpoint does not have the authority (paused=true). Skipping.";
61                         }
62                 } catch (const std::exception& ex) {
63                         Log(LogWarning, "Checkable")
64                                 << "Exception occurred during notification '" << notification->GetName() << "' for checkable '"
65                                 << GetName() << "': " << DiagnosticInformation(ex, false);
66                 }
67         }
68 }
69
70 std::set<Notification::Ptr> Checkable::GetNotifications() const
71 {
72         boost::mutex::scoped_lock lock(m_NotificationMutex);
73         return m_Notifications;
74 }
75
76 void Checkable::RegisterNotification(const Notification::Ptr& notification)
77 {
78         boost::mutex::scoped_lock lock(m_NotificationMutex);
79         m_Notifications.insert(notification);
80 }
81
82 void Checkable::UnregisterNotification(const Notification::Ptr& notification)
83 {
84         boost::mutex::scoped_lock lock(m_NotificationMutex);
85         m_Notifications.erase(notification);
86 }