1 /******************************************************************************
3 * Copyright (C) 2012 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 <boost/tuple/tuple.hpp>
29 #include <boost/foreach.hpp>
30 #include <boost/exception/diagnostic_information.hpp>
32 using namespace icinga;
34 REGISTER_TYPE(Notification);
36 Notification::Notification(const Dictionary::Ptr& serializedUpdate)
37 : DynamicObject(serializedUpdate)
39 RegisterAttribute("notification_command", Attribute_Config, &m_NotificationCommand);
40 RegisterAttribute("notification_interval", Attribute_Config, &m_NotificationInterval);
41 RegisterAttribute("notification_period", Attribute_Config, &m_NotificationPeriod);
42 RegisterAttribute("last_notification", Attribute_Replicated, &m_LastNotification);
43 RegisterAttribute("next_notification", Attribute_Replicated, &m_NextNotification);
44 RegisterAttribute("macros", Attribute_Config, &m_Macros);
45 RegisterAttribute("users", Attribute_Config, &m_Users);
46 RegisterAttribute("groups", Attribute_Config, &m_Groups);
47 RegisterAttribute("times", Attribute_Config, &m_Times);
48 RegisterAttribute("host_name", Attribute_Config, &m_HostName);
49 RegisterAttribute("service", Attribute_Config, &m_Service);
50 RegisterAttribute("export_macros", Attribute_Config, &m_ExportMacros);
53 Notification::~Notification(void)
55 Service::InvalidateNotificationsCache();
58 Notification::Ptr Notification::GetByName(const String& name)
60 DynamicObject::Ptr configObject = DynamicObject::GetObject("Notification", name);
62 return dynamic_pointer_cast<Notification>(configObject);
65 Service::Ptr Notification::GetService(void) const
67 Host::Ptr host = Host::GetByName(m_HostName);
70 return Service::Ptr();
72 if (m_Service.IsEmpty())
73 return host->GetHostCheckService();
75 return host->GetServiceByShortName(m_Service);
78 NotificationCommand::Ptr Notification::GetNotificationCommand(void) const
80 return NotificationCommand::GetByName(m_NotificationCommand);
83 Dictionary::Ptr Notification::GetMacros(void) const
88 Array::Ptr Notification::GetExportMacros(void) const
90 return m_ExportMacros;
93 std::set<User::Ptr> Notification::GetUsers(void) const
95 std::set<User::Ptr> result;
97 Array::Ptr users = m_Users;
100 ObjectLock olock(users);
102 BOOST_FOREACH(const String& name, users) {
103 User::Ptr user = User::GetByName(name);
115 std::set<UserGroup::Ptr> Notification::GetGroups(void) const
117 std::set<UserGroup::Ptr> result;
119 Array::Ptr groups = m_Groups;
122 ObjectLock olock(groups);
124 BOOST_FOREACH(const String& name, groups) {
125 UserGroup::Ptr ug = UserGroup::GetByName(name);
137 Dictionary::Ptr Notification::GetTimes(void) const
142 double Notification::GetNotificationInterval(void) const
144 if (m_NotificationInterval.IsEmpty())
147 return m_NotificationInterval;
150 TimePeriod::Ptr Notification::GetNotificationPeriod(void) const
152 return TimePeriod::GetByName(m_NotificationPeriod);
155 double Notification::GetLastNotification(void) const
157 if (m_LastNotification.IsEmpty())
160 return m_LastNotification;
164 * Sets the timestamp when the last notification was sent.
166 void Notification::SetLastNotification(double time)
168 m_LastNotification = time;
169 Touch("last_notification");
172 double Notification::GetNextNotification(void) const
174 if (m_NextNotification.IsEmpty())
177 return m_NextNotification;
181 * Sets the timestamp when the next periodical notification should be sent.
182 * This does not affect notifications that are sent for state changes.
184 void Notification::SetNextNotification(double time)
186 m_NextNotification = time;
187 Touch("next_notification");
190 String Notification::NotificationTypeToString(NotificationType type)
193 case NotificationDowntimeStart:
194 return "DOWNTIMESTART";
195 case NotificationDowntimeEnd:
196 return "DOWNTIMEEND";
197 case NotificationDowntimeRemoved:
198 return "DOWNTIMECANCELLED";
199 case NotificationCustom:
201 case NotificationAcknowledgement:
202 return "ACKNOWLEDGEMENT";
203 case NotificationProblem:
205 case NotificationRecovery:
208 return "UNKNOWN_NOTIFICATION";
212 void Notification::BeginExecuteNotification(NotificationType type, const Dictionary::Ptr& cr, bool force)
217 TimePeriod::Ptr tp = GetNotificationPeriod();
219 if (tp && !tp->IsInside(Utility::GetTime())) {
220 Log(LogInformation, "icinga", "Not sending notifications for notification object '" + GetName() + "': not in timeperiod");
224 double now = Utility::GetTime();
225 Dictionary::Ptr times = GetTimes();
226 Service::Ptr service = GetService();
228 if (times && times->Contains("begin") && now < service->GetLastHardStateChange() + times->Get("begin")) {
229 Log(LogInformation, "icinga", "Not sending notifications for notification object '" + GetName() + "': before escalation range");
233 if (times && times->Contains("end") && now > service->GetLastHardStateChange() + times->Get("end")) {
234 Log(LogInformation, "icinga", "Not sending notifications for notification object '" + GetName() + "': after escalation range");
240 ObjectLock olock(this);
242 SetLastNotification(Utility::GetTime());
245 std::set<User::Ptr> allUsers;
247 std::set<User::Ptr> users = GetUsers();
248 std::copy(users.begin(), users.end(), std::inserter(allUsers, allUsers.begin()));
250 BOOST_FOREACH(const UserGroup::Ptr& ug, GetGroups()) {
251 std::set<User::Ptr> members = ug->GetMembers();
252 std::copy(members.begin(), members.end(), std::inserter(allUsers, allUsers.begin()));
255 BOOST_FOREACH(const User::Ptr& user, allUsers) {
256 Log(LogDebug, "icinga", "Sending notification for user '" + user->GetName() + "'");
257 Utility::QueueAsyncCallback(boost::bind(&Notification::ExecuteNotificationHelper, this, type, user, cr, force));
261 void Notification::ExecuteNotificationHelper(NotificationType type, const User::Ptr& user, const Dictionary::Ptr& cr, bool ignore_timeperiod)
265 if (!ignore_timeperiod) {
266 TimePeriod::Ptr tp = user->GetNotificationPeriod();
268 if (tp && !tp->IsInside(Utility::GetTime())) {
269 Log(LogInformation, "icinga", "Not sending notifications for notification object '" +
270 GetName() + " and user '" + user->GetName() + "': user not in timeperiod");
276 GetNotificationCommand()->Execute(GetSelf(), user, cr, type);
278 Log(LogInformation, "icinga", "Completed sending notification for service '" + GetService()->GetName() + "'");
279 } catch (const std::exception& ex) {
280 std::ostringstream msgbuf;
281 msgbuf << "Exception occured during notification for service '"
282 << GetService()->GetName() << "': " << boost::diagnostic_information(ex);
283 String message = msgbuf.str();
285 Log(LogWarning, "icinga", message);
289 void Notification::OnAttributeChanged(const String& name)
293 if (name == "host_name" || name == "service")
294 Service::InvalidateNotificationsCache();
297 bool Notification::ResolveMacro(const String& macro, const Dictionary::Ptr&, String *result) const
299 Dictionary::Ptr macros = GetMacros();
301 if (macros && macros->Contains(macro)) {
302 *result = macros->Get(macro);