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. *
|
|
|
|
******************************************************************************/
|
|
|
|
|
|
|
|
#include "i2-icinga.h"
|
|
|
|
|
|
|
|
using namespace icinga;
|
|
|
|
|
2013-02-27 12:44:51 +01:00
|
|
|
boost::mutex Service::m_NotificationMutex;
|
2013-02-09 15:20:10 +01:00
|
|
|
map<String, set<Notification::WeakPtr> > Service::m_NotificationsCache;
|
2013-03-06 11:03:50 +01:00
|
|
|
bool Service::m_NotificationsCacheNeedsUpdate = false;
|
|
|
|
Timer::Ptr Service::m_NotificationsCacheTimer;
|
2013-02-09 15:20:10 +01:00
|
|
|
|
2013-03-02 09:07:47 +01:00
|
|
|
/**
|
|
|
|
* @threadsafety Always.
|
|
|
|
*/
|
2013-03-07 12:04:20 +01:00
|
|
|
void Service::RequestNotifications(NotificationType type, const Dictionary::Ptr& cr)
|
2013-02-09 15:20:10 +01:00
|
|
|
{
|
2013-03-06 11:03:50 +01:00
|
|
|
{
|
|
|
|
ObjectLock olock(this);
|
|
|
|
SetLastNotification(Utility::GetTime());
|
|
|
|
}
|
2013-03-02 09:07:47 +01:00
|
|
|
|
2013-02-09 15:20:10 +01:00
|
|
|
RequestMessage msg;
|
|
|
|
msg.SetMethod("icinga::SendNotifications");
|
|
|
|
|
|
|
|
NotificationRequestMessage params;
|
|
|
|
msg.SetParams(params);
|
|
|
|
|
|
|
|
params.SetService(GetName());
|
|
|
|
params.SetType(type);
|
2013-03-07 12:04:20 +01:00
|
|
|
params.SetCheckResult(cr);
|
2013-02-09 15:20:10 +01:00
|
|
|
|
2013-02-10 12:45:39 +01:00
|
|
|
Logger::Write(LogDebug, "icinga", "Sending notification anycast request for service '" + GetName() + "'");
|
2013-02-09 15:20:10 +01:00
|
|
|
EndpointManager::GetInstance()->SendAnycastMessage(Endpoint::Ptr(), msg);
|
|
|
|
}
|
|
|
|
|
2013-03-02 09:07:47 +01:00
|
|
|
/**
|
|
|
|
* @threadsafety Always.
|
|
|
|
*/
|
2013-03-07 12:04:20 +01:00
|
|
|
void Service::SendNotifications(NotificationType type, const Dictionary::Ptr& cr)
|
2013-02-09 15:20:10 +01:00
|
|
|
{
|
2013-03-02 09:07:47 +01:00
|
|
|
if (!GetEnableNotifications()) {
|
|
|
|
Logger::Write(LogInformation, "icinga", "Notifications are disabled for service '" + GetName() + "'.");
|
2013-02-26 12:37:25 +01:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2013-03-02 09:07:47 +01:00
|
|
|
Logger::Write(LogInformation, "icinga", "Sending notifications for service '" + GetName() + "'");
|
2013-02-09 15:20:10 +01:00
|
|
|
|
2013-03-02 09:07:47 +01:00
|
|
|
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-02 09:07:47 +01:00
|
|
|
Logger::Write(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-03-07 12:04:20 +01:00
|
|
|
notification->BeginExecuteNotification(type, cr);
|
2013-02-24 08:26:10 +01:00
|
|
|
} catch (const exception& ex) {
|
|
|
|
stringstream msgbuf;
|
|
|
|
msgbuf << "Exception occured during notification for service '"
|
2013-03-02 09:07:47 +01:00
|
|
|
<< GetName() << "': " << diagnostic_information(ex);
|
2013-02-24 08:26:10 +01:00
|
|
|
String message = msgbuf.str();
|
|
|
|
|
|
|
|
Logger::Write(LogWarning, "icinga", message);
|
|
|
|
}
|
2013-02-09 15:20:10 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-03-02 09:07:47 +01:00
|
|
|
/**
|
|
|
|
* @threadsafety Always.
|
|
|
|
*/
|
2013-02-27 15:23:25 +01:00
|
|
|
void Service::InvalidateNotificationsCache(void)
|
|
|
|
{
|
2013-03-02 09:07:47 +01:00
|
|
|
boost::mutex::scoped_lock lock(m_NotificationMutex);
|
2013-02-27 16:04:49 +01:00
|
|
|
|
2013-03-06 11:03:50 +01:00
|
|
|
if (m_NotificationsCacheNeedsUpdate)
|
|
|
|
return; /* Someone else has already requested a refresh. */
|
2013-02-27 16:04:49 +01:00
|
|
|
|
2013-03-06 11:03:50 +01:00
|
|
|
if (!m_NotificationsCacheTimer) {
|
|
|
|
m_NotificationsCacheTimer = boost::make_shared<Timer>();
|
|
|
|
m_NotificationsCacheTimer->SetInterval(0.5);
|
|
|
|
m_NotificationsCacheTimer->OnTimerExpired.connect(boost::bind(&Service::RefreshNotificationsCache));
|
2013-03-06 13:01:51 +01:00
|
|
|
m_NotificationsCacheTimer->Start();
|
2013-03-06 11:03:50 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
m_NotificationsCacheNeedsUpdate = true;
|
2013-02-27 15:23:25 +01:00
|
|
|
}
|
|
|
|
|
2013-03-02 09:07:47 +01:00
|
|
|
/**
|
|
|
|
* @threadsafety Always.
|
|
|
|
*/
|
2013-02-27 12:44:51 +01:00
|
|
|
void Service::RefreshNotificationsCache(void)
|
2013-02-09 15:20:10 +01:00
|
|
|
{
|
2013-02-27 15:23:25 +01:00
|
|
|
{
|
|
|
|
boost::mutex::scoped_lock lock(m_NotificationMutex);
|
|
|
|
|
2013-03-06 13:01:51 +01:00
|
|
|
if (!m_NotificationsCacheNeedsUpdate)
|
|
|
|
return;
|
|
|
|
|
2013-03-06 11:03:50 +01:00
|
|
|
m_NotificationsCacheNeedsUpdate = false;
|
2013-02-27 15:23:25 +01:00
|
|
|
}
|
|
|
|
|
2013-03-15 11:18:56 +01:00
|
|
|
Logger::Write(LogDebug, "icinga", "Updating Service notifications cache.");
|
2013-03-06 13:01:51 +01:00
|
|
|
|
2013-02-27 12:44:51 +01:00
|
|
|
map<String, set<Notification::WeakPtr> > newNotificationsCache;
|
2013-02-09 15:20:10 +01:00
|
|
|
|
2013-02-18 23:44:24 +01:00
|
|
|
BOOST_FOREACH(const DynamicObject::Ptr& object, DynamicType::GetObjects("Notification")) {
|
2013-02-09 15:20:10 +01:00
|
|
|
const Notification::Ptr& notification = static_pointer_cast<Notification>(object);
|
|
|
|
|
2013-03-02 09:07:47 +01:00
|
|
|
Service::Ptr service = notification->GetService();
|
2013-02-27 12:44:51 +01:00
|
|
|
|
2013-03-02 09:07:47 +01:00
|
|
|
if (!service)
|
|
|
|
continue;
|
2013-02-27 12:44:51 +01:00
|
|
|
|
2013-03-02 09:07:47 +01:00
|
|
|
newNotificationsCache[service->GetName()].insert(notification);
|
2013-02-09 15:20:10 +01:00
|
|
|
}
|
|
|
|
|
2013-02-27 12:44:51 +01:00
|
|
|
boost::mutex::scoped_lock lock(m_NotificationMutex);
|
|
|
|
m_NotificationsCache.swap(newNotificationsCache);
|
2013-02-09 15:20:10 +01:00
|
|
|
}
|
|
|
|
|
2013-03-02 09:07:47 +01:00
|
|
|
/**
|
|
|
|
* @threadsafety Always.
|
|
|
|
*/
|
|
|
|
set<Notification::Ptr> Service::GetNotifications(void) const
|
2013-02-09 15:20:10 +01:00
|
|
|
{
|
|
|
|
set<Notification::Ptr> notifications;
|
|
|
|
|
2013-02-27 12:44:51 +01:00
|
|
|
{
|
|
|
|
boost::mutex::scoped_lock lock(m_NotificationMutex);
|
2013-02-09 15:20:10 +01:00
|
|
|
|
2013-03-02 09:07:47 +01:00
|
|
|
BOOST_FOREACH(const Notification::WeakPtr& wservice, m_NotificationsCache[GetName()]) {
|
2013-02-27 12:44:51 +01:00
|
|
|
Notification::Ptr notification = wservice.lock();
|
2013-02-09 15:20:10 +01:00
|
|
|
|
2013-02-27 12:44:51 +01:00
|
|
|
if (!notification)
|
|
|
|
continue;
|
2013-02-09 15:20:10 +01:00
|
|
|
|
2013-02-27 12:44:51 +01:00
|
|
|
notifications.insert(notification);
|
|
|
|
}
|
2013-02-09 15:20:10 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
return notifications;
|
|
|
|
}
|
|
|
|
|
2013-03-02 09:07:47 +01:00
|
|
|
/**
|
|
|
|
* @threadsafety Always.
|
|
|
|
*/
|
2013-02-09 15:20:10 +01:00
|
|
|
template<typename TDict>
|
|
|
|
static void CopyNotificationAttributes(TDict notificationDesc, const ConfigItemBuilder::Ptr& builder)
|
|
|
|
{
|
|
|
|
/* TODO: we only need to copy macros if this is an inline definition,
|
|
|
|
* i.e. "typeid(notificationDesc)" != Notification, however for now we just
|
|
|
|
* copy them anyway. */
|
|
|
|
Value macros = notificationDesc->Get("macros");
|
|
|
|
if (!macros.IsEmpty())
|
|
|
|
builder->AddExpression("macros", OperatorPlus, macros);
|
|
|
|
|
2013-02-24 08:26:10 +01:00
|
|
|
Value users = notificationDesc->Get("users");
|
|
|
|
if (!users.IsEmpty())
|
|
|
|
builder->AddExpression("users", OperatorPlus, users);
|
|
|
|
|
2013-02-27 21:49:03 +01:00
|
|
|
Value groups = notificationDesc->Get("groups");
|
|
|
|
if (!groups.IsEmpty())
|
|
|
|
builder->AddExpression("groups", OperatorPlus, groups);
|
|
|
|
|
2013-02-09 15:20:10 +01:00
|
|
|
/*Value notificationInterval = notificationDesc->Get("notification_interval");
|
|
|
|
if (!notificationInterval.IsEmpty())
|
|
|
|
builder->AddExpression("notification_interval", OperatorSet, notificationInterval);*/
|
|
|
|
}
|
|
|
|
|
2013-03-04 15:52:42 +01:00
|
|
|
void Service::UpdateSlaveNotifications(void)
|
2013-02-09 15:20:10 +01:00
|
|
|
{
|
2013-02-26 10:13:54 +01:00
|
|
|
Dictionary::Ptr oldNotifications;
|
|
|
|
vector<Dictionary::Ptr> notificationDescsList;
|
|
|
|
ConfigItem::Ptr item;
|
2013-02-09 15:20:10 +01:00
|
|
|
|
2013-03-04 15:52:42 +01:00
|
|
|
item = 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 */
|
|
|
|
if (!item)
|
2013-03-04 15:52:42 +01:00
|
|
|
return;
|
2013-02-26 10:13:54 +01:00
|
|
|
|
|
|
|
{
|
2013-03-04 15:52:42 +01:00
|
|
|
ObjectLock olock(this);
|
|
|
|
oldNotifications = m_SlaveNotifications;
|
2013-02-26 10:13:54 +01:00
|
|
|
}
|
2013-02-09 15:20:10 +01:00
|
|
|
|
2013-03-04 15:52:42 +01:00
|
|
|
notificationDescsList.push_back(Get("notifications"));
|
|
|
|
|
2013-02-09 15:20:10 +01:00
|
|
|
Dictionary::Ptr newNotifications;
|
|
|
|
newNotifications = boost::make_shared<Dictionary>();
|
|
|
|
|
2013-03-04 15:52:42 +01:00
|
|
|
Host::Ptr host = GetHost();
|
2013-03-01 12:07:52 +01:00
|
|
|
|
2013-03-04 15:52:42 +01:00
|
|
|
if (!host)
|
|
|
|
return;
|
2013-02-19 23:02:08 +01:00
|
|
|
|
2013-03-04 15:52:42 +01:00
|
|
|
notificationDescsList.push_back(host->Get("notifications"));
|
2013-02-19 23:02:08 +01:00
|
|
|
|
2013-02-09 15:20:10 +01:00
|
|
|
BOOST_FOREACH(const Dictionary::Ptr& notificationDescs, notificationDescsList) {
|
|
|
|
if (!notificationDescs)
|
|
|
|
continue;
|
|
|
|
|
2013-02-26 10:13:54 +01:00
|
|
|
ObjectLock olock(notificationDescs);
|
|
|
|
|
2013-02-09 15:20:10 +01:00
|
|
|
String nfcname;
|
|
|
|
Value nfcdesc;
|
|
|
|
BOOST_FOREACH(tie(nfcname, nfcdesc), notificationDescs) {
|
|
|
|
stringstream namebuf;
|
2013-03-04 15:52:42 +01: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-04 15:52:42 +01:00
|
|
|
CopyNotificationAttributes(this, builder);
|
2013-02-09 15:20:10 +01:00
|
|
|
|
2013-03-14 12:17:46 +01:00
|
|
|
if (!nfcdesc.IsObjectType<Dictionary>())
|
|
|
|
BOOST_THROW_EXCEPTION(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-14 12:17:46 +01:00
|
|
|
CopyNotificationAttributes(notification, builder);
|
|
|
|
|
2013-02-09 15:20:10 +01:00
|
|
|
ConfigItem::Ptr notificationItem = builder->Compile();
|
2013-03-02 09:07:47 +01:00
|
|
|
notificationItem->Commit();
|
2013-02-09 15:20:10 +01:00
|
|
|
|
|
|
|
newNotifications->Set(name, notificationItem);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (oldNotifications) {
|
2013-03-01 12:07:52 +01:00
|
|
|
ObjectLock olock(oldNotifications);
|
|
|
|
|
2013-02-09 15:20:10 +01:00
|
|
|
ConfigItem::Ptr notification;
|
|
|
|
BOOST_FOREACH(tie(tuples::ignore, notification), oldNotifications) {
|
|
|
|
if (!notification)
|
|
|
|
continue;
|
|
|
|
|
|
|
|
if (!newNotifications->Contains(notification->GetName()))
|
|
|
|
notification->Unregister();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-02-26 10:13:54 +01:00
|
|
|
{
|
2013-03-04 15:52:42 +01:00
|
|
|
ObjectLock olock(this);
|
|
|
|
m_SlaveNotifications = newNotifications;
|
2013-02-26 10:13:54 +01:00
|
|
|
}
|
2013-02-09 15:20:10 +01:00
|
|
|
}
|
2013-02-09 17:27:32 +01:00
|
|
|
|
2013-03-02 09:07:47 +01:00
|
|
|
/**
|
|
|
|
* @threadsafety Always.
|
|
|
|
*/
|
2013-02-09 17:27:32 +01:00
|
|
|
double Service::GetLastNotification(void) const
|
|
|
|
{
|
2013-02-26 12:37:25 +01:00
|
|
|
if (m_LastNotification.IsEmpty())
|
|
|
|
return 0;
|
|
|
|
else
|
|
|
|
return m_LastNotification;
|
2013-02-09 17:27:32 +01:00
|
|
|
}
|
|
|
|
|
2013-03-02 09:07:47 +01:00
|
|
|
/**
|
|
|
|
* @threadsafety Always.
|
|
|
|
*/
|
2013-02-09 17:27:32 +01:00
|
|
|
void Service::SetLastNotification(double time)
|
|
|
|
{
|
2013-02-26 10:13:54 +01:00
|
|
|
m_LastNotification = time;
|
|
|
|
Touch("last_notification");
|
2013-02-09 17:27:32 +01:00
|
|
|
}
|
|
|
|
|
2013-03-02 09:07:47 +01:00
|
|
|
/**
|
|
|
|
* @threadsafety Always.
|
|
|
|
*/
|
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-03-02 09:07:47 +01:00
|
|
|
/**
|
|
|
|
* @threadsafety Always.
|
|
|
|
*/
|
2013-02-26 12:37:25 +01:00
|
|
|
void Service::SetEnableNotifications(bool enabled)
|
2013-02-09 17:27:32 +01:00
|
|
|
{
|
2013-02-26 12:37:25 +01:00
|
|
|
m_EnableNotifications = enabled;
|
|
|
|
Touch("enable_notifications");
|
2013-02-09 17:27:32 +01:00
|
|
|
}
|
2013-02-27 12:44:51 +01:00
|
|
|
|
2013-03-02 09:07:47 +01:00
|
|
|
/**
|
|
|
|
* @threadsafety Always.
|
|
|
|
*/
|
2013-02-27 12:44:51 +01:00
|
|
|
double Service::GetNotificationInterval(void) const
|
|
|
|
{
|
|
|
|
if (m_NotificationInterval.IsEmpty())
|
|
|
|
return 300;
|
|
|
|
else
|
|
|
|
return m_NotificationInterval;
|
|
|
|
}
|