]> granicus.if.org Git - icinga2/blob - lib/icinga/notification.cpp
Fix cppcheck warnings.
[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)
165 {
166         assert(!OwnsLock());
167
168         vector<Dictionary::Ptr> macroDicts;
169
170         Dictionary::Ptr notificationMacros = boost::make_shared<Dictionary>();
171         notificationMacros->Set("NOTIFICATIONTYPE", NotificationTypeToString(type));
172         macroDicts.push_back(notificationMacros);
173
174         macroDicts.push_back(GetMacros());
175
176         Service::Ptr service = GetService();
177
178         if (service) {
179                 macroDicts.push_back(service->GetMacros());
180                 macroDicts.push_back(service->CalculateDynamicMacros());
181
182                 Host::Ptr host = service->GetHost();
183
184                 if (host) {
185                         macroDicts.push_back(host->GetMacros());
186                         macroDicts.push_back(host->CalculateDynamicMacros());
187                 }
188         }
189
190         IcingaApplication::Ptr app = IcingaApplication::GetInstance();
191         macroDicts.push_back(app->GetMacros());
192
193         macroDicts.push_back(IcingaApplication::CalculateDynamicMacros());
194
195         Dictionary::Ptr macros = MacroProcessor::MergeMacroDicts(macroDicts);
196
197         set<User::Ptr> allUsers;
198
199         set<User::Ptr> users = GetUsers();
200         std::copy(users.begin(), users.end(), std::inserter(allUsers, allUsers.begin()));
201
202         BOOST_FOREACH(const UserGroup::Ptr& ug, GetGroups()) {
203                 set<User::Ptr> members = ug->GetMembers();
204                 std::copy(members.begin(), members.end(), std::inserter(allUsers, allUsers.begin()));
205         }
206
207         BOOST_FOREACH(const User::Ptr& user, allUsers) {
208                 Logger::Write(LogDebug, "icinga", "Sending notification for user " + user->GetName());
209                 BeginExecuteNotificationHelper(macros, type, user);
210         }
211
212         if (allUsers.empty()) {
213                 /* Send a notification even if there are no users specified. */
214                 BeginExecuteNotificationHelper(macros, type, User::Ptr());
215         }
216 }
217
218 /**
219  * @threadsafety Always.
220  */
221 void Notification::BeginExecuteNotificationHelper(const Dictionary::Ptr& notificationMacros, NotificationType type, const User::Ptr& user)
222 {
223         assert(!OwnsLock());
224
225         vector<Dictionary::Ptr> macroDicts;
226
227         if (user) {
228                 macroDicts.push_back(user->GetMacros());
229                 macroDicts.push_back(user->CalculateDynamicMacros());
230         }
231
232         macroDicts.push_back(notificationMacros);
233
234         Dictionary::Ptr macros = MacroProcessor::MergeMacroDicts(macroDicts);
235
236         Notification::Ptr self = GetSelf();
237
238         vector<Value> arguments;
239         arguments.push_back(self);
240         arguments.push_back(macros);
241         arguments.push_back(type);
242
243         ScriptTask::Ptr task;
244         task = MakeMethodTask("notify", arguments);
245
246         if (!task) {
247                 Logger::Write(LogWarning, "icinga", "Notification object '" + GetName() + "' doesn't have a 'notify' method.");
248
249                 return;
250         }
251
252         {
253                 ObjectLock olock(this);
254
255                 /* We need to keep the task object alive until the completion handler is called. */
256                 m_Tasks.insert(task);
257         }
258
259         task->Start(boost::bind(&Notification::NotificationCompletedHandler, self, _1));
260 }
261
262 /**
263  * @threadsafety Always.
264  */
265 void Notification::NotificationCompletedHandler(const ScriptTask::Ptr& task)
266 {
267         assert(!OwnsLock());
268
269         {
270                 ObjectLock olock(this);
271
272                 m_Tasks.erase(task);
273         }
274
275         try {
276                 task->GetResult();
277
278                 Logger::Write(LogInformation, "icinga", "Completed sending notification for service '" + GetService()->GetName() + "'");
279         } catch (const exception& ex) {
280                 stringstream msgbuf;
281                 msgbuf << "Exception occured during notification for service '"
282                        << GetService()->GetName() << "': " << diagnostic_information(ex);
283                 String message = msgbuf.str();
284
285                 Logger::Write(LogWarning, "icinga", message);
286         }
287 }
288
289 /**
290  * @threadsafety Always.
291  */
292 void Notification::OnAttributeChanged(const String& name)
293 {
294         assert(!OwnsLock());
295
296         if (name == "host_name" || name == "service")
297                 Service::InvalidateNotificationsCache();
298 }