]> granicus.if.org Git - icinga2/blob - lib/icinga/checkable-notification.cpp
Remove logger_fwd.hpp
[icinga2] / lib / icinga / checkable-notification.cpp
1 /******************************************************************************
2  * Icinga 2                                                                   *
3  * Copyright (C) 2012-2014 Icinga Development Team (http://www.icinga.org)    *
4  *                                                                            *
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.                     *
9  *                                                                            *
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.                               *
14  *                                                                            *
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  ******************************************************************************/
19
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>
28
29 using namespace icinga;
30
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;
37
38 void Checkable::ResetNotificationNumbers(void)
39 {
40         BOOST_FOREACH(const Notification::Ptr& notification, GetNotifications()) {
41                 ObjectLock olock(notification);
42                 notification->ResetNotificationNumber();
43         }
44 }
45
46 void Checkable::SendNotifications(NotificationType type, const CheckResult::Ptr& cr, const String& author, const String& text)
47 {
48         CONTEXT("Sending notifications for object '" + GetName() + "'");
49
50         bool force = GetForceNextNotification();
51
52         if (!IcingaApplication::GetInstance()->GetEnableNotifications() || !GetEnableNotifications()) {
53                 if (!force) {
54                         Log(LogInformation, "Checkable", "Notifications are disabled for service '" + GetName() + "'.");
55                         return;
56                 }
57
58                 SetForceNextNotification(false);
59         }
60
61         Log(LogInformation, "Checkable", "Checking for configured notifications for object '" + GetName() + "'");
62
63         std::set<Notification::Ptr> notifications = GetNotifications();
64
65         if (notifications.empty())
66                 Log(LogInformation, "Checkable", "Checkable '" + GetName() + "' does not have any notifications.");
67
68         Log(LogDebug, "Checkable", "Checkable '" + GetName() + "' has " + Convert::ToString(notifications.size()) + " notification(s).");
69
70         BOOST_FOREACH(const Notification::Ptr& notification, notifications) {
71                 try {
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();
78
79                         Log(LogWarning, "Checkable", message);
80                 }
81         }
82 }
83
84 std::set<Notification::Ptr> Checkable::GetNotifications(void) const
85 {
86         return m_Notifications;
87 }
88
89 void Checkable::AddNotification(const Notification::Ptr& notification)
90 {
91         m_Notifications.insert(notification);
92 }
93
94 void Checkable::RemoveNotification(const Notification::Ptr& notification)
95 {
96         m_Notifications.erase(notification);
97 }
98
99 bool Checkable::GetEnableNotifications(void) const
100 {
101         if (!GetOverrideEnableNotifications().IsEmpty())
102                 return GetOverrideEnableNotifications();
103         else
104                 return GetEnableNotificationsRaw();
105 }
106
107 void Checkable::SetEnableNotifications(bool enabled, const MessageOrigin& origin)
108 {
109         SetOverrideEnableNotifications(enabled);
110
111         OnEnableNotificationsChanged(GetSelf(), enabled, origin);
112 }
113
114 bool Checkable::GetForceNextNotification(void) const
115 {
116         return GetForceNextNotificationRaw();
117 }
118
119 void Checkable::SetForceNextNotification(bool forced, const MessageOrigin& origin)
120 {
121         SetForceNextNotificationRaw(forced);
122
123         OnForceNextNotificationChanged(GetSelf(), forced, origin);
124 }