icinga2/lib/icinga/notification.cpp

281 lines
7.6 KiB
C++
Raw Normal View History

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. *
******************************************************************************/
#include "i2-icinga.h"
using namespace icinga;
2013-03-01 12:07:52 +01:00
REGISTER_TYPE(Notification);
2013-02-09 11:42:22 +01:00
Notification::Notification(const Dictionary::Ptr& properties)
: DynamicObject(properties)
{
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 11:42:22 +01:00
Notification::~Notification(void)
{
2013-02-27 15:23:25 +01:00
Service::InvalidateNotificationsCache();
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);
}
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())
return Host::GetHostCheckService(host);
2013-02-09 11:42:22 +01:00
else
return Host::GetServiceByShortName(host, m_Service);
2013-02-09 11:42:22 +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
}
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-02-24 08:26:10 +01:00
set<User::Ptr> Notification::GetUsers(void) const
{
set<User::Ptr> result;
2013-02-26 10:13:54 +01:00
Dictionary::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-02-24 08:26:10 +01:00
String name;
BOOST_FOREACH(tie(tuples::ignore, name), users) {
2013-02-27 21:49:03 +01:00
User::Ptr user = User::GetByName(name);
if (!user)
continue;
result.insert(user);
}
}
return result;
}
set<UserGroup::Ptr> Notification::GetGroups(void) const
{
set<UserGroup::Ptr> result;
Dictionary::Ptr groups = m_Groups;
if (groups) {
2013-03-01 12:07:52 +01:00
ObjectLock olock(groups);
2013-02-27 21:49:03 +01:00
String name;
BOOST_FOREACH(tie(tuples::ignore, name), groups) {
UserGroup::Ptr ug = UserGroup::GetByName(name);
if (!ug)
continue;
result.insert(ug);
2013-02-24 08:26:10 +01:00
}
}
return result;
}
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:
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";
}
}
void Notification::BeginExecuteNotification(const Notification::Ptr& self, NotificationType type)
{
vector<Dictionary::Ptr> macroDicts;
Dictionary::Ptr notificationMacros = boost::make_shared<Dictionary>();
notificationMacros->Set("NOTIFICATIONTYPE", NotificationTypeToString(type));
macroDicts.push_back(notificationMacros);
Service::Ptr service;
2013-02-24 08:26:10 +01:00
set<User::Ptr> users;
2013-02-27 21:49:03 +01:00
set<UserGroup::Ptr> groups;
2013-02-24 01:10:34 +01:00
{
ObjectLock olock(self);
macroDicts.push_back(self->GetMacros());
service = self->GetService();
2013-02-24 08:26:10 +01:00
users = self->GetUsers();
2013-02-27 21:49:03 +01:00
groups = self->GetGroups();
2013-02-24 01:10:34 +01:00
}
Host::Ptr host;
String service_name;
{
ObjectLock olock(service);
macroDicts.push_back(service->GetMacros());
service_name = service->GetName();
host = service->GetHost();
}
macroDicts.push_back(Service::CalculateDynamicMacros(service));
{
ObjectLock olock(host);
macroDicts.push_back(host->GetMacros());
macroDicts.push_back(Host::CalculateDynamicMacros(host));
}
IcingaApplication::Ptr app = IcingaApplication::GetInstance();
{
ObjectLock olock(app);
macroDicts.push_back(app->GetMacros());
}
macroDicts.push_back(IcingaApplication::CalculateDynamicMacros(app));
Dictionary::Ptr macros = MacroProcessor::MergeMacroDicts(macroDicts);
2013-02-27 21:49:03 +01:00
set<User::Ptr> allUsers;
std::copy(users.begin(), users.end(), std::inserter(allUsers, allUsers.begin()));
BOOST_FOREACH(const UserGroup::Ptr& ug, groups) {
set<User::Ptr> members = UserGroup::GetMembers(ug);
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) {
String user_name;
{
ObjectLock olock(user);
user_name = user->GetName();
}
Logger::Write(LogDebug, "icinga", "Sending notification for user " + user_name);
2013-02-24 08:26:10 +01:00
BeginExecuteNotificationHelper(self, macros, type, user);
}
2013-02-27 21:49:03 +01:00
if (allUsers.size() == 0) {
2013-02-24 08:26:10 +01:00
/* Send a notification even if there are no users specified. */
BeginExecuteNotificationHelper(self, macros, type, User::Ptr());
}
}
void Notification::BeginExecuteNotificationHelper(const Notification::Ptr& self, const Dictionary::Ptr& notificationMacros, NotificationType type, const User::Ptr& user)
{
vector<Dictionary::Ptr> macroDicts;
if (user) {
{
ObjectLock olock(user);
2013-02-24 08:26:10 +01:00
macroDicts.push_back(user->GetMacros());
}
macroDicts.push_back(User::CalculateDynamicMacros(user));
}
macroDicts.push_back(notificationMacros);
Dictionary::Ptr macros = MacroProcessor::MergeMacroDicts(macroDicts);
2013-02-19 07:26:52 +01:00
2013-02-24 01:10:34 +01:00
ScriptTask::Ptr task;
2013-02-09 11:42:22 +01:00
2013-02-24 01:10:34 +01:00
{
ObjectLock olock(self);
2013-02-24 08:26:10 +01:00
vector<Value> arguments;
arguments.push_back(self);
arguments.push_back(macros);
arguments.push_back(type);
2013-02-24 01:10:34 +01:00
task = self->MakeMethodTask("notify", arguments);
2013-02-24 01:10:34 +01:00
if (!task) {
Logger::Write(LogWarning, "icinga", "Notification object '" + self->GetName() + "' doesn't have a 'notify' method.");
2013-02-24 01:10:34 +01:00
return;
}
/* We need to keep the task object alive until the completion handler is called. */
self->m_Tasks.insert(task);
}
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
}
void Notification::NotificationCompletedHandler(const ScriptTask::Ptr& task)
{
m_Tasks.erase(task);
try {
2013-03-01 12:07:52 +01:00
task->GetResult();
2013-02-09 11:42:22 +01:00
Logger::Write(LogInformation, "icinga", "Completed sending notification for service '" + GetService()->GetName() + "'");
} catch (const exception& ex) {
stringstream msgbuf;
msgbuf << "Exception occured during notification for service '"
<< GetService()->GetName() << "': " << diagnostic_information(ex);
String message = msgbuf.str();
Logger::Write(LogWarning, "icinga", message);
}
}
void Notification::OnAttributeChanged(const String& name, const Value& oldValue)
{
if (name == "host_name" || name == "service")
2013-02-27 15:23:25 +01:00
Service::InvalidateNotificationsCache();
}