2013-02-09 09:20:10 -05:00
|
|
|
/******************************************************************************
|
|
|
|
|
* Icinga 2 *
|
2014-03-18 20:02:29 -04:00
|
|
|
* Copyright (C) 2012-2014 Icinga Development Team (http://www.icinga.org) *
|
2013-02-09 09:20:10 -05: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 10:23:35 -04:00
|
|
|
#include "icinga/checkable.hpp"
|
|
|
|
|
#include "icinga/icingaapplication.hpp"
|
|
|
|
|
#include "base/objectlock.hpp"
|
|
|
|
|
#include "base/logger_fwd.hpp"
|
|
|
|
|
#include "base/exception.hpp"
|
|
|
|
|
#include "base/context.hpp"
|
|
|
|
|
#include "base/convert.hpp"
|
2013-03-16 16:18:53 -04:00
|
|
|
#include <boost/foreach.hpp>
|
2013-02-09 09:20:10 -05:00
|
|
|
|
|
|
|
|
using namespace icinga;
|
|
|
|
|
|
2014-04-03 09:36:13 -04:00
|
|
|
boost::signals2::signal<void (const Notification::Ptr&, const Checkable::Ptr&, const std::set<User::Ptr>&,
|
|
|
|
|
const NotificationType&, const CheckResult::Ptr&, const String&, const String&)> Checkable::OnNotificationSentToAllUsers;
|
|
|
|
|
boost::signals2::signal<void (const Notification::Ptr&, const Checkable::Ptr&, const std::set<User::Ptr>&,
|
|
|
|
|
const NotificationType&, const CheckResult::Ptr&, const String&, const String&)> Checkable::OnNotificationSendStart;
|
|
|
|
|
boost::signals2::signal<void (const Notification::Ptr&, const Checkable::Ptr&, const User::Ptr&,
|
|
|
|
|
const NotificationType&, const CheckResult::Ptr&, const String&, const String&, const String&)> Checkable::OnNotificationSentToUser;
|
|
|
|
|
|
|
|
|
|
void Checkable::ResetNotificationNumbers(void)
|
2013-07-18 11:04:09 -04:00
|
|
|
{
|
|
|
|
|
BOOST_FOREACH(const Notification::Ptr& notification, GetNotifications()) {
|
|
|
|
|
ObjectLock olock(notification);
|
|
|
|
|
notification->ResetNotificationNumber();
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2014-04-03 09:36:13 -04:00
|
|
|
void Checkable::SendNotifications(NotificationType type, const CheckResult::Ptr& cr, const String& author, const String& text)
|
2013-02-09 09:20:10 -05:00
|
|
|
{
|
2014-04-03 09:36:13 -04:00
|
|
|
CONTEXT("Sending notifications for object '" + GetName() + "'");
|
2013-11-19 01:49:41 -05:00
|
|
|
|
2013-08-30 09:56:03 -04:00
|
|
|
bool force = GetForceNextNotification();
|
2013-03-21 08:42:46 -04:00
|
|
|
|
2013-10-08 05:57:35 -04:00
|
|
|
if (!IcingaApplication::GetInstance()->GetEnableNotifications() || !GetEnableNotifications()) {
|
2013-08-30 09:56:03 -04:00
|
|
|
if (!force) {
|
2014-05-28 07:45:45 -04:00
|
|
|
Log(LogInformation, "Checkable", "Notifications are disabled for service '" + GetName() + "'.");
|
2013-03-21 08:22:26 -04:00
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
SetForceNextNotification(false);
|
2013-02-26 06:37:25 -05:00
|
|
|
}
|
|
|
|
|
|
2014-05-28 07:45:45 -04:00
|
|
|
Log(LogInformation, "Checkable", "Sending notifications for object '" + GetName() + "'");
|
2013-02-09 09:20:10 -05:00
|
|
|
|
2013-03-16 16:18:53 -04:00
|
|
|
std::set<Notification::Ptr> notifications = GetNotifications();
|
2013-02-09 09:20:10 -05:00
|
|
|
|
2013-03-06 09:41:13 -05:00
|
|
|
if (notifications.empty())
|
2014-05-28 07:45:45 -04:00
|
|
|
Log(LogInformation, "Checkable", "Checkable '" + GetName() + "' does not have any notifications.");
|
2013-02-09 09:20:10 -05:00
|
|
|
|
2014-05-28 07:45:45 -04:00
|
|
|
Log(LogDebug, "Checkable", "Checkable '" + GetName() + "' has " + Convert::ToString(notifications.size()) + " notification(s).");
|
2014-01-27 11:22:48 -05:00
|
|
|
|
2013-02-09 09:20:10 -05:00
|
|
|
BOOST_FOREACH(const Notification::Ptr& notification, notifications) {
|
2013-02-24 02:26:10 -05:00
|
|
|
try {
|
2013-07-01 05:17:58 -04:00
|
|
|
notification->BeginExecuteNotification(type, cr, force, author, text);
|
2013-03-16 16:18:53 -04:00
|
|
|
} catch (const std::exception& ex) {
|
|
|
|
|
std::ostringstream msgbuf;
|
2013-02-24 02:26:10 -05:00
|
|
|
msgbuf << "Exception occured during notification for service '"
|
2013-11-20 15:55:14 -05:00
|
|
|
<< GetName() << "': " << DiagnosticInformation(ex);
|
2013-02-24 02:26:10 -05:00
|
|
|
String message = msgbuf.str();
|
|
|
|
|
|
2014-05-28 07:45:45 -04:00
|
|
|
Log(LogWarning, "Checkable", message);
|
2013-02-24 02:26:10 -05:00
|
|
|
}
|
2013-02-09 09:20:10 -05:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2014-04-03 09:36:13 -04:00
|
|
|
std::set<Notification::Ptr> Checkable::GetNotifications(void) const
|
2013-02-27 09:23:25 -05:00
|
|
|
{
|
2013-08-20 05:06:04 -04:00
|
|
|
return m_Notifications;
|
2013-02-27 09:23:25 -05:00
|
|
|
}
|
|
|
|
|
|
2014-04-03 09:36:13 -04:00
|
|
|
void Checkable::AddNotification(const Notification::Ptr& notification)
|
2013-02-09 09:20:10 -05:00
|
|
|
{
|
2013-08-20 05:06:04 -04:00
|
|
|
m_Notifications.insert(notification);
|
2013-02-09 09:20:10 -05:00
|
|
|
}
|
|
|
|
|
|
2014-04-03 09:36:13 -04:00
|
|
|
void Checkable::RemoveNotification(const Notification::Ptr& notification)
|
2013-02-09 09:20:10 -05:00
|
|
|
{
|
2013-08-20 05:06:04 -04:00
|
|
|
m_Notifications.erase(notification);
|
2013-02-09 09:20:10 -05:00
|
|
|
}
|
|
|
|
|
|
2014-04-03 09:36:13 -04:00
|
|
|
bool Checkable::GetEnableNotifications(void) const
|
2013-02-09 11:27:32 -05:00
|
|
|
{
|
2013-11-26 05:18:05 -05:00
|
|
|
if (!GetOverrideEnableNotifications().IsEmpty())
|
|
|
|
|
return GetOverrideEnableNotifications();
|
|
|
|
|
else
|
2013-11-26 05:34:33 -05:00
|
|
|
return GetEnableNotificationsRaw();
|
2013-02-09 11:27:32 -05:00
|
|
|
}
|
|
|
|
|
|
2014-05-03 14:02:22 -04:00
|
|
|
void Checkable::SetEnableNotifications(bool enabled, const MessageOrigin& origin)
|
2013-02-09 11:27:32 -05:00
|
|
|
{
|
2014-01-20 05:12:03 -05:00
|
|
|
SetOverrideEnableNotifications(enabled);
|
2013-08-29 07:06:36 -04:00
|
|
|
|
2014-05-03 14:02:22 -04:00
|
|
|
OnEnableNotificationsChanged(GetSelf(), enabled, origin);
|
2013-02-09 11:27:32 -05:00
|
|
|
}
|
2013-03-21 08:22:26 -04:00
|
|
|
|
2014-04-03 09:36:13 -04:00
|
|
|
bool Checkable::GetForceNextNotification(void) const
|
2013-03-21 08:22:26 -04:00
|
|
|
{
|
2013-10-26 03:41:45 -04:00
|
|
|
return GetForceNextNotificationRaw();
|
2013-03-21 08:22:26 -04:00
|
|
|
}
|
|
|
|
|
|
2014-05-03 14:02:22 -04:00
|
|
|
void Checkable::SetForceNextNotification(bool forced, const MessageOrigin& origin)
|
2013-03-21 08:22:26 -04:00
|
|
|
{
|
2013-10-26 03:41:45 -04:00
|
|
|
SetForceNextNotificationRaw(forced);
|
2013-08-29 05:37:51 -04:00
|
|
|
|
2014-05-03 14:02:22 -04:00
|
|
|
OnForceNextNotificationChanged(GetSelf(), forced, origin);
|
2013-03-21 08:22:26 -04:00
|
|
|
}
|