icinga2/lib/icinga/service-notification.cpp

194 lines
6.5 KiB
C++
Raw Normal View History

/******************************************************************************
* Icinga 2 *
2014-01-09 00:32:11 +01:00
* Copyright (C) 2012-present 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. *
******************************************************************************/
2013-03-16 21:18:53 +01:00
#include "icinga/service.h"
2013-10-08 11:57:35 +02:00
#include "icinga/icingaapplication.h"
2013-03-17 20:19:29 +01:00
#include "base/dynamictype.h"
2013-03-16 21:18:53 +01:00
#include "base/objectlock.h"
#include "base/logger_fwd.h"
#include "base/timer.h"
#include "base/utility.h"
#include "base/exception.h"
#include "base/context.h"
2013-03-17 20:19:29 +01:00
#include "config/configitembuilder.h"
2013-03-16 21:18:53 +01:00
#include <boost/foreach.hpp>
using namespace icinga;
boost::signals2::signal<void (const Notification::Ptr&, const Service::Ptr&, const std::set<User::Ptr>&,
const NotificationType&, const CheckResult::Ptr&, const String&, const String&)> Service::OnNotificationSentToAllUsers;
boost::signals2::signal<void (const Notification::Ptr&, const Service::Ptr&, const std::set<User::Ptr>&,
const NotificationType&, const CheckResult::Ptr&, const String&, const String&)> Service::OnNotificationSendStart;
boost::signals2::signal<void (const Notification::Ptr&, const Service::Ptr&, const User::Ptr&,
const NotificationType&, const CheckResult::Ptr&, const String&, const String&, const String&)> Service::OnNotificationSentToUser;
2013-07-18 17:04:09 +02:00
void Service::ResetNotificationNumbers(void)
{
BOOST_FOREACH(const Notification::Ptr& notification, GetNotifications()) {
ObjectLock olock(notification);
notification->ResetNotificationNumber();
}
}
void Service::SendNotifications(NotificationType type, const CheckResult::Ptr& cr, const String& author, const String& text)
{
CONTEXT("Sending notifications for service '" + GetShortName() + "' on host '" + GetHost()->GetName() + "'");
bool force = GetForceNextNotification();
2013-10-08 11:57:35 +02:00
if (!IcingaApplication::GetInstance()->GetEnableNotifications() || !GetEnableNotifications()) {
if (!force) {
Log(LogInformation, "icinga", "Notifications are disabled for service '" + GetName() + "'.");
return;
}
SetForceNextNotification(false);
}
2013-03-16 21:18:53 +01:00
Log(LogInformation, "icinga", "Sending notifications for service '" + 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())
2013-03-16 21:18:53 +01:00
Log(LogInformation, "icinga", "Service '" + GetName() + "' does not have any notifications.");
Log(LogDebug, "icinga", "Service '" + GetName() + "' has " + 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();
2013-03-16 21:18:53 +01:00
Log(LogWarning, "icinga", message);
2013-02-24 08:26:10 +01:00
}
}
}
std::set<Notification::Ptr> Service::GetNotifications(void) const
2013-02-27 15:23:25 +01:00
{
return m_Notifications;
2013-02-27 15:23:25 +01:00
}
void Service::AddNotification(const Notification::Ptr& notification)
{
m_Notifications.insert(notification);
}
void Service::RemoveNotification(const Notification::Ptr& notification)
{
m_Notifications.erase(notification);
}
2013-03-04 15:52:42 +01:00
void Service::UpdateSlaveNotifications(void)
{
/* Service notification descs */
Dictionary::Ptr descs = GetNotificationDescriptions();
if (!descs || descs->GetLength() == 0)
2013-03-04 15:52:42 +01:00
return;
2013-02-26 10:13:54 +01:00
ConfigItem::Ptr item = ConfigItem::GetObject("Service", GetName());
ObjectLock olock(descs);
2013-03-01 12:07:52 +01:00
BOOST_FOREACH(const Dictionary::Pair& kv, descs) {
std::ostringstream namebuf;
namebuf << GetName() << "!" << kv.first;
String name = namebuf.str();
2013-02-19 23:02:08 +01:00
std::vector<String> path;
path.push_back("notifications");
path.push_back(kv.first);
2013-02-19 23:02:08 +01:00
ExpressionList::Ptr exprl;
{
ObjectLock ilock(item);
exprl = item->GetLinkedExpressionList();
}
DebugInfo di;
exprl->FindDebugInfoPath(path, di);
if (di.Path.IsEmpty())
di = item->GetDebugInfo();
2013-02-26 10:13:54 +01:00
ConfigItemBuilder::Ptr builder = make_shared<ConfigItemBuilder>(di);
builder->SetType("Notification");
builder->SetName(name);
2013-09-25 09:19:25 +02:00
builder->AddExpression("host", OperatorSet, GetHost()->GetName());
builder->AddExpression("service", OperatorSet, GetShortName());
Dictionary::Ptr notification = kv.second;
2013-09-24 13:13:14 +02:00
Array::Ptr templates = notification->Get("templates");
2013-09-24 13:13:14 +02:00
if (templates) {
ObjectLock tlock(templates);
BOOST_FOREACH(const Value& tmpl, templates) {
builder->AddParent(tmpl);
}
}
/* Clone attributes from the notification expression list. */
ExpressionList::Ptr nfc_exprl = make_shared<ExpressionList>();
exprl->ExtractPath(path, nfc_exprl);
builder->AddExpressionList(nfc_exprl);
ConfigItem::Ptr notificationItem = builder->Compile();
notificationItem->Register();
DynamicObject::Ptr dobj = notificationItem->Commit();
dobj->OnConfigLoaded();
}
}
bool Service::GetEnableNotifications(void) const
{
if (!GetOverrideEnableNotifications().IsEmpty())
return GetOverrideEnableNotifications();
else
return GetEnableNotificationsRaw();
}
void Service::SetEnableNotifications(bool enabled, const String& authority)
{
SetOverrideEnableNotifications(enabled);
2013-11-10 16:53:57 +01:00
OnEnableNotificationsChanged(GetSelf(), enabled, authority);
}
bool Service::GetForceNextNotification(void) const
{
2013-10-26 09:41:45 +02:00
return GetForceNextNotificationRaw();
}
void Service::SetForceNextNotification(bool forced, const String& authority)
{
2013-10-26 09:41:45 +02:00
SetForceNextNotificationRaw(forced);
2013-11-10 16:53:57 +01:00
OnForceNextNotificationChanged(GetSelf(), forced, authority);
}