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("last_notification", Attribute_Replicated, &m_LastNotification);
40 RegisterAttribute("next_notification", Attribute_Replicated, &m_NextNotification);
41 RegisterAttribute("macros", Attribute_Config, &m_Macros);
42 RegisterAttribute("users", Attribute_Config, &m_Users);
43 RegisterAttribute("groups", Attribute_Config, &m_Groups);
44 RegisterAttribute("host_name", Attribute_Config, &m_HostName);
45 RegisterAttribute("service", Attribute_Config, &m_Service);
48 Notification::~Notification(void)
50 Service::InvalidateNotificationsCache();
54 * @threadsafety Always.
56 Notification::Ptr Notification::GetByName(const String& name)
58 DynamicObject::Ptr configObject = DynamicObject::GetObject("Notification", name);
60 return dynamic_pointer_cast<Notification>(configObject);
64 * @threadsafety Always.
66 Service::Ptr Notification::GetService(void) const
68 Host::Ptr host = Host::GetByName(m_HostName);
71 return Service::Ptr();
73 if (m_Service.IsEmpty())
74 return host->GetHostCheckService();
76 return host->GetServiceByShortName(m_Service);
80 * @threadsafety Always.
82 Value Notification::GetNotificationCommand(void) const
84 return m_NotificationCommand;
88 * @threadsafety Always.
90 Dictionary::Ptr Notification::GetMacros(void) const
96 * @threadsafety Always.
98 std::set<User::Ptr> Notification::GetUsers(void) const
100 std::set<User::Ptr> result;
102 Array::Ptr users = m_Users;
105 ObjectLock olock(users);
107 BOOST_FOREACH(const String& name, users) {
108 User::Ptr user = User::GetByName(name);
121 * @threadsafety Always.
123 std::set<UserGroup::Ptr> Notification::GetGroups(void) const
125 std::set<UserGroup::Ptr> result;
127 Array::Ptr groups = m_Groups;
130 ObjectLock olock(groups);
132 BOOST_FOREACH(const String& name, groups) {
133 UserGroup::Ptr ug = UserGroup::GetByName(name);
146 * @threadsafety Always.
148 double Notification::GetNotificationInterval(void) const
150 if (m_NotificationInterval.IsEmpty())
153 return m_NotificationInterval;
157 * @threadsafety Always.
159 double Notification::GetLastNotification(void) const
161 if (m_LastNotification.IsEmpty())
164 return m_LastNotification;
168 * Sets the timestamp when the last notification was sent.
170 void Notification::SetLastNotification(double time)
172 m_LastNotification = time;
173 Touch("last_notification");
177 * @threadsafety Always.
179 double Notification::GetNextNotification(void) const
181 if (m_NextNotification.IsEmpty())
184 return m_NextNotification;
188 * Sets the timestamp when the next periodical notification should be sent.
189 * This does not affect notifications that are sent for state changes.
191 void Notification::SetNextNotification(double time)
193 m_NextNotification = time;
194 Touch("next_notification");
198 * @threadsafety Always.
200 String Notification::NotificationTypeToString(NotificationType type)
203 case NotificationDowntimeStart:
204 return "DOWNTIMESTART";
205 case NotificationDowntimeEnd:
206 return "DOWNTIMEEND";
207 case NotificationDowntimeRemoved:
208 return "DOWNTIMECANCELLED";
209 case NotificationCustom:
211 case NotificationAcknowledgement:
212 return "ACKNOWLEDGEMENT";
213 case NotificationProblem:
215 case NotificationRecovery:
218 return "UNKNOWN_NOTIFICATION";
223 * @threadsafety Always.
225 void Notification::BeginExecuteNotification(NotificationType type, const Dictionary::Ptr& cr)
230 ObjectLock olock(this);
232 SetLastNotification(Utility::GetTime());
235 Dictionary::Ptr macros = cr->Get("macros");
237 std::set<User::Ptr> allUsers;
239 std::set<User::Ptr> users = GetUsers();
240 std::copy(users.begin(), users.end(), std::inserter(allUsers, allUsers.begin()));
242 BOOST_FOREACH(const UserGroup::Ptr& ug, GetGroups()) {
243 std::set<User::Ptr> members = ug->GetMembers();
244 std::copy(members.begin(), members.end(), std::inserter(allUsers, allUsers.begin()));
247 BOOST_FOREACH(const User::Ptr& user, allUsers) {
248 Log(LogDebug, "icinga", "Sending notification for user " + user->GetName());
249 BeginExecuteNotificationHelper(macros, type, user);
252 if (allUsers.empty()) {
253 /* Send a notification even if there are no users specified. */
254 BeginExecuteNotificationHelper(macros, type, User::Ptr());
259 * @threadsafety Always.
261 void Notification::BeginExecuteNotificationHelper(const Dictionary::Ptr& notificationMacros, NotificationType type, const User::Ptr& user)
265 std::vector<Dictionary::Ptr> macroDicts;
268 macroDicts.push_back(user->GetMacros());
269 macroDicts.push_back(user->CalculateDynamicMacros());
272 macroDicts.push_back(notificationMacros);
274 Dictionary::Ptr macros = MacroProcessor::MergeMacroDicts(macroDicts);
276 Notification::Ptr self = GetSelf();
278 std::vector<Value> arguments;
279 arguments.push_back(self);
280 arguments.push_back(macros);
281 arguments.push_back(type);
283 ScriptTask::Ptr task = MakeMethodTask("notify", arguments);
286 Log(LogWarning, "icinga", "Notification object '" + GetName() + "' doesn't have a 'notify' method.");
292 ObjectLock olock(this);
294 /* We need to keep the task object alive until the completion handler is called. */
295 m_Tasks.insert(task);
298 task->Start(boost::bind(&Notification::NotificationCompletedHandler, self, _1));
302 * @threadsafety Always.
304 void Notification::NotificationCompletedHandler(const ScriptTask::Ptr& task)
309 ObjectLock olock(this);
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() << "': " << boost::diagnostic_information(ex);
322 String message = msgbuf.str();
324 Log(LogWarning, "icinga", message);
329 * @threadsafety Always.
331 void Notification::OnAttributeChanged(const String& name)
335 if (name == "host_name" || name == "service")
336 Service::InvalidateNotificationsCache();