icinga2/lib/icinga/notification.cpp

119 lines
3.9 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;
REGISTER_TYPE(Notification, NULL);
Notification::Notification(const Dictionary::Ptr& properties)
: DynamicObject(properties)
{
Service::InvalidateNotificationsCache();
}
2013-02-09 11:42:22 +01:00
Notification::~Notification(void)
{
Service::InvalidateNotificationsCache();
}
2013-02-09 11:42:22 +01:00
bool Notification::Exists(const String& name)
{
return (DynamicObject::GetObject("Notification", name));
}
Notification::Ptr Notification::GetByName(const String& name)
{
DynamicObject::Ptr configObject = DynamicObject::GetObject("Notification", name);
if (!configObject)
BOOST_THROW_EXCEPTION(invalid_argument("Notification '" + name + "' does not exist."));
return dynamic_pointer_cast<Notification>(configObject);
}
Service::Ptr Notification::GetService(void) const
{
Host::Ptr host = Host::GetByName(Get("host_name"));
String service = Get("service");
if (service.IsEmpty())
return host->GetHostCheckService();
else
return host->GetServiceByShortName(service);
}
Value Notification::GetNotificationCommand(void) const
2013-02-09 11:42:22 +01:00
{
return Get("notification_command");
}
Dictionary::Ptr Notification::GetMacros(void) const
{
return Get("macros");
}
void Notification::SendNotification(NotificationType type)
2013-02-09 11:42:22 +01:00
{
vector<Value> arguments;
arguments.push_back(static_cast<Notification::Ptr>(GetSelf()));
arguments.push_back(type);
2013-02-19 07:26:52 +01:00
ScriptTask::Ptr task = MakeMethodTask("notify", arguments);
2013-02-09 11:42:22 +01:00
if (!task) {
Logger::Write(LogWarning, "icinga", "Notification object '" + GetName() + "' doesn't have a 'notify' method.");
return;
}
2013-02-19 07:26:52 +01:00
/* We need to keep the task object alive until the completion handler is called. */
m_Tasks.insert(task);
2013-02-09 11:42:22 +01:00
2013-02-19 07:26:52 +01:00
task->Start(boost::bind(&Notification::NotificationCompletedHandler, this, _1));
2013-02-09 11:42:22 +01:00
}
void Notification::NotificationCompletedHandler(const ScriptTask::Ptr& task)
{
m_Tasks.erase(task);
try {
2013-02-19 23:02:08 +01:00
{
ObjectLock tlock(task);
(void) 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")
Service::InvalidateNotificationsCache();
}