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);
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);
46 return dynamic_pointer_cast<Notification>(configObject);
49 Service::Ptr Notification::GetService(void) const
51 Host::Ptr host = Host::GetByName(m_HostName);
54 return Service::Ptr();
56 if (m_Service.IsEmpty())
57 return Host::GetHostCheckService(host);
59 return Host::GetServiceByShortName(host, m_Service);
62 Value Notification::GetNotificationCommand(void) const
64 return m_NotificationCommand;
67 Dictionary::Ptr Notification::GetMacros(void) const
72 set<User::Ptr> Notification::GetUsers(void) const
74 set<User::Ptr> result;
76 Dictionary::Ptr users = m_Users;
79 ObjectLock olock(users);
82 BOOST_FOREACH(tie(tuples::ignore, name), users) {
83 User::Ptr user = User::GetByName(name);
95 set<UserGroup::Ptr> Notification::GetGroups(void) const
97 set<UserGroup::Ptr> result;
99 Dictionary::Ptr groups = m_Groups;
102 ObjectLock olock(groups);
105 BOOST_FOREACH(tie(tuples::ignore, name), groups) {
106 UserGroup::Ptr ug = UserGroup::GetByName(name);
118 String Notification::NotificationTypeToString(NotificationType type)
121 case NotificationDowntimeStart:
122 return "DOWNTIMESTART";
123 case NotificationDowntimeEnd:
124 return "DOWNTIMEEND";
125 case NotificationDowntimeRemoved:
126 return "DOWNTIMECANCELLED";
127 case NotificationCustom:
129 case NotificationAcknowledgement:
130 return "ACKNOWLEDGEMENT";
131 case NotificationProblem:
133 case NotificationRecovery:
136 return "UNKNOWN_NOTIFICATION";
140 void Notification::BeginExecuteNotification(const Notification::Ptr& self, NotificationType type)
143 vector<Dictionary::Ptr> macroDicts;
145 Dictionary::Ptr notificationMacros = boost::make_shared<Dictionary>();
146 notificationMacros->Set("NOTIFICATIONTYPE", NotificationTypeToString(type));
147 macroDicts.push_back(notificationMacros);
149 Service::Ptr service;
150 set<User::Ptr> users;
151 set<UserGroup::Ptr> groups;
154 ObjectLock olock(self);
155 macroDicts.push_back(self->GetMacros());
156 service = self->GetService();
157 users = self->GetUsers();
158 groups = self->GetGroups();
165 ObjectLock olock(service);
166 macroDicts.push_back(service->GetMacros());
167 service_name = service->GetName();
168 host = service->GetHost();
171 macroDicts.push_back(Service::CalculateDynamicMacros(service));
174 ObjectLock olock(host);
175 macroDicts.push_back(host->GetMacros());
176 macroDicts.push_back(Host::CalculateDynamicMacros(host));
179 IcingaApplication::Ptr app = IcingaApplication::GetInstance();
182 ObjectLock olock(app);
183 macroDicts.push_back(app->GetMacros());
186 macroDicts.push_back(IcingaApplication::CalculateDynamicMacros(app));
188 Dictionary::Ptr macros = MacroProcessor::MergeMacroDicts(macroDicts);
190 set<User::Ptr> allUsers;
192 std::copy(users.begin(), users.end(), std::inserter(allUsers, allUsers.begin()));
194 BOOST_FOREACH(const UserGroup::Ptr& ug, groups) {
195 set<User::Ptr> members = UserGroup::GetMembers(ug);
196 std::copy(members.begin(), members.end(), std::inserter(allUsers, allUsers.begin()));
199 BOOST_FOREACH(const User::Ptr& user, allUsers) {
203 ObjectLock olock(user);
204 user_name = user->GetName();
207 Logger::Write(LogDebug, "icinga", "Sending notification for user " + user_name);
208 BeginExecuteNotificationHelper(self, macros, type, user);
211 if (allUsers.size() == 0) {
212 /* Send a notification even if there are no users specified. */
213 BeginExecuteNotificationHelper(self, macros, type, User::Ptr());
217 void Notification::BeginExecuteNotificationHelper(const Notification::Ptr& self, const Dictionary::Ptr& notificationMacros, NotificationType type, const User::Ptr& user)
219 vector<Dictionary::Ptr> macroDicts;
223 ObjectLock olock(user);
224 macroDicts.push_back(user->GetMacros());
227 macroDicts.push_back(User::CalculateDynamicMacros(user));
230 macroDicts.push_back(notificationMacros);
232 Dictionary::Ptr macros = MacroProcessor::MergeMacroDicts(macroDicts);
234 ScriptTask::Ptr task;
237 ObjectLock olock(self);
239 vector<Value> arguments;
240 arguments.push_back(self);
241 arguments.push_back(macros);
242 arguments.push_back(type);
243 task = self->MakeMethodTask("notify", arguments);
246 Logger::Write(LogWarning, "icinga", "Notification object '" + self->GetName() + "' doesn't have a 'notify' method.");
251 /* We need to keep the task object alive until the completion handler is called. */
252 self->m_Tasks.insert(task);
255 task->Start(boost::bind(&Notification::NotificationCompletedHandler, self, _1));
258 void Notification::NotificationCompletedHandler(const ScriptTask::Ptr& task)
265 Logger::Write(LogInformation, "icinga", "Completed sending notification for service '" + GetService()->GetName() + "'");
266 } catch (const exception& ex) {
268 msgbuf << "Exception occured during notification for service '"
269 << GetService()->GetName() << "': " << diagnostic_information(ex);
270 String message = msgbuf.str();
272 Logger::Write(LogWarning, "icinga", message);
276 void Notification::OnAttributeChanged(const String& name, const Value& oldValue)
278 if (name == "host_name" || name == "service")
279 Service::InvalidateNotificationsCache();