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/macroprocessor.h"
22 #include "icinga/service.h"
23 #include "base/dynamictype.h"
24 #include "base/objectlock.h"
25 #include "base/logger_fwd.h"
26 #include <boost/tuple/tuple.hpp>
27 #include <boost/foreach.hpp>
28 #include <boost/exception/diagnostic_information.hpp>
30 using namespace icinga;
32 REGISTER_TYPE(Notification);
34 Notification::Notification(const Dictionary::Ptr& serializedUpdate)
35 : DynamicObject(serializedUpdate)
37 RegisterAttribute("notification_command", Attribute_Config, &m_NotificationCommand);
38 RegisterAttribute("notification_interval", Attribute_Config, &m_NotificationInterval);
39 RegisterAttribute("notification_period", Attribute_Config, &m_NotificationPeriod);
40 RegisterAttribute("last_notification", Attribute_Replicated, &m_LastNotification);
41 RegisterAttribute("next_notification", Attribute_Replicated, &m_NextNotification);
42 RegisterAttribute("macros", Attribute_Config, &m_Macros);
43 RegisterAttribute("users", Attribute_Config, &m_Users);
44 RegisterAttribute("groups", Attribute_Config, &m_Groups);
45 RegisterAttribute("host_name", Attribute_Config, &m_HostName);
46 RegisterAttribute("service", Attribute_Config, &m_Service);
49 Notification::~Notification(void)
51 Service::InvalidateNotificationsCache();
54 Notification::Ptr Notification::GetByName(const String& name)
56 DynamicObject::Ptr configObject = DynamicObject::GetObject("Notification", name);
58 return dynamic_pointer_cast<Notification>(configObject);
61 Service::Ptr Notification::GetService(void) const
63 Host::Ptr host = Host::GetByName(m_HostName);
66 return Service::Ptr();
68 if (m_Service.IsEmpty())
69 return host->GetHostCheckService();
71 return host->GetServiceByShortName(m_Service);
74 Value Notification::GetNotificationCommand(void) const
76 return m_NotificationCommand;
79 Dictionary::Ptr Notification::GetMacros(void) const
84 Array::Ptr Notification::GetExportMacros(void) const
86 return m_ExportMacros;
89 std::set<User::Ptr> Notification::GetUsers(void) const
91 std::set<User::Ptr> result;
93 Array::Ptr users = m_Users;
96 ObjectLock olock(users);
98 BOOST_FOREACH(const String& name, users) {
99 User::Ptr user = User::GetByName(name);
111 std::set<UserGroup::Ptr> Notification::GetGroups(void) const
113 std::set<UserGroup::Ptr> result;
115 Array::Ptr groups = m_Groups;
118 ObjectLock olock(groups);
120 BOOST_FOREACH(const String& name, groups) {
121 UserGroup::Ptr ug = UserGroup::GetByName(name);
133 double Notification::GetNotificationInterval(void) const
135 if (m_NotificationInterval.IsEmpty())
138 return m_NotificationInterval;
141 TimePeriod::Ptr Notification::GetNotificationPeriod(void) const
143 return TimePeriod::GetByName(m_NotificationPeriod);
146 double Notification::GetLastNotification(void) const
148 if (m_LastNotification.IsEmpty())
151 return m_LastNotification;
155 * Sets the timestamp when the last notification was sent.
157 void Notification::SetLastNotification(double time)
159 m_LastNotification = time;
160 Touch("last_notification");
163 double Notification::GetNextNotification(void) const
165 if (m_NextNotification.IsEmpty())
168 return m_NextNotification;
172 * Sets the timestamp when the next periodical notification should be sent.
173 * This does not affect notifications that are sent for state changes.
175 void Notification::SetNextNotification(double time)
177 m_NextNotification = time;
178 Touch("next_notification");
181 String Notification::NotificationTypeToString(NotificationType type)
184 case NotificationDowntimeStart:
185 return "DOWNTIMESTART";
186 case NotificationDowntimeEnd:
187 return "DOWNTIMEEND";
188 case NotificationDowntimeRemoved:
189 return "DOWNTIMECANCELLED";
190 case NotificationCustom:
192 case NotificationAcknowledgement:
193 return "ACKNOWLEDGEMENT";
194 case NotificationProblem:
196 case NotificationRecovery:
199 return "UNKNOWN_NOTIFICATION";
203 void Notification::BeginExecuteNotification(NotificationType type, const Dictionary::Ptr& cr, bool ignore_timeperiod)
207 if (!ignore_timeperiod) {
208 TimePeriod::Ptr tp = GetNotificationPeriod();
210 if (tp && !tp->IsInside(Utility::GetTime())) {
211 Log(LogInformation, "icinga", "Not sending notifications for notification object '" + GetName() + "': not in timeperiod");
217 ObjectLock olock(this);
219 SetLastNotification(Utility::GetTime());
222 std::set<User::Ptr> allUsers;
224 std::set<User::Ptr> users = GetUsers();
225 std::copy(users.begin(), users.end(), std::inserter(allUsers, allUsers.begin()));
227 BOOST_FOREACH(const UserGroup::Ptr& ug, GetGroups()) {
228 std::set<User::Ptr> members = ug->GetMembers();
229 std::copy(members.begin(), members.end(), std::inserter(allUsers, allUsers.begin()));
232 BOOST_FOREACH(const User::Ptr& user, allUsers) {
233 Log(LogDebug, "icinga", "Sending notification for user " + user->GetName());
234 BeginExecuteNotificationHelper(type, user, cr, ignore_timeperiod);
238 void Notification::BeginExecuteNotificationHelper(NotificationType type, const User::Ptr& user, const Dictionary::Ptr& cr, bool ignore_timeperiod)
242 if (!ignore_timeperiod) {
243 TimePeriod::Ptr tp = user->GetNotificationPeriod();
245 if (tp && !tp->IsInside(Utility::GetTime())) {
246 Log(LogInformation, "icinga", "Not sending notifications for notification object '" +
247 GetName() + " and user '" + user->GetName() + "': user not in timeperiod");
252 Notification::Ptr self = GetSelf();
254 std::vector<Value> arguments;
255 arguments.push_back(self);
256 arguments.push_back(user);
257 arguments.push_back(cr);
258 arguments.push_back(type);
260 ScriptTask::Ptr task = MakeMethodTask("notify", arguments);
263 Log(LogWarning, "icinga", "Notification object '" + GetName() + "' doesn't have a 'notify' method.");
269 ObjectLock olock(this);
271 /* We need to keep the task object alive until the completion handler is called. */
272 m_Tasks.insert(task);
275 task->Start(boost::bind(&Notification::NotificationCompletedHandler, self, _1));
278 void Notification::NotificationCompletedHandler(const ScriptTask::Ptr& task)
283 ObjectLock olock(this);
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() << "': " << boost::diagnostic_information(ex);
296 String message = msgbuf.str();
298 Log(LogWarning, "icinga", message);
302 void Notification::OnAttributeChanged(const String& name)
306 if (name == "host_name" || name == "service")
307 Service::InvalidateNotificationsCache();
310 bool Notification::ResolveMacro(const String& macro, const Dictionary::Ptr& cr, String *result) const
312 Dictionary::Ptr macros = GetMacros();
314 if (macros && macros->Contains(macro)) {
315 *result = macros->Get(macro);