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("groups", Attribute_Config, &m_Groups);
33 RegisterAttribute("host_name", Attribute_Config, &m_HostName);
34 RegisterAttribute("service", Attribute_Config, &m_Service);
37 Notification::~Notification(void)
39 Service::InvalidateNotificationsCache();
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(m_HostName);
57 return Service::Ptr();
59 if (m_Service.IsEmpty())
60 return Host::GetHostCheckService(host);
62 return Host::GetServiceByShortName(host, m_Service);
65 Value Notification::GetNotificationCommand(void) const
67 return m_NotificationCommand;
70 Dictionary::Ptr Notification::GetMacros(void) const
75 set<User::Ptr> Notification::GetUsers(void) const
77 set<User::Ptr> result;
79 Dictionary::Ptr users = m_Users;
83 BOOST_FOREACH(tie(tuples::ignore, name), users) {
84 User::Ptr user = User::GetByName(name);
96 set<UserGroup::Ptr> Notification::GetGroups(void) const
98 set<UserGroup::Ptr> result;
100 Dictionary::Ptr groups = m_Groups;
104 BOOST_FOREACH(tie(tuples::ignore, name), groups) {
105 UserGroup::Ptr ug = UserGroup::GetByName(name);
117 String Notification::NotificationTypeToString(NotificationType type)
120 case NotificationDowntimeStart:
121 return "DOWNTIMESTART";
122 case NotificationDowntimeEnd:
123 return "DOWNTIMEEND";
124 case NotificationDowntimeRemoved:
125 return "DOWNTIMECANCELLED";
126 case NotificationCustom:
128 case NotificationAcknowledgement:
129 return "ACKNOWLEDGEMENT";
130 case NotificationProblem:
132 case NotificationRecovery:
135 return "UNKNOWN_NOTIFICATION";
139 void Notification::BeginExecuteNotification(const Notification::Ptr& self, NotificationType type)
142 vector<Dictionary::Ptr> macroDicts;
144 Dictionary::Ptr notificationMacros = boost::make_shared<Dictionary>();
145 notificationMacros->Set("NOTIFICATIONTYPE", NotificationTypeToString(type));
146 macroDicts.push_back(notificationMacros);
148 Service::Ptr service;
149 set<User::Ptr> users;
150 set<UserGroup::Ptr> groups;
153 ObjectLock olock(self);
154 macroDicts.push_back(self->GetMacros());
155 service = self->GetService();
156 users = self->GetUsers();
157 groups = self->GetGroups();
164 ObjectLock olock(service);
165 macroDicts.push_back(service->GetMacros());
166 service_name = service->GetName();
167 host = service->GetHost();
170 macroDicts.push_back(Service::CalculateDynamicMacros(service));
173 ObjectLock olock(host);
174 macroDicts.push_back(host->GetMacros());
175 macroDicts.push_back(Host::CalculateDynamicMacros(host));
178 IcingaApplication::Ptr app = IcingaApplication::GetInstance();
181 ObjectLock olock(app);
182 macroDicts.push_back(app->GetMacros());
185 macroDicts.push_back(IcingaApplication::CalculateDynamicMacros(app));
187 Dictionary::Ptr macros = MacroProcessor::MergeMacroDicts(macroDicts);
189 set<User::Ptr> allUsers;
191 std::copy(users.begin(), users.end(), std::inserter(allUsers, allUsers.begin()));
193 BOOST_FOREACH(const UserGroup::Ptr& ug, groups) {
194 set<User::Ptr> members = UserGroup::GetMembers(ug);
195 std::copy(members.begin(), members.end(), std::inserter(allUsers, allUsers.begin()));
198 BOOST_FOREACH(const User::Ptr& user, allUsers) {
202 ObjectLock olock(user);
203 user_name = user->GetName();
206 Logger::Write(LogDebug, "icinga", "Sending notification for user " + user_name);
207 BeginExecuteNotificationHelper(self, macros, type, user);
210 if (allUsers.size() == 0) {
211 /* Send a notification even if there are no users specified. */
212 BeginExecuteNotificationHelper(self, macros, type, User::Ptr());
216 void Notification::BeginExecuteNotificationHelper(const Notification::Ptr& self, const Dictionary::Ptr& notificationMacros, NotificationType type, const User::Ptr& user)
218 vector<Dictionary::Ptr> macroDicts;
222 ObjectLock olock(user);
223 macroDicts.push_back(user->GetMacros());
226 macroDicts.push_back(User::CalculateDynamicMacros(user));
229 macroDicts.push_back(notificationMacros);
231 Dictionary::Ptr macros = MacroProcessor::MergeMacroDicts(macroDicts);
233 ScriptTask::Ptr task;
236 ObjectLock olock(self);
238 vector<Value> arguments;
239 arguments.push_back(self);
240 arguments.push_back(macros);
241 arguments.push_back(type);
242 task = self->MakeMethodTask("notify", arguments);
245 Logger::Write(LogWarning, "icinga", "Notification object '" + self->GetName() + "' doesn't have a 'notify' method.");
250 /* We need to keep the task object alive until the completion handler is called. */
251 self->m_Tasks.insert(task);
254 task->Start(boost::bind(&Notification::NotificationCompletedHandler, self, _1));
257 void Notification::NotificationCompletedHandler(const ScriptTask::Ptr& task)
263 ObjectLock tlock(task);
264 (void) task->GetResult();
267 Logger::Write(LogInformation, "icinga", "Completed sending notification for service '" + GetService()->GetName() + "'");
268 } catch (const exception& ex) {
270 msgbuf << "Exception occured during notification for service '"
271 << GetService()->GetName() << "': " << diagnostic_information(ex);
272 String message = msgbuf.str();
274 Logger::Write(LogWarning, "icinga", message);
278 void Notification::OnAttributeChanged(const String& name, const Value& oldValue)
280 if (name == "host_name" || name == "service")
281 Service::InvalidateNotificationsCache();