1 /******************************************************************************
3 * Copyright (C) 2012-2014 Icinga Development Team (http://www.icinga.org) *
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. *
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. *
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 ******************************************************************************/
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 "base/initialize.h"
31 #include "base/scriptvariable.h"
32 #include <boost/foreach.hpp>
34 using namespace icinga;
36 REGISTER_TYPE(Notification);
37 INITIALIZE_ONCE(&Notification::StaticInitialize);
39 boost::signals2::signal<void (const Notification::Ptr&, double, const String&)> Notification::OnNextNotificationChanged;
41 void Notification::StaticInitialize(void)
43 ScriptVariable::Set("NotificationDowntimeStart", NotificationDowntimeStart, true, true);
44 ScriptVariable::Set("NotificationDowntimeEnd", NotificationDowntimeEnd, true, true);
45 ScriptVariable::Set("NotificationDowntimeRemoved", NotificationDowntimeRemoved, true, true);
46 ScriptVariable::Set("NotificationCustom", NotificationCustom, true, true);
47 ScriptVariable::Set("NotificationAcknowledgement", NotificationAcknowledgement, true, true);
48 ScriptVariable::Set("NotificationProblem", NotificationProblem, true, true);
49 ScriptVariable::Set("NotificationRecovery", NotificationRecovery, true, true);
50 ScriptVariable::Set("NotificationFlappingStart", NotificationFlappingStart, true, true);
51 ScriptVariable::Set("NotificationFlappingEnd", NotificationFlappingEnd, true, true);
53 ScriptVariable::Set("NotificationFilterDowntimeStart", 1 << NotificationDowntimeStart, true, true);
54 ScriptVariable::Set("NotificationFilterDowntimeEnd", 1 << NotificationDowntimeEnd, true, true);
55 ScriptVariable::Set("NotificationFilterDowntimeRemoved", 1 << NotificationDowntimeRemoved, true, true);
56 ScriptVariable::Set("NotificationFilterCustom", 1 << NotificationCustom, true, true);
57 ScriptVariable::Set("NotificationFilterAcknowledgement", 1 << NotificationAcknowledgement, true, true);
58 ScriptVariable::Set("NotificationFilterProblem", 1 << NotificationProblem, true, true);
59 ScriptVariable::Set("NotificationFilterRecovery", 1 << NotificationRecovery, true, true);
60 ScriptVariable::Set("NotificationFilterFlappingStart", 1 << NotificationFlappingStart, true, true);
61 ScriptVariable::Set("NotificationFilterFlappingEnd", 1 << NotificationFlappingEnd, true, true);
64 void Notification::Start(void)
66 DynamicObject::Start();
68 GetCheckable()->AddNotification(GetSelf());
71 void Notification::Stop(void)
73 DynamicObject::Stop();
75 GetCheckable()->RemoveNotification(GetSelf());
78 Checkable::Ptr Notification::GetCheckable(void) const
80 Host::Ptr host = Host::GetByName(GetHostRaw());
82 if (GetServiceRaw().IsEmpty())
85 return host->GetServiceByShortName(GetServiceRaw());
88 NotificationCommand::Ptr Notification::GetNotificationCommand(void) const
90 return NotificationCommand::GetByName(GetNotificationCommandRaw());
93 std::set<User::Ptr> Notification::GetUsers(void) const
95 std::set<User::Ptr> result;
97 Array::Ptr users = GetUsersRaw();
100 ObjectLock olock(users);
102 BOOST_FOREACH(const String& name, users) {
103 User::Ptr user = User::GetByName(name);
115 std::set<UserGroup::Ptr> Notification::GetUserGroups(void) const
117 std::set<UserGroup::Ptr> result;
119 Array::Ptr groups = GetUserGroupsRaw();
122 ObjectLock olock(groups);
124 BOOST_FOREACH(const String& name, groups) {
125 UserGroup::Ptr ug = UserGroup::GetByName(name);
137 TimePeriod::Ptr Notification::GetNotificationPeriod(void) const
139 return TimePeriod::GetByName(GetNotificationPeriodRaw());
142 double Notification::GetNextNotification(void) const
144 return GetNextNotificationRaw();
148 * Sets the timestamp when the next periodical notification should be sent.
149 * This does not affect notifications that are sent for state changes.
151 void Notification::SetNextNotification(double time, const String& authority)
153 SetNextNotificationRaw(time);
155 OnNextNotificationChanged(GetSelf(), time, authority);
158 void Notification::UpdateNotificationNumber(void)
160 SetNotificationNumber(GetNotificationNumber() + 1);
163 void Notification::ResetNotificationNumber(void)
165 SetNotificationNumber(0);
168 String Notification::NotificationTypeToString(NotificationType type)
171 case NotificationDowntimeStart:
172 return "DOWNTIMESTART";
173 case NotificationDowntimeEnd:
174 return "DOWNTIMEEND";
175 case NotificationDowntimeRemoved:
176 return "DOWNTIMECANCELLED";
177 case NotificationCustom:
179 case NotificationAcknowledgement:
180 return "ACKNOWLEDGEMENT";
181 case NotificationProblem:
183 case NotificationRecovery:
185 case NotificationFlappingStart:
186 return "FLAPPINGSTART";
187 case NotificationFlappingEnd:
188 return "FLAPPINGEND";
190 return "UNKNOWN_NOTIFICATION";
194 void Notification::BeginExecuteNotification(NotificationType type, const CheckResult::Ptr& cr, bool force, const String& author, const String& text)
198 Checkable::Ptr checkable = GetCheckable();
201 TimePeriod::Ptr tp = GetNotificationPeriod();
203 if (tp && !tp->IsInside(Utility::GetTime())) {
204 Log(LogInformation, "icinga", "Not sending notifications for notification object '" + GetName() + "': not in timeperiod");
208 double now = Utility::GetTime();
209 Dictionary::Ptr times = GetTimes();
211 if (type == NotificationProblem) {
212 if (times && times->Contains("begin") && now < checkable->GetLastHardStateChange() + times->Get("begin")) {
213 Log(LogInformation, "icinga", "Not sending notifications for notification object '" + GetName() + "': before escalation range");
217 if (times && times->Contains("end") && now > checkable->GetLastHardStateChange() + times->Get("end")) {
218 Log(LogInformation, "icinga", "Not sending notifications for notification object '" + GetName() + "': after escalation range");
223 unsigned long ftype = 1 << type;
225 Log(LogDebug, "icinga", "FType=" + Convert::ToString(ftype) + ", TypeFilter=" + Convert::ToString(GetNotificationTypeFilter()));
227 if (!(ftype & GetNotificationTypeFilter())) {
228 Log(LogInformation, "icinga", "Not sending notifications for notification object '" + GetName() + "': type filter does not match");
232 Service::Ptr service = dynamic_pointer_cast<Service>(checkable);
236 host = service->GetHost();
238 host = static_pointer_cast<Host>(checkable);
240 unsigned long fstate;
243 fstate = 1 << service->GetState();
245 fstate = 1 << host->GetState();
247 if (!(fstate & GetNotificationStateFilter())) {
248 Log(LogInformation, "icinga", "Not sending notifications for notification object '" + GetName() + "': state filter does not match");
254 ObjectLock olock(this);
256 double now = Utility::GetTime();
257 SetLastNotification(now);
259 if (type == NotificationProblem)
260 SetLastProblemNotification(now);
263 std::set<User::Ptr> allUsers;
265 std::set<User::Ptr> users = GetUsers();
266 std::copy(users.begin(), users.end(), std::inserter(allUsers, allUsers.begin()));
268 BOOST_FOREACH(const UserGroup::Ptr& ug, GetUserGroups()) {
269 std::set<User::Ptr> members = ug->GetMembers();
270 std::copy(members.begin(), members.end(), std::inserter(allUsers, allUsers.begin()));
273 Service::OnNotificationSendStart(GetSelf(), checkable, allUsers, type, cr, author, text);
275 std::set<User::Ptr> allNotifiedUsers;
276 BOOST_FOREACH(const User::Ptr& user, allUsers) {
277 if (!CheckNotificationUserFilters(type, user, force))
280 Log(LogDebug, "icinga", "Sending notification for user '" + user->GetName() + "'");
281 Utility::QueueAsyncCallback(boost::bind(&Notification::ExecuteNotificationHelper, this, type, user, cr, force, author, text));
283 /* collect all notified users */
284 allNotifiedUsers.insert(user);
287 /* used in db_ido for notification history */
288 Service::OnNotificationSentToAllUsers(GetSelf(), checkable, allNotifiedUsers, type, cr, author, text);
291 bool Notification::CheckNotificationUserFilters(NotificationType type, const User::Ptr& user, bool force)
296 TimePeriod::Ptr tp = user->GetNotificationPeriod();
298 if (tp && !tp->IsInside(Utility::GetTime())) {
299 Log(LogInformation, "icinga", "Not sending notifications for notification object '" +
300 GetName() + " and user '" + user->GetName() + "': user not in timeperiod");
304 unsigned long ftype = 1 << type;
306 if (!(ftype & user->GetNotificationTypeFilter())) {
307 Log(LogInformation, "icinga", "Not sending notifications for notification object '" +
308 GetName() + " and user '" + user->GetName() + "': type filter does not match");
312 Checkable::Ptr checkable = GetCheckable();
313 Service::Ptr service = dynamic_pointer_cast<Service>(checkable);
317 host = service->GetHost();
319 host = static_pointer_cast<Host>(checkable);
321 unsigned long fstate;
324 fstate = 1 << service->GetState();
326 fstate = 1 << host->GetState();
328 if (!(fstate & user->GetNotificationStateFilter())) {
329 Log(LogInformation, "icinga", "Not sending notifications for notification object '" +
330 GetName() + " and user '" + user->GetName() + "': state filter does not match");
338 void Notification::ExecuteNotificationHelper(NotificationType type, const User::Ptr& user, const CheckResult::Ptr& cr, bool force, const String& author, const String& text)
343 NotificationCommand::Ptr command = GetNotificationCommand();
346 Log(LogDebug, "icinga", "No notification_command found for notification '" + GetName() + "'. Skipping execution.");
350 command->Execute(GetSelf(), user, cr, type, author, text);
353 ObjectLock olock(this);
354 UpdateNotificationNumber();
355 SetLastNotification(Utility::GetTime());
358 /* required by compatlogger */
359 Service::OnNotificationSentToUser(GetSelf(), GetCheckable(), user, type, cr, author, text, command->GetName());
361 Log(LogInformation, "icinga", "Completed sending notification for object '" + GetCheckable()->GetName() + "'");
362 } catch (const std::exception& ex) {
363 std::ostringstream msgbuf;
364 msgbuf << "Exception occured during notification for object '"
365 << GetCheckable()->GetName() << "': " << DiagnosticInformation(ex);
366 Log(LogWarning, "icinga", msgbuf.str());
370 bool Notification::ResolveMacro(const String& macro, const CheckResult::Ptr&, String *result) const
372 Dictionary::Ptr vars = GetVars();
374 if (macro.SubStr(0, 13) == "notification.") {
375 String key = vars->Get(macro.SubStr(13));
377 if (vars && vars->Contains(key)) {
378 *result = vars->Get(key);