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