icinga2/lib/icinga/service-notification.cpp

339 lines
9.2 KiB
C++
Raw Normal View History

/******************************************************************************
* 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;
boost::mutex Service::m_NotificationMutex;
map<String, set<Notification::WeakPtr> > Service::m_NotificationsCache;
2013-02-27 15:23:25 +01:00
bool Service::m_NotificationsCacheValid = true;
void Service::RequestNotifications(NotificationType type)
{
RequestMessage msg;
msg.SetMethod("icinga::SendNotifications");
NotificationRequestMessage params;
msg.SetParams(params);
params.SetService(GetName());
params.SetType(type);
2013-02-10 12:45:39 +01:00
Logger::Write(LogDebug, "icinga", "Sending notification anycast request for service '" + GetName() + "'");
EndpointManager::GetInstance()->SendAnycastMessage(Endpoint::Ptr(), msg);
SetLastNotification(Utility::GetTime());
}
void Service::SendNotifications(const Service::Ptr& self, NotificationType type)
{
String service_name;
bool enable_notifications;
{
ObjectLock olock(self);
service_name = self->GetName();
enable_notifications = self->GetEnableNotifications();
}
if (!enable_notifications) {
Logger::Write(LogInformation, "icinga", "Notifications are disabled for service '" + service_name + "'.");
return;
}
Logger::Write(LogInformation, "icinga", "Sending notifications for service '" + service_name + "'");
set<Notification::Ptr> notifications = GetNotifications(self);
if (notifications.size() == 0)
Logger::Write(LogInformation, "icinga", "Service '" + service_name + "' does not have any notifications.");
BOOST_FOREACH(const Notification::Ptr& notification, notifications) {
2013-02-24 08:26:10 +01:00
try {
Notification::BeginExecuteNotification(notification, type);
} catch (const exception& ex) {
stringstream msgbuf;
msgbuf << "Exception occured during notification for service '"
<< service_name << "': " << diagnostic_information(ex);
2013-02-24 08:26:10 +01:00
String message = msgbuf.str();
Logger::Write(LogWarning, "icinga", message);
}
}
}
2013-02-27 15:23:25 +01:00
void Service::InvalidateNotificationsCache(void)
{
{
boost::mutex::scoped_lock lock(m_NotificationMutex);
2013-02-27 16:04:49 +01:00
if (m_NotificationsCacheValid)
Utility::QueueAsyncCallback(boost::bind(&Service::RefreshNotificationsCache));
2013-02-27 15:23:25 +01:00
m_NotificationsCacheValid = false;
}
}
void Service::RefreshNotificationsCache(void)
{
2013-02-27 15:23:25 +01:00
{
boost::mutex::scoped_lock lock(m_NotificationMutex);
if (m_NotificationsCacheValid)
return;
m_NotificationsCacheValid = true;
}
map<String, set<Notification::WeakPtr> > newNotificationsCache;
2013-02-18 23:44:24 +01:00
BOOST_FOREACH(const DynamicObject::Ptr& object, DynamicType::GetObjects("Notification")) {
const Notification::Ptr& notification = static_pointer_cast<Notification>(object);
Service::Ptr service;
{
ObjectLock olock(notification);
service = notification->GetService();
}
String service_name;
{
ObjectLock olock(service);
service_name = service->GetName();
}
newNotificationsCache[service_name].insert(notification);
}
boost::mutex::scoped_lock lock(m_NotificationMutex);
m_NotificationsCache.swap(newNotificationsCache);
}
set<Notification::Ptr> Service::GetNotifications(const Service::Ptr& self)
{
String name;
{
ObjectLock olock(self);
name = self->GetName();
}
set<Notification::Ptr> notifications;
{
boost::mutex::scoped_lock lock(m_NotificationMutex);
BOOST_FOREACH(const Notification::WeakPtr& wservice, m_NotificationsCache[name]) {
Notification::Ptr notification = wservice.lock();
if (!notification)
continue;
notifications.insert(notification);
}
}
return notifications;
}
template<typename TDict>
static void CopyNotificationAttributes(TDict notificationDesc, const ConfigItemBuilder::Ptr& builder)
{
2013-03-01 12:07:52 +01:00
ObjectLock olock(notificationDesc);
/* 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);
/*Value notificationInterval = notificationDesc->Get("notification_interval");
if (!notificationInterval.IsEmpty())
builder->AddExpression("notification_interval", OperatorSet, notificationInterval);*/
}
2013-02-26 10:13:54 +01:00
void Service::UpdateSlaveNotifications(const Service::Ptr& self)
{
2013-02-26 10:13:54 +01:00
Dictionary::Ptr oldNotifications;
Host::Ptr host;
vector<Dictionary::Ptr> notificationDescsList;
2013-02-27 15:23:25 +01:00
String service_name, short_name;
2013-02-26 10:13:54 +01:00
ConfigItem::Ptr item;
2013-02-26 10:13:54 +01:00
{
ObjectLock olock(self);
item = ConfigItem::GetObject("Service", self->GetName());
/* Don't create slave notifications unless we own this object
* and it's not a template. */
if (!item || self->IsAbstract())
return;
2013-02-26 10:13:54 +01:00
service_name = self->GetName();
2013-02-27 15:23:25 +01:00
short_name = self->GetShortName();
2013-02-26 10:13:54 +01:00
oldNotifications = self->m_SlaveNotifications;
host = self->GetHost();
notificationDescsList.push_back(self->Get("notifications"));
}
DebugInfo debug_info;
{
ObjectLock ilock(item);
debug_info = item->GetDebugInfo();
}
Dictionary::Ptr newNotifications;
newNotifications = boost::make_shared<Dictionary>();
2013-03-01 12:07:52 +01:00
ObjectLock nlock(newNotifications);
2013-02-19 23:02:08 +01:00
String host_name;
{
ObjectLock olock(host);
notificationDescsList.push_back(host->Get("notifications"));
host_name = host->GetName();
}
BOOST_FOREACH(const Dictionary::Ptr& notificationDescs, notificationDescsList) {
if (!notificationDescs)
continue;
2013-02-26 10:13:54 +01:00
ObjectLock olock(notificationDescs);
String nfcname;
Value nfcdesc;
BOOST_FOREACH(tie(nfcname, nfcdesc), notificationDescs) {
if (nfcdesc.IsScalar())
nfcname = nfcdesc;
stringstream namebuf;
2013-02-26 10:13:54 +01:00
namebuf << service_name << "-" << nfcname;
String name = namebuf.str();
2013-02-26 10:13:54 +01:00
ConfigItemBuilder::Ptr builder = boost::make_shared<ConfigItemBuilder>(debug_info);
builder->SetType("Notification");
builder->SetName(name);
2013-02-19 23:02:08 +01:00
builder->AddExpression("host_name", OperatorSet, host_name);
2013-02-27 15:23:25 +01:00
builder->AddExpression("service", OperatorSet, short_name);
2013-02-26 10:13:54 +01:00
CopyNotificationAttributes(self, builder);
if (nfcdesc.IsScalar()) {
builder->AddParent(nfcdesc);
} else if (nfcdesc.IsObjectType<Dictionary>()) {
Dictionary::Ptr notification = nfcdesc;
2013-02-26 10:13:54 +01:00
ObjectLock nlock(notification);
Dictionary::Ptr templates = notification->Get("templates");
if (templates) {
2013-03-01 12:07:52 +01:00
ObjectLock tlock(templates);
String tmpl;
BOOST_FOREACH(tie(tuples::ignore, tmpl), templates) {
builder->AddParent(tmpl);
}
} else {
builder->AddParent(nfcname);
}
CopyNotificationAttributes(notification, builder);
} else {
BOOST_THROW_EXCEPTION(invalid_argument("Notification description must be either a string or a dictionary."));
}
ConfigItem::Ptr notificationItem = builder->Compile();
2013-02-20 19:52:25 +01:00
ConfigItem::Commit(notificationItem);
newNotifications->Set(name, notificationItem);
}
}
if (oldNotifications) {
2013-03-01 12:07:52 +01:00
ObjectLock olock(oldNotifications);
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
{
ObjectLock olock(self);
self->m_SlaveNotifications = newNotifications;
}
}
double Service::GetLastNotification(void) const
{
if (m_LastNotification.IsEmpty())
return 0;
else
return m_LastNotification;
}
void Service::SetLastNotification(double time)
{
2013-02-26 10:13:54 +01:00
m_LastNotification = time;
Touch("last_notification");
}
bool Service::GetEnableNotifications(void) const
{
if (m_EnableNotifications.IsEmpty())
return true;
else
return m_EnableNotifications;
}
void Service::SetEnableNotifications(bool enabled)
{
m_EnableNotifications = enabled;
Touch("enable_notifications");
}
double Service::GetNotificationInterval(void) const
{
if (m_NotificationInterval.IsEmpty())
return 300;
else
return m_NotificationInterval;
}