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 "icinga/notificationmessage.h"
25 #include "remoting/endpointmanager.h"
26 #include "base/dynamictype.h"
27 #include "base/objectlock.h"
28 #include "base/logger_fwd.h"
29 #include "base/utility.h"
30 #include "base/convert.h"
31 #include <boost/tuple/tuple.hpp>
32 #include <boost/foreach.hpp>
33 #include <boost/exception/diagnostic_information.hpp>
35 using namespace icinga;
37 REGISTER_TYPE(Notification);
39 Notification::Notification(const Dictionary::Ptr& serializedUpdate)
40 : DynamicObject(serializedUpdate)
42 RegisterAttribute("notification_command", Attribute_Config, &m_NotificationCommand);
43 RegisterAttribute("notification_interval", Attribute_Config, &m_NotificationInterval);
44 RegisterAttribute("notification_period", Attribute_Config, &m_NotificationPeriod);
45 RegisterAttribute("last_notification", Attribute_Replicated, &m_LastNotification);
46 RegisterAttribute("next_notification", Attribute_Replicated, &m_NextNotification);
47 RegisterAttribute("macros", Attribute_Config, &m_Macros);
48 RegisterAttribute("users", Attribute_Config, &m_Users);
49 RegisterAttribute("groups", Attribute_Config, &m_Groups);
50 RegisterAttribute("times", Attribute_Config, &m_Times);
51 RegisterAttribute("notification_type_filter", Attribute_Config, &m_NotificationTypeFilter);
52 RegisterAttribute("notification_state_filter", Attribute_Config, &m_NotificationStateFilter);
53 RegisterAttribute("host_name", Attribute_Config, &m_HostName);
54 RegisterAttribute("service", Attribute_Config, &m_Service);
55 RegisterAttribute("export_macros", Attribute_Config, &m_ExportMacros);
58 Notification::~Notification(void)
60 Service::InvalidateNotificationsCache();
63 Notification::Ptr Notification::GetByName(const String& name)
65 DynamicObject::Ptr configObject = DynamicObject::GetObject("Notification", name);
67 return dynamic_pointer_cast<Notification>(configObject);
70 Service::Ptr Notification::GetService(void) const
72 Host::Ptr host = Host::GetByName(m_HostName);
75 return Service::Ptr();
77 if (m_Service.IsEmpty())
78 return host->GetHostCheckService();
80 return host->GetServiceByShortName(m_Service);
83 NotificationCommand::Ptr Notification::GetNotificationCommand(void) const
85 return NotificationCommand::GetByName(m_NotificationCommand);
88 Dictionary::Ptr Notification::GetMacros(void) const
93 Array::Ptr Notification::GetExportMacros(void) const
95 return m_ExportMacros;
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);
120 std::set<UserGroup::Ptr> Notification::GetGroups(void) const
122 std::set<UserGroup::Ptr> result;
124 Array::Ptr groups = m_Groups;
127 ObjectLock olock(groups);
129 BOOST_FOREACH(const String& name, groups) {
130 UserGroup::Ptr ug = UserGroup::GetByName(name);
142 Dictionary::Ptr Notification::GetTimes(void) const
147 unsigned long Notification::GetNotificationTypeFilter(void) const
149 if (m_NotificationTypeFilter.IsEmpty())
150 return ~(unsigned long)0; /* All states. */
152 return m_NotificationTypeFilter;
155 unsigned long Notification::GetNotificationStateFilter(void) const
157 if (m_NotificationStateFilter.IsEmpty())
158 return ~(unsigned long)0; /* All states. */
160 return m_NotificationStateFilter;
163 double Notification::GetNotificationInterval(void) const
165 if (m_NotificationInterval.IsEmpty())
168 return m_NotificationInterval;
171 TimePeriod::Ptr Notification::GetNotificationPeriod(void) const
173 return TimePeriod::GetByName(m_NotificationPeriod);
176 double Notification::GetLastNotification(void) const
178 if (m_LastNotification.IsEmpty())
181 return m_LastNotification;
185 * Sets the timestamp when the last notification was sent.
187 void Notification::SetLastNotification(double time)
189 m_LastNotification = time;
190 Touch("last_notification");
193 double Notification::GetNextNotification(void) const
195 if (m_NextNotification.IsEmpty())
198 return m_NextNotification;
202 * Sets the timestamp when the next periodical notification should be sent.
203 * This does not affect notifications that are sent for state changes.
205 void Notification::SetNextNotification(double time)
207 m_NextNotification = time;
208 Touch("next_notification");
211 String Notification::NotificationTypeToString(NotificationType type)
214 case NotificationDowntimeStart:
215 return "DOWNTIMESTART";
216 case NotificationDowntimeEnd:
217 return "DOWNTIMEEND";
218 case NotificationDowntimeRemoved:
219 return "DOWNTIMECANCELLED";
220 case NotificationCustom:
222 case NotificationAcknowledgement:
223 return "ACKNOWLEDGEMENT";
224 case NotificationProblem:
226 case NotificationRecovery:
228 case NotificationFlappingStart:
229 return "FLAPPINGSTART";
230 case NotificationFlappingEnd:
231 return "FLAPPINGEND";
233 return "UNKNOWN_NOTIFICATION";
237 void Notification::BeginExecuteNotification(NotificationType type, const Dictionary::Ptr& cr, bool force)
242 TimePeriod::Ptr tp = GetNotificationPeriod();
244 if (tp && !tp->IsInside(Utility::GetTime())) {
245 Log(LogInformation, "icinga", "Not sending notifications for notification object '" + GetName() + "': not in timeperiod");
249 double now = Utility::GetTime();
250 Dictionary::Ptr times = GetTimes();
251 Service::Ptr service = GetService();
253 if (times && times->Contains("begin") && now < service->GetLastHardStateChange() + times->Get("begin")) {
254 Log(LogInformation, "icinga", "Not sending notifications for notification object '" + GetName() + "': before escalation range");
258 if (times && times->Contains("end") && now > service->GetLastHardStateChange() + times->Get("end")) {
259 Log(LogInformation, "icinga", "Not sending notifications for notification object '" + GetName() + "': after escalation range");
263 unsigned long ftype = 1 << type;
265 Log(LogDebug, "icinga", "FType=" + Convert::ToString(ftype) + ", TypeFilter=" + Convert::ToString(GetNotificationTypeFilter()));
267 if (!(ftype & GetNotificationTypeFilter())) {
268 Log(LogInformation, "icinga", "Not sending notifications for notification object '" + GetName() + "': type filter does not match");
272 unsigned long fstate = 1 << GetService()->GetState();
274 if (!(fstate & GetNotificationStateFilter())) {
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->GetNotificationTypeFilter())) {
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->GetNotificationStateFilter())) {
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);
336 rm.SetMethod("icinga::NotificationSent");
338 NotificationMessage params;
339 String comment_id = GetService()->GetLastCommentID();
340 Dictionary::Ptr comment = Service::GetCommentByID(comment_id);
345 author = comment->Get("author");
346 text = comment->Get("text");
347 Log(LogDebug, "icinga", "notification for service '" + GetService()->GetName() + "' with author " + author + "and text " + text);
349 params.SetService(GetService()->GetName());
350 params.SetUser(user->GetName());
351 params.SetType(type);
352 params.SetAuthor(author); // figure out how to receive these attributes properly from service->comments TODO
353 params.SetCommentText(text);
354 params.SetCheckResult(cr);
356 rm.SetParams(params);
358 EndpointManager::GetInstance()->SendMulticastMessage(rm);
360 Log(LogInformation, "icinga", "Completed sending notification for service '" + GetService()->GetName() + "'");
361 } catch (const std::exception& ex) {
362 std::ostringstream msgbuf;
363 msgbuf << "Exception occured during notification for service '"
364 << GetService()->GetName() << "': " << boost::diagnostic_information(ex);
365 String message = msgbuf.str();
367 Log(LogWarning, "icinga", message);
371 void Notification::OnAttributeChanged(const String& name)
375 if (name == "host_name" || name == "service")
376 Service::InvalidateNotificationsCache();
379 bool Notification::ResolveMacro(const String& macro, const Dictionary::Ptr&, String *result) const
381 Dictionary::Ptr macros = GetMacros();
383 if (macros && macros->Contains(macro)) {
384 *result = macros->Get(macro);