]> granicus.if.org Git - icinga2/blob - lib/notification/notificationcomponent.cpp
Merge pull request #7078 from Icinga/feature/deprecate-command-pipe-adjust-logs
[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         /* Function already checks whether 'api' feature is enabled. */
68         Endpoint::Ptr myEndpoint = Endpoint::GetLocalEndpoint();
69
70         for (const Notification::Ptr& notification : ConfigType::GetObjectsByType<Notification>()) {
71                 if (!notification->IsActive())
72                         continue;
73
74                 String notificationName = notification->GetName();
75
76                 /* Skip notification if paused, in a cluster setup & HA feature is enabled. */
77                 if (notification->IsPaused() && myEndpoint && GetEnableHA()) {
78                         Log(LogNotice, "NotificationComponent")
79                                 << "Reminder notification '" << notificationName << "': HA cluster active, this endpoint does not have the authority (paused=true). Skipping.";
80                         continue;
81                 }
82
83                 Checkable::Ptr checkable = notification->GetCheckable();
84
85                 if (!IcingaApplication::GetInstance()->GetEnableNotifications() || !checkable->GetEnableNotifications())
86                         continue;
87
88                 if (notification->GetInterval() <= 0 && notification->GetNoMoreNotifications()) {
89                         Log(LogNotice, "NotificationComponent")
90                                 << "Reminder notification '" << notificationName << "': Notification was sent out once and interval=0 disables reminder notifications.";
91                         continue;
92                 }
93
94                 if (notification->GetNextNotification() > now)
95                         continue;
96
97                 bool reachable = checkable->IsReachable(DependencyNotification);
98
99                 {
100                         ObjectLock olock(notification);
101                         notification->SetNextNotification(Utility::GetTime() + notification->GetInterval());
102                 }
103
104                 {
105                         Host::Ptr host;
106                         Service::Ptr service;
107                         tie(host, service) = GetHostService(checkable);
108
109                         ObjectLock olock(checkable);
110
111                         if (checkable->GetStateType() == StateTypeSoft)
112                                 continue;
113
114                         /* Don't send reminder notifications for OK/Up states. */
115                         if ((service && service->GetState() == ServiceOK) || (!service && host->GetState() == HostUp))
116                                 continue;
117
118                         /* Skip in runtime filters. */
119                         if (!reachable || checkable->IsInDowntime() || checkable->IsAcknowledged() || checkable->IsFlapping())
120                                 continue;
121                 }
122
123                 try {
124                         Log(LogNotice, "NotificationComponent")
125                                 << "Attempting to send reminder notification '" << notificationName << "'.";
126
127                         notification->BeginExecuteNotification(NotificationProblem, checkable->GetLastCheckResult(), false, true);
128                 } catch (const std::exception& ex) {
129                         Log(LogWarning, "NotificationComponent")
130                                 << "Exception occurred during notification for object '"
131                                 << notificationName << "': " << DiagnosticInformation(ex, false);
132                 }
133         }
134 }
135
136 /**
137  * Processes icinga::SendNotifications messages.
138  */
139 void NotificationComponent::SendNotificationsHandler(const Checkable::Ptr& checkable, NotificationType type,
140         const CheckResult::Ptr& cr, const String& author, const String& text)
141 {
142         checkable->SendNotifications(type, cr, author, text);
143 }