]> granicus.if.org Git - icinga2/blob - lib/icinga/notification.cpp
Merge branch 'feature/debian-packaging-4988' into next
[icinga2] / lib / icinga / notification.cpp
1 /******************************************************************************
2  * Icinga 2                                                                   *
3  * Copyright (C) 2012-2013 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 "base/dynamictype.h"
25 #include "base/objectlock.h"
26 #include "base/logger_fwd.h"
27 #include "base/utility.h"
28 #include "base/convert.h"
29 #include <boost/tuple/tuple.hpp>
30 #include <boost/foreach.hpp>
31 #include <boost/exception/diagnostic_information.hpp>
32
33 using namespace icinga;
34
35 REGISTER_NTYPE(Notification);
36 REGISTER_TYPE(Notification);
37
38 boost::signals2::signal<void (const Notification::Ptr&, double, const String&)> Notification::OnNextNotificationChanged;
39
40 void Notification::Start(void)
41 {
42         DynamicObject::Start();
43
44         GetService()->AddNotification(GetSelf());
45 }
46
47 void Notification::Stop(void)
48 {
49         DynamicObject::Stop();
50
51         GetService()->RemoveNotification(GetSelf());
52 }
53
54 Service::Ptr Notification::GetService(void) const
55 {
56         Host::Ptr host = Host::GetByName(GetHostRaw());
57
58         if (!host)
59                 return Service::Ptr();
60
61         if (GetServiceRaw().IsEmpty())
62                 return host->GetCheckService();
63         else
64                 return host->GetServiceByShortName(GetServiceRaw());
65 }
66
67 NotificationCommand::Ptr Notification::GetNotificationCommand(void) const
68 {
69         return NotificationCommand::GetByName(GetNotificationCommandRaw());
70 }
71
72 std::set<User::Ptr> Notification::GetUsers(void) const
73 {
74         std::set<User::Ptr> result;
75
76         Array::Ptr users = GetUsersRaw();
77
78         if (users) {
79                 ObjectLock olock(users);
80
81                 BOOST_FOREACH(const String& name, users) {
82                         User::Ptr user = User::GetByName(name);
83
84                         if (!user)
85                                 continue;
86
87                         result.insert(user);
88                 }
89         }
90
91         return result;
92 }
93
94 std::set<UserGroup::Ptr> Notification::GetUserGroups(void) const
95 {
96         std::set<UserGroup::Ptr> result;
97
98         Array::Ptr groups = GetUserGroupsRaw();
99
100         if (groups) {
101                 ObjectLock olock(groups);
102
103                 BOOST_FOREACH(const String& name, groups) {
104                         UserGroup::Ptr ug = UserGroup::GetByName(name);
105
106                         if (!ug)
107                                 continue;
108
109                         result.insert(ug);
110                 }
111         }
112
113         return result;
114 }
115
116 TimePeriod::Ptr Notification::GetNotificationPeriod(void) const
117 {
118         return TimePeriod::GetByName(GetNotificationPeriodRaw());
119 }
120
121 double Notification::GetNextNotification(void) const
122 {
123         return GetNextNotificationRaw();
124 }
125
126 /**
127  * Sets the timestamp when the next periodical notification should be sent.
128  * This does not affect notifications that are sent for state changes.
129  */
130 void Notification::SetNextNotification(double time, const String& authority)
131 {
132         SetNextNotificationRaw(time);
133
134         Utility::QueueAsyncCallback(boost::bind(boost::ref(OnNextNotificationChanged), GetSelf(), time, authority));
135 }
136
137 void Notification::UpdateNotificationNumber(void)
138 {
139         SetNotificationNumber(GetNotificationNumber() + 1);
140 }
141
142 void Notification::ResetNotificationNumber(void)
143 {
144         SetNotificationNumber(0);
145 }
146
147 String Notification::NotificationTypeToString(NotificationType type)
148 {
149         switch (type) {
150                 case NotificationDowntimeStart:
151                         return "DOWNTIMESTART";
152                 case NotificationDowntimeEnd:
153                         return "DOWNTIMEEND";
154                 case NotificationDowntimeRemoved:
155                         return "DOWNTIMECANCELLED";
156                 case NotificationCustom:
157                         return "CUSTOM";
158                 case NotificationAcknowledgement:
159                         return "ACKNOWLEDGEMENT";
160                 case NotificationProblem:
161                         return "PROBLEM";
162                 case NotificationRecovery:
163                         return "RECOVERY";
164                 case NotificationFlappingStart:
165                         return "FLAPPINGSTART";
166                 case NotificationFlappingEnd:
167                         return "FLAPPINGEND";
168                 default:
169                         return "UNKNOWN_NOTIFICATION";
170         }
171 }
172
173 void Notification::BeginExecuteNotification(NotificationType type, const Dictionary::Ptr& cr, bool force, const String& author, const String& text)
174 {
175         ASSERT(!OwnsLock());
176
177         if (!force) {
178                 TimePeriod::Ptr tp = GetNotificationPeriod();
179
180                 if (tp && !tp->IsInside(Utility::GetTime())) {
181                         Log(LogInformation, "icinga", "Not sending notifications for notification object '" + GetName() + "': not in timeperiod");
182                         return;
183                 }
184
185                 double now = Utility::GetTime();
186                 Dictionary::Ptr times = GetTimes();
187                 Service::Ptr service = GetService();
188
189                 if (type == NotificationProblem) {
190                         if (times && times->Contains("begin") && now < service->GetLastHardStateChange() + times->Get("begin")) {
191                                 Log(LogInformation, "icinga", "Not sending notifications for notification object '" + GetName() + "': before escalation range");
192                                 return;
193                         }
194
195                         if (times && times->Contains("end") && now > service->GetLastHardStateChange() + times->Get("end")) {
196                                 Log(LogInformation, "icinga", "Not sending notifications for notification object '" + GetName() + "': after escalation range");
197                                 return;
198                         }
199                 }
200
201                 unsigned long ftype = 1 << type;
202
203                 Log(LogDebug, "icinga", "FType=" + Convert::ToString(ftype) + ", TypeFilter=" + Convert::ToString(GetNotificationTypeFilter()));
204
205                 if (!(ftype & GetNotificationTypeFilter())) {
206                         Log(LogInformation, "icinga", "Not sending notifications for notification object '" + GetName() + "': type filter does not match");
207                         return;
208                 }
209
210                 unsigned long fstate = 1 << GetService()->GetState();
211
212                 if (!(fstate & GetNotificationStateFilter())) {
213                         Log(LogInformation, "icinga", "Not sending notifications for notification object '" + GetName() + "': state filter does not match");
214                         return;
215                 }
216         }
217
218         {
219                 ObjectLock olock(this);
220
221                 SetLastNotification(Utility::GetTime());
222         }
223
224         std::set<User::Ptr> allUsers;
225
226         std::set<User::Ptr> users = GetUsers();
227         std::copy(users.begin(), users.end(), std::inserter(allUsers, allUsers.begin()));
228
229         BOOST_FOREACH(const UserGroup::Ptr& ug, GetUserGroups()) {
230                 std::set<User::Ptr> members = ug->GetMembers();
231                 std::copy(members.begin(), members.end(), std::inserter(allUsers, allUsers.begin()));
232         }
233
234         unsigned long notified_users = 0;
235         BOOST_FOREACH(const User::Ptr& user, allUsers) {
236                 Log(LogDebug, "icinga", "Sending notification for user '" + user->GetName() + "'");
237                 Utility::QueueAsyncCallback(boost::bind(&Notification::ExecuteNotificationHelper, this, type, user, cr, force, author, text));
238                 notified_users++;
239         }
240
241         Service::OnNotificationSentToAllUsers(GetService(), allUsers, type, cr, author, text);
242 }
243
244 void Notification::ExecuteNotificationHelper(NotificationType type, const User::Ptr& user, const Dictionary::Ptr& cr, bool force, const String& author, const String& text)
245 {
246         ASSERT(!OwnsLock());
247
248         if (!force) {
249                 TimePeriod::Ptr tp = user->GetNotificationPeriod();
250
251                 if (tp && !tp->IsInside(Utility::GetTime())) {
252                         Log(LogInformation, "icinga", "Not sending notifications for notification object '" +
253                             GetName() + " and user '" + user->GetName() + "': user not in timeperiod");
254                         return;
255                 }
256
257                 unsigned long ftype = 1 << type;
258
259                 if (!(ftype & user->GetNotificationTypeFilter())) {
260                         Log(LogInformation, "icinga", "Not sending notifications for notification object '" +
261                             GetName() + " and user '" + user->GetName() + "': type filter does not match");
262                         return;
263                 }
264
265                 unsigned long fstate = 1 << GetService()->GetState();
266
267                 if (!(fstate & user->GetNotificationStateFilter())) {
268                         Log(LogInformation, "icinga", "Not sending notifications for notification object '" +
269                             GetName() + " and user '" + user->GetName() + "': state filter does not match");
270                         return;
271                 }
272         }
273
274         try {
275                 NotificationCommand::Ptr command = GetNotificationCommand();
276
277                 if (!command) {
278                         Log(LogDebug, "icinga", "No notification_command found for notification '" + GetName() + "'. Skipping execution.");
279                         return;
280                 }
281
282                 command->Execute(GetSelf(), user, cr, type, author, text);
283
284                 {
285                         ObjectLock olock(this);
286                         UpdateNotificationNumber();
287                         SetLastNotification(Utility::GetTime());
288                 }
289
290                 Service::OnNotificationSentToUser(GetService(), user, type, cr, author, text, command->GetName());
291
292                 Log(LogInformation, "icinga", "Completed sending notification for service '" + GetService()->GetName() + "'");
293         } catch (const std::exception& ex) {
294                 std::ostringstream msgbuf;
295                 msgbuf << "Exception occured during notification for service '"
296                        << GetService()->GetName() << "': " << boost::diagnostic_information(ex);
297                 Log(LogWarning, "icinga", msgbuf.str());
298         }
299 }
300
301 bool Notification::ResolveMacro(const String& macro, const Dictionary::Ptr&, String *result) const
302 {
303         Dictionary::Ptr macros = GetMacros();
304
305         if (macros && macros->Contains(macro)) {
306                 *result = macros->Get(macro);
307                 return true;
308         }
309
310         return false;
311 }