2013-02-09 15:20:10 +01:00
|
|
|
/******************************************************************************
|
|
|
|
* Icinga 2 *
|
2018-10-18 09:27:04 +02:00
|
|
|
* Copyright (C) 2012-2018 Icinga Development Team (https://icinga.com/) *
|
2013-02-09 15:20:10 +01:00
|
|
|
* *
|
|
|
|
* This program is free software; you can redistribute it and/or *
|
|
|
|
* modify it under the terms of the GNU General Public License *
|
|
|
|
* as published by the Free Software Foundation; either version 2 *
|
|
|
|
* of the License, or (at your option) any later version. *
|
|
|
|
* *
|
|
|
|
* This program is distributed in the hope that it will be useful, *
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of *
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
|
|
|
|
* GNU General Public License for more details. *
|
|
|
|
* *
|
|
|
|
* You should have received a copy of the GNU General Public License *
|
|
|
|
* along with this program; if not, write to the Free Software Foundation *
|
|
|
|
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA. *
|
|
|
|
******************************************************************************/
|
|
|
|
|
2014-05-25 16:23:35 +02:00
|
|
|
#include "icinga/checkable.hpp"
|
|
|
|
#include "icinga/icingaapplication.hpp"
|
|
|
|
#include "base/objectlock.hpp"
|
2014-10-19 14:21:12 +02:00
|
|
|
#include "base/logger.hpp"
|
2014-05-25 16:23:35 +02:00
|
|
|
#include "base/exception.hpp"
|
|
|
|
#include "base/context.hpp"
|
|
|
|
#include "base/convert.hpp"
|
2013-02-09 15:20:10 +01:00
|
|
|
|
|
|
|
using namespace icinga;
|
|
|
|
|
2014-04-03 15:36:13 +02:00
|
|
|
boost::signals2::signal<void (const Notification::Ptr&, const Checkable::Ptr&, const std::set<User::Ptr>&,
|
2017-12-19 15:50:05 +01:00
|
|
|
const NotificationType&, const CheckResult::Ptr&, const String&, const String&,
|
|
|
|
const MessageOrigin::Ptr&)> Checkable::OnNotificationSentToAllUsers;
|
2014-04-03 15:36:13 +02:00
|
|
|
boost::signals2::signal<void (const Notification::Ptr&, const Checkable::Ptr&, const User::Ptr&,
|
2017-12-19 15:50:05 +01:00
|
|
|
const NotificationType&, const CheckResult::Ptr&, const String&, const String&, const String&,
|
|
|
|
const MessageOrigin::Ptr&)> Checkable::OnNotificationSentToUser;
|
2014-04-03 15:36:13 +02:00
|
|
|
|
2018-01-04 04:25:35 +01:00
|
|
|
void Checkable::ResetNotificationNumbers()
|
2013-07-18 17:04:09 +02:00
|
|
|
{
|
2016-08-25 06:19:44 +02:00
|
|
|
for (const Notification::Ptr& notification : GetNotifications()) {
|
2013-07-18 17:04:09 +02:00
|
|
|
ObjectLock olock(notification);
|
|
|
|
notification->ResetNotificationNumber();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-04-03 15:36:13 +02:00
|
|
|
void Checkable::SendNotifications(NotificationType type, const CheckResult::Ptr& cr, const String& author, const String& text)
|
2013-02-09 15:20:10 +01:00
|
|
|
{
|
2014-04-03 15:36:13 +02:00
|
|
|
CONTEXT("Sending notifications for object '" + GetName() + "'");
|
2013-11-19 07:49:41 +01:00
|
|
|
|
2013-08-30 15:56:03 +02:00
|
|
|
bool force = GetForceNextNotification();
|
2013-03-21 13:42:46 +01:00
|
|
|
|
2016-11-17 11:46:40 +01:00
|
|
|
SetForceNextNotification(false);
|
|
|
|
|
2013-10-08 11:57:35 +02:00
|
|
|
if (!IcingaApplication::GetInstance()->GetEnableNotifications() || !GetEnableNotifications()) {
|
2013-08-30 15:56:03 +02:00
|
|
|
if (!force) {
|
2014-10-19 17:52:17 +02:00
|
|
|
Log(LogInformation, "Checkable")
|
2017-12-19 15:50:05 +01:00
|
|
|
<< "Notifications are disabled for service '" << GetName() << "'.";
|
2013-03-21 13:22:26 +01:00
|
|
|
return;
|
|
|
|
}
|
2013-02-26 12:37:25 +01:00
|
|
|
}
|
|
|
|
|
2014-10-19 17:52:17 +02:00
|
|
|
Log(LogInformation, "Checkable")
|
2017-12-19 15:50:05 +01:00
|
|
|
<< "Checking for configured notifications for object '" << GetName() << "'";
|
2013-02-09 15:20:10 +01:00
|
|
|
|
2013-03-16 21:18:53 +01:00
|
|
|
std::set<Notification::Ptr> notifications = GetNotifications();
|
2013-02-09 15:20:10 +01:00
|
|
|
|
2013-03-06 15:41:13 +01:00
|
|
|
if (notifications.empty())
|
2014-10-19 17:52:17 +02:00
|
|
|
Log(LogInformation, "Checkable")
|
2017-12-19 15:50:05 +01:00
|
|
|
<< "Checkable '" << GetName() << "' does not have any notifications.";
|
2013-02-09 15:20:10 +01:00
|
|
|
|
2014-10-19 17:52:17 +02:00
|
|
|
Log(LogDebug, "Checkable")
|
2017-12-19 15:50:05 +01:00
|
|
|
<< "Checkable '" << GetName() << "' has " << notifications.size() << " notification(s).";
|
2014-01-27 17:22:48 +01:00
|
|
|
|
2016-08-25 06:19:44 +02:00
|
|
|
for (const Notification::Ptr& notification : notifications) {
|
2013-02-24 08:26:10 +01:00
|
|
|
try {
|
2016-06-07 12:44:12 +02:00
|
|
|
if (!notification->IsPaused())
|
2016-08-12 14:49:29 +02:00
|
|
|
notification->BeginExecuteNotification(type, cr, force, false, author, text);
|
2013-03-16 21:18:53 +01:00
|
|
|
} catch (const std::exception& ex) {
|
2014-10-19 17:52:17 +02:00
|
|
|
Log(LogWarning, "Checkable")
|
2018-07-21 10:38:09 +02:00
|
|
|
<< "Exception occurred during notification for service '"
|
2017-12-19 15:50:05 +01:00
|
|
|
<< GetName() << "': " << DiagnosticInformation(ex);
|
2013-02-24 08:26:10 +01:00
|
|
|
}
|
2013-02-09 15:20:10 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-01-04 04:25:35 +01:00
|
|
|
std::set<Notification::Ptr> Checkable::GetNotifications() const
|
2013-02-27 15:23:25 +01:00
|
|
|
{
|
2015-01-29 13:18:46 +01:00
|
|
|
boost::mutex::scoped_lock lock(m_NotificationMutex);
|
2013-08-20 11:06:04 +02:00
|
|
|
return m_Notifications;
|
2013-02-27 15:23:25 +01:00
|
|
|
}
|
|
|
|
|
2015-08-20 17:18:48 +02:00
|
|
|
void Checkable::RegisterNotification(const Notification::Ptr& notification)
|
2013-02-09 15:20:10 +01:00
|
|
|
{
|
2015-01-29 13:18:46 +01:00
|
|
|
boost::mutex::scoped_lock lock(m_NotificationMutex);
|
2013-08-20 11:06:04 +02:00
|
|
|
m_Notifications.insert(notification);
|
2013-02-09 15:20:10 +01:00
|
|
|
}
|
|
|
|
|
2015-08-20 17:18:48 +02:00
|
|
|
void Checkable::UnregisterNotification(const Notification::Ptr& notification)
|
2013-02-09 15:20:10 +01:00
|
|
|
{
|
2015-01-29 13:18:46 +01:00
|
|
|
boost::mutex::scoped_lock lock(m_NotificationMutex);
|
2013-08-20 11:06:04 +02:00
|
|
|
m_Notifications.erase(notification);
|
2013-02-09 15:20:10 +01:00
|
|
|
}
|