]> granicus.if.org Git - icinga2/blob - lib/icinga/checkable-notification.cpp
Merge pull request #6934 from Icinga/bugfix/boost-version-6933
[icinga2] / lib / icinga / checkable-notification.cpp
1 /******************************************************************************
2  * Icinga 2                                                                   *
3  * Copyright (C) 2012-2018 Icinga Development Team (https://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         String checkableName = GetName();
48
49         CONTEXT("Sending notifications for object '" + checkableName + "'");
50
51         bool force = GetForceNextNotification();
52
53         SetForceNextNotification(false);
54
55         if (!IcingaApplication::GetInstance()->GetEnableNotifications() || !GetEnableNotifications()) {
56                 if (!force) {
57                         Log(LogInformation, "Checkable")
58                                 << "Notifications are disabled for checkable '" << checkableName << "'.";
59                         return;
60                 }
61         }
62
63         std::set<Notification::Ptr> notifications = GetNotifications();
64
65         Log(LogInformation, "Checkable")
66                 << "Checkable '" << checkableName << "' has " << notifications.size() << " notification(s). Proceeding with filters, successful sends will be logged.";
67
68         if (notifications.empty())
69                 return;
70
71         for (const Notification::Ptr& notification : notifications) {
72                 try {
73                         if (!notification->IsPaused()) {
74                                 notification->BeginExecuteNotification(type, cr, force, false, author, text);
75                         } else {
76                                 Log(LogNotice, "Notification")
77                                         << "Notification '" << notification->GetName() << "': HA cluster active, this endpoint does not have the authority (paused=true). Skipping.";
78                         }
79                 } catch (const std::exception& ex) {
80                         Log(LogWarning, "Checkable")
81                                 << "Exception occurred during notification '" << notification->GetName() << "' for checkable '"
82                                 << GetName() << "': " << DiagnosticInformation(ex, false);
83                 }
84         }
85 }
86
87 std::set<Notification::Ptr> Checkable::GetNotifications() const
88 {
89         boost::mutex::scoped_lock lock(m_NotificationMutex);
90         return m_Notifications;
91 }
92
93 void Checkable::RegisterNotification(const Notification::Ptr& notification)
94 {
95         boost::mutex::scoped_lock lock(m_NotificationMutex);
96         m_Notifications.insert(notification);
97 }
98
99 void Checkable::UnregisterNotification(const Notification::Ptr& notification)
100 {
101         boost::mutex::scoped_lock lock(m_NotificationMutex);
102         m_Notifications.erase(notification);
103 }