]> granicus.if.org Git - icinga2/blob - lib/icinga/notification.cpp
Make sure we don't block on write() while holding locks.
[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         Service::InvalidateNotificationsCache();
30 }
31
32 Notification::~Notification(void)
33 {
34         Service::InvalidateNotificationsCache();
35 }
36
37 bool Notification::Exists(const String& name)
38 {
39         return (DynamicObject::GetObject("Notification", name));
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(Get("host_name"));
55         String service = Get("service");
56
57         if (service.IsEmpty())
58                 return host->GetHostCheckService();
59         else
60                 return host->GetServiceByShortName(service);
61 }
62
63 Value Notification::GetNotificationCommand(void) const
64 {
65         return Get("notification_command");
66 }
67
68 Dictionary::Ptr Notification::GetMacros(void) const
69 {
70         return Get("macros");
71 }
72
73 set<User::Ptr> Notification::GetUsers(void) const
74 {
75         set<User::Ptr> result;
76
77         Dictionary::Ptr users = Get("users");
78
79         if (users) {
80                 String name;
81                 BOOST_FOREACH(tie(tuples::ignore, name), users) {
82                         result.insert(User::GetByName(name));
83                 }
84         }
85
86         return result;
87 }
88
89 String Notification::NotificationTypeToString(NotificationType type)
90 {
91         switch (type) {
92                 case NotificationDowntimeStart:
93                         return "DOWNTIMESTART";
94                 case NotificationDowntimeEnd:
95                         return "DOWNTIMEEND";
96                 case NotificationDowntimeRemoved:
97                         return "DOWNTIMECANCELLED";
98                 case NotificationCustom:
99                         return "CUSTOM";
100                 case NotificationAcknowledgement:
101                         return "ACKNOWLEDGEMENT";
102                 case NotificationProblem:
103                         return "PROBLEM";
104                 case NotificationRecovery:
105                         return "RECOVERY";
106                 default:
107                         return "UNKNOWN_NOTIFICATION";
108         }
109 }
110
111 void Notification::BeginExecuteNotification(const Notification::Ptr& self, NotificationType type)
112 {
113
114         vector<Dictionary::Ptr> macroDicts;
115
116         Dictionary::Ptr notificationMacros = boost::make_shared<Dictionary>();
117         notificationMacros->Set("NOTIFICATIONTYPE", NotificationTypeToString(type));
118         macroDicts.push_back(notificationMacros);
119
120         Service::Ptr service;
121         set<User::Ptr> users;
122
123         {
124                 ObjectLock olock(self);
125                 macroDicts.push_back(self->GetMacros());
126                 service = self->GetService();
127                 users = self->GetUsers();
128         }
129
130         Host::Ptr host;
131         String service_name;
132
133         {
134                 ObjectLock olock(service);
135                 macroDicts.push_back(service->GetMacros());
136                 service_name = service->GetName();
137                 host = service->GetHost();
138         }
139
140         macroDicts.push_back(Service::CalculateDynamicMacros(service));
141
142         {
143                 ObjectLock olock(host);
144                 macroDicts.push_back(host->GetMacros());
145                 macroDicts.push_back(Host::CalculateDynamicMacros(host));
146         }
147
148         IcingaApplication::Ptr app = IcingaApplication::GetInstance();
149
150         {
151                 ObjectLock olock(app);
152                 macroDicts.push_back(app->GetMacros());
153         }
154
155         macroDicts.push_back(IcingaApplication::CalculateDynamicMacros(app));
156
157         Dictionary::Ptr macros = MacroProcessor::MergeMacroDicts(macroDicts);
158
159         int count = 0;
160
161         BOOST_FOREACH(const User::Ptr& user, users) {
162                 BeginExecuteNotificationHelper(self, macros, type, user);
163                 count++;
164         }
165
166         if (count == 0) {
167                 /* Send a notification even if there are no users specified. */
168                 BeginExecuteNotificationHelper(self, macros, type, User::Ptr());
169         }
170 }
171
172 void Notification::BeginExecuteNotificationHelper(const Notification::Ptr& self, const Dictionary::Ptr& notificationMacros, NotificationType type, const User::Ptr& user)
173 {
174         vector<Dictionary::Ptr> macroDicts;
175
176         if (user) {
177                 {
178                         ObjectLock olock(self);
179                         macroDicts.push_back(user->GetMacros());
180                 }
181
182                 macroDicts.push_back(User::CalculateDynamicMacros(user));
183         }
184
185         macroDicts.push_back(notificationMacros);
186
187         Dictionary::Ptr macros = MacroProcessor::MergeMacroDicts(macroDicts);
188
189         ScriptTask::Ptr task;
190
191         {
192                 ObjectLock olock(self);
193
194                 vector<Value> arguments;
195                 arguments.push_back(self);
196                 arguments.push_back(macros);
197                 arguments.push_back(type);
198                 task = self->MakeMethodTask("notify", arguments);
199
200                 if (!task) {
201                         Logger::Write(LogWarning, "icinga", "Notification object '" + self->GetName() + "' doesn't have a 'notify' method.");
202
203                         return;
204                 }
205
206                 /* We need to keep the task object alive until the completion handler is called. */
207                 self->m_Tasks.insert(task);
208         }
209
210         task->Start(boost::bind(&Notification::NotificationCompletedHandler, self, _1));
211 }
212
213 void Notification::NotificationCompletedHandler(const ScriptTask::Ptr& task)
214 {
215         m_Tasks.erase(task);
216
217         try {
218                 {
219                         ObjectLock tlock(task);
220                         (void) task->GetResult();
221                 }
222
223                 Logger::Write(LogInformation, "icinga", "Completed sending notification for service '" + GetService()->GetName() + "'");
224         } catch (const exception& ex) {
225                 stringstream msgbuf;
226                 msgbuf << "Exception occured during notification for service '"
227                        << GetService()->GetName() << "': " << diagnostic_information(ex);
228                 String message = msgbuf.str();
229
230                 Logger::Write(LogWarning, "icinga", message);
231         }
232 }
233
234 void Notification::OnAttributeChanged(const String& name, const Value& oldValue)
235 {
236         if (name == "host_name" || name == "service")
237                 Service::InvalidateNotificationsCache();
238 }