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);
36 Notification::~Notification(void)
38 Service::InvalidateNotificationsCache();
41 Notification::Ptr Notification::GetByName(const String& name)
43 DynamicObject::Ptr configObject = DynamicObject::GetObject("Notification", name);
46 BOOST_THROW_EXCEPTION(invalid_argument("Notification '" + name + "' does not exist."));
48 return dynamic_pointer_cast<Notification>(configObject);
51 Service::Ptr Notification::GetService(void) const
53 Host::Ptr host = Host::GetByName(m_HostName);
56 return Service::Ptr();
58 if (m_Service.IsEmpty())
59 return Host::GetHostCheckService(host);
61 return Host::GetServiceByShortName(host, m_Service);
64 Value Notification::GetNotificationCommand(void) const
66 return m_NotificationCommand;
69 Dictionary::Ptr Notification::GetMacros(void) const
74 set<User::Ptr> Notification::GetUsers(void) const
76 set<User::Ptr> result;
78 Dictionary::Ptr users = m_Users;
82 BOOST_FOREACH(tie(tuples::ignore, name), users) {
83 result.insert(User::GetByName(name));
90 String Notification::NotificationTypeToString(NotificationType type)
93 case NotificationDowntimeStart:
94 return "DOWNTIMESTART";
95 case NotificationDowntimeEnd:
97 case NotificationDowntimeRemoved:
98 return "DOWNTIMECANCELLED";
99 case NotificationCustom:
101 case NotificationAcknowledgement:
102 return "ACKNOWLEDGEMENT";
103 case NotificationProblem:
105 case NotificationRecovery:
108 return "UNKNOWN_NOTIFICATION";
112 void Notification::BeginExecuteNotification(const Notification::Ptr& self, NotificationType type)
115 vector<Dictionary::Ptr> macroDicts;
117 Dictionary::Ptr notificationMacros = boost::make_shared<Dictionary>();
118 notificationMacros->Set("NOTIFICATIONTYPE", NotificationTypeToString(type));
119 macroDicts.push_back(notificationMacros);
121 Service::Ptr service;
122 set<User::Ptr> users;
125 ObjectLock olock(self);
126 macroDicts.push_back(self->GetMacros());
127 service = self->GetService();
128 users = self->GetUsers();
135 ObjectLock olock(service);
136 macroDicts.push_back(service->GetMacros());
137 service_name = service->GetName();
138 host = service->GetHost();
141 macroDicts.push_back(Service::CalculateDynamicMacros(service));
144 ObjectLock olock(host);
145 macroDicts.push_back(host->GetMacros());
146 macroDicts.push_back(Host::CalculateDynamicMacros(host));
149 IcingaApplication::Ptr app = IcingaApplication::GetInstance();
152 ObjectLock olock(app);
153 macroDicts.push_back(app->GetMacros());
156 macroDicts.push_back(IcingaApplication::CalculateDynamicMacros(app));
158 Dictionary::Ptr macros = MacroProcessor::MergeMacroDicts(macroDicts);
162 BOOST_FOREACH(const User::Ptr& user, users) {
163 BeginExecuteNotificationHelper(self, macros, type, user);
168 /* Send a notification even if there are no users specified. */
169 BeginExecuteNotificationHelper(self, macros, type, User::Ptr());
173 void Notification::BeginExecuteNotificationHelper(const Notification::Ptr& self, const Dictionary::Ptr& notificationMacros, NotificationType type, const User::Ptr& user)
175 vector<Dictionary::Ptr> macroDicts;
179 ObjectLock olock(self);
180 macroDicts.push_back(user->GetMacros());
183 macroDicts.push_back(User::CalculateDynamicMacros(user));
186 macroDicts.push_back(notificationMacros);
188 Dictionary::Ptr macros = MacroProcessor::MergeMacroDicts(macroDicts);
190 ScriptTask::Ptr task;
193 ObjectLock olock(self);
195 vector<Value> arguments;
196 arguments.push_back(self);
197 arguments.push_back(macros);
198 arguments.push_back(type);
199 task = self->MakeMethodTask("notify", arguments);
202 Logger::Write(LogWarning, "icinga", "Notification object '" + self->GetName() + "' doesn't have a 'notify' method.");
207 /* We need to keep the task object alive until the completion handler is called. */
208 self->m_Tasks.insert(task);
211 task->Start(boost::bind(&Notification::NotificationCompletedHandler, self, _1));
214 void Notification::NotificationCompletedHandler(const ScriptTask::Ptr& task)
220 ObjectLock tlock(task);
221 (void) task->GetResult();
224 Logger::Write(LogInformation, "icinga", "Completed sending notification for service '" + GetService()->GetName() + "'");
225 } catch (const exception& ex) {
227 msgbuf << "Exception occured during notification for service '"
228 << GetService()->GetName() << "': " << diagnostic_information(ex);
229 String message = msgbuf.str();
231 Logger::Write(LogWarning, "icinga", message);
235 void Notification::OnAttributeChanged(const String& name, const Value& oldValue)
237 if (name == "host_name" || name == "service")
238 Service::InvalidateNotificationsCache();