2013-02-09 15:20:10 +01:00
|
|
|
/******************************************************************************
|
|
|
|
* Icinga 2 *
|
|
|
|
* Copyright (C) 2012 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-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-03-17 20:19:29 +01:00
|
|
|
#include "config/configitembuilder.h"
|
2013-03-15 18:21:29 +01:00
|
|
|
#include <boost/tuple/tuple.hpp>
|
2013-03-16 21:18:53 +01:00
|
|
|
#include <boost/smart_ptr/make_shared.hpp>
|
|
|
|
#include <boost/foreach.hpp>
|
|
|
|
#include <boost/exception/diagnostic_information.hpp>
|
2013-02-09 15:20:10 +01:00
|
|
|
|
|
|
|
using namespace icinga;
|
|
|
|
|
2013-08-20 11:06:04 +02:00
|
|
|
boost::signals2::signal<void (const Service::Ptr&, const User::Ptr&, const NotificationType&, const Dictionary::Ptr&, const String&, const String&)> Service::OnNotificationSentChanged;
|
2013-02-09 15:20:10 +01:00
|
|
|
|
2013-08-20 11:06:04 +02:00
|
|
|
Dictionary::Ptr Service::GetNotificationDescriptions(void) const
|
|
|
|
{
|
|
|
|
return m_NotificationDescriptions;
|
|
|
|
}
|
2013-07-01 17:56:21 +02: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-07-01 11:17:58 +02:00
|
|
|
void Service::SendNotifications(NotificationType type, const Dictionary::Ptr& cr, const String& author, const String& text)
|
2013-02-09 15:20:10 +01:00
|
|
|
{
|
2013-03-21 13:42:46 +01:00
|
|
|
bool force = false;
|
|
|
|
|
2013-03-02 09:07:47 +01:00
|
|
|
if (!GetEnableNotifications()) {
|
2013-03-21 13:22:26 +01:00
|
|
|
if (!GetForceNextNotification()) {
|
|
|
|
Log(LogInformation, "icinga", "Notifications are disabled for service '" + GetName() + "'.");
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2013-03-21 13:42:46 +01:00
|
|
|
force = true;
|
2013-03-21 13:22:26 +01:00
|
|
|
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
|
|
|
|
|
|
|
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-03-16 21:18:53 +01:00
|
|
|
<< GetName() << "': " << boost::diagnostic_information(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-03-19 15:15:57 +01:00
|
|
|
ConfigItem::Ptr serviceItem, hostItem;
|
2013-02-09 15:20:10 +01:00
|
|
|
|
2013-03-19 15:15:57 +01:00
|
|
|
serviceItem = ConfigItem::GetObject("Service", GetName());
|
2013-02-26 10:13:54 +01:00
|
|
|
|
2013-03-11 12:04:10 +01:00
|
|
|
/* Don't create slave notifications unless we own this object */
|
2013-03-19 15:15:57 +01:00
|
|
|
if (!serviceItem)
|
|
|
|
return;
|
|
|
|
|
|
|
|
Host::Ptr host = GetHost();
|
|
|
|
|
|
|
|
if (!host)
|
|
|
|
return;
|
|
|
|
|
|
|
|
hostItem = ConfigItem::GetObject("Host", host->GetName());
|
|
|
|
|
|
|
|
/* Don't create slave notifications unless we own the host */
|
|
|
|
if (!hostItem)
|
2013-03-04 15:52:42 +01:00
|
|
|
return;
|
2013-02-26 10:13:54 +01:00
|
|
|
|
2013-03-19 15:15:57 +01:00
|
|
|
std::vector<Dictionary::Ptr> descLists;
|
|
|
|
|
2013-08-20 11:06:04 +02:00
|
|
|
descLists.push_back(GetNotificationDescriptions());
|
|
|
|
descLists.push_back(host->GetNotificationDescriptions());
|
2013-03-01 12:07:52 +01:00
|
|
|
|
2013-03-19 15:15:57 +01:00
|
|
|
for (int i = 0; i < 2; i++) {
|
|
|
|
Dictionary::Ptr descs;
|
|
|
|
ConfigItem::Ptr item;
|
2013-02-19 23:02:08 +01:00
|
|
|
|
2013-03-19 15:15:57 +01:00
|
|
|
if (i == 0) {
|
|
|
|
/* Host notification descs */
|
2013-08-20 11:06:04 +02:00
|
|
|
descs = host->GetNotificationDescriptions();
|
2013-03-19 15:15:57 +01:00
|
|
|
item = hostItem;
|
|
|
|
} else {
|
|
|
|
/* Service notification descs */
|
2013-08-20 11:06:04 +02:00
|
|
|
descs = GetNotificationDescriptions();
|
2013-03-19 15:15:57 +01:00
|
|
|
item = serviceItem;
|
|
|
|
}
|
2013-02-19 23:02:08 +01:00
|
|
|
|
2013-03-19 15:15:57 +01:00
|
|
|
if (!descs)
|
2013-02-09 15:20:10 +01:00
|
|
|
continue;
|
|
|
|
|
2013-03-19 15:15:57 +01:00
|
|
|
ObjectLock olock(descs);
|
2013-02-26 10:13:54 +01:00
|
|
|
|
2013-02-09 15:20:10 +01:00
|
|
|
String nfcname;
|
|
|
|
Value nfcdesc;
|
2013-03-19 15:15:57 +01:00
|
|
|
BOOST_FOREACH(boost::tie(nfcname, nfcdesc), descs) {
|
2013-03-16 21:18:53 +01:00
|
|
|
std::ostringstream namebuf;
|
2013-05-07 09:22:39 +02:00
|
|
|
namebuf << GetName() << ":" << nfcname;
|
2013-02-09 15:20:10 +01:00
|
|
|
String name = namebuf.str();
|
|
|
|
|
2013-03-04 15:52:42 +01:00
|
|
|
ConfigItemBuilder::Ptr builder = boost::make_shared<ConfigItemBuilder>(item->GetDebugInfo());
|
2013-02-09 15:20:10 +01:00
|
|
|
builder->SetType("Notification");
|
|
|
|
builder->SetName(name);
|
2013-03-04 15:52:42 +01:00
|
|
|
builder->AddExpression("host_name", OperatorSet, host->GetName());
|
|
|
|
builder->AddExpression("service", OperatorSet, GetShortName());
|
2013-02-09 15:20:10 +01:00
|
|
|
|
2013-03-14 12:17:46 +01:00
|
|
|
if (!nfcdesc.IsObjectType<Dictionary>())
|
2013-03-16 21:18:53 +01:00
|
|
|
BOOST_THROW_EXCEPTION(std::invalid_argument("Notification description must be a dictionary."));
|
2013-02-09 15:20:10 +01:00
|
|
|
|
2013-03-14 12:17:46 +01:00
|
|
|
Dictionary::Ptr notification = nfcdesc;
|
2013-02-16 09:28:05 +01:00
|
|
|
|
2013-03-14 12:17:46 +01:00
|
|
|
Array::Ptr templates = notification->Get("templates");
|
2013-03-01 12:07:52 +01:00
|
|
|
|
2013-03-14 12:17:46 +01:00
|
|
|
if (templates) {
|
|
|
|
ObjectLock tlock(templates);
|
2013-02-09 15:20:10 +01:00
|
|
|
|
2013-03-14 12:17:46 +01:00
|
|
|
BOOST_FOREACH(const Value& tmpl, templates) {
|
|
|
|
builder->AddParent(tmpl);
|
|
|
|
}
|
2013-02-09 15:20:10 +01:00
|
|
|
}
|
|
|
|
|
2013-03-20 10:08:27 +01:00
|
|
|
/* Clone attributes from the host/service object. */
|
2013-03-19 14:47:19 +01:00
|
|
|
std::set<String, string_iless> keys;
|
|
|
|
keys.insert("users");
|
|
|
|
keys.insert("groups");
|
|
|
|
keys.insert("notification_interval");
|
2013-03-21 13:42:46 +01:00
|
|
|
keys.insert("notification_period");
|
2013-06-26 09:50:04 +02:00
|
|
|
keys.insert("notification_type_filter");
|
|
|
|
keys.insert("notification_state_filter");
|
2013-03-22 14:40:55 +01:00
|
|
|
keys.insert("export_macros");
|
2013-03-19 14:47:19 +01:00
|
|
|
|
|
|
|
ExpressionList::Ptr svc_exprl = boost::make_shared<ExpressionList>();
|
|
|
|
item->GetLinkedExpressionList()->ExtractFiltered(keys, svc_exprl);
|
|
|
|
builder->AddExpressionList(svc_exprl);
|
|
|
|
|
|
|
|
/* Clone attributes from the notification expression list. */
|
|
|
|
std::vector<String> path;
|
|
|
|
path.push_back("notifications");
|
|
|
|
path.push_back(nfcname);
|
|
|
|
|
|
|
|
ExpressionList::Ptr nfc_exprl = boost::make_shared<ExpressionList>();
|
|
|
|
item->GetLinkedExpressionList()->ExtractPath(path, nfc_exprl);
|
|
|
|
builder->AddExpressionList(nfc_exprl);
|
2013-03-14 12:17:46 +01:00
|
|
|
|
2013-02-09 15:20:10 +01:00
|
|
|
ConfigItem::Ptr notificationItem = builder->Compile();
|
2013-08-28 11:12:20 +02:00
|
|
|
DynamicObject::Ptr dobj = notificationItem->Commit();
|
|
|
|
dobj->Start();
|
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-02-26 12:37:25 +01:00
|
|
|
if (m_EnableNotifications.IsEmpty())
|
|
|
|
return true;
|
|
|
|
else
|
|
|
|
return m_EnableNotifications;
|
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
|
|
|
{
|
2013-02-26 12:37:25 +01:00
|
|
|
m_EnableNotifications = enabled;
|
2013-08-29 13:06:36 +02:00
|
|
|
|
|
|
|
Utility::QueueAsyncCallback(bind(boost::ref(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
|
|
|
|
{
|
|
|
|
if (m_ForceNextNotification.IsEmpty())
|
|
|
|
return false;
|
|
|
|
|
|
|
|
return static_cast<bool>(m_ForceNextNotification);
|
|
|
|
}
|
|
|
|
|
2013-08-29 11:37:51 +02:00
|
|
|
void Service::SetForceNextNotification(bool forced, const String& authority)
|
2013-03-21 13:22:26 +01:00
|
|
|
{
|
|
|
|
m_ForceNextNotification = forced ? 1 : 0;
|
2013-08-29 11:37:51 +02:00
|
|
|
|
|
|
|
Utility::QueueAsyncCallback(bind(boost::ref(OnForceNextNotificationChanged), GetSelf(), forced, authority));
|
2013-03-21 13:22:26 +01:00
|
|
|
}
|