1 /******************************************************************************
3 * Copyright (C) 2012-2014 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/checkable.hpp"
21 #include "icinga/icingaapplication.hpp"
22 #include "base/objectlock.hpp"
23 #include "base/logger.hpp"
24 #include "base/exception.hpp"
25 #include "base/context.hpp"
26 #include "base/convert.hpp"
27 #include <boost/foreach.hpp>
29 using namespace icinga;
31 boost::signals2::signal<void (const Notification::Ptr&, const Checkable::Ptr&, const std::set<User::Ptr>&,
32 const NotificationType&, const CheckResult::Ptr&, const String&, const String&)> Checkable::OnNotificationSentToAllUsers;
33 boost::signals2::signal<void (const Notification::Ptr&, const Checkable::Ptr&, const std::set<User::Ptr>&,
34 const NotificationType&, const CheckResult::Ptr&, const String&, const String&)> Checkable::OnNotificationSendStart;
35 boost::signals2::signal<void (const Notification::Ptr&, const Checkable::Ptr&, const User::Ptr&,
36 const NotificationType&, const CheckResult::Ptr&, const String&, const String&, const String&)> Checkable::OnNotificationSentToUser;
38 void Checkable::ResetNotificationNumbers(void)
40 BOOST_FOREACH(const Notification::Ptr& notification, GetNotifications()) {
41 ObjectLock olock(notification);
42 notification->ResetNotificationNumber();
46 void Checkable::SendNotifications(NotificationType type, const CheckResult::Ptr& cr, const String& author, const String& text)
48 CONTEXT("Sending notifications for object '" + GetName() + "'");
50 bool force = GetForceNextNotification();
52 if (!IcingaApplication::GetInstance()->GetEnableNotifications() || !GetEnableNotifications()) {
54 Log(LogInformation, "Checkable", "Notifications are disabled for service '" + GetName() + "'.");
58 SetForceNextNotification(false);
61 Log(LogInformation, "Checkable", "Checking for configured notifications for object '" + GetName() + "'");
63 std::set<Notification::Ptr> notifications = GetNotifications();
65 if (notifications.empty())
66 Log(LogInformation, "Checkable", "Checkable '" + GetName() + "' does not have any notifications.");
68 Log(LogDebug, "Checkable", "Checkable '" + GetName() + "' has " + Convert::ToString(notifications.size()) + " notification(s).");
70 BOOST_FOREACH(const Notification::Ptr& notification, notifications) {
72 notification->BeginExecuteNotification(type, cr, force, author, text);
73 } catch (const std::exception& ex) {
74 std::ostringstream msgbuf;
75 msgbuf << "Exception occured during notification for service '"
76 << GetName() << "': " << DiagnosticInformation(ex);
77 String message = msgbuf.str();
79 Log(LogWarning, "Checkable", message);
84 std::set<Notification::Ptr> Checkable::GetNotifications(void) const
86 return m_Notifications;
89 void Checkable::AddNotification(const Notification::Ptr& notification)
91 m_Notifications.insert(notification);
94 void Checkable::RemoveNotification(const Notification::Ptr& notification)
96 m_Notifications.erase(notification);
99 bool Checkable::GetEnableNotifications(void) const
101 if (!GetOverrideEnableNotifications().IsEmpty())
102 return GetOverrideEnableNotifications();
104 return GetEnableNotificationsRaw();
107 void Checkable::SetEnableNotifications(bool enabled, const MessageOrigin& origin)
109 SetOverrideEnableNotifications(enabled);
111 OnEnableNotificationsChanged(GetSelf(), enabled, origin);
114 bool Checkable::GetForceNextNotification(void) const
116 return GetForceNextNotificationRaw();
119 void Checkable::SetForceNextNotification(bool forced, const MessageOrigin& origin)
121 SetForceNextNotificationRaw(forced);
123 OnForceNextNotificationChanged(GetSelf(), forced, origin);