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 "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_TYPE(Notification);
37 Notification::Notification(const Dictionary::Ptr& serializedUpdate)
38 : DynamicObject(serializedUpdate)
40 RegisterAttribute("notification_command", Attribute_Config, &m_NotificationCommand);
41 RegisterAttribute("notification_interval", Attribute_Config, &m_NotificationInterval);
42 RegisterAttribute("notification_period", Attribute_Config, &m_NotificationPeriod);
43 RegisterAttribute("last_notification", Attribute_Replicated, &m_LastNotification);
44 RegisterAttribute("next_notification", Attribute_Replicated, &m_NextNotification);
45 RegisterAttribute("macros", Attribute_Config, &m_Macros);
46 RegisterAttribute("users", Attribute_Config, &m_Users);
47 RegisterAttribute("groups", Attribute_Config, &m_Groups);
48 RegisterAttribute("times", Attribute_Config, &m_Times);
49 RegisterAttribute("type_filter", Attribute_Config, &m_TypeFilter);
50 RegisterAttribute("state_filter", Attribute_Config, &m_StateFilter);
51 RegisterAttribute("host_name", Attribute_Config, &m_HostName);
52 RegisterAttribute("service", Attribute_Config, &m_Service);
53 RegisterAttribute("export_macros", Attribute_Config, &m_ExportMacros);
56 Notification::~Notification(void)
58 Service::InvalidateNotificationsCache();
61 Notification::Ptr Notification::GetByName(const String& name)
63 DynamicObject::Ptr configObject = DynamicObject::GetObject("Notification", name);
65 return dynamic_pointer_cast<Notification>(configObject);
68 Service::Ptr Notification::GetService(void) const
70 Host::Ptr host = Host::GetByName(m_HostName);
73 return Service::Ptr();
75 if (m_Service.IsEmpty())
76 return host->GetHostCheckService();
78 return host->GetServiceByShortName(m_Service);
81 NotificationCommand::Ptr Notification::GetNotificationCommand(void) const
83 return NotificationCommand::GetByName(m_NotificationCommand);
86 Dictionary::Ptr Notification::GetMacros(void) const
91 Array::Ptr Notification::GetExportMacros(void) const
93 return m_ExportMacros;
96 std::set<User::Ptr> Notification::GetUsers(void) const
98 std::set<User::Ptr> result;
100 Array::Ptr users = m_Users;
103 ObjectLock olock(users);
105 BOOST_FOREACH(const String& name, users) {
106 User::Ptr user = User::GetByName(name);
118 std::set<UserGroup::Ptr> Notification::GetGroups(void) const
120 std::set<UserGroup::Ptr> result;
122 Array::Ptr groups = m_Groups;
125 ObjectLock olock(groups);
127 BOOST_FOREACH(const String& name, groups) {
128 UserGroup::Ptr ug = UserGroup::GetByName(name);
140 Dictionary::Ptr Notification::GetTimes(void) const
145 unsigned long Notification::GetTypeFilter(void) const
147 if (m_TypeFilter.IsEmpty())
148 return ~(unsigned long)0; /* All states. */
153 unsigned long Notification::GetStateFilter(void) const
155 if (m_StateFilter.IsEmpty())
156 return ~(unsigned long)0; /* All states. */
158 return m_StateFilter;
161 double Notification::GetNotificationInterval(void) const
163 if (m_NotificationInterval.IsEmpty())
166 return m_NotificationInterval;
169 TimePeriod::Ptr Notification::GetNotificationPeriod(void) const
171 return TimePeriod::GetByName(m_NotificationPeriod);
174 double Notification::GetLastNotification(void) const
176 if (m_LastNotification.IsEmpty())
179 return m_LastNotification;
183 * Sets the timestamp when the last notification was sent.
185 void Notification::SetLastNotification(double time)
187 m_LastNotification = time;
188 Touch("last_notification");
191 double Notification::GetNextNotification(void) const
193 if (m_NextNotification.IsEmpty())
196 return m_NextNotification;
200 * Sets the timestamp when the next periodical notification should be sent.
201 * This does not affect notifications that are sent for state changes.
203 void Notification::SetNextNotification(double time)
205 m_NextNotification = time;
206 Touch("next_notification");
209 String Notification::NotificationTypeToString(NotificationType type)
212 case NotificationDowntimeStart:
213 return "DOWNTIMESTART";
214 case NotificationDowntimeEnd:
215 return "DOWNTIMEEND";
216 case NotificationDowntimeRemoved:
217 return "DOWNTIMECANCELLED";
218 case NotificationCustom:
220 case NotificationAcknowledgement:
221 return "ACKNOWLEDGEMENT";
222 case NotificationProblem:
224 case NotificationRecovery:
226 case NotificationFlappingStart:
227 return "FLAPPINGSTART";
228 case NotificationFlappingEnd:
229 return "FLAPPINGEND";
231 return "UNKNOWN_NOTIFICATION";
235 void Notification::BeginExecuteNotification(NotificationType type, const Dictionary::Ptr& cr, bool force)
240 TimePeriod::Ptr tp = GetNotificationPeriod();
242 if (tp && !tp->IsInside(Utility::GetTime())) {
243 Log(LogInformation, "icinga", "Not sending notifications for notification object '" + GetName() + "': not in timeperiod");
247 double now = Utility::GetTime();
248 Dictionary::Ptr times = GetTimes();
249 Service::Ptr service = GetService();
251 if (times && times->Contains("begin") && now < service->GetLastHardStateChange() + times->Get("begin")) {
252 Log(LogInformation, "icinga", "Not sending notifications for notification object '" + GetName() + "': before escalation range");
256 if (times && times->Contains("end") && now > service->GetLastHardStateChange() + times->Get("end")) {
257 Log(LogInformation, "icinga", "Not sending notifications for notification object '" + GetName() + "': after escalation range");
261 unsigned long ftype = 1 << type;
263 Log(LogDebug, "icinga", "FType=" + Convert::ToString(ftype) + ", TypeFilter=" + Convert::ToString(GetTypeFilter()));
265 if (!(ftype & GetTypeFilter())) {
266 Log(LogInformation, "icinga", "Not sending notifications for notification object '" + GetName() + "': type filter does not match");
270 unsigned long fstate = 1 << GetService()->GetState();
272 Log(LogDebug, "icinga", "FState=" + Convert::ToString(fstate) + ", StateFilter=" + Convert::ToString(GetStateFilter()));
274 if (!(fstate & GetStateFilter())) {
275 Log(LogInformation, "icinga", "Not sending notifications for notification object '" + GetName() + "': state filter does not match");
281 ObjectLock olock(this);
283 SetLastNotification(Utility::GetTime());
286 std::set<User::Ptr> allUsers;
288 std::set<User::Ptr> users = GetUsers();
289 std::copy(users.begin(), users.end(), std::inserter(allUsers, allUsers.begin()));
291 BOOST_FOREACH(const UserGroup::Ptr& ug, GetGroups()) {
292 std::set<User::Ptr> members = ug->GetMembers();
293 std::copy(members.begin(), members.end(), std::inserter(allUsers, allUsers.begin()));
296 BOOST_FOREACH(const User::Ptr& user, allUsers) {
297 Log(LogDebug, "icinga", "Sending notification for user '" + user->GetName() + "'");
298 Utility::QueueAsyncCallback(boost::bind(&Notification::ExecuteNotificationHelper, this, type, user, cr, force));
302 void Notification::ExecuteNotificationHelper(NotificationType type, const User::Ptr& user, const Dictionary::Ptr& cr, bool force)
307 TimePeriod::Ptr tp = user->GetNotificationPeriod();
309 if (tp && !tp->IsInside(Utility::GetTime())) {
310 Log(LogInformation, "icinga", "Not sending notifications for notification object '" +
311 GetName() + " and user '" + user->GetName() + "': user not in timeperiod");
315 unsigned long ftype = 1 << type;
317 if (!(ftype & user->GetTypeFilter())) {
318 Log(LogInformation, "icinga", "Not sending notifications for notification object '" +
319 GetName() + " and user '" + user->GetName() + "': type filter does not match");
323 unsigned long fstate = 1 << GetService()->GetState();
325 if (!(fstate & user->GetStateFilter())) {
326 Log(LogInformation, "icinga", "Not sending notifications for notification object '" +
327 GetName() + " and user '" + user->GetName() + "': state filter does not match");
333 GetNotificationCommand()->Execute(GetSelf(), user, cr, type);
335 Log(LogInformation, "icinga", "Completed sending notification for service '" + GetService()->GetName() + "'");
336 } catch (const std::exception& ex) {
337 std::ostringstream msgbuf;
338 msgbuf << "Exception occured during notification for service '"
339 << GetService()->GetName() << "': " << boost::diagnostic_information(ex);
340 String message = msgbuf.str();
342 Log(LogWarning, "icinga", message);
346 void Notification::OnAttributeChanged(const String& name)
350 if (name == "host_name" || name == "service")
351 Service::InvalidateNotificationsCache();
354 bool Notification::ResolveMacro(const String& macro, const Dictionary::Ptr&, String *result) const
356 Dictionary::Ptr macros = GetMacros();
358 if (macros && macros->Contains(macro)) {
359 *result = macros->Get(macro);