1 /* Icinga 2 | (c) 2012 Icinga GmbH | GPLv2+ */
3 #include "icinga/checkable.hpp"
4 #include "icinga/host.hpp"
5 #include "icinga/icingaapplication.hpp"
6 #include "icinga/service.hpp"
7 #include "base/dictionary.hpp"
8 #include "base/objectlock.hpp"
9 #include "base/logger.hpp"
10 #include "base/exception.hpp"
11 #include "base/context.hpp"
12 #include "base/convert.hpp"
13 #include "remote/apilistener.hpp"
15 using namespace icinga;
17 boost::signals2::signal<void (const Notification::Ptr&, const Checkable::Ptr&, const std::set<User::Ptr>&,
18 const NotificationType&, const CheckResult::Ptr&, const String&, const String&,
19 const MessageOrigin::Ptr&)> Checkable::OnNotificationSentToAllUsers;
20 boost::signals2::signal<void (const Notification::Ptr&, const Checkable::Ptr&, const User::Ptr&,
21 const NotificationType&, const CheckResult::Ptr&, const NotificationResult::Ptr&, const String&,
22 const String&, const String&, const MessageOrigin::Ptr&)> Checkable::OnNotificationSentToUser;
24 void Checkable::ResetNotificationNumbers()
26 for (const Notification::Ptr& notification : GetNotifications()) {
27 ObjectLock olock(notification);
28 notification->ResetNotificationNumber();
32 void Checkable::SendNotifications(NotificationType type, const CheckResult::Ptr& cr, const String& author, const String& text)
34 String checkableName = GetName();
36 CONTEXT("Sending notifications for object '" + checkableName + "'");
38 bool force = GetForceNextNotification();
40 SetForceNextNotification(false);
42 if (!IcingaApplication::GetInstance()->GetEnableNotifications() || !GetEnableNotifications()) {
44 Log(LogInformation, "Checkable")
45 << "Notifications are disabled for checkable '" << checkableName << "'.";
50 std::set<Notification::Ptr> notifications = GetNotifications();
52 String notificationTypeName = Notification::NotificationTypeToString(type);
54 // Bail early if there are no notifications.
55 if (notifications.empty()) {
56 Log(LogNotice, "Checkable")
57 << "Skipping checkable '" << checkableName << "' which doesn't have any notification objects configured.";
61 Log(LogInformation, "Checkable")
62 << "Checkable '" << checkableName << "' has " << notifications.size()
63 << " notification(s). Checking filters for type '" << notificationTypeName << "', sends will be logged.";
65 for (const Notification::Ptr& notification : notifications) {
66 // Re-send stashed notifications from cold startup.
67 if (ApiListener::UpdatedObjectAuthority()) {
69 if (!notification->IsPaused()) {
70 auto stashedNotifications (notification->GetStashedNotifications());
72 if (stashedNotifications->GetLength()) {
73 Log(LogNotice, "Notification")
74 << "Notification '" << notification->GetName() << "': there are some stashed notifications. Stashing notification to preserve order.";
76 stashedNotifications->Add(new Dictionary({
85 notification->BeginExecuteNotification(type, cr, force, false, author, text);
88 Log(LogNotice, "Notification")
89 << "Notification '" << notification->GetName() << "': HA cluster active, this endpoint does not have the authority (paused=true). Skipping.";
91 } catch (const std::exception& ex) {
92 Log(LogWarning, "Checkable")
93 << "Exception occurred during notification '" << notification->GetName() << "' for checkable '"
94 << GetName() << "': " << DiagnosticInformation(ex, false);
97 // Cold startup phase. Stash notification for later.
98 Log(LogNotice, "Notification")
99 << "Notification '" << notification->GetName() << "': object authority hasn't been updated, yet. Stashing notification.";
101 notification->GetStashedNotifications()->Add(new Dictionary({
113 std::set<Notification::Ptr> Checkable::GetNotifications() const
115 boost::mutex::scoped_lock lock(m_NotificationMutex);
116 return m_Notifications;
119 void Checkable::RegisterNotification(const Notification::Ptr& notification)
121 boost::mutex::scoped_lock lock(m_NotificationMutex);
122 m_Notifications.insert(notification);
125 void Checkable::UnregisterNotification(const Notification::Ptr& notification)
127 boost::mutex::scoped_lock lock(m_NotificationMutex);
128 m_Notifications.erase(notification);
131 static void FireSuppressedNotifications(Checkable* checkable)
133 if (!checkable->IsActive())
136 if (checkable->IsPaused())
139 if (!checkable->GetEnableNotifications())
142 int suppressed_types (checkable->GetSuppressedNotifications());
143 if (!suppressed_types)
148 for (auto type : {NotificationProblem, NotificationRecovery, NotificationFlappingStart, NotificationFlappingEnd}) {
149 if (suppressed_types & type) {
151 auto cr (checkable->GetLastCheckResult());
154 case NotificationProblem:
155 still_applies = cr && !checkable->IsStateOK(cr->GetState()) && checkable->GetStateType() == StateTypeHard;
157 case NotificationRecovery:
158 still_applies = cr && checkable->IsStateOK(cr->GetState());
160 case NotificationFlappingStart:
161 still_applies = checkable->IsFlapping();
163 case NotificationFlappingEnd:
164 still_applies = !checkable->IsFlapping();
171 bool still_suppressed;
174 case NotificationProblem:
176 case NotificationRecovery:
177 still_suppressed = !checkable->IsReachable(DependencyNotification) || checkable->IsInDowntime() || checkable->IsAcknowledged();
179 case NotificationFlappingStart:
181 case NotificationFlappingEnd:
182 still_suppressed = checkable->IsInDowntime();
188 if (!still_suppressed && checkable->GetEnableActiveChecks()) {
189 /* If e.g. the downtime just ended, but the service is still not ok, we would re-send the stashed problem notification.
190 * But if the next check result recovers the service soon, we would send a recovery notification soon after the problem one.
191 * This is not desired, especially for lots of services at once.
192 * Because of that if there's likely to be a check result soon,
193 * we delay the re-sending of the stashed notification until the next check.
194 * That check either doesn't change anything and we finally re-send the stashed problem notification
195 * or recovers the service and we drop the stashed notification. */
197 /* One minute unless the check interval is too short so the next check will always run during the next minute. */
198 auto threshold (checkable->GetCheckInterval() - 10);
202 else if (threshold < 0)
205 still_suppressed = checkable->GetNextCheck() <= Utility::GetTime() + threshold;
208 if (!still_suppressed) {
209 Checkable::OnNotificationsRequested(checkable, type, cr, "", "", nullptr);
220 ObjectLock olock (checkable);
222 int suppressed_types_before (checkable->GetSuppressedNotifications());
223 int suppressed_types_after (suppressed_types_before & ~subtract);
225 if (suppressed_types_after != suppressed_types_before) {
226 checkable->SetSuppressedNotifications(suppressed_types_after);
232 * Re-sends all notifications previously suppressed by e.g. downtimes if the notification reason still applies.
234 void Checkable::FireSuppressedNotifications(const Timer * const&)
236 for (auto& host : ConfigType::GetObjectsByType<Host>()) {
237 ::FireSuppressedNotifications(host.get());
240 for (auto& service : ConfigType::GetObjectsByType<Service>()) {
241 ::FireSuppressedNotifications(service.get());