]> granicus.if.org Git - icinga2/blob - lib/icinga/apievents.cpp
Merge pull request #5929 from Icinga/feature/remove-boost-assign
[icinga2] / lib / icinga / apievents.cpp
1 /******************************************************************************
2  * Icinga 2                                                                   *
3  * Copyright (C) 2012-2018 Icinga Development Team (https://www.icinga.com/)  *
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 "icinga/apievents.hpp"
21 #include "icinga/service.hpp"
22 #include "remote/eventqueue.hpp"
23 #include "base/initialize.hpp"
24 #include "base/serializer.hpp"
25 #include "base/logger.hpp"
26
27 using namespace icinga;
28
29 INITIALIZE_ONCE(&ApiEvents::StaticInitialize);
30
31 void ApiEvents::StaticInitialize(void)
32 {
33         Checkable::OnNewCheckResult.connect(&ApiEvents::CheckResultHandler);
34         Checkable::OnStateChange.connect(&ApiEvents::StateChangeHandler);
35         Checkable::OnNotificationSentToAllUsers.connect(&ApiEvents::NotificationSentToAllUsersHandler);
36
37         Checkable::OnFlappingChanged.connect(&ApiEvents::FlappingChangedHandler);
38
39         Checkable::OnAcknowledgementSet.connect(&ApiEvents::AcknowledgementSetHandler);
40         Checkable::OnAcknowledgementCleared.connect(&ApiEvents::AcknowledgementClearedHandler);
41
42         Comment::OnCommentAdded.connect(&ApiEvents::CommentAddedHandler);
43         Comment::OnCommentRemoved.connect(&ApiEvents::CommentRemovedHandler);
44
45         Downtime::OnDowntimeAdded.connect(&ApiEvents::DowntimeAddedHandler);
46         Downtime::OnDowntimeRemoved.connect(&ApiEvents::DowntimeRemovedHandler);
47         Downtime::OnDowntimeStarted.connect(&ApiEvents::DowntimeStartedHandler);
48         Downtime::OnDowntimeTriggered.connect(&ApiEvents::DowntimeTriggeredHandler);
49 }
50
51 void ApiEvents::CheckResultHandler(const Checkable::Ptr& checkable, const CheckResult::Ptr& cr, const MessageOrigin::Ptr& origin)
52 {
53         std::vector<EventQueue::Ptr> queues = EventQueue::GetQueuesForType("CheckResult");
54
55         if (queues.empty())
56                 return;
57
58         Log(LogDebug, "ApiEvents", "Processing event type 'CheckResult'.");
59
60         Dictionary::Ptr result = new Dictionary();
61         result->Set("type", "CheckResult");
62         result->Set("timestamp", Utility::GetTime());
63
64         Host::Ptr host;
65         Service::Ptr service;
66         tie(host, service) = GetHostService(checkable);
67
68         result->Set("host", host->GetName());
69         if (service)
70                 result->Set("service", service->GetShortName());
71
72         result->Set("check_result", Serialize(cr));
73
74         for (const EventQueue::Ptr& queue : queues) {
75                 queue->ProcessEvent(result);
76         }
77 }
78
79 void ApiEvents::StateChangeHandler(const Checkable::Ptr& checkable, const CheckResult::Ptr& cr, StateType type, const MessageOrigin::Ptr& origin)
80 {
81         std::vector<EventQueue::Ptr> queues = EventQueue::GetQueuesForType("StateChange");
82
83         if (queues.empty())
84                 return;
85
86         Log(LogDebug, "ApiEvents", "Processing event type 'StateChange'.");
87
88         Dictionary::Ptr result = new Dictionary();
89         result->Set("type", "StateChange");
90         result->Set("timestamp", Utility::GetTime());
91
92         Host::Ptr host;
93         Service::Ptr service;
94         tie(host, service) = GetHostService(checkable);
95
96         result->Set("host", host->GetName());
97         if (service)
98                 result->Set("service", service->GetShortName());
99
100         result->Set("state", service ? static_cast<int>(service->GetState()) : static_cast<int>(host->GetState()));
101         result->Set("state_type", checkable->GetStateType());
102         result->Set("check_result", Serialize(cr));
103
104         for (const EventQueue::Ptr& queue : queues) {
105                 queue->ProcessEvent(result);
106         }
107 }
108
109 void ApiEvents::NotificationSentToAllUsersHandler(const Notification::Ptr& notification,
110         const Checkable::Ptr& checkable, const std::set<User::Ptr>& users, NotificationType type,
111         const CheckResult::Ptr& cr, const String& author, const String& text, const MessageOrigin::Ptr& origin)
112 {
113         std::vector<EventQueue::Ptr> queues = EventQueue::GetQueuesForType("Notification");
114
115         if (queues.empty())
116                 return;
117
118         Log(LogDebug, "ApiEvents", "Processing event type 'Notification'.");
119
120         Dictionary::Ptr result = new Dictionary();
121         result->Set("type", "Notification");
122         result->Set("timestamp", Utility::GetTime());
123
124         Host::Ptr host;
125         Service::Ptr service;
126         tie(host, service) = GetHostService(checkable);
127
128         result->Set("host", host->GetName());
129         if (service)
130                 result->Set("service", service->GetShortName());
131
132         Array::Ptr userNames = new Array();
133
134         for (const User::Ptr& user : users) {
135                 userNames->Add(user->GetName());
136         }
137
138         result->Set("users", userNames);
139         result->Set("notification_type", Notification::NotificationTypeToString(type));
140         result->Set("author", author);
141         result->Set("text", text);
142         result->Set("check_result", Serialize(cr));
143
144         for (const EventQueue::Ptr& queue : queues) {
145                 queue->ProcessEvent(result);
146         }
147 }
148
149 void ApiEvents::FlappingChangedHandler(const Checkable::Ptr& checkable, const MessageOrigin::Ptr& origin)
150 {
151         std::vector<EventQueue::Ptr> queues = EventQueue::GetQueuesForType("Flapping");
152
153         if (queues.empty())
154                 return;
155
156         Log(LogDebug, "ApiEvents", "Processing event type 'Flapping'.");
157
158         Dictionary::Ptr result = new Dictionary();
159         result->Set("type", "Flapping");
160         result->Set("timestamp", Utility::GetTime());
161
162         Host::Ptr host;
163         Service::Ptr service;
164         tie(host, service) = GetHostService(checkable);
165
166         result->Set("host", host->GetName());
167         if (service)
168                 result->Set("service", service->GetShortName());
169
170         result->Set("state", service ? static_cast<int>(service->GetState()) : static_cast<int>(host->GetState()));
171         result->Set("state_type", checkable->GetStateType());
172         result->Set("is_flapping", checkable->IsFlapping());
173         result->Set("flapping_current", checkable->GetFlappingCurrent());
174         result->Set("threshold_low", checkable->GetFlappingThresholdLow());
175         result->Set("threshold_high", checkable->GetFlappingThresholdHigh());
176
177         for (const EventQueue::Ptr& queue : queues) {
178                 queue->ProcessEvent(result);
179         }
180 }
181
182 void ApiEvents::AcknowledgementSetHandler(const Checkable::Ptr& checkable,
183         const String& author, const String& comment, AcknowledgementType type,
184         bool notify, bool persistent, double expiry, const MessageOrigin::Ptr& origin)
185 {
186         std::vector<EventQueue::Ptr> queues = EventQueue::GetQueuesForType("AcknowledgementSet");
187
188         if (queues.empty())
189                 return;
190
191         Log(LogDebug, "ApiEvents", "Processing event type 'AcknowledgementSet'.");
192
193         Dictionary::Ptr result = new Dictionary();
194         result->Set("type", "AcknowledgementSet");
195         result->Set("timestamp", Utility::GetTime());
196
197         Host::Ptr host;
198         Service::Ptr service;
199         tie(host, service) = GetHostService(checkable);
200
201         result->Set("host", host->GetName());
202         if (service)
203                 result->Set("service", service->GetShortName());
204
205         result->Set("state", service ? static_cast<int>(service->GetState()) : static_cast<int>(host->GetState()));
206         result->Set("state_type", checkable->GetStateType());
207
208         result->Set("author", author);
209         result->Set("comment", comment);
210         result->Set("acknowledgement_type", type);
211         result->Set("notify", notify);
212         result->Set("persistent", persistent);
213         result->Set("expiry", expiry);
214
215         for (const EventQueue::Ptr& queue : queues) {
216                 queue->ProcessEvent(result);
217         }
218 }
219
220 void ApiEvents::AcknowledgementClearedHandler(const Checkable::Ptr& checkable, const MessageOrigin::Ptr& origin)
221 {
222         std::vector<EventQueue::Ptr> queues = EventQueue::GetQueuesForType("AcknowledgementCleared");
223
224         if (queues.empty())
225                 return;
226
227         Log(LogDebug, "ApiEvents", "Processing event type 'AcknowledgementCleared'.");
228
229         Dictionary::Ptr result = new Dictionary();
230         result->Set("type", "AcknowledgementCleared");
231         result->Set("timestamp", Utility::GetTime());
232
233         Host::Ptr host;
234         Service::Ptr service;
235         tie(host, service) = GetHostService(checkable);
236
237         result->Set("host", host->GetName());
238         if (service)
239                 result->Set("service", service->GetShortName());
240
241         result->Set("state", service ? static_cast<int>(service->GetState()) : static_cast<int>(host->GetState()));
242         result->Set("state_type", checkable->GetStateType());
243
244         for (const EventQueue::Ptr& queue : queues) {
245                 queue->ProcessEvent(result);
246         }
247
248         result->Set("acknowledgement_type", AcknowledgementNone);
249 }
250
251 void ApiEvents::CommentAddedHandler(const Comment::Ptr& comment)
252 {
253         std::vector<EventQueue::Ptr> queues = EventQueue::GetQueuesForType("CommentAdded");
254
255         if (queues.empty())
256                 return;
257
258         Log(LogDebug, "ApiEvents", "Processing event type 'CommentAdded'.");
259
260         Dictionary::Ptr result = new Dictionary();
261         result->Set("type", "CommentAdded");
262         result->Set("timestamp", Utility::GetTime());
263
264         result->Set("comment", Serialize(comment, FAConfig | FAState));
265
266         for (const EventQueue::Ptr& queue : queues) {
267                 queue->ProcessEvent(result);
268         }
269 }
270
271 void ApiEvents::CommentRemovedHandler(const Comment::Ptr& comment)
272 {
273         std::vector<EventQueue::Ptr> queues = EventQueue::GetQueuesForType("CommentRemoved");
274
275         if (queues.empty())
276                 return;
277
278         Log(LogDebug, "ApiEvents", "Processing event type 'CommentRemoved'.");
279
280         Dictionary::Ptr result = new Dictionary();
281         result->Set("type", "CommentRemoved");
282         result->Set("timestamp", Utility::GetTime());
283
284         result->Set("comment", Serialize(comment, FAConfig | FAState));
285
286         for (const EventQueue::Ptr& queue : queues) {
287                 queue->ProcessEvent(result);
288         }
289 }
290
291 void ApiEvents::DowntimeAddedHandler(const Downtime::Ptr& downtime)
292 {
293         std::vector<EventQueue::Ptr> queues = EventQueue::GetQueuesForType("DowntimeAdded");
294
295         if (queues.empty())
296                 return;
297
298         Log(LogDebug, "ApiEvents", "Processing event type 'DowntimeAdded'.");
299
300         Dictionary::Ptr result = new Dictionary();
301         result->Set("type", "DowntimeAdded");
302         result->Set("timestamp", Utility::GetTime());
303
304         result->Set("downtime", Serialize(downtime, FAConfig | FAState));
305
306         for (const EventQueue::Ptr& queue : queues) {
307                 queue->ProcessEvent(result);
308         }
309 }
310
311 void ApiEvents::DowntimeRemovedHandler(const Downtime::Ptr& downtime)
312 {
313         std::vector<EventQueue::Ptr> queues = EventQueue::GetQueuesForType("DowntimeRemoved");
314
315         if (queues.empty())
316                 return;
317
318         Log(LogDebug, "ApiEvents", "Processing event type 'DowntimeRemoved'.");
319
320         Dictionary::Ptr result = new Dictionary();
321         result->Set("type", "DowntimeRemoved");
322         result->Set("timestamp", Utility::GetTime());
323
324         result->Set("downtime", Serialize(downtime, FAConfig | FAState));
325
326         for (const EventQueue::Ptr& queue : queues) {
327                 queue->ProcessEvent(result);
328         }
329 }
330
331 void ApiEvents::DowntimeStartedHandler(const Downtime::Ptr& downtime)
332 {
333         std::vector<EventQueue::Ptr> queues = EventQueue::GetQueuesForType("DowntimeStarted");
334
335         if (queues.empty())
336                 return;
337
338         Log(LogDebug, "ApiEvents", "Processing event type 'DowntimeStarted'.");
339
340         Dictionary::Ptr result = new Dictionary();
341         result->Set("type", "DowntimeStarted");
342         result->Set("timestamp", Utility::GetTime());
343
344         result->Set("downtime", Serialize(downtime, FAConfig | FAState));
345
346         for (const EventQueue::Ptr& queue : queues) {
347                 queue->ProcessEvent(result);
348         }
349 }
350
351 void ApiEvents::DowntimeTriggeredHandler(const Downtime::Ptr& downtime)
352 {
353         std::vector<EventQueue::Ptr> queues = EventQueue::GetQueuesForType("DowntimeTriggered");
354
355         if (queues.empty())
356                 return;
357
358         Log(LogDebug, "ApiEvents", "Processing event type 'DowntimeTriggered'.");
359
360         Dictionary::Ptr result = new Dictionary();
361         result->Set("type", "DowntimeTriggered");
362         result->Set("timestamp", Utility::GetTime());
363
364         result->Set("downtime", Serialize(downtime, FAConfig | FAState));
365
366         for (const EventQueue::Ptr& queue : queues) {
367                 queue->ProcessEvent(result);
368         }
369 }