]> granicus.if.org Git - icinga2/blob - lib/notification/notificationcomponent.cpp
Merge pull request #7002 from Icinga/bugfix/check_network-percent-6155
[icinga2] / lib / notification / notificationcomponent.cpp
1 /* Icinga 2 | (c) 2012 Icinga GmbH | GPLv2+ */
2
3 #include "notification/notificationcomponent.hpp"
4 #include "notification/notificationcomponent-ti.cpp"
5 #include "icinga/service.hpp"
6 #include "icinga/icingaapplication.hpp"
7 #include "base/configtype.hpp"
8 #include "base/objectlock.hpp"
9 #include "base/logger.hpp"
10 #include "base/utility.hpp"
11 #include "base/exception.hpp"
12 #include "base/statsfunction.hpp"
13
14 using namespace icinga;
15
16 REGISTER_TYPE(NotificationComponent);
17
18 REGISTER_STATSFUNCTION(NotificationComponent, &NotificationComponent::StatsFunc);
19
20 void NotificationComponent::StatsFunc(const Dictionary::Ptr& status, const Array::Ptr&)
21 {
22         DictionaryData nodes;
23
24         for (const NotificationComponent::Ptr& notification_component : ConfigType::GetObjectsByType<NotificationComponent>()) {
25                 nodes.emplace_back(notification_component->GetName(), 1); //add more stats
26         }
27
28         status->Set("notificationcomponent", new Dictionary(std::move(nodes)));
29 }
30
31 /**
32  * Starts the component.
33  */
34 void NotificationComponent::Start(bool runtimeCreated)
35 {
36         ObjectImpl<NotificationComponent>::Start(runtimeCreated);
37
38         Log(LogInformation, "NotificationComponent")
39                 << "'" << GetName() << "' started.";
40
41         Checkable::OnNotificationsRequested.connect(std::bind(&NotificationComponent::SendNotificationsHandler, this, _1,
42                 _2, _3, _4, _5));
43
44         m_NotificationTimer = new Timer();
45         m_NotificationTimer->SetInterval(5);
46         m_NotificationTimer->OnTimerExpired.connect(std::bind(&NotificationComponent::NotificationTimerHandler, this));
47         m_NotificationTimer->Start();
48 }
49
50 void NotificationComponent::Stop(bool runtimeRemoved)
51 {
52         Log(LogInformation, "NotificationComponent")
53                 << "'" << GetName() << "' stopped.";
54
55         ObjectImpl<NotificationComponent>::Stop(runtimeRemoved);
56 }
57
58 /**
59  * Periodically sends notifications.
60  *
61  * @param - Event arguments for the timer.
62  */
63 void NotificationComponent::NotificationTimerHandler()
64 {
65         double now = Utility::GetTime();
66
67         for (const Notification::Ptr& notification : ConfigType::GetObjectsByType<Notification>()) {
68                 if (!notification->IsActive())
69                         continue;
70
71                 String notificationName = notification->GetName();
72
73                 if (notification->IsPaused() && GetEnableHA()) {
74                         Log(LogNotice, "NotificationComponent")
75                                 << "Reminder notification '" << notificationName << "': HA cluster active, this endpoint does not have the authority (paused=true). Skipping.";
76                         continue;
77                 }
78
79                 Checkable::Ptr checkable = notification->GetCheckable();
80
81                 if (!IcingaApplication::GetInstance()->GetEnableNotifications() || !checkable->GetEnableNotifications())
82                         continue;
83
84                 if (notification->GetInterval() <= 0 && notification->GetNoMoreNotifications()) {
85                         Log(LogNotice, "NotificationComponent")
86                                 << "Reminder notification '" << notificationName << "': Notification was sent out once and interval=0 disables reminder notifications.";
87                         continue;
88                 }
89
90                 if (notification->GetNextNotification() > now)
91                         continue;
92
93                 bool reachable = checkable->IsReachable(DependencyNotification);
94
95                 {
96                         ObjectLock olock(notification);
97                         notification->SetNextNotification(Utility::GetTime() + notification->GetInterval());
98                 }
99
100                 {
101                         Host::Ptr host;
102                         Service::Ptr service;
103                         tie(host, service) = GetHostService(checkable);
104
105                         ObjectLock olock(checkable);
106
107                         if (checkable->GetStateType() == StateTypeSoft)
108                                 continue;
109
110                         /* Don't send reminder notifications for OK/Up states. */
111                         if ((service && service->GetState() == ServiceOK) || (!service && host->GetState() == HostUp))
112                                 continue;
113
114                         /* Skip in runtime filters. */
115                         if (!reachable || checkable->IsInDowntime() || checkable->IsAcknowledged() || checkable->IsFlapping())
116                                 continue;
117                 }
118
119                 try {
120                         Log(LogNotice, "NotificationComponent")
121                                 << "Attempting to send reminder notification '" << notificationName << "'.";
122
123                         notification->BeginExecuteNotification(NotificationProblem, checkable->GetLastCheckResult(), false, true);
124                 } catch (const std::exception& ex) {
125                         Log(LogWarning, "NotificationComponent")
126                                 << "Exception occurred during notification for object '"
127                                 << notificationName << "': " << DiagnosticInformation(ex, false);
128                 }
129         }
130 }
131
132 /**
133  * Processes icinga::SendNotifications messages.
134  */
135 void NotificationComponent::SendNotificationsHandler(const Checkable::Ptr& checkable, NotificationType type,
136         const CheckResult::Ptr& cr, const String& author, const String& text)
137 {
138         checkable->SendNotifications(type, cr, author, text);
139 }