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 boost::signals2::signal<void (const Notification::Ptr&, double, const String&)> Notification::OnNextNotificationChanged;
39 void Notification::Start(void)
41 DynamicObject::Start();
43 GetService()->AddNotification(GetSelf());
46 void Notification::Stop(void)
48 DynamicObject::Stop();
50 GetService()->RemoveNotification(GetSelf());
53 Service::Ptr Notification::GetService(void) const
55 Host::Ptr host = Host::GetByName(m_HostName);
58 return Service::Ptr();
60 if (m_Service.IsEmpty())
61 return host->GetHostCheckService();
63 return host->GetServiceByShortName(m_Service);
66 NotificationCommand::Ptr Notification::GetNotificationCommand(void) const
68 return NotificationCommand::GetByName(m_NotificationCommand);
71 Dictionary::Ptr Notification::GetMacros(void) const
76 Array::Ptr Notification::GetExportMacros(void) const
78 return m_ExportMacros;
81 std::set<User::Ptr> Notification::GetUsers(void) const
83 std::set<User::Ptr> result;
85 Array::Ptr users = m_Users;
88 ObjectLock olock(users);
90 BOOST_FOREACH(const String& name, users) {
91 User::Ptr user = User::GetByName(name);
103 std::set<UserGroup::Ptr> Notification::GetGroups(void) const
105 std::set<UserGroup::Ptr> result;
107 Array::Ptr groups = m_Groups;
110 ObjectLock olock(groups);
112 BOOST_FOREACH(const String& name, groups) {
113 UserGroup::Ptr ug = UserGroup::GetByName(name);
125 Dictionary::Ptr Notification::GetTimes(void) const
130 unsigned long Notification::GetNotificationTypeFilter(void) const
132 if (m_NotificationTypeFilter.IsEmpty())
133 return ~(unsigned long)0; /* All states. */
135 return m_NotificationTypeFilter;
138 unsigned long Notification::GetNotificationStateFilter(void) const
140 if (m_NotificationStateFilter.IsEmpty())
141 return ~(unsigned long)0; /* All states. */
143 return m_NotificationStateFilter;
146 double Notification::GetNotificationInterval(void) const
148 if (m_NotificationInterval.IsEmpty())
151 return m_NotificationInterval;
154 TimePeriod::Ptr Notification::GetNotificationPeriod(void) const
156 return TimePeriod::GetByName(m_NotificationPeriod);
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;
175 double Notification::GetNextNotification(void) const
177 if (m_NextNotification.IsEmpty())
180 return m_NextNotification;
184 * Sets the timestamp when the next periodical notification should be sent.
185 * This does not affect notifications that are sent for state changes.
187 void Notification::SetNextNotification(double time, const String& authority)
189 m_NextNotification = time;
191 Utility::QueueAsyncCallback(boost::bind(boost::ref(OnNextNotificationChanged), GetSelf(), time, authority));
194 long Notification::GetNotificationNumber(void) const
196 if (m_NotificationNumber.IsEmpty())
199 return m_NotificationNumber;
202 void Notification::UpdateNotificationNumber(void)
204 m_NotificationNumber = m_NotificationNumber + 1;
207 void Notification::ResetNotificationNumber(void)
209 m_NotificationNumber = 0;
212 String Notification::NotificationTypeToString(NotificationType type)
215 case NotificationDowntimeStart:
216 return "DOWNTIMESTART";
217 case NotificationDowntimeEnd:
218 return "DOWNTIMEEND";
219 case NotificationDowntimeRemoved:
220 return "DOWNTIMECANCELLED";
221 case NotificationCustom:
223 case NotificationAcknowledgement:
224 return "ACKNOWLEDGEMENT";
225 case NotificationProblem:
227 case NotificationRecovery:
229 case NotificationFlappingStart:
230 return "FLAPPINGSTART";
231 case NotificationFlappingEnd:
232 return "FLAPPINGEND";
234 return "UNKNOWN_NOTIFICATION";
238 void Notification::BeginExecuteNotification(NotificationType type, const Dictionary::Ptr& cr, bool force, const String& author, const String& text)
243 TimePeriod::Ptr tp = GetNotificationPeriod();
245 if (tp && !tp->IsInside(Utility::GetTime())) {
246 Log(LogInformation, "icinga", "Not sending notifications for notification object '" + GetName() + "': not in timeperiod");
250 double now = Utility::GetTime();
251 Dictionary::Ptr times = GetTimes();
252 Service::Ptr service = GetService();
254 if (type == NotificationProblem) {
255 if (times && times->Contains("begin") && now < service->GetLastHardStateChange() + times->Get("begin")) {
256 Log(LogInformation, "icinga", "Not sending notifications for notification object '" + GetName() + "': before escalation range");
260 if (times && times->Contains("end") && now > service->GetLastHardStateChange() + times->Get("end")) {
261 Log(LogInformation, "icinga", "Not sending notifications for notification object '" + GetName() + "': after escalation range");
266 unsigned long ftype = 1 << type;
268 Log(LogDebug, "icinga", "FType=" + Convert::ToString(ftype) + ", TypeFilter=" + Convert::ToString(GetNotificationTypeFilter()));
270 if (!(ftype & GetNotificationTypeFilter())) {
271 Log(LogInformation, "icinga", "Not sending notifications for notification object '" + GetName() + "': type filter does not match");
275 unsigned long fstate = 1 << GetService()->GetState();
277 if (!(fstate & GetNotificationStateFilter())) {
278 Log(LogInformation, "icinga", "Not sending notifications for notification object '" + GetName() + "': state filter does not match");
284 ObjectLock olock(this);
286 SetLastNotification(Utility::GetTime());
289 std::set<User::Ptr> allUsers;
291 std::set<User::Ptr> users = GetUsers();
292 std::copy(users.begin(), users.end(), std::inserter(allUsers, allUsers.begin()));
294 BOOST_FOREACH(const UserGroup::Ptr& ug, GetGroups()) {
295 std::set<User::Ptr> members = ug->GetMembers();
296 std::copy(members.begin(), members.end(), std::inserter(allUsers, allUsers.begin()));
299 BOOST_FOREACH(const User::Ptr& user, allUsers) {
300 Log(LogDebug, "icinga", "Sending notification for user '" + user->GetName() + "'");
301 Utility::QueueAsyncCallback(boost::bind(&Notification::ExecuteNotificationHelper, this, type, user, cr, force, author, text));
305 void Notification::ExecuteNotificationHelper(NotificationType type, const User::Ptr& user, const Dictionary::Ptr& cr, bool force, const String& author, const String& text)
310 TimePeriod::Ptr tp = user->GetNotificationPeriod();
312 if (tp && !tp->IsInside(Utility::GetTime())) {
313 Log(LogInformation, "icinga", "Not sending notifications for notification object '" +
314 GetName() + " and user '" + user->GetName() + "': user not in timeperiod");
318 unsigned long ftype = 1 << type;
320 if (!(ftype & user->GetNotificationTypeFilter())) {
321 Log(LogInformation, "icinga", "Not sending notifications for notification object '" +
322 GetName() + " and user '" + user->GetName() + "': type filter does not match");
326 unsigned long fstate = 1 << GetService()->GetState();
328 if (!(fstate & user->GetNotificationStateFilter())) {
329 Log(LogInformation, "icinga", "Not sending notifications for notification object '" +
330 GetName() + " and user '" + user->GetName() + "': state filter does not match");
336 GetNotificationCommand()->Execute(GetSelf(), user, cr, type);
339 ObjectLock olock(this);
340 UpdateNotificationNumber();
341 SetLastNotification(Utility::GetTime());
344 Service::OnNotificationSentChanged(GetService(), user, type, cr, author, text);
346 Log(LogInformation, "icinga", "Completed sending notification for service '" + GetService()->GetName() + "'");
347 } catch (const std::exception& ex) {
348 std::ostringstream msgbuf;
349 msgbuf << "Exception occured during notification for service '"
350 << GetService()->GetName() << "': " << boost::diagnostic_information(ex);
351 Log(LogWarning, "icinga", msgbuf.str());
355 bool Notification::ResolveMacro(const String& macro, const Dictionary::Ptr&, String *result) const
357 Dictionary::Ptr macros = GetMacros();
359 if (macros && macros->Contains(macro)) {
360 *result = macros->Get(macro);
367 void Notification::InternalSerialize(const Dictionary::Ptr& bag, int attributeTypes) const
369 DynamicObject::InternalSerialize(bag, attributeTypes);
371 if (attributeTypes & Attribute_Config) {
372 bag->Set("notification_command", m_NotificationCommand);
373 bag->Set("notification_interval", m_NotificationInterval);
374 bag->Set("notification_period", m_NotificationPeriod);
375 bag->Set("macros", m_Macros);
376 bag->Set("users", m_Users);
377 bag->Set("groups", m_Groups);
378 bag->Set("times", m_Times);
379 bag->Set("notification_type_filter", m_NotificationTypeFilter);
380 bag->Set("notification_state_filter", m_NotificationStateFilter);
381 bag->Set("host_name", m_HostName);
382 bag->Set("export_macros", m_ExportMacros);
383 bag->Set("service", m_Service);
386 if (attributeTypes & Attribute_State) {
387 bag->Set("last_notification", m_LastNotification);
388 bag->Set("next_notification", m_NextNotification);
389 bag->Set("notification_number", m_NotificationNumber);
393 void Notification::InternalDeserialize(const Dictionary::Ptr& bag, int attributeTypes)
395 DynamicObject::InternalDeserialize(bag, attributeTypes);
397 if (attributeTypes & Attribute_Config) {
398 m_NotificationCommand = bag->Get("notification_command");
399 m_NotificationInterval = bag->Get("notification_interval");
400 m_NotificationPeriod = bag->Get("notification_period");
401 m_Macros = bag->Get("macros");
402 m_Users = bag->Get("users");
403 m_Groups = bag->Get("groups");
404 m_Times = bag->Get("times");
405 m_NotificationTypeFilter = bag->Get("notification_type_filter");
406 m_NotificationStateFilter = bag->Get("notification_state_filter");
407 m_HostName = bag->Get("host_name");
408 m_ExportMacros = bag->Get("export_macros");
409 m_Service = bag->Get("service");
412 if (attributeTypes & Attribute_State) {
413 m_LastNotification = bag->Get("last_notification");
414 m_NextNotification = bag->Get("next_notification");
415 m_NotificationNumber = bag->Get("notification_number");