]> granicus.if.org Git - icinga2/blob - lib/icinga/notification.cpp
Fixed incorrect lock in Notification::BeginExecuteNotificationHelper().
[icinga2] / lib / icinga / notification.cpp
1 /******************************************************************************
2  * Icinga 2                                                                   *
3  * Copyright (C) 2012 Icinga Development Team (http://www.icinga.org/)        *
4  *                                                                            *
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.                     *
9  *                                                                            *
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.                               *
14  *                                                                            *
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  ******************************************************************************/
19
20 #include "i2-icinga.h"
21
22 using namespace icinga;
23
24 REGISTER_TYPE(Notification, NULL);
25
26 Notification::Notification(const Dictionary::Ptr& properties)
27         : DynamicObject(properties)
28 {
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);
35 }
36
37 Notification::~Notification(void)
38 {
39         Service::InvalidateNotificationsCache();
40 }
41
42 Notification::Ptr Notification::GetByName(const String& name)
43 {
44         DynamicObject::Ptr configObject = DynamicObject::GetObject("Notification", name);
45
46         if (!configObject)
47                 BOOST_THROW_EXCEPTION(invalid_argument("Notification '" + name + "' does not exist."));
48
49         return dynamic_pointer_cast<Notification>(configObject);
50 }
51
52 Service::Ptr Notification::GetService(void) const
53 {
54         Host::Ptr host = Host::GetByName(m_HostName);
55
56         if (!host)
57                 return Service::Ptr();
58
59         if (m_Service.IsEmpty())
60                 return Host::GetHostCheckService(host);
61         else
62                 return Host::GetServiceByShortName(host, m_Service);
63 }
64
65 Value Notification::GetNotificationCommand(void) const
66 {
67         return m_NotificationCommand;
68 }
69
70 Dictionary::Ptr Notification::GetMacros(void) const
71 {
72         return m_Macros;
73 }
74
75 set<User::Ptr> Notification::GetUsers(void) const
76 {
77         set<User::Ptr> result;
78
79         Dictionary::Ptr users = m_Users;
80
81         if (users) {
82                 String name;
83                 BOOST_FOREACH(tie(tuples::ignore, name), users) {
84                         User::Ptr user = User::GetByName(name);
85
86                         if (!user)
87                                 continue;
88
89                         result.insert(user);
90                 }
91         }
92
93         return result;
94 }
95
96 set<UserGroup::Ptr> Notification::GetGroups(void) const
97 {
98         set<UserGroup::Ptr> result;
99
100         Dictionary::Ptr groups = m_Groups;
101
102         if (groups) {
103                 String name;
104                 BOOST_FOREACH(tie(tuples::ignore, name), groups) {
105                         UserGroup::Ptr ug = UserGroup::GetByName(name);
106
107                         if (!ug)
108                                 continue;
109
110                         result.insert(ug);
111                 }
112         }
113
114         return result;
115 }
116
117 String Notification::NotificationTypeToString(NotificationType type)
118 {
119         switch (type) {
120                 case NotificationDowntimeStart:
121                         return "DOWNTIMESTART";
122                 case NotificationDowntimeEnd:
123                         return "DOWNTIMEEND";
124                 case NotificationDowntimeRemoved:
125                         return "DOWNTIMECANCELLED";
126                 case NotificationCustom:
127                         return "CUSTOM";
128                 case NotificationAcknowledgement:
129                         return "ACKNOWLEDGEMENT";
130                 case NotificationProblem:
131                         return "PROBLEM";
132                 case NotificationRecovery:
133                         return "RECOVERY";
134                 default:
135                         return "UNKNOWN_NOTIFICATION";
136         }
137 }
138
139 void Notification::BeginExecuteNotification(const Notification::Ptr& self, NotificationType type)
140 {
141
142         vector<Dictionary::Ptr> macroDicts;
143
144         Dictionary::Ptr notificationMacros = boost::make_shared<Dictionary>();
145         notificationMacros->Set("NOTIFICATIONTYPE", NotificationTypeToString(type));
146         macroDicts.push_back(notificationMacros);
147
148         Service::Ptr service;
149         set<User::Ptr> users;
150         set<UserGroup::Ptr> groups;
151
152         {
153                 ObjectLock olock(self);
154                 macroDicts.push_back(self->GetMacros());
155                 service = self->GetService();
156                 users = self->GetUsers();
157                 groups = self->GetGroups();
158         }
159
160         Host::Ptr host;
161         String service_name;
162
163         {
164                 ObjectLock olock(service);
165                 macroDicts.push_back(service->GetMacros());
166                 service_name = service->GetName();
167                 host = service->GetHost();
168         }
169
170         macroDicts.push_back(Service::CalculateDynamicMacros(service));
171
172         {
173                 ObjectLock olock(host);
174                 macroDicts.push_back(host->GetMacros());
175                 macroDicts.push_back(Host::CalculateDynamicMacros(host));
176         }
177
178         IcingaApplication::Ptr app = IcingaApplication::GetInstance();
179
180         {
181                 ObjectLock olock(app);
182                 macroDicts.push_back(app->GetMacros());
183         }
184
185         macroDicts.push_back(IcingaApplication::CalculateDynamicMacros(app));
186
187         Dictionary::Ptr macros = MacroProcessor::MergeMacroDicts(macroDicts);
188
189         set<User::Ptr> allUsers;
190
191         std::copy(users.begin(), users.end(), std::inserter(allUsers, allUsers.begin()));
192
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()));
196         }
197
198         BOOST_FOREACH(const User::Ptr& user, allUsers) {
199                 String user_name;
200
201                 {
202                         ObjectLock olock(user);
203                         user_name = user->GetName();
204                 }
205
206                 Logger::Write(LogDebug, "icinga", "Sending notification for user " + user_name);
207                 BeginExecuteNotificationHelper(self, macros, type, user);
208         }
209
210         if (allUsers.size() == 0) {
211                 /* Send a notification even if there are no users specified. */
212                 BeginExecuteNotificationHelper(self, macros, type, User::Ptr());
213         }
214 }
215
216 void Notification::BeginExecuteNotificationHelper(const Notification::Ptr& self, const Dictionary::Ptr& notificationMacros, NotificationType type, const User::Ptr& user)
217 {
218         vector<Dictionary::Ptr> macroDicts;
219
220         if (user) {
221                 {
222                         ObjectLock olock(user);
223                         macroDicts.push_back(user->GetMacros());
224                 }
225
226                 macroDicts.push_back(User::CalculateDynamicMacros(user));
227         }
228
229         macroDicts.push_back(notificationMacros);
230
231         Dictionary::Ptr macros = MacroProcessor::MergeMacroDicts(macroDicts);
232
233         ScriptTask::Ptr task;
234
235         {
236                 ObjectLock olock(self);
237
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);
243
244                 if (!task) {
245                         Logger::Write(LogWarning, "icinga", "Notification object '" + self->GetName() + "' doesn't have a 'notify' method.");
246
247                         return;
248                 }
249
250                 /* We need to keep the task object alive until the completion handler is called. */
251                 self->m_Tasks.insert(task);
252         }
253
254         task->Start(boost::bind(&Notification::NotificationCompletedHandler, self, _1));
255 }
256
257 void Notification::NotificationCompletedHandler(const ScriptTask::Ptr& task)
258 {
259         m_Tasks.erase(task);
260
261         try {
262                 {
263                         ObjectLock tlock(task);
264                         (void) task->GetResult();
265                 }
266
267                 Logger::Write(LogInformation, "icinga", "Completed sending notification for service '" + GetService()->GetName() + "'");
268         } catch (const exception& ex) {
269                 stringstream msgbuf;
270                 msgbuf << "Exception occured during notification for service '"
271                        << GetService()->GetName() << "': " << diagnostic_information(ex);
272                 String message = msgbuf.str();
273
274                 Logger::Write(LogWarning, "icinga", message);
275         }
276 }
277
278 void Notification::OnAttributeChanged(const String& name, const Value& oldValue)
279 {
280         if (name == "host_name" || name == "service")
281                 Service::InvalidateNotificationsCache();
282 }