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