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