1 /******************************************************************************
3 * Copyright (C) 2012-2018 Icinga Development Team (https://icinga.com/) *
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"
28 using namespace icinga;
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;
37 void Checkable::ResetNotificationNumbers()
39 for (const Notification::Ptr& notification : GetNotifications()) {
40 ObjectLock olock(notification);
41 notification->ResetNotificationNumber();
45 void Checkable::SendNotifications(NotificationType type, const CheckResult::Ptr& cr, const String& author, const String& text)
47 String checkableName = GetName();
49 CONTEXT("Sending notifications for object '" + checkableName + "'");
51 bool force = GetForceNextNotification();
53 SetForceNextNotification(false);
55 if (!IcingaApplication::GetInstance()->GetEnableNotifications() || !GetEnableNotifications()) {
57 Log(LogInformation, "Checkable")
58 << "Notifications are disabled for checkable '" << checkableName << "'.";
63 std::set<Notification::Ptr> notifications = GetNotifications();
65 Log(LogInformation, "Checkable")
66 << "Checkable '" << checkableName << "' has " << notifications.size() << " notification(s). Proceeding with filters, successful sends will be logged.";
68 if (notifications.empty())
71 for (const Notification::Ptr& notification : notifications) {
73 if (!notification->IsPaused()) {
74 notification->BeginExecuteNotification(type, cr, force, false, author, text);
76 Log(LogNotice, "Notification")
77 << "Notification '" << notification->GetName() << "': HA cluster active, this endpoint does not have the authority (paused=true). Skipping.";
79 } catch (const std::exception& ex) {
80 Log(LogWarning, "Checkable")
81 << "Exception occurred during notification '" << notification->GetName() << "' for checkable '"
82 << GetName() << "': " << DiagnosticInformation(ex, false);
87 std::set<Notification::Ptr> Checkable::GetNotifications() const
89 boost::mutex::scoped_lock lock(m_NotificationMutex);
90 return m_Notifications;
93 void Checkable::RegisterNotification(const Notification::Ptr& notification)
95 boost::mutex::scoped_lock lock(m_NotificationMutex);
96 m_Notifications.insert(notification);
99 void Checkable::UnregisterNotification(const Notification::Ptr& notification)
101 boost::mutex::scoped_lock lock(m_NotificationMutex);
102 m_Notifications.erase(notification);