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