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 <boost/tuple/tuple.hpp>
30 #include <boost/foreach.hpp>
31 #include <boost/exception/diagnostic_information.hpp>
33 using namespace icinga;
35 REGISTER_NTYPE(Notification);
36 REGISTER_TYPE(Notification);
38 boost::signals2::signal<void (const Notification::Ptr&, double, const String&)> Notification::OnNextNotificationChanged;
40 void Notification::Start(void)
42 DynamicObject::Start();
44 GetService()->AddNotification(GetSelf());
47 void Notification::Stop(void)
49 DynamicObject::Stop();
51 GetService()->RemoveNotification(GetSelf());
54 Service::Ptr Notification::GetService(void) const
56 Host::Ptr host = Host::GetByName(GetHostRaw());
59 return Service::Ptr();
61 if (GetServiceRaw().IsEmpty())
62 return host->GetCheckService();
64 return host->GetServiceByShortName(GetServiceRaw());
67 NotificationCommand::Ptr Notification::GetNotificationCommand(void) const
69 return NotificationCommand::GetByName(GetNotificationCommandRaw());
72 std::set<User::Ptr> Notification::GetUsers(void) const
74 std::set<User::Ptr> result;
76 Array::Ptr users = GetUsersRaw();
79 ObjectLock olock(users);
81 BOOST_FOREACH(const String& name, users) {
82 User::Ptr user = User::GetByName(name);
94 std::set<UserGroup::Ptr> Notification::GetUserGroups(void) const
96 std::set<UserGroup::Ptr> result;
98 Array::Ptr groups = GetUserGroupsRaw();
101 ObjectLock olock(groups);
103 BOOST_FOREACH(const String& name, groups) {
104 UserGroup::Ptr ug = UserGroup::GetByName(name);
116 TimePeriod::Ptr Notification::GetNotificationPeriod(void) const
118 return TimePeriod::GetByName(GetNotificationPeriodRaw());
121 double Notification::GetNextNotification(void) const
123 return GetNextNotificationRaw();
127 * Sets the timestamp when the next periodical notification should be sent.
128 * This does not affect notifications that are sent for state changes.
130 void Notification::SetNextNotification(double time, const String& authority)
132 SetNextNotificationRaw(time);
134 Utility::QueueAsyncCallback(boost::bind(boost::ref(OnNextNotificationChanged), GetSelf(), time, authority));
137 void Notification::UpdateNotificationNumber(void)
139 SetNotificationNumber(GetNotificationNumber() + 1);
142 void Notification::ResetNotificationNumber(void)
144 SetNotificationNumber(0);
147 String Notification::NotificationTypeToString(NotificationType type)
150 case NotificationDowntimeStart:
151 return "DOWNTIMESTART";
152 case NotificationDowntimeEnd:
153 return "DOWNTIMEEND";
154 case NotificationDowntimeRemoved:
155 return "DOWNTIMECANCELLED";
156 case NotificationCustom:
158 case NotificationAcknowledgement:
159 return "ACKNOWLEDGEMENT";
160 case NotificationProblem:
162 case NotificationRecovery:
164 case NotificationFlappingStart:
165 return "FLAPPINGSTART";
166 case NotificationFlappingEnd:
167 return "FLAPPINGEND";
169 return "UNKNOWN_NOTIFICATION";
173 void Notification::BeginExecuteNotification(NotificationType type, const Dictionary::Ptr& cr, bool force, const String& author, const String& text)
178 TimePeriod::Ptr tp = GetNotificationPeriod();
180 if (tp && !tp->IsInside(Utility::GetTime())) {
181 Log(LogInformation, "icinga", "Not sending notifications for notification object '" + GetName() + "': not in timeperiod");
185 double now = Utility::GetTime();
186 Dictionary::Ptr times = GetTimes();
187 Service::Ptr service = GetService();
189 if (type == NotificationProblem) {
190 if (times && times->Contains("begin") && now < service->GetLastHardStateChange() + times->Get("begin")) {
191 Log(LogInformation, "icinga", "Not sending notifications for notification object '" + GetName() + "': before escalation range");
195 if (times && times->Contains("end") && now > service->GetLastHardStateChange() + times->Get("end")) {
196 Log(LogInformation, "icinga", "Not sending notifications for notification object '" + GetName() + "': after escalation range");
201 unsigned long ftype = 1 << type;
203 Log(LogDebug, "icinga", "FType=" + Convert::ToString(ftype) + ", TypeFilter=" + Convert::ToString(GetNotificationTypeFilter()));
205 if (!(ftype & GetNotificationTypeFilter())) {
206 Log(LogInformation, "icinga", "Not sending notifications for notification object '" + GetName() + "': type filter does not match");
210 unsigned long fstate = 1 << GetService()->GetState();
212 if (!(fstate & GetNotificationStateFilter())) {
213 Log(LogInformation, "icinga", "Not sending notifications for notification object '" + GetName() + "': state filter does not match");
219 ObjectLock olock(this);
221 SetLastNotification(Utility::GetTime());
224 std::set<User::Ptr> allUsers;
226 std::set<User::Ptr> users = GetUsers();
227 std::copy(users.begin(), users.end(), std::inserter(allUsers, allUsers.begin()));
229 BOOST_FOREACH(const UserGroup::Ptr& ug, GetUserGroups()) {
230 std::set<User::Ptr> members = ug->GetMembers();
231 std::copy(members.begin(), members.end(), std::inserter(allUsers, allUsers.begin()));
234 unsigned long notified_users = 0;
235 BOOST_FOREACH(const User::Ptr& user, allUsers) {
236 Log(LogDebug, "icinga", "Sending notification for user '" + user->GetName() + "'");
237 Utility::QueueAsyncCallback(boost::bind(&Notification::ExecuteNotificationHelper, this, type, user, cr, force, author, text));
241 Service::OnNotificationSentToAllUsers(GetService(), allUsers, type, cr, author, text);
244 void Notification::ExecuteNotificationHelper(NotificationType type, const User::Ptr& user, const Dictionary::Ptr& cr, bool force, const String& author, const String& text)
249 TimePeriod::Ptr tp = user->GetNotificationPeriod();
251 if (tp && !tp->IsInside(Utility::GetTime())) {
252 Log(LogInformation, "icinga", "Not sending notifications for notification object '" +
253 GetName() + " and user '" + user->GetName() + "': user not in timeperiod");
257 unsigned long ftype = 1 << type;
259 if (!(ftype & user->GetNotificationTypeFilter())) {
260 Log(LogInformation, "icinga", "Not sending notifications for notification object '" +
261 GetName() + " and user '" + user->GetName() + "': type filter does not match");
265 unsigned long fstate = 1 << GetService()->GetState();
267 if (!(fstate & user->GetNotificationStateFilter())) {
268 Log(LogInformation, "icinga", "Not sending notifications for notification object '" +
269 GetName() + " and user '" + user->GetName() + "': state filter does not match");
275 NotificationCommand::Ptr command = GetNotificationCommand();
278 Log(LogDebug, "icinga", "No notification_command found for notification '" + GetName() + "'. Skipping execution.");
282 command->Execute(GetSelf(), user, cr, type, author, text);
285 ObjectLock olock(this);
286 UpdateNotificationNumber();
287 SetLastNotification(Utility::GetTime());
290 Service::OnNotificationSentToUser(GetService(), user, type, cr, author, text, command->GetName());
292 Log(LogInformation, "icinga", "Completed sending notification for service '" + GetService()->GetName() + "'");
293 } catch (const std::exception& ex) {
294 std::ostringstream msgbuf;
295 msgbuf << "Exception occured during notification for service '"
296 << GetService()->GetName() << "': " << boost::diagnostic_information(ex);
297 Log(LogWarning, "icinga", msgbuf.str());
301 bool Notification::ResolveMacro(const String& macro, const Dictionary::Ptr&, String *result) const
303 Dictionary::Ptr macros = GetMacros();
305 if (macros && macros->Contains(macro)) {
306 *result = macros->Get(macro);