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 <boost/foreach.hpp>
32 using namespace icinga;
34 REGISTER_TYPE(Notification);
36 boost::signals2::signal<void (const Notification::Ptr&, double, const String&)> Notification::OnNextNotificationChanged;
38 void Notification::Start(void)
40 DynamicObject::Start();
42 GetService()->AddNotification(GetSelf());
45 void Notification::Stop(void)
47 DynamicObject::Stop();
49 GetService()->RemoveNotification(GetSelf());
52 Service::Ptr Notification::GetService(void) const
54 Host::Ptr host = Host::GetByName(GetHostRaw());
56 if (GetServiceRaw().IsEmpty())
57 return host->GetCheckService();
59 return host->GetServiceByShortName(GetServiceRaw());
62 NotificationCommand::Ptr Notification::GetNotificationCommand(void) const
64 return NotificationCommand::GetByName(GetNotificationCommandRaw());
67 std::set<User::Ptr> Notification::GetUsers(void) const
69 std::set<User::Ptr> result;
71 Array::Ptr users = GetUsersRaw();
74 ObjectLock olock(users);
76 BOOST_FOREACH(const String& name, users) {
77 User::Ptr user = User::GetByName(name);
89 std::set<UserGroup::Ptr> Notification::GetUserGroups(void) const
91 std::set<UserGroup::Ptr> result;
93 Array::Ptr groups = GetUserGroupsRaw();
96 ObjectLock olock(groups);
98 BOOST_FOREACH(const String& name, groups) {
99 UserGroup::Ptr ug = UserGroup::GetByName(name);
111 TimePeriod::Ptr Notification::GetNotificationPeriod(void) const
113 return TimePeriod::GetByName(GetNotificationPeriodRaw());
116 double Notification::GetNextNotification(void) const
118 return GetNextNotificationRaw();
122 * Sets the timestamp when the next periodical notification should be sent.
123 * This does not affect notifications that are sent for state changes.
125 void Notification::SetNextNotification(double time, const String& authority)
127 SetNextNotificationRaw(time);
129 OnNextNotificationChanged(GetSelf(), time, authority);
132 void Notification::UpdateNotificationNumber(void)
134 SetNotificationNumber(GetNotificationNumber() + 1);
137 void Notification::ResetNotificationNumber(void)
139 SetNotificationNumber(0);
142 String Notification::NotificationTypeToString(NotificationType type)
145 case NotificationDowntimeStart:
146 return "DOWNTIMESTART";
147 case NotificationDowntimeEnd:
148 return "DOWNTIMEEND";
149 case NotificationDowntimeRemoved:
150 return "DOWNTIMECANCELLED";
151 case NotificationCustom:
153 case NotificationAcknowledgement:
154 return "ACKNOWLEDGEMENT";
155 case NotificationProblem:
157 case NotificationRecovery:
159 case NotificationFlappingStart:
160 return "FLAPPINGSTART";
161 case NotificationFlappingEnd:
162 return "FLAPPINGEND";
164 return "UNKNOWN_NOTIFICATION";
168 void Notification::BeginExecuteNotification(NotificationType type, const CheckResult::Ptr& cr, bool force, const String& author, const String& text)
173 TimePeriod::Ptr tp = GetNotificationPeriod();
175 if (tp && !tp->IsInside(Utility::GetTime())) {
176 Log(LogInformation, "icinga", "Not sending notifications for notification object '" + GetName() + "': not in timeperiod");
180 double now = Utility::GetTime();
181 Dictionary::Ptr times = GetTimes();
182 Service::Ptr service = GetService();
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");
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");
196 unsigned long ftype = 1 << type;
198 Log(LogDebug, "icinga", "FType=" + Convert::ToString(ftype) + ", TypeFilter=" + Convert::ToString(GetNotificationTypeFilter()));
200 if (!(ftype & GetNotificationTypeFilter())) {
201 Log(LogInformation, "icinga", "Not sending notifications for notification object '" + GetName() + "': type filter does not match");
205 unsigned long fstate = 1 << GetService()->GetState();
207 if (!(fstate & GetNotificationStateFilter())) {
208 Log(LogInformation, "icinga", "Not sending notifications for notification object '" + GetName() + "': state filter does not match");
214 ObjectLock olock(this);
216 double now = Utility::GetTime();
217 SetLastNotification(now);
219 if (type == NotificationProblem)
220 SetLastProblemNotification(now);
223 std::set<User::Ptr> allUsers;
225 std::set<User::Ptr> users = GetUsers();
226 std::copy(users.begin(), users.end(), std::inserter(allUsers, allUsers.begin()));
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()));
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));
240 Service::OnNotificationSentToAllUsers(GetService(), allUsers, type, cr, author, text);
243 void Notification::ExecuteNotificationHelper(NotificationType type, const User::Ptr& user, const CheckResult::Ptr& cr, bool force, const String& author, const String& text)
248 TimePeriod::Ptr tp = user->GetNotificationPeriod();
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");
256 unsigned long ftype = 1 << type;
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");
264 unsigned long fstate = 1 << GetService()->GetState();
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");
274 NotificationCommand::Ptr command = GetNotificationCommand();
277 Log(LogDebug, "icinga", "No notification_command found for notification '" + GetName() + "'. Skipping execution.");
281 command->Execute(GetSelf(), user, cr, type, author, text);
284 ObjectLock olock(this);
285 UpdateNotificationNumber();
286 SetLastNotification(Utility::GetTime());
289 Service::OnNotificationSentToUser(GetService(), user, type, cr, author, text, command->GetName());
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());
300 bool Notification::ResolveMacro(const String& macro, const CheckResult::Ptr&, String *result) const
302 Dictionary::Ptr macros = GetMacros();
304 if (macros && macros->Contains(macro)) {
305 *result = macros->Get(macro);