1 /******************************************************************************
3 * Copyright (C) 2012 Icinga Development Team (http://www.icinga.org/) *
5 * This program is free software; you can redistribute it and/or *
6 * modify it under the terms of the GNU General Public License *
7 * as published by the Free Software Foundation; either version 2 *
8 * of the License, or (at your option) any later version. *
10 * This program is distributed in the hope that it will be useful, *
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of *
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
13 * GNU General Public License for more details. *
15 * You should have received a copy of the GNU General Public License *
16 * along with this program; if not, write to the Free Software Foundation *
17 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA. *
18 ******************************************************************************/
20 #include "i2-icinga.h"
22 using namespace icinga;
24 REGISTER_TYPE(Notification, NULL);
26 Notification::Notification(const Dictionary::Ptr& properties)
27 : DynamicObject(properties)
29 Service::InvalidateNotificationsCache();
32 Notification::~Notification(void)
34 Service::InvalidateNotificationsCache();
37 bool Notification::Exists(const String& name)
39 return (DynamicObject::GetObject("Notification", name));
42 Notification::Ptr Notification::GetByName(const String& name)
44 DynamicObject::Ptr configObject = DynamicObject::GetObject("Notification", name);
47 BOOST_THROW_EXCEPTION(invalid_argument("Notification '" + name + "' does not exist."));
49 return dynamic_pointer_cast<Notification>(configObject);
52 Service::Ptr Notification::GetService(void) const
54 Host::Ptr host = Host::GetByName(Get("host_name"));
55 String service = Get("service");
57 if (service.IsEmpty())
58 return host->GetHostCheckService();
60 return host->GetServiceByShortName(service);
63 Value Notification::GetNotificationCommand(void) const
65 return Get("notification_command");
68 Dictionary::Ptr Notification::GetMacros(void) const
73 void Notification::SendNotification(NotificationType type)
75 vector<Value> arguments;
76 arguments.push_back(static_cast<Notification::Ptr>(GetSelf()));
77 arguments.push_back(type);
79 task = InvokeMethod("notify", arguments, boost::bind(&Notification::NotificationCompletedHandler, this, _1));
82 Logger::Write(LogWarning, "icinga", "Notification object '" + GetName() + "' doesn't have a 'notify' method.");
87 if (!task->IsFinished()) {
88 /* We need to keep the task object alive until the completion handler is called. */
94 void Notification::NotificationCompletedHandler(const ScriptTask::Ptr& task)
99 (void) task->GetResult();
101 Logger::Write(LogInformation, "icinga", "Completed sending notification for service '" + GetService()->GetName() + "'");
102 } catch (const exception& ex) {
104 msgbuf << "Exception occured during notification for service '"
105 << GetService()->GetName() << "': " << diagnostic_information(ex);
106 String message = msgbuf.str();
108 Logger::Write(LogWarning, "icinga", message);
112 void Notification::OnAttributeChanged(const String& name, const Value& oldValue)
114 if (name == "host_name" || name == "service")
115 Service::InvalidateNotificationsCache();