]> granicus.if.org Git - icinga2/blob - lib/icinga/notification.cpp
Remove the replication feature and clean up the code.
[icinga2] / lib / icinga / notification.cpp
1 /******************************************************************************
2  * Icinga 2                                                                   *
3  * Copyright (C) 2012 Icinga Development Team (http://www.icinga.org/)        *
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 "icinga/notification.h"
21 #include "icinga/notificationcommand.h"
22 #include "icinga/macroprocessor.h"
23 #include "icinga/service.h"
24 #include "remoting/endpointmanager.h"
25 #include "base/dynamictype.h"
26 #include "base/objectlock.h"
27 #include "base/logger_fwd.h"
28 #include "base/utility.h"
29 #include "base/convert.h"
30 #include <boost/tuple/tuple.hpp>
31 #include <boost/foreach.hpp>
32 #include <boost/exception/diagnostic_information.hpp>
33
34 using namespace icinga;
35
36 REGISTER_TYPE(Notification);
37
38 void Notification::Start(void)
39 {
40         DynamicObject::Start();
41
42         GetService()->AddNotification(GetSelf());
43 }
44
45 void Notification::Stop(void)
46 {
47         DynamicObject::Stop();
48
49         GetService()->RemoveNotification(GetSelf());
50 }
51
52 Service::Ptr Notification::GetService(void) const
53 {
54         Host::Ptr host = Host::GetByName(m_HostName);
55
56         if (!host)
57                 return Service::Ptr();
58
59         if (m_Service.IsEmpty())
60                 return host->GetHostCheckService();
61         else
62                 return host->GetServiceByShortName(m_Service);
63 }
64
65 NotificationCommand::Ptr Notification::GetNotificationCommand(void) const
66 {
67         return NotificationCommand::GetByName(m_NotificationCommand);
68 }
69
70 Dictionary::Ptr Notification::GetMacros(void) const
71 {
72         return m_Macros;
73 }
74
75 Array::Ptr Notification::GetExportMacros(void) const
76 {
77         return m_ExportMacros;
78 }
79
80 std::set<User::Ptr> Notification::GetUsers(void) const
81 {
82         std::set<User::Ptr> result;
83
84         Array::Ptr users = m_Users;
85
86         if (users) {
87                 ObjectLock olock(users);
88
89                 BOOST_FOREACH(const String& name, users) {
90                         User::Ptr user = User::GetByName(name);
91
92                         if (!user)
93                                 continue;
94
95                         result.insert(user);
96                 }
97         }
98
99         return result;
100 }
101
102 std::set<UserGroup::Ptr> Notification::GetGroups(void) const
103 {
104         std::set<UserGroup::Ptr> result;
105
106         Array::Ptr groups = m_Groups;
107
108         if (groups) {
109                 ObjectLock olock(groups);
110
111                 BOOST_FOREACH(const String& name, groups) {
112                         UserGroup::Ptr ug = UserGroup::GetByName(name);
113
114                         if (!ug)
115                                 continue;
116
117                         result.insert(ug);
118                 }
119         }
120
121         return result;
122 }
123
124 Dictionary::Ptr Notification::GetTimes(void) const
125 {
126         return m_Times;
127 }
128
129 unsigned long Notification::GetNotificationTypeFilter(void) const
130 {
131         if (m_NotificationTypeFilter.IsEmpty())
132                 return ~(unsigned long)0; /* All states. */
133         else
134                 return m_NotificationTypeFilter;
135 }
136
137 unsigned long Notification::GetNotificationStateFilter(void) const
138 {
139         if (m_NotificationStateFilter.IsEmpty())
140                 return ~(unsigned long)0; /* All states. */
141         else
142                 return m_NotificationStateFilter;
143 }
144
145 double Notification::GetNotificationInterval(void) const
146 {
147         if (m_NotificationInterval.IsEmpty())
148                 return 300;
149         else
150                 return m_NotificationInterval;
151 }
152
153 TimePeriod::Ptr Notification::GetNotificationPeriod(void) const
154 {
155         return TimePeriod::GetByName(m_NotificationPeriod);
156 }
157
158 double Notification::GetLastNotification(void) const
159 {
160         if (m_LastNotification.IsEmpty())
161                 return 0;
162         else
163                 return m_LastNotification;
164 }
165
166 /**
167  * Sets the timestamp when the last notification was sent.
168  */
169 void Notification::SetLastNotification(double time)
170 {
171         m_LastNotification = time;
172 }
173
174 double Notification::GetNextNotification(void) const
175 {
176         if (m_NextNotification.IsEmpty())
177                 return 0;
178         else
179                 return m_NextNotification;
180 }
181
182 /**
183  * Sets the timestamp when the next periodical notification should be sent.
184  * This does not affect notifications that are sent for state changes.
185  */
186 void Notification::SetNextNotification(double time)
187 {
188         m_NextNotification = time;
189 }
190
191 long Notification::GetNotificationNumber(void) const
192 {
193         if (m_NotificationNumber.IsEmpty())
194                 return 0;
195         else
196                 return m_NotificationNumber;
197 }
198
199 void Notification::UpdateNotificationNumber(void)
200 {
201         m_NotificationNumber = m_NotificationNumber + 1;
202 }
203
204 void Notification::ResetNotificationNumber(void)
205 {
206         m_NotificationNumber = 0;
207 }
208
209 String Notification::NotificationTypeToString(NotificationType type)
210 {
211         switch (type) {
212                 case NotificationDowntimeStart:
213                         return "DOWNTIMESTART";
214                 case NotificationDowntimeEnd:
215                         return "DOWNTIMEEND";
216                 case NotificationDowntimeRemoved:
217                         return "DOWNTIMECANCELLED";
218                 case NotificationCustom:
219                         return "CUSTOM";
220                 case NotificationAcknowledgement:
221                         return "ACKNOWLEDGEMENT";
222                 case NotificationProblem:
223                         return "PROBLEM";
224                 case NotificationRecovery:
225                         return "RECOVERY";
226                 case NotificationFlappingStart:
227                         return "FLAPPINGSTART";
228                 case NotificationFlappingEnd:
229                         return "FLAPPINGEND";
230                 default:
231                         return "UNKNOWN_NOTIFICATION";
232         }
233 }
234
235 void Notification::BeginExecuteNotification(NotificationType type, const Dictionary::Ptr& cr, bool force, const String& author, const String& text)
236 {
237         ASSERT(!OwnsLock());
238
239         if (!force) {
240                 TimePeriod::Ptr tp = GetNotificationPeriod();
241
242                 if (tp && !tp->IsInside(Utility::GetTime())) {
243                         Log(LogInformation, "icinga", "Not sending notifications for notification object '" + GetName() + "': not in timeperiod");
244                         return;
245                 }
246
247                 double now = Utility::GetTime();
248                 Dictionary::Ptr times = GetTimes();
249                 Service::Ptr service = GetService();
250
251                 if (type == NotificationProblem) {
252                         if (times && times->Contains("begin") && now < service->GetLastHardStateChange() + times->Get("begin")) {
253                                 Log(LogInformation, "icinga", "Not sending notifications for notification object '" + GetName() + "': before escalation range");
254                                 return;
255                         }
256
257                         if (times && times->Contains("end") && now > service->GetLastHardStateChange() + times->Get("end")) {
258                                 Log(LogInformation, "icinga", "Not sending notifications for notification object '" + GetName() + "': after escalation range");
259                                 return;
260                         }
261                 }
262
263                 unsigned long ftype = 1 << type;
264
265                 Log(LogDebug, "icinga", "FType=" + Convert::ToString(ftype) + ", TypeFilter=" + Convert::ToString(GetNotificationTypeFilter()));
266
267                 if (!(ftype & GetNotificationTypeFilter())) {
268                         Log(LogInformation, "icinga", "Not sending notifications for notification object '" + GetName() + "': type filter does not match");
269                         return;
270                 }
271
272                 unsigned long fstate = 1 << GetService()->GetState();
273
274                 if (!(fstate & GetNotificationStateFilter())) {
275                         Log(LogInformation, "icinga", "Not sending notifications for notification object '" + GetName() + "': state filter does not match");
276                         return;
277                 }
278         }
279
280         {
281                 ObjectLock olock(this);
282
283                 SetLastNotification(Utility::GetTime());
284         }
285
286         std::set<User::Ptr> allUsers;
287
288         std::set<User::Ptr> users = GetUsers();
289         std::copy(users.begin(), users.end(), std::inserter(allUsers, allUsers.begin()));
290
291         BOOST_FOREACH(const UserGroup::Ptr& ug, GetGroups()) {
292                 std::set<User::Ptr> members = ug->GetMembers();
293                 std::copy(members.begin(), members.end(), std::inserter(allUsers, allUsers.begin()));
294         }
295
296         BOOST_FOREACH(const User::Ptr& user, allUsers) {
297                 Log(LogDebug, "icinga", "Sending notification for user '" + user->GetName() + "'");
298                 Utility::QueueAsyncCallback(boost::bind(&Notification::ExecuteNotificationHelper, this, type, user, cr, force, author, text));
299         }
300 }
301
302 void Notification::ExecuteNotificationHelper(NotificationType type, const User::Ptr& user, const Dictionary::Ptr& cr, bool force, const String& author, const String& text)
303 {
304         ASSERT(!OwnsLock());
305
306         if (!force) {
307                 TimePeriod::Ptr tp = user->GetNotificationPeriod();
308
309                 if (tp && !tp->IsInside(Utility::GetTime())) {
310                         Log(LogInformation, "icinga", "Not sending notifications for notification object '" +
311                             GetName() + " and user '" + user->GetName() + "': user not in timeperiod");
312                         return;
313                 }
314
315                 unsigned long ftype = 1 << type;
316
317                 if (!(ftype & user->GetNotificationTypeFilter())) {
318                         Log(LogInformation, "icinga", "Not sending notifications for notification object '" +
319                             GetName() + " and user '" + user->GetName() + "': type filter does not match");
320                         return;
321                 }
322
323                 unsigned long fstate = 1 << GetService()->GetState();
324
325                 if (!(fstate & user->GetNotificationStateFilter())) {
326                         Log(LogInformation, "icinga", "Not sending notifications for notification object '" +
327                             GetName() + " and user '" + user->GetName() + "': state filter does not match");
328                         return;
329                 }
330         }
331
332         try {
333                 GetNotificationCommand()->Execute(GetSelf(), user, cr, type);
334
335                 {
336                         ObjectLock olock(this);
337                         UpdateNotificationNumber();
338                         SetLastNotification(Utility::GetTime());
339                 }
340
341                 Service::OnNotificationSentChanged(GetSelf(), user, type, cr, author, text);
342
343                 Log(LogInformation, "icinga", "Completed sending notification for service '" + GetService()->GetName() + "'");
344         } catch (const std::exception& ex) {
345                 std::ostringstream msgbuf;
346                 msgbuf << "Exception occured during notification for service '"
347                        << GetService()->GetName() << "': " << boost::diagnostic_information(ex);
348                 String message = msgbuf.str();
349
350                 Log(LogWarning, "icinga", message);
351         }
352 }
353
354 bool Notification::ResolveMacro(const String& macro, const Dictionary::Ptr&, String *result) const
355 {
356         Dictionary::Ptr macros = GetMacros();
357
358         if (macros && macros->Contains(macro)) {
359                 *result = macros->Get(macro);
360                 return true;
361         }
362
363         return false;
364 }
365
366 void Notification::InternalSerialize(const Dictionary::Ptr& bag, int attributeTypes) const
367 {
368         DynamicObject::InternalSerialize(bag, attributeTypes);
369
370         bag->Set("notification_command", m_NotificationCommand);
371         bag->Set("notification_interval", m_NotificationInterval);
372         bag->Set("notification_period", m_NotificationPeriod);
373         bag->Set("last_notification", m_LastNotification);
374         bag->Set("next_notification", m_NextNotification);
375         bag->Set("notification_number", m_NotificationNumber);
376         bag->Set("macros", m_Macros);
377         bag->Set("users", m_Users);
378         bag->Set("groups", m_Groups);
379         bag->Set("times", m_Times);
380         bag->Set("notification_type_filter", m_NotificationTypeFilter);
381         bag->Set("notification_state_filter", m_NotificationStateFilter);
382         bag->Set("host_name", m_HostName);
383         bag->Set("export_macros", m_ExportMacros);
384         bag->Set("service", m_Service);
385 }
386
387 void Notification::InternalDeserialize(const Dictionary::Ptr& bag, int attributeTypes)
388 {
389         DynamicObject::InternalDeserialize(bag, attributeTypes);
390
391         m_NotificationCommand = bag->Get("notification_command");
392         m_NotificationInterval = bag->Get("notification_interval");
393         m_NotificationPeriod = bag->Get("notification_period");
394         m_LastNotification = bag->Get("last_notification");
395         m_NextNotification = bag->Get("next_notification");
396         m_NotificationNumber = bag->Get("notification_number");
397         m_Macros = bag->Get("macros");
398         m_Users = bag->Get("users");
399         m_Groups = bag->Get("groups");
400         m_Times = bag->Get("times");
401         m_NotificationTypeFilter = bag->Get("notification_type_filter");
402         m_NotificationStateFilter = bag->Get("notification_state_filter");
403         m_HostName = bag->Get("host_name");
404         m_ExportMacros = bag->Get("export_macros");
405         m_Service = bag->Get("service");
406 }