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 "base/utility.h"
27 #include <boost/tuple/tuple.hpp>
28 #include <boost/foreach.hpp>
29 #include <boost/exception/diagnostic_information.hpp>
31 using namespace icinga;
33 REGISTER_TYPE(Notification);
35 Notification::Notification(const Dictionary::Ptr& serializedUpdate)
36 : DynamicObject(serializedUpdate)
38 RegisterAttribute("notification_command", Attribute_Config, &m_NotificationCommand);
39 RegisterAttribute("notification_interval", Attribute_Config, &m_NotificationInterval);
40 RegisterAttribute("notification_period", Attribute_Config, &m_NotificationPeriod);
41 RegisterAttribute("last_notification", Attribute_Replicated, &m_LastNotification);
42 RegisterAttribute("next_notification", Attribute_Replicated, &m_NextNotification);
43 RegisterAttribute("macros", Attribute_Config, &m_Macros);
44 RegisterAttribute("users", Attribute_Config, &m_Users);
45 RegisterAttribute("groups", Attribute_Config, &m_Groups);
46 RegisterAttribute("host_name", Attribute_Config, &m_HostName);
47 RegisterAttribute("service", Attribute_Config, &m_Service);
48 RegisterAttribute("export_macros", Attribute_Config, &m_ExportMacros);
51 Notification::~Notification(void)
53 Service::InvalidateNotificationsCache();
56 Notification::Ptr Notification::GetByName(const String& name)
58 DynamicObject::Ptr configObject = DynamicObject::GetObject("Notification", name);
60 return dynamic_pointer_cast<Notification>(configObject);
63 Service::Ptr Notification::GetService(void) const
65 Host::Ptr host = Host::GetByName(m_HostName);
68 return Service::Ptr();
70 if (m_Service.IsEmpty())
71 return host->GetHostCheckService();
73 return host->GetServiceByShortName(m_Service);
76 Value Notification::GetNotificationCommand(void) const
78 return m_NotificationCommand;
81 Dictionary::Ptr Notification::GetMacros(void) const
86 Array::Ptr Notification::GetExportMacros(void) const
88 return m_ExportMacros;
91 std::set<User::Ptr> Notification::GetUsers(void) const
93 std::set<User::Ptr> result;
95 Array::Ptr users = m_Users;
98 ObjectLock olock(users);
100 BOOST_FOREACH(const String& name, users) {
101 User::Ptr user = User::GetByName(name);
113 std::set<UserGroup::Ptr> Notification::GetGroups(void) const
115 std::set<UserGroup::Ptr> result;
117 Array::Ptr groups = m_Groups;
120 ObjectLock olock(groups);
122 BOOST_FOREACH(const String& name, groups) {
123 UserGroup::Ptr ug = UserGroup::GetByName(name);
135 double Notification::GetNotificationInterval(void) const
137 if (m_NotificationInterval.IsEmpty())
140 return m_NotificationInterval;
143 TimePeriod::Ptr Notification::GetNotificationPeriod(void) const
145 return TimePeriod::GetByName(m_NotificationPeriod);
148 double Notification::GetLastNotification(void) const
150 if (m_LastNotification.IsEmpty())
153 return m_LastNotification;
157 * Sets the timestamp when the last notification was sent.
159 void Notification::SetLastNotification(double time)
161 m_LastNotification = time;
162 Touch("last_notification");
165 double Notification::GetNextNotification(void) const
167 if (m_NextNotification.IsEmpty())
170 return m_NextNotification;
174 * Sets the timestamp when the next periodical notification should be sent.
175 * This does not affect notifications that are sent for state changes.
177 void Notification::SetNextNotification(double time)
179 m_NextNotification = time;
180 Touch("next_notification");
183 String Notification::NotificationTypeToString(NotificationType type)
186 case NotificationDowntimeStart:
187 return "DOWNTIMESTART";
188 case NotificationDowntimeEnd:
189 return "DOWNTIMEEND";
190 case NotificationDowntimeRemoved:
191 return "DOWNTIMECANCELLED";
192 case NotificationCustom:
194 case NotificationAcknowledgement:
195 return "ACKNOWLEDGEMENT";
196 case NotificationProblem:
198 case NotificationRecovery:
201 return "UNKNOWN_NOTIFICATION";
205 void Notification::BeginExecuteNotification(NotificationType type, const Dictionary::Ptr& cr, bool ignore_timeperiod)
209 if (!ignore_timeperiod) {
210 TimePeriod::Ptr tp = GetNotificationPeriod();
212 if (tp && !tp->IsInside(Utility::GetTime())) {
213 Log(LogInformation, "icinga", "Not sending notifications for notification object '" + GetName() + "': not in timeperiod");
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, GetGroups()) {
230 std::set<User::Ptr> members = ug->GetMembers();
231 std::copy(members.begin(), members.end(), std::inserter(allUsers, allUsers.begin()));
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, ignore_timeperiod));
240 void Notification::ExecuteNotificationHelper(NotificationType type, const User::Ptr& user, const Dictionary::Ptr& cr, bool ignore_timeperiod)
244 if (!ignore_timeperiod) {
245 TimePeriod::Ptr tp = user->GetNotificationPeriod();
247 if (tp && !tp->IsInside(Utility::GetTime())) {
248 Log(LogInformation, "icinga", "Not sending notifications for notification object '" +
249 GetName() + " and user '" + user->GetName() + "': user not in timeperiod");
254 Notification::Ptr self = GetSelf();
256 std::vector<Value> arguments;
257 arguments.push_back(self);
258 arguments.push_back(user);
259 arguments.push_back(cr);
260 arguments.push_back(type);
263 InvokeMethod("notify", arguments);
265 Log(LogInformation, "icinga", "Completed sending notification for service '" + GetService()->GetName() + "'");
266 } catch (const std::exception& ex) {
267 std::ostringstream msgbuf;
268 msgbuf << "Exception occured during notification for service '"
269 << GetService()->GetName() << "': " << boost::diagnostic_information(ex);
270 String message = msgbuf.str();
272 Log(LogWarning, "icinga", message);
276 void Notification::OnAttributeChanged(const String& name)
280 if (name == "host_name" || name == "service")
281 Service::InvalidateNotificationsCache();
284 bool Notification::ResolveMacro(const String& macro, const Dictionary::Ptr&, String *result) const
286 Dictionary::Ptr macros = GetMacros();
288 if (macros && macros->Contains(macro)) {
289 *result = macros->Get(macro);