icinga2/lib/icinga/checkable-notification.cpp

125 lines
4.9 KiB
C++
Raw Normal View History

/******************************************************************************
* Icinga 2 *
* Copyright (C) 2012-2014 Icinga Development Team (http://www.icinga.org) *
* *
* 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"
#include "base/logger_fwd.hpp"
#include "base/exception.hpp"
#include "base/context.hpp"
#include "base/convert.hpp"
2013-03-16 21:18:53 +01:00
#include <boost/foreach.hpp>
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>&,
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 17:04:09 +02:00
{
BOOST_FOREACH(const Notification::Ptr& notification, GetNotifications()) {
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)
{
2014-04-03 15:36:13 +02:00
CONTEXT("Sending notifications for object '" + GetName() + "'");
bool force = GetForceNextNotification();
2013-10-08 11:57:35 +02:00
if (!IcingaApplication::GetInstance()->GetEnableNotifications() || !GetEnableNotifications()) {
if (!force) {
Log(LogInformation, "Checkable", "Notifications are disabled for service '" + GetName() + "'.");
return;
}
SetForceNextNotification(false);
}
Log(LogInformation, "Checkable", "Checking for configured notifications for object '" + GetName() + "'");
2013-03-16 21:18:53 +01:00
std::set<Notification::Ptr> notifications = GetNotifications();
2013-03-06 15:41:13 +01:00
if (notifications.empty())
Log(LogInformation, "Checkable", "Checkable '" + GetName() + "' does not have any notifications.");
Log(LogDebug, "Checkable", "Checkable '" + GetName() + "' has " + Convert::ToString(notifications.size()) + " notification(s).");
BOOST_FOREACH(const Notification::Ptr& notification, notifications) {
2013-02-24 08:26:10 +01:00
try {
notification->BeginExecuteNotification(type, cr, force, author, text);
2013-03-16 21:18:53 +01:00
} catch (const std::exception& ex) {
std::ostringstream msgbuf;
2013-02-24 08:26:10 +01:00
msgbuf << "Exception occured during notification for service '"
<< GetName() << "': " << DiagnosticInformation(ex);
2013-02-24 08:26:10 +01:00
String message = msgbuf.str();
Log(LogWarning, "Checkable", message);
2013-02-24 08:26:10 +01:00
}
}
}
2014-04-03 15:36:13 +02:00
std::set<Notification::Ptr> Checkable::GetNotifications(void) const
2013-02-27 15:23:25 +01:00
{
return m_Notifications;
2013-02-27 15:23:25 +01:00
}
2014-04-03 15:36:13 +02:00
void Checkable::AddNotification(const Notification::Ptr& notification)
{
m_Notifications.insert(notification);
}
2014-04-03 15:36:13 +02:00
void Checkable::RemoveNotification(const Notification::Ptr& notification)
{
m_Notifications.erase(notification);
}
2014-04-03 15:36:13 +02:00
bool Checkable::GetEnableNotifications(void) const
{
if (!GetOverrideEnableNotifications().IsEmpty())
return GetOverrideEnableNotifications();
else
return GetEnableNotificationsRaw();
}
void Checkable::SetEnableNotifications(bool enabled, const MessageOrigin& origin)
{
SetOverrideEnableNotifications(enabled);
OnEnableNotificationsChanged(GetSelf(), enabled, origin);
}
2014-04-03 15:36:13 +02:00
bool Checkable::GetForceNextNotification(void) const
{
2013-10-26 09:41:45 +02:00
return GetForceNextNotificationRaw();
}
void Checkable::SetForceNextNotification(bool forced, const MessageOrigin& origin)
{
2013-10-26 09:41:45 +02:00
SetForceNextNotificationRaw(forced);
OnForceNextNotificationChanged(GetSelf(), forced, origin);
}