]> granicus.if.org Git - icinga2/blob - lib/icinga/notification.cpp
Merge branch 'feature/checks-5049' 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_TYPE(Notification);
36
37 boost::signals2::signal<void (const Notification::Ptr&, double, const String&)> Notification::OnNextNotificationChanged;
38
39 void Notification::Start(void)
40 {
41         DynamicObject::Start();
42
43         GetService()->AddNotification(GetSelf());
44 }
45
46 void Notification::Stop(void)
47 {
48         DynamicObject::Stop();
49
50         GetService()->RemoveNotification(GetSelf());
51 }
52
53 Service::Ptr Notification::GetService(void) const
54 {
55         Host::Ptr host = Host::GetByName(GetHostRaw());
56
57         if (!host)
58                 return Service::Ptr();
59
60         if (GetServiceRaw().IsEmpty())
61                 return host->GetCheckService();
62         else
63                 return host->GetServiceByShortName(GetServiceRaw());
64 }
65
66 NotificationCommand::Ptr Notification::GetNotificationCommand(void) const
67 {
68         return NotificationCommand::GetByName(GetNotificationCommandRaw());
69 }
70
71 std::set<User::Ptr> Notification::GetUsers(void) const
72 {
73         std::set<User::Ptr> result;
74
75         Array::Ptr users = GetUsersRaw();
76
77         if (users) {
78                 ObjectLock olock(users);
79
80                 BOOST_FOREACH(const String& name, users) {
81                         User::Ptr user = User::GetByName(name);
82
83                         if (!user)
84                                 continue;
85
86                         result.insert(user);
87                 }
88         }
89
90         return result;
91 }
92
93 std::set<UserGroup::Ptr> Notification::GetUserGroups(void) const
94 {
95         std::set<UserGroup::Ptr> result;
96
97         Array::Ptr groups = GetUserGroupsRaw();
98
99         if (groups) {
100                 ObjectLock olock(groups);
101
102                 BOOST_FOREACH(const String& name, groups) {
103                         UserGroup::Ptr ug = UserGroup::GetByName(name);
104
105                         if (!ug)
106                                 continue;
107
108                         result.insert(ug);
109                 }
110         }
111
112         return result;
113 }
114
115 TimePeriod::Ptr Notification::GetNotificationPeriod(void) const
116 {
117         return TimePeriod::GetByName(GetNotificationPeriodRaw());
118 }
119
120 double Notification::GetNextNotification(void) const
121 {
122         return GetNextNotificationRaw();
123 }
124
125 /**
126  * Sets the timestamp when the next periodical notification should be sent.
127  * This does not affect notifications that are sent for state changes.
128  */
129 void Notification::SetNextNotification(double time, const String& authority)
130 {
131         SetNextNotificationRaw(time);
132
133         OnNextNotificationChanged(GetSelf(), time, authority);
134 }
135
136 void Notification::UpdateNotificationNumber(void)
137 {
138         SetNotificationNumber(GetNotificationNumber() + 1);
139 }
140
141 void Notification::ResetNotificationNumber(void)
142 {
143         SetNotificationNumber(0);
144 }
145
146 String Notification::NotificationTypeToString(NotificationType type)
147 {
148         switch (type) {
149                 case NotificationDowntimeStart:
150                         return "DOWNTIMESTART";
151                 case NotificationDowntimeEnd:
152                         return "DOWNTIMEEND";
153                 case NotificationDowntimeRemoved:
154                         return "DOWNTIMECANCELLED";
155                 case NotificationCustom:
156                         return "CUSTOM";
157                 case NotificationAcknowledgement:
158                         return "ACKNOWLEDGEMENT";
159                 case NotificationProblem:
160                         return "PROBLEM";
161                 case NotificationRecovery:
162                         return "RECOVERY";
163                 case NotificationFlappingStart:
164                         return "FLAPPINGSTART";
165                 case NotificationFlappingEnd:
166                         return "FLAPPINGEND";
167                 default:
168                         return "UNKNOWN_NOTIFICATION";
169         }
170 }
171
172 void Notification::BeginExecuteNotification(NotificationType type, const CheckResult::Ptr& cr, bool force, const String& author, const String& text)
173 {
174         ASSERT(!OwnsLock());
175
176         if (!force) {
177                 TimePeriod::Ptr tp = GetNotificationPeriod();
178
179                 if (tp && !tp->IsInside(Utility::GetTime())) {
180                         Log(LogInformation, "icinga", "Not sending notifications for notification object '" + GetName() + "': not in timeperiod");
181                         return;
182                 }
183
184                 double now = Utility::GetTime();
185                 Dictionary::Ptr times = GetTimes();
186                 Service::Ptr service = GetService();
187
188                 if (type == NotificationProblem) {
189                         if (times && times->Contains("begin") && now < service->GetLastHardStateChange() + times->Get("begin")) {
190                                 Log(LogInformation, "icinga", "Not sending notifications for notification object '" + GetName() + "': before escalation range");
191                                 return;
192                         }
193
194                         if (times && times->Contains("end") && now > service->GetLastHardStateChange() + times->Get("end")) {
195                                 Log(LogInformation, "icinga", "Not sending notifications for notification object '" + GetName() + "': after escalation range");
196                                 return;
197                         }
198                 }
199
200                 unsigned long ftype = 1 << type;
201
202                 Log(LogDebug, "icinga", "FType=" + Convert::ToString(ftype) + ", TypeFilter=" + Convert::ToString(GetNotificationTypeFilter()));
203
204                 if (!(ftype & GetNotificationTypeFilter())) {
205                         Log(LogInformation, "icinga", "Not sending notifications for notification object '" + GetName() + "': type filter does not match");
206                         return;
207                 }
208
209                 unsigned long fstate = 1 << GetService()->GetState();
210
211                 if (!(fstate & GetNotificationStateFilter())) {
212                         Log(LogInformation, "icinga", "Not sending notifications for notification object '" + GetName() + "': state filter does not match");
213                         return;
214                 }
215         }
216
217         {
218                 ObjectLock olock(this);
219
220                 SetLastNotification(Utility::GetTime());
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() << "': " << boost::diagnostic_information(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 }