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