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