]> granicus.if.org Git - icinga2/blob - lib/icinga/checkable-notification.cpp
Merge pull request #5948 from Icinga/doc/install
[icinga2] / lib / icinga / checkable-notification.cpp
1 /******************************************************************************
2  * Icinga 2                                                                   *
3  * Copyright (C) 2012-2018 Icinga Development Team (https://www.icinga.com/)  *
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
28 using namespace icinga;
29
30 boost::signals2::signal<void (const Notification::Ptr&, const Checkable::Ptr&, const std::set<User::Ptr>&,
31         const NotificationType&, const CheckResult::Ptr&, const String&, const String&,
32         const MessageOrigin::Ptr&)> Checkable::OnNotificationSentToAllUsers;
33 boost::signals2::signal<void (const Notification::Ptr&, const Checkable::Ptr&, const User::Ptr&,
34         const NotificationType&, const CheckResult::Ptr&, const String&, const String&, const String&,
35         const MessageOrigin::Ptr&)> Checkable::OnNotificationSentToUser;
36
37 void Checkable::ResetNotificationNumbers()
38 {
39         for (const Notification::Ptr& notification : GetNotifications()) {
40                 ObjectLock olock(notification);
41                 notification->ResetNotificationNumber();
42         }
43 }
44
45 void Checkable::SendNotifications(NotificationType type, const CheckResult::Ptr& cr, const String& author, const String& text)
46 {
47         CONTEXT("Sending notifications for object '" + GetName() + "'");
48
49         bool force = GetForceNextNotification();
50
51         SetForceNextNotification(false);
52
53         if (!IcingaApplication::GetInstance()->GetEnableNotifications() || !GetEnableNotifications()) {
54                 if (!force) {
55                         Log(LogInformation, "Checkable")
56                                 << "Notifications are disabled for service '" << GetName() << "'.";
57                         return;
58                 }
59         }
60
61         Log(LogInformation, "Checkable")
62                 << "Checking for configured notifications for object '" << GetName() << "'";
63
64         std::set<Notification::Ptr> notifications = GetNotifications();
65
66         if (notifications.empty())
67                 Log(LogInformation, "Checkable")
68                         << "Checkable '" << GetName() << "' does not have any notifications.";
69
70         Log(LogDebug, "Checkable")
71                 << "Checkable '" << GetName() << "' has " << notifications.size() << " notification(s).";
72
73         for (const Notification::Ptr& notification : notifications) {
74                 try {
75                         if (!notification->IsPaused())
76                                 notification->BeginExecuteNotification(type, cr, force, false, author, text);
77                 } catch (const std::exception& ex) {
78                         Log(LogWarning, "Checkable")
79                                 << "Exception occured during notification for service '"
80                                 << GetName() << "': " << DiagnosticInformation(ex);
81                 }
82         }
83 }
84
85 std::set<Notification::Ptr> Checkable::GetNotifications() const
86 {
87         boost::mutex::scoped_lock lock(m_NotificationMutex);
88         return m_Notifications;
89 }
90
91 void Checkable::RegisterNotification(const Notification::Ptr& notification)
92 {
93         boost::mutex::scoped_lock lock(m_NotificationMutex);
94         m_Notifications.insert(notification);
95 }
96
97 void Checkable::UnregisterNotification(const Notification::Ptr& notification)
98 {
99         boost::mutex::scoped_lock lock(m_NotificationMutex);
100         m_Notifications.erase(notification);
101 }