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