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)
|
2013-02-09 15:20:10 +01:00
|
|
|
{
|
|
|
|
Service::InvalidateNotificationsCache();
|
|
|
|
}
|
2013-02-09 11:42:22 +01:00
|
|
|
|
|
|
|
Notification::~Notification(void)
|
2013-02-09 15:20:10 +01:00
|
|
|
{
|
|
|
|
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);
|
|
|
|
}
|
|
|
|
|
2013-02-13 20:08:09 +01:00
|
|
|
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");
|
|
|
|
}
|
|
|
|
|
2013-02-09 15:20:10 +01:00
|
|
|
void Notification::SendNotification(NotificationType type)
|
2013-02-09 11:42:22 +01:00
|
|
|
{
|
|
|
|
vector<Value> arguments;
|
|
|
|
arguments.push_back(static_cast<Notification::Ptr>(GetSelf()));
|
2013-02-09 15:20:10 +01:00
|
|
|
arguments.push_back(type);
|
2013-02-09 11:42:22 +01:00
|
|
|
ScriptTask::Ptr task;
|
|
|
|
task = InvokeMethod("notify", arguments, boost::bind(&Notification::NotificationCompletedHandler, this, _1));
|
|
|
|
|
2013-02-09 15:20:10 +01:00
|
|
|
if (!task) {
|
|
|
|
Logger::Write(LogWarning, "icinga", "Notification object '" + GetName() + "' doesn't have a 'notify' method.");
|
|
|
|
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2013-02-09 11:42:22 +01:00
|
|
|
if (!task->IsFinished()) {
|
|
|
|
/* We need to keep the task object alive until the completion handler is called. */
|
|
|
|
|
|
|
|
m_Tasks.insert(task);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void Notification::NotificationCompletedHandler(const ScriptTask::Ptr& task)
|
|
|
|
{
|
|
|
|
m_Tasks.erase(task);
|
|
|
|
|
|
|
|
try {
|
|
|
|
(void) task->GetResult();
|
|
|
|
|
|
|
|
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);
|
|
|
|
}
|
|
|
|
}
|
2013-02-11 14:01:52 +01:00
|
|
|
|
|
|
|
void Notification::OnAttributeChanged(const String& name, const Value& oldValue)
|
|
|
|
{
|
|
|
|
if (name == "host_name" || name == "service")
|
|
|
|
Service::InvalidateNotificationsCache();
|
|
|
|
}
|