2013-02-09 11:42:22 +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-17 20:19:29 +01:00
|
|
|
#include "icinga/notification.h"
|
|
|
|
#include "icinga/macroprocessor.h"
|
|
|
|
#include "icinga/service.h"
|
2013-03-16 21:18:53 +01:00
|
|
|
#include "base/dynamictype.h"
|
|
|
|
#include "base/objectlock.h"
|
|
|
|
#include "base/logger_fwd.h"
|
2013-03-15 18:21:29 +01:00
|
|
|
#include <boost/tuple/tuple.hpp>
|
2013-03-16 21:18:53 +01:00
|
|
|
#include <boost/foreach.hpp>
|
|
|
|
#include <boost/exception/diagnostic_information.hpp>
|
2013-02-09 11:42:22 +01:00
|
|
|
|
|
|
|
using namespace icinga;
|
|
|
|
|
2013-03-01 12:07:52 +01:00
|
|
|
REGISTER_TYPE(Notification);
|
2013-02-09 11:42:22 +01:00
|
|
|
|
2013-03-14 23:52:52 +01:00
|
|
|
Notification::Notification(const Dictionary::Ptr& serializedUpdate)
|
|
|
|
: DynamicObject(serializedUpdate)
|
2013-02-09 15:20:10 +01:00
|
|
|
{
|
2013-02-26 10:13:54 +01:00
|
|
|
RegisterAttribute("notification_command", Attribute_Config, &m_NotificationCommand);
|
|
|
|
RegisterAttribute("macros", Attribute_Config, &m_Macros);
|
|
|
|
RegisterAttribute("users", Attribute_Config, &m_Users);
|
2013-02-27 21:49:03 +01:00
|
|
|
RegisterAttribute("groups", Attribute_Config, &m_Groups);
|
2013-02-26 10:13:54 +01:00
|
|
|
RegisterAttribute("host_name", Attribute_Config, &m_HostName);
|
|
|
|
RegisterAttribute("service", Attribute_Config, &m_Service);
|
2013-02-09 15:20:10 +01:00
|
|
|
}
|
2013-02-09 11:42:22 +01:00
|
|
|
|
|
|
|
Notification::~Notification(void)
|
2013-02-09 15:20:10 +01:00
|
|
|
{
|
2013-02-27 15:23:25 +01:00
|
|
|
Service::InvalidateNotificationsCache();
|
2013-02-09 11:42:22 +01:00
|
|
|
}
|
|
|
|
|
2013-03-02 09:07:47 +01:00
|
|
|
/**
|
|
|
|
* @threadsafety Always.
|
|
|
|
*/
|
2013-02-09 11:42:22 +01:00
|
|
|
Notification::Ptr Notification::GetByName(const String& name)
|
|
|
|
{
|
|
|
|
DynamicObject::Ptr configObject = DynamicObject::GetObject("Notification", name);
|
|
|
|
|
|
|
|
return dynamic_pointer_cast<Notification>(configObject);
|
|
|
|
}
|
|
|
|
|
2013-03-02 09:07:47 +01:00
|
|
|
/**
|
|
|
|
* @threadsafety Always.
|
|
|
|
*/
|
2013-02-09 11:42:22 +01:00
|
|
|
Service::Ptr Notification::GetService(void) const
|
|
|
|
{
|
2013-02-26 10:13:54 +01:00
|
|
|
Host::Ptr host = Host::GetByName(m_HostName);
|
2013-02-09 11:42:22 +01:00
|
|
|
|
2013-02-27 15:23:25 +01:00
|
|
|
if (!host)
|
|
|
|
return Service::Ptr();
|
|
|
|
|
2013-02-26 10:13:54 +01:00
|
|
|
if (m_Service.IsEmpty())
|
2013-03-02 09:07:47 +01:00
|
|
|
return host->GetHostCheckService();
|
2013-02-09 11:42:22 +01:00
|
|
|
else
|
2013-03-02 09:07:47 +01:00
|
|
|
return host->GetServiceByShortName(m_Service);
|
2013-02-09 11:42:22 +01:00
|
|
|
}
|
|
|
|
|
2013-03-02 09:07:47 +01:00
|
|
|
/**
|
|
|
|
* @threadsafety Always.
|
|
|
|
*/
|
2013-02-13 20:08:09 +01:00
|
|
|
Value Notification::GetNotificationCommand(void) const
|
2013-02-09 11:42:22 +01:00
|
|
|
{
|
2013-02-26 10:13:54 +01:00
|
|
|
return m_NotificationCommand;
|
2013-02-09 11:42:22 +01:00
|
|
|
}
|
|
|
|
|
2013-03-02 09:07:47 +01:00
|
|
|
/**
|
|
|
|
* @threadsafety Always.
|
|
|
|
*/
|
2013-02-09 11:42:22 +01:00
|
|
|
Dictionary::Ptr Notification::GetMacros(void) const
|
|
|
|
{
|
2013-02-26 10:13:54 +01:00
|
|
|
return m_Macros;
|
2013-02-09 11:42:22 +01:00
|
|
|
}
|
|
|
|
|
2013-03-02 09:07:47 +01:00
|
|
|
/**
|
|
|
|
* @threadsafety Always.
|
|
|
|
*/
|
2013-03-16 21:18:53 +01:00
|
|
|
std::set<User::Ptr> Notification::GetUsers(void) const
|
2013-02-24 08:26:10 +01:00
|
|
|
{
|
2013-03-16 21:18:53 +01:00
|
|
|
std::set<User::Ptr> result;
|
2013-02-24 08:26:10 +01:00
|
|
|
|
2013-03-15 18:21:29 +01:00
|
|
|
Array::Ptr users = m_Users;
|
2013-02-24 08:26:10 +01:00
|
|
|
|
|
|
|
if (users) {
|
2013-03-01 12:07:52 +01:00
|
|
|
ObjectLock olock(users);
|
|
|
|
|
2013-03-15 18:21:29 +01:00
|
|
|
BOOST_FOREACH(const String& name, users) {
|
2013-02-27 21:49:03 +01:00
|
|
|
User::Ptr user = User::GetByName(name);
|
|
|
|
|
|
|
|
if (!user)
|
|
|
|
continue;
|
|
|
|
|
|
|
|
result.insert(user);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
2013-03-02 09:07:47 +01:00
|
|
|
/**
|
|
|
|
* @threadsafety Always.
|
|
|
|
*/
|
2013-03-16 21:18:53 +01:00
|
|
|
std::set<UserGroup::Ptr> Notification::GetGroups(void) const
|
2013-02-27 21:49:03 +01:00
|
|
|
{
|
2013-03-16 21:18:53 +01:00
|
|
|
std::set<UserGroup::Ptr> result;
|
2013-02-27 21:49:03 +01:00
|
|
|
|
2013-03-15 18:21:29 +01:00
|
|
|
Array::Ptr groups = m_Groups;
|
2013-02-27 21:49:03 +01:00
|
|
|
|
|
|
|
if (groups) {
|
2013-03-01 12:07:52 +01:00
|
|
|
ObjectLock olock(groups);
|
|
|
|
|
2013-03-15 18:21:29 +01:00
|
|
|
BOOST_FOREACH(const String& name, groups) {
|
2013-02-27 21:49:03 +01:00
|
|
|
UserGroup::Ptr ug = UserGroup::GetByName(name);
|
|
|
|
|
|
|
|
if (!ug)
|
|
|
|
continue;
|
|
|
|
|
|
|
|
result.insert(ug);
|
2013-02-24 08:26:10 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
2013-03-02 09:07:47 +01:00
|
|
|
/**
|
|
|
|
* @threadsafety Always.
|
|
|
|
*/
|
2013-02-24 01:10:34 +01:00
|
|
|
String Notification::NotificationTypeToString(NotificationType type)
|
2013-02-09 11:42:22 +01:00
|
|
|
{
|
2013-02-24 01:10:34 +01:00
|
|
|
switch (type) {
|
|
|
|
case NotificationDowntimeStart:
|
|
|
|
return "DOWNTIMESTART";
|
|
|
|
case NotificationDowntimeEnd:
|
|
|
|
return "DOWNTIMEEND";
|
|
|
|
case NotificationDowntimeRemoved:
|
|
|
|
return "DOWNTIMECANCELLED";
|
|
|
|
case NotificationCustom:
|
2013-02-24 12:47:24 +01:00
|
|
|
return "CUSTOM";
|
|
|
|
case NotificationAcknowledgement:
|
|
|
|
return "ACKNOWLEDGEMENT";
|
2013-02-24 01:10:34 +01:00
|
|
|
case NotificationProblem:
|
|
|
|
return "PROBLEM";
|
|
|
|
case NotificationRecovery:
|
|
|
|
return "RECOVERY";
|
|
|
|
default:
|
|
|
|
return "UNKNOWN_NOTIFICATION";
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-03-02 09:07:47 +01:00
|
|
|
/**
|
|
|
|
* @threadsafety Always.
|
|
|
|
*/
|
2013-03-07 12:04:20 +01:00
|
|
|
void Notification::BeginExecuteNotification(NotificationType type, const Dictionary::Ptr& cr)
|
2013-02-24 01:10:34 +01:00
|
|
|
{
|
2013-03-07 16:00:10 +01:00
|
|
|
ASSERT(!OwnsLock());
|
2013-02-24 01:10:34 +01:00
|
|
|
|
2013-03-07 12:04:20 +01:00
|
|
|
Dictionary::Ptr macros = cr->Get("macros");
|
2013-02-24 01:10:34 +01:00
|
|
|
|
2013-03-16 21:18:53 +01:00
|
|
|
std::set<User::Ptr> allUsers;
|
2013-02-27 21:49:03 +01:00
|
|
|
|
2013-03-16 21:18:53 +01:00
|
|
|
std::set<User::Ptr> users = GetUsers();
|
2013-02-27 21:49:03 +01:00
|
|
|
std::copy(users.begin(), users.end(), std::inserter(allUsers, allUsers.begin()));
|
|
|
|
|
2013-03-02 09:07:47 +01:00
|
|
|
BOOST_FOREACH(const UserGroup::Ptr& ug, GetGroups()) {
|
2013-03-16 21:18:53 +01:00
|
|
|
std::set<User::Ptr> members = ug->GetMembers();
|
2013-02-27 21:49:03 +01:00
|
|
|
std::copy(members.begin(), members.end(), std::inserter(allUsers, allUsers.begin()));
|
|
|
|
}
|
2013-02-24 08:26:10 +01:00
|
|
|
|
2013-02-27 21:49:03 +01:00
|
|
|
BOOST_FOREACH(const User::Ptr& user, allUsers) {
|
2013-03-16 21:18:53 +01:00
|
|
|
Log(LogDebug, "icinga", "Sending notification for user " + user->GetName());
|
2013-03-02 09:07:47 +01:00
|
|
|
BeginExecuteNotificationHelper(macros, type, user);
|
2013-02-24 08:26:10 +01:00
|
|
|
}
|
|
|
|
|
2013-03-06 15:41:13 +01:00
|
|
|
if (allUsers.empty()) {
|
2013-02-24 08:26:10 +01:00
|
|
|
/* Send a notification even if there are no users specified. */
|
2013-03-02 09:07:47 +01:00
|
|
|
BeginExecuteNotificationHelper(macros, type, User::Ptr());
|
2013-02-24 08:26:10 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-03-02 09:07:47 +01:00
|
|
|
/**
|
|
|
|
* @threadsafety Always.
|
|
|
|
*/
|
|
|
|
void Notification::BeginExecuteNotificationHelper(const Dictionary::Ptr& notificationMacros, NotificationType type, const User::Ptr& user)
|
2013-02-24 08:26:10 +01:00
|
|
|
{
|
2013-03-07 16:00:10 +01:00
|
|
|
ASSERT(!OwnsLock());
|
2013-03-02 09:07:47 +01:00
|
|
|
|
2013-03-16 21:18:53 +01:00
|
|
|
std::vector<Dictionary::Ptr> macroDicts;
|
2013-02-24 08:26:10 +01:00
|
|
|
|
|
|
|
if (user) {
|
2013-03-02 09:07:47 +01:00
|
|
|
macroDicts.push_back(user->GetMacros());
|
|
|
|
macroDicts.push_back(user->CalculateDynamicMacros());
|
2013-02-24 08:26:10 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
macroDicts.push_back(notificationMacros);
|
|
|
|
|
|
|
|
Dictionary::Ptr macros = MacroProcessor::MergeMacroDicts(macroDicts);
|
2013-02-19 07:26:52 +01:00
|
|
|
|
2013-03-02 09:07:47 +01:00
|
|
|
Notification::Ptr self = GetSelf();
|
2013-02-09 11:42:22 +01:00
|
|
|
|
2013-03-16 21:18:53 +01:00
|
|
|
std::vector<Value> arguments;
|
2013-03-02 09:07:47 +01:00
|
|
|
arguments.push_back(self);
|
|
|
|
arguments.push_back(macros);
|
|
|
|
arguments.push_back(type);
|
2013-02-24 08:26:10 +01:00
|
|
|
|
2013-03-13 16:04:53 +01:00
|
|
|
ScriptTask::Ptr task = MakeMethodTask("notify", arguments);
|
2013-02-09 15:20:10 +01:00
|
|
|
|
2013-03-08 14:56:23 +01:00
|
|
|
if (!task) {
|
2013-03-16 21:18:53 +01:00
|
|
|
Log(LogWarning, "icinga", "Notification object '" + GetName() + "' doesn't have a 'notify' method.");
|
2013-03-08 14:56:23 +01:00
|
|
|
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2013-03-02 09:07:47 +01:00
|
|
|
{
|
|
|
|
ObjectLock olock(this);
|
2013-02-24 01:10:34 +01:00
|
|
|
|
|
|
|
/* We need to keep the task object alive until the completion handler is called. */
|
2013-03-02 09:07:47 +01:00
|
|
|
m_Tasks.insert(task);
|
2013-02-24 01:10:34 +01:00
|
|
|
}
|
2013-02-09 11:42:22 +01:00
|
|
|
|
2013-02-24 01:10:34 +01:00
|
|
|
task->Start(boost::bind(&Notification::NotificationCompletedHandler, self, _1));
|
2013-02-09 11:42:22 +01:00
|
|
|
}
|
|
|
|
|
2013-03-02 09:07:47 +01:00
|
|
|
/**
|
|
|
|
* @threadsafety Always.
|
|
|
|
*/
|
2013-02-09 11:42:22 +01:00
|
|
|
void Notification::NotificationCompletedHandler(const ScriptTask::Ptr& task)
|
|
|
|
{
|
2013-03-07 16:00:10 +01:00
|
|
|
ASSERT(!OwnsLock());
|
2013-03-02 09:07:47 +01:00
|
|
|
|
2013-03-04 15:52:42 +01:00
|
|
|
{
|
|
|
|
ObjectLock olock(this);
|
|
|
|
|
|
|
|
m_Tasks.erase(task);
|
|
|
|
}
|
2013-02-09 11:42:22 +01:00
|
|
|
|
|
|
|
try {
|
2013-03-01 12:07:52 +01:00
|
|
|
task->GetResult();
|
2013-02-09 11:42:22 +01:00
|
|
|
|
2013-03-16 21:18:53 +01:00
|
|
|
Log(LogInformation, "icinga", "Completed sending notification for service '" + GetService()->GetName() + "'");
|
|
|
|
} catch (const std::exception& ex) {
|
|
|
|
std::ostringstream msgbuf;
|
2013-02-09 11:42:22 +01:00
|
|
|
msgbuf << "Exception occured during notification for service '"
|
2013-03-16 21:18:53 +01:00
|
|
|
<< GetService()->GetName() << "': " << boost::diagnostic_information(ex);
|
2013-02-09 11:42:22 +01:00
|
|
|
String message = msgbuf.str();
|
|
|
|
|
2013-03-16 21:18:53 +01:00
|
|
|
Log(LogWarning, "icinga", message);
|
2013-02-09 11:42:22 +01:00
|
|
|
}
|
|
|
|
}
|
2013-02-11 14:01:52 +01:00
|
|
|
|
2013-03-02 09:07:47 +01:00
|
|
|
/**
|
|
|
|
* @threadsafety Always.
|
|
|
|
*/
|
2013-03-04 15:52:42 +01:00
|
|
|
void Notification::OnAttributeChanged(const String& name)
|
2013-02-11 14:01:52 +01:00
|
|
|
{
|
2013-03-07 16:00:10 +01:00
|
|
|
ASSERT(!OwnsLock());
|
2013-03-02 09:07:47 +01:00
|
|
|
|
2013-02-11 14:01:52 +01:00
|
|
|
if (name == "host_name" || name == "service")
|
2013-02-27 15:23:25 +01:00
|
|
|
Service::InvalidateNotificationsCache();
|
2013-02-11 14:01:52 +01:00
|
|
|
}
|