1 /******************************************************************************
3 * Copyright (C) 2012-2018 Icinga Development Team (https://www.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 CONTEXT("Sending notifications for object '" + GetName() + "'");
49 bool force = GetForceNextNotification();
51 SetForceNextNotification(false);
53 if (!IcingaApplication::GetInstance()->GetEnableNotifications() || !GetEnableNotifications()) {
55 Log(LogInformation, "Checkable")
56 << "Notifications are disabled for service '" << GetName() << "'.";
61 Log(LogInformation, "Checkable")
62 << "Checking for configured notifications for object '" << GetName() << "'";
64 std::set<Notification::Ptr> notifications = GetNotifications();
66 if (notifications.empty())
67 Log(LogInformation, "Checkable")
68 << "Checkable '" << GetName() << "' does not have any notifications.";
70 Log(LogDebug, "Checkable")
71 << "Checkable '" << GetName() << "' has " << notifications.size() << " notification(s).";
73 for (const Notification::Ptr& notification : notifications) {
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 occurred during notification for service '"
80 << GetName() << "': " << DiagnosticInformation(ex);
85 std::set<Notification::Ptr> Checkable::GetNotifications() const
87 boost::mutex::scoped_lock lock(m_NotificationMutex);
88 return m_Notifications;
91 void Checkable::RegisterNotification(const Notification::Ptr& notification)
93 boost::mutex::scoped_lock lock(m_NotificationMutex);
94 m_Notifications.insert(notification);
97 void Checkable::UnregisterNotification(const Notification::Ptr& notification)
99 boost::mutex::scoped_lock lock(m_NotificationMutex);
100 m_Notifications.erase(notification);