]> granicus.if.org Git - icinga2/blob - lib/notification/notificationcomponent.cpp
Merge pull request #5717 from hrld/patch-1
[icinga2] / lib / notification / notificationcomponent.cpp
1 /******************************************************************************
2  * Icinga 2                                                                   *
3  * Copyright (C) 2012-2017 Icinga Development Team (https://www.icinga.com/)  *
4  *                                                                            *
5  * This program is free software; you can redistribute it and/or              *
6  * modify it under the terms of the GNU General Public License                *
7  * as published by the Free Software Foundation; either version 2             *
8  * of the License, or (at your option) any later version.                     *
9  *                                                                            *
10  * This program is distributed in the hope that it will be useful,            *
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of             *
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the              *
13  * GNU General Public License for more details.                               *
14  *                                                                            *
15  * You should have received a copy of the GNU General Public License          *
16  * along with this program; if not, write to the Free Software Foundation     *
17  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA.             *
18  ******************************************************************************/
19
20 #include "notification/notificationcomponent.hpp"
21 #include "notification/notificationcomponent.tcpp"
22 #include "icinga/service.hpp"
23 #include "icinga/icingaapplication.hpp"
24 #include "base/configtype.hpp"
25 #include "base/objectlock.hpp"
26 #include "base/logger.hpp"
27 #include "base/utility.hpp"
28 #include "base/exception.hpp"
29 #include "base/statsfunction.hpp"
30
31 using namespace icinga;
32
33 REGISTER_TYPE(NotificationComponent);
34
35 REGISTER_STATSFUNCTION(NotificationComponent, &NotificationComponent::StatsFunc);
36
37 void NotificationComponent::StatsFunc(const Dictionary::Ptr& status, const Array::Ptr&)
38 {
39         Dictionary::Ptr nodes = new Dictionary();
40
41         for (const NotificationComponent::Ptr& notification_component : ConfigType::GetObjectsByType<NotificationComponent>()) {
42                 nodes->Set(notification_component->GetName(), 1); //add more stats
43         }
44
45         status->Set("notificationcomponent", nodes);
46 }
47
48 /**
49  * Starts the component.
50  */
51 void NotificationComponent::Start(bool runtimeCreated)
52 {
53         ObjectImpl<NotificationComponent>::Start(runtimeCreated);
54
55         Log(LogInformation, "NotificationComponent")
56             << "'" << GetName() << "' started.";
57
58         Checkable::OnNotificationsRequested.connect(std::bind(&NotificationComponent::SendNotificationsHandler, this, _1,
59             _2, _3, _4, _5));
60
61         m_NotificationTimer = new Timer();
62         m_NotificationTimer->SetInterval(5);
63         m_NotificationTimer->OnTimerExpired.connect(std::bind(&NotificationComponent::NotificationTimerHandler, this));
64         m_NotificationTimer->Start();
65 }
66
67 void NotificationComponent::Stop(bool runtimeRemoved)
68 {
69         Log(LogInformation, "NotificationComponent")
70             << "'" << GetName() << "' stopped.";
71
72         ObjectImpl<NotificationComponent>::Stop(runtimeRemoved);
73 }
74
75 /**
76  * Periodically sends notifications.
77  *
78  * @param - Event arguments for the timer.
79  */
80 void NotificationComponent::NotificationTimerHandler(void)
81 {
82         double now = Utility::GetTime();
83
84         for (const Notification::Ptr& notification : ConfigType::GetObjectsByType<Notification>()) {
85                 if (!notification->IsActive())
86                         continue;
87
88                 if (notification->IsPaused() && GetEnableHA())
89                         continue;
90
91                 Checkable::Ptr checkable = notification->GetCheckable();
92
93                 if (!IcingaApplication::GetInstance()->GetEnableNotifications() || !checkable->GetEnableNotifications())
94                         continue;
95
96                 if (notification->GetInterval() <= 0 && notification->GetNoMoreNotifications())
97                         continue;
98
99                 if (notification->GetNextNotification() > now)
100                         continue;
101
102                 bool reachable = checkable->IsReachable(DependencyNotification);
103
104                 {
105                         ObjectLock olock(notification);
106                         notification->SetNextNotification(Utility::GetTime() + notification->GetInterval());
107                 }
108
109                 {
110                         Host::Ptr host;
111                         Service::Ptr service;
112                         tie(host, service) = GetHostService(checkable);
113
114                         ObjectLock olock(checkable);
115
116                         if (checkable->GetStateType() == StateTypeSoft)
117                                 continue;
118
119                         if ((service && service->GetState() == ServiceOK) || (!service && host->GetState() == HostUp))
120                                 continue;
121
122                         if (!reachable || checkable->IsInDowntime() || checkable->IsAcknowledged())
123                                 continue;
124                 }
125
126                 try {
127                         Log(LogNotice, "NotificationComponent")
128                             << "Attempting to send reminder notification '" << notification->GetName() << "'";
129                         notification->BeginExecuteNotification(NotificationProblem, checkable->GetLastCheckResult(), false, true);
130                 } catch (const std::exception& ex) {
131                         Log(LogWarning, "NotificationComponent")
132                             << "Exception occured during notification for object '"
133                             << GetName() << "': " << DiagnosticInformation(ex);
134                 }
135         }
136 }
137
138 /**
139  * Processes icinga::SendNotifications messages.
140  */
141 void NotificationComponent::SendNotificationsHandler(const Checkable::Ptr& checkable, NotificationType type,
142     const CheckResult::Ptr& cr, const String& author, const String& text)
143 {
144         checkable->SendNotifications(type, cr, author, text);
145 }