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