1 /* Icinga 2 | (c) 2012 Icinga GmbH | GPLv2+ */
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"
11 using namespace icinga;
13 INITIALIZE_ONCE(&ApiEvents::StaticInitialize);
15 void ApiEvents::StaticInitialize()
17 Checkable::OnNewCheckResult.connect(&ApiEvents::CheckResultHandler);
18 Checkable::OnStateChange.connect(&ApiEvents::StateChangeHandler);
19 Checkable::OnNotificationSentToAllUsers.connect(&ApiEvents::NotificationSentToAllUsersHandler);
21 Checkable::OnFlappingChanged.connect(&ApiEvents::FlappingChangedHandler);
23 Checkable::OnAcknowledgementSet.connect(&ApiEvents::AcknowledgementSetHandler);
24 Checkable::OnAcknowledgementCleared.connect(&ApiEvents::AcknowledgementClearedHandler);
26 Comment::OnCommentAdded.connect(&ApiEvents::CommentAddedHandler);
27 Comment::OnCommentRemoved.connect(&ApiEvents::CommentRemovedHandler);
29 Downtime::OnDowntimeAdded.connect(&ApiEvents::DowntimeAddedHandler);
30 Downtime::OnDowntimeRemoved.connect(&ApiEvents::DowntimeRemovedHandler);
31 Downtime::OnDowntimeStarted.connect(&ApiEvents::DowntimeStartedHandler);
32 Downtime::OnDowntimeTriggered.connect(&ApiEvents::DowntimeTriggeredHandler);
35 void ApiEvents::CheckResultHandler(const Checkable::Ptr& checkable, const CheckResult::Ptr& cr, const MessageOrigin::Ptr& origin)
37 std::vector<EventQueue::Ptr> queues = EventQueue::GetQueuesForType("CheckResult");
42 Log(LogDebug, "ApiEvents", "Processing event type 'CheckResult'.");
44 Dictionary::Ptr result = new Dictionary();
45 result->Set("type", "CheckResult");
46 result->Set("timestamp", Utility::GetTime());
50 tie(host, service) = GetHostService(checkable);
52 result->Set("host", host->GetName());
54 result->Set("service", service->GetShortName());
56 result->Set("check_result", Serialize(cr));
58 for (const EventQueue::Ptr& queue : queues) {
59 queue->ProcessEvent(result);
63 void ApiEvents::StateChangeHandler(const Checkable::Ptr& checkable, const CheckResult::Ptr& cr, StateType type, const MessageOrigin::Ptr& origin)
65 std::vector<EventQueue::Ptr> queues = EventQueue::GetQueuesForType("StateChange");
70 Log(LogDebug, "ApiEvents", "Processing event type 'StateChange'.");
72 Dictionary::Ptr result = new Dictionary();
73 result->Set("type", "StateChange");
74 result->Set("timestamp", Utility::GetTime());
78 tie(host, service) = GetHostService(checkable);
80 result->Set("host", host->GetName());
82 result->Set("service", service->GetShortName());
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));
88 for (const EventQueue::Ptr& queue : queues) {
89 queue->ProcessEvent(result);
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)
97 std::vector<EventQueue::Ptr> queues = EventQueue::GetQueuesForType("Notification");
102 Log(LogDebug, "ApiEvents", "Processing event type 'Notification'.");
104 Dictionary::Ptr result = new Dictionary();
105 result->Set("type", "Notification");
106 result->Set("timestamp", Utility::GetTime());
109 Service::Ptr service;
110 tie(host, service) = GetHostService(checkable);
112 result->Set("host", host->GetName());
114 result->Set("service", service->GetShortName());
116 NotificationCommand::Ptr command = notification->GetCommand();
119 result->Set("command", command->GetName());
123 for (const User::Ptr& user : users) {
124 userNames.push_back(user->GetName());
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));
133 for (const EventQueue::Ptr& queue : queues) {
134 queue->ProcessEvent(result);
138 void ApiEvents::FlappingChangedHandler(const Checkable::Ptr& checkable, const MessageOrigin::Ptr& origin)
140 std::vector<EventQueue::Ptr> queues = EventQueue::GetQueuesForType("Flapping");
145 Log(LogDebug, "ApiEvents", "Processing event type 'Flapping'.");
147 Dictionary::Ptr result = new Dictionary();
148 result->Set("type", "Flapping");
149 result->Set("timestamp", Utility::GetTime());
152 Service::Ptr service;
153 tie(host, service) = GetHostService(checkable);
155 result->Set("host", host->GetName());
157 result->Set("service", service->GetShortName());
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());
166 for (const EventQueue::Ptr& queue : queues) {
167 queue->ProcessEvent(result);
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)
175 std::vector<EventQueue::Ptr> queues = EventQueue::GetQueuesForType("AcknowledgementSet");
180 Log(LogDebug, "ApiEvents", "Processing event type 'AcknowledgementSet'.");
182 Dictionary::Ptr result = new Dictionary();
183 result->Set("type", "AcknowledgementSet");
184 result->Set("timestamp", Utility::GetTime());
187 Service::Ptr service;
188 tie(host, service) = GetHostService(checkable);
190 result->Set("host", host->GetName());
192 result->Set("service", service->GetShortName());
194 result->Set("state", service ? static_cast<int>(service->GetState()) : static_cast<int>(host->GetState()));
195 result->Set("state_type", checkable->GetStateType());
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);
204 for (const EventQueue::Ptr& queue : queues) {
205 queue->ProcessEvent(result);
209 void ApiEvents::AcknowledgementClearedHandler(const Checkable::Ptr& checkable, const MessageOrigin::Ptr& origin)
211 std::vector<EventQueue::Ptr> queues = EventQueue::GetQueuesForType("AcknowledgementCleared");
216 Log(LogDebug, "ApiEvents", "Processing event type 'AcknowledgementCleared'.");
218 Dictionary::Ptr result = new Dictionary();
219 result->Set("type", "AcknowledgementCleared");
220 result->Set("timestamp", Utility::GetTime());
223 Service::Ptr service;
224 tie(host, service) = GetHostService(checkable);
226 result->Set("host", host->GetName());
228 result->Set("service", service->GetShortName());
230 result->Set("state", service ? static_cast<int>(service->GetState()) : static_cast<int>(host->GetState()));
231 result->Set("state_type", checkable->GetStateType());
233 for (const EventQueue::Ptr& queue : queues) {
234 queue->ProcessEvent(result);
237 result->Set("acknowledgement_type", AcknowledgementNone);
240 void ApiEvents::CommentAddedHandler(const Comment::Ptr& comment)
242 std::vector<EventQueue::Ptr> queues = EventQueue::GetQueuesForType("CommentAdded");
247 Log(LogDebug, "ApiEvents", "Processing event type 'CommentAdded'.");
249 Dictionary::Ptr result = new Dictionary({
250 { "type", "CommentAdded" },
251 { "timestamp", Utility::GetTime() },
252 { "comment", Serialize(comment, FAConfig | FAState) }
255 for (const EventQueue::Ptr& queue : queues) {
256 queue->ProcessEvent(result);
260 void ApiEvents::CommentRemovedHandler(const Comment::Ptr& comment)
262 std::vector<EventQueue::Ptr> queues = EventQueue::GetQueuesForType("CommentRemoved");
267 Log(LogDebug, "ApiEvents", "Processing event type 'CommentRemoved'.");
269 Dictionary::Ptr result = new Dictionary({
270 { "type", "CommentRemoved" },
271 { "timestamp", Utility::GetTime() },
272 { "comment", Serialize(comment, FAConfig | FAState) }
275 for (const EventQueue::Ptr& queue : queues) {
276 queue->ProcessEvent(result);
280 void ApiEvents::DowntimeAddedHandler(const Downtime::Ptr& downtime)
282 std::vector<EventQueue::Ptr> queues = EventQueue::GetQueuesForType("DowntimeAdded");
287 Log(LogDebug, "ApiEvents", "Processing event type 'DowntimeAdded'.");
289 Dictionary::Ptr result = new Dictionary({
290 { "type", "DowntimeAdded" },
291 { "timestamp", Utility::GetTime() },
292 { "downtime", Serialize(downtime, FAConfig | FAState) }
295 for (const EventQueue::Ptr& queue : queues) {
296 queue->ProcessEvent(result);
300 void ApiEvents::DowntimeRemovedHandler(const Downtime::Ptr& downtime)
302 std::vector<EventQueue::Ptr> queues = EventQueue::GetQueuesForType("DowntimeRemoved");
307 Log(LogDebug, "ApiEvents", "Processing event type 'DowntimeRemoved'.");
309 Dictionary::Ptr result = new Dictionary({
310 { "type", "DowntimeRemoved" },
311 { "timestamp", Utility::GetTime() },
312 { "downtime", Serialize(downtime, FAConfig | FAState) }
315 for (const EventQueue::Ptr& queue : queues) {
316 queue->ProcessEvent(result);
320 void ApiEvents::DowntimeStartedHandler(const Downtime::Ptr& downtime)
322 std::vector<EventQueue::Ptr> queues = EventQueue::GetQueuesForType("DowntimeStarted");
327 Log(LogDebug, "ApiEvents", "Processing event type 'DowntimeStarted'.");
329 Dictionary::Ptr result = new Dictionary({
330 { "type", "DowntimeStarted" },
331 { "timestamp", Utility::GetTime() },
332 { "downtime", Serialize(downtime, FAConfig | FAState) }
335 for (const EventQueue::Ptr& queue : queues) {
336 queue->ProcessEvent(result);
340 void ApiEvents::DowntimeTriggeredHandler(const Downtime::Ptr& downtime)
342 std::vector<EventQueue::Ptr> queues = EventQueue::GetQueuesForType("DowntimeTriggered");
347 Log(LogDebug, "ApiEvents", "Processing event type 'DowntimeTriggered'.");
349 Dictionary::Ptr result = new Dictionary({
350 { "type", "DowntimeTriggered" },
351 { "timestamp", Utility::GetTime() },
352 { "downtime", Serialize(downtime, FAConfig | FAState) }
355 for (const EventQueue::Ptr& queue : queues) {
356 queue->ProcessEvent(result);