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 RegisterAttribute("notification_command", Attribute_Config, &m_NotificationCommand);
30 RegisterAttribute("macros", Attribute_Config, &m_Macros);
31 RegisterAttribute("users", Attribute_Config, &m_Users);
32 RegisterAttribute("host_name", Attribute_Config, &m_HostName);
33 RegisterAttribute("service", Attribute_Config, &m_Service);
35 Service::InvalidateNotificationsCache();
38 Notification::~Notification(void)
40 Service::InvalidateNotificationsCache();
43 bool Notification::Exists(const String& name)
45 return (DynamicObject::GetObject("Notification", name));
48 Notification::Ptr Notification::GetByName(const String& name)
50 DynamicObject::Ptr configObject = DynamicObject::GetObject("Notification", name);
53 BOOST_THROW_EXCEPTION(invalid_argument("Notification '" + name + "' does not exist."));
55 return dynamic_pointer_cast<Notification>(configObject);
58 Service::Ptr Notification::GetService(void) const
60 Host::Ptr host = Host::GetByName(m_HostName);
62 if (m_Service.IsEmpty())
63 return host->GetHostCheckService();
65 return host->GetServiceByShortName(m_Service);
68 Value Notification::GetNotificationCommand(void) const
70 return m_NotificationCommand;
73 Dictionary::Ptr Notification::GetMacros(void) const
78 set<User::Ptr> Notification::GetUsers(void) const
80 set<User::Ptr> result;
82 Dictionary::Ptr users = m_Users;
86 BOOST_FOREACH(tie(tuples::ignore, name), users) {
87 result.insert(User::GetByName(name));
94 String Notification::NotificationTypeToString(NotificationType type)
97 case NotificationDowntimeStart:
98 return "DOWNTIMESTART";
99 case NotificationDowntimeEnd:
100 return "DOWNTIMEEND";
101 case NotificationDowntimeRemoved:
102 return "DOWNTIMECANCELLED";
103 case NotificationCustom:
105 case NotificationAcknowledgement:
106 return "ACKNOWLEDGEMENT";
107 case NotificationProblem:
109 case NotificationRecovery:
112 return "UNKNOWN_NOTIFICATION";
116 void Notification::BeginExecuteNotification(const Notification::Ptr& self, NotificationType type)
119 vector<Dictionary::Ptr> macroDicts;
121 Dictionary::Ptr notificationMacros = boost::make_shared<Dictionary>();
122 notificationMacros->Set("NOTIFICATIONTYPE", NotificationTypeToString(type));
123 macroDicts.push_back(notificationMacros);
125 Service::Ptr service;
126 set<User::Ptr> users;
129 ObjectLock olock(self);
130 macroDicts.push_back(self->GetMacros());
131 service = self->GetService();
132 users = self->GetUsers();
139 ObjectLock olock(service);
140 macroDicts.push_back(service->GetMacros());
141 service_name = service->GetName();
142 host = service->GetHost();
145 macroDicts.push_back(Service::CalculateDynamicMacros(service));
148 ObjectLock olock(host);
149 macroDicts.push_back(host->GetMacros());
150 macroDicts.push_back(Host::CalculateDynamicMacros(host));
153 IcingaApplication::Ptr app = IcingaApplication::GetInstance();
156 ObjectLock olock(app);
157 macroDicts.push_back(app->GetMacros());
160 macroDicts.push_back(IcingaApplication::CalculateDynamicMacros(app));
162 Dictionary::Ptr macros = MacroProcessor::MergeMacroDicts(macroDicts);
166 BOOST_FOREACH(const User::Ptr& user, users) {
167 BeginExecuteNotificationHelper(self, macros, type, user);
172 /* Send a notification even if there are no users specified. */
173 BeginExecuteNotificationHelper(self, macros, type, User::Ptr());
177 void Notification::BeginExecuteNotificationHelper(const Notification::Ptr& self, const Dictionary::Ptr& notificationMacros, NotificationType type, const User::Ptr& user)
179 vector<Dictionary::Ptr> macroDicts;
183 ObjectLock olock(self);
184 macroDicts.push_back(user->GetMacros());
187 macroDicts.push_back(User::CalculateDynamicMacros(user));
190 macroDicts.push_back(notificationMacros);
192 Dictionary::Ptr macros = MacroProcessor::MergeMacroDicts(macroDicts);
194 ScriptTask::Ptr task;
197 ObjectLock olock(self);
199 vector<Value> arguments;
200 arguments.push_back(self);
201 arguments.push_back(macros);
202 arguments.push_back(type);
203 task = self->MakeMethodTask("notify", arguments);
206 Logger::Write(LogWarning, "icinga", "Notification object '" + self->GetName() + "' doesn't have a 'notify' method.");
211 /* We need to keep the task object alive until the completion handler is called. */
212 self->m_Tasks.insert(task);
215 task->Start(boost::bind(&Notification::NotificationCompletedHandler, self, _1));
218 void Notification::NotificationCompletedHandler(const ScriptTask::Ptr& task)
224 ObjectLock tlock(task);
225 (void) task->GetResult();
228 Logger::Write(LogInformation, "icinga", "Completed sending notification for service '" + GetService()->GetName() + "'");
229 } catch (const exception& ex) {
231 msgbuf << "Exception occured during notification for service '"
232 << GetService()->GetName() << "': " << diagnostic_information(ex);
233 String message = msgbuf.str();
235 Logger::Write(LogWarning, "icinga", message);
239 void Notification::OnAttributeChanged(const String& name, const Value& oldValue)
241 if (name == "host_name" || name == "service")
242 Service::InvalidateNotificationsCache();