1 /******************************************************************************
3 * Copyright (C) 2012-2013 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 GetService()->AddNotification(GetSelf());
71 void Notification::Stop(void)
73 DynamicObject::Stop();
75 GetService()->RemoveNotification(GetSelf());
78 Service::Ptr Notification::GetService(void) const
80 Host::Ptr host = Host::GetByName(GetHostRaw());
82 if (GetServiceRaw().IsEmpty())
83 return host->GetCheckService();
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)
199 TimePeriod::Ptr tp = GetNotificationPeriod();
201 if (tp && !tp->IsInside(Utility::GetTime())) {
202 Log(LogInformation, "icinga", "Not sending notifications for notification object '" + GetName() + "': not in timeperiod");
206 double now = Utility::GetTime();
207 Dictionary::Ptr times = GetTimes();
208 Service::Ptr service = GetService();
210 if (type == NotificationProblem) {
211 if (times && times->Contains("begin") && now < service->GetLastHardStateChange() + times->Get("begin")) {
212 Log(LogInformation, "icinga", "Not sending notifications for notification object '" + GetName() + "': before escalation range");
216 if (times && times->Contains("end") && now > service->GetLastHardStateChange() + times->Get("end")) {
217 Log(LogInformation, "icinga", "Not sending notifications for notification object '" + GetName() + "': after escalation range");
222 unsigned long ftype = 1 << type;
224 Log(LogDebug, "icinga", "FType=" + Convert::ToString(ftype) + ", TypeFilter=" + Convert::ToString(GetNotificationTypeFilter()));
226 if (!(ftype & GetNotificationTypeFilter())) {
227 Log(LogInformation, "icinga", "Not sending notifications for notification object '" + GetName() + "': type filter does not match");
231 unsigned long fstate = 1 << GetService()->GetState();
233 if (!(fstate & GetNotificationStateFilter())) {
234 Log(LogInformation, "icinga", "Not sending notifications for notification object '" + GetName() + "': state filter does not match");
240 ObjectLock olock(this);
242 double now = Utility::GetTime();
243 SetLastNotification(now);
245 if (type == NotificationProblem)
246 SetLastProblemNotification(now);
249 std::set<User::Ptr> allUsers;
251 std::set<User::Ptr> users = GetUsers();
252 std::copy(users.begin(), users.end(), std::inserter(allUsers, allUsers.begin()));
254 BOOST_FOREACH(const UserGroup::Ptr& ug, GetUserGroups()) {
255 std::set<User::Ptr> members = ug->GetMembers();
256 std::copy(members.begin(), members.end(), std::inserter(allUsers, allUsers.begin()));
259 unsigned long notified_users = 0;
260 BOOST_FOREACH(const User::Ptr& user, allUsers) {
261 Log(LogDebug, "icinga", "Sending notification for user '" + user->GetName() + "'");
262 Utility::QueueAsyncCallback(boost::bind(&Notification::ExecuteNotificationHelper, this, type, user, cr, force, author, text));
266 Service::OnNotificationSentToAllUsers(GetService(), allUsers, type, cr, author, text);
269 void Notification::ExecuteNotificationHelper(NotificationType type, const User::Ptr& user, const CheckResult::Ptr& cr, bool force, const String& author, const String& text)
274 TimePeriod::Ptr tp = user->GetNotificationPeriod();
276 if (tp && !tp->IsInside(Utility::GetTime())) {
277 Log(LogInformation, "icinga", "Not sending notifications for notification object '" +
278 GetName() + " and user '" + user->GetName() + "': user not in timeperiod");
282 unsigned long ftype = 1 << type;
284 if (!(ftype & user->GetNotificationTypeFilter())) {
285 Log(LogInformation, "icinga", "Not sending notifications for notification object '" +
286 GetName() + " and user '" + user->GetName() + "': type filter does not match");
290 unsigned long fstate = 1 << GetService()->GetState();
292 if (!(fstate & user->GetNotificationStateFilter())) {
293 Log(LogInformation, "icinga", "Not sending notifications for notification object '" +
294 GetName() + " and user '" + user->GetName() + "': state filter does not match");
300 NotificationCommand::Ptr command = GetNotificationCommand();
303 Log(LogDebug, "icinga", "No notification_command found for notification '" + GetName() + "'. Skipping execution.");
307 command->Execute(GetSelf(), user, cr, type, author, text);
310 ObjectLock olock(this);
311 UpdateNotificationNumber();
312 SetLastNotification(Utility::GetTime());
315 Service::OnNotificationSentToUser(GetService(), user, type, cr, author, text, command->GetName());
317 Log(LogInformation, "icinga", "Completed sending notification for service '" + GetService()->GetName() + "'");
318 } catch (const std::exception& ex) {
319 std::ostringstream msgbuf;
320 msgbuf << "Exception occured during notification for service '"
321 << GetService()->GetName() << "': " << DiagnosticInformation(ex);
322 Log(LogWarning, "icinga", msgbuf.str());
326 bool Notification::ResolveMacro(const String& macro, const CheckResult::Ptr&, String *result) const
328 if (macro.SubStr(0, 13) == "_NOTIFICATION") {
329 Dictionary::Ptr custom = GetCustom();
330 *result = custom ? custom->Get(macro.SubStr(13)) : "";
334 Dictionary::Ptr macros = GetMacros();
336 if (macros && macros->Contains(macro)) {
337 *result = macros->Get(macro);