1 /******************************************************************************
3 * Copyright (C) 2012-2018 Icinga Development Team (https://icinga.com/) *
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. *
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. *
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 ******************************************************************************/
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"
28 using namespace icinga;
30 INITIALIZE_ONCE(&ApiEvents::StaticInitialize);
32 void ApiEvents::StaticInitialize()
34 Checkable::OnNewCheckResult.connect(&ApiEvents::CheckResultHandler);
35 Checkable::OnStateChange.connect(&ApiEvents::StateChangeHandler);
36 Checkable::OnNotificationSentToAllUsers.connect(&ApiEvents::NotificationSentToAllUsersHandler);
38 Checkable::OnFlappingChanged.connect(&ApiEvents::FlappingChangedHandler);
40 Checkable::OnAcknowledgementSet.connect(&ApiEvents::AcknowledgementSetHandler);
41 Checkable::OnAcknowledgementCleared.connect(&ApiEvents::AcknowledgementClearedHandler);
43 Comment::OnCommentAdded.connect(&ApiEvents::CommentAddedHandler);
44 Comment::OnCommentRemoved.connect(&ApiEvents::CommentRemovedHandler);
46 Downtime::OnDowntimeAdded.connect(&ApiEvents::DowntimeAddedHandler);
47 Downtime::OnDowntimeRemoved.connect(&ApiEvents::DowntimeRemovedHandler);
48 Downtime::OnDowntimeStarted.connect(&ApiEvents::DowntimeStartedHandler);
49 Downtime::OnDowntimeTriggered.connect(&ApiEvents::DowntimeTriggeredHandler);
52 void ApiEvents::CheckResultHandler(const Checkable::Ptr& checkable, const CheckResult::Ptr& cr, const MessageOrigin::Ptr& origin)
54 std::vector<EventQueue::Ptr> queues = EventQueue::GetQueuesForType("CheckResult");
59 Log(LogDebug, "ApiEvents", "Processing event type 'CheckResult'.");
61 Dictionary::Ptr result = new Dictionary();
62 result->Set("type", "CheckResult");
63 result->Set("timestamp", Utility::GetTime());
67 tie(host, service) = GetHostService(checkable);
69 result->Set("host", host->GetName());
71 result->Set("service", service->GetShortName());
73 result->Set("check_result", Serialize(cr));
75 for (const EventQueue::Ptr& queue : queues) {
76 queue->ProcessEvent(result);
80 void ApiEvents::StateChangeHandler(const Checkable::Ptr& checkable, const CheckResult::Ptr& cr, StateType type, const MessageOrigin::Ptr& origin)
82 std::vector<EventQueue::Ptr> queues = EventQueue::GetQueuesForType("StateChange");
87 Log(LogDebug, "ApiEvents", "Processing event type 'StateChange'.");
89 Dictionary::Ptr result = new Dictionary();
90 result->Set("type", "StateChange");
91 result->Set("timestamp", Utility::GetTime());
95 tie(host, service) = GetHostService(checkable);
97 result->Set("host", host->GetName());
99 result->Set("service", service->GetShortName());
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));
105 for (const EventQueue::Ptr& queue : queues) {
106 queue->ProcessEvent(result);
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)
114 std::vector<EventQueue::Ptr> queues = EventQueue::GetQueuesForType("Notification");
119 Log(LogDebug, "ApiEvents", "Processing event type 'Notification'.");
121 Dictionary::Ptr result = new Dictionary();
122 result->Set("type", "Notification");
123 result->Set("timestamp", Utility::GetTime());
126 Service::Ptr service;
127 tie(host, service) = GetHostService(checkable);
129 result->Set("host", host->GetName());
131 result->Set("service", service->GetShortName());
133 NotificationCommand::Ptr command = notification->GetCommand();
136 result->Set("command", command->GetName());
140 for (const User::Ptr& user : users) {
141 userNames.push_back(user->GetName());
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));
150 for (const EventQueue::Ptr& queue : queues) {
151 queue->ProcessEvent(result);
155 void ApiEvents::FlappingChangedHandler(const Checkable::Ptr& checkable, const MessageOrigin::Ptr& origin)
157 std::vector<EventQueue::Ptr> queues = EventQueue::GetQueuesForType("Flapping");
162 Log(LogDebug, "ApiEvents", "Processing event type 'Flapping'.");
164 Dictionary::Ptr result = new Dictionary();
165 result->Set("type", "Flapping");
166 result->Set("timestamp", Utility::GetTime());
169 Service::Ptr service;
170 tie(host, service) = GetHostService(checkable);
172 result->Set("host", host->GetName());
174 result->Set("service", service->GetShortName());
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());
183 for (const EventQueue::Ptr& queue : queues) {
184 queue->ProcessEvent(result);
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)
192 std::vector<EventQueue::Ptr> queues = EventQueue::GetQueuesForType("AcknowledgementSet");
197 Log(LogDebug, "ApiEvents", "Processing event type 'AcknowledgementSet'.");
199 Dictionary::Ptr result = new Dictionary();
200 result->Set("type", "AcknowledgementSet");
201 result->Set("timestamp", Utility::GetTime());
204 Service::Ptr service;
205 tie(host, service) = GetHostService(checkable);
207 result->Set("host", host->GetName());
209 result->Set("service", service->GetShortName());
211 result->Set("state", service ? static_cast<int>(service->GetState()) : static_cast<int>(host->GetState()));
212 result->Set("state_type", checkable->GetStateType());
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);
221 for (const EventQueue::Ptr& queue : queues) {
222 queue->ProcessEvent(result);
226 void ApiEvents::AcknowledgementClearedHandler(const Checkable::Ptr& checkable, const MessageOrigin::Ptr& origin)
228 std::vector<EventQueue::Ptr> queues = EventQueue::GetQueuesForType("AcknowledgementCleared");
233 Log(LogDebug, "ApiEvents", "Processing event type 'AcknowledgementCleared'.");
235 Dictionary::Ptr result = new Dictionary();
236 result->Set("type", "AcknowledgementCleared");
237 result->Set("timestamp", Utility::GetTime());
240 Service::Ptr service;
241 tie(host, service) = GetHostService(checkable);
243 result->Set("host", host->GetName());
245 result->Set("service", service->GetShortName());
247 result->Set("state", service ? static_cast<int>(service->GetState()) : static_cast<int>(host->GetState()));
248 result->Set("state_type", checkable->GetStateType());
250 for (const EventQueue::Ptr& queue : queues) {
251 queue->ProcessEvent(result);
254 result->Set("acknowledgement_type", AcknowledgementNone);
257 void ApiEvents::CommentAddedHandler(const Comment::Ptr& comment)
259 std::vector<EventQueue::Ptr> queues = EventQueue::GetQueuesForType("CommentAdded");
264 Log(LogDebug, "ApiEvents", "Processing event type 'CommentAdded'.");
266 Dictionary::Ptr result = new Dictionary({
267 { "type", "CommentAdded" },
268 { "timestamp", Utility::GetTime() },
269 { "comment", Serialize(comment, FAConfig | FAState) }
272 for (const EventQueue::Ptr& queue : queues) {
273 queue->ProcessEvent(result);
277 void ApiEvents::CommentRemovedHandler(const Comment::Ptr& comment)
279 std::vector<EventQueue::Ptr> queues = EventQueue::GetQueuesForType("CommentRemoved");
284 Log(LogDebug, "ApiEvents", "Processing event type 'CommentRemoved'.");
286 Dictionary::Ptr result = new Dictionary({
287 { "type", "CommentRemoved" },
288 { "timestamp", Utility::GetTime() },
289 { "comment", Serialize(comment, FAConfig | FAState) }
292 for (const EventQueue::Ptr& queue : queues) {
293 queue->ProcessEvent(result);
297 void ApiEvents::DowntimeAddedHandler(const Downtime::Ptr& downtime)
299 std::vector<EventQueue::Ptr> queues = EventQueue::GetQueuesForType("DowntimeAdded");
304 Log(LogDebug, "ApiEvents", "Processing event type 'DowntimeAdded'.");
306 Dictionary::Ptr result = new Dictionary({
307 { "type", "DowntimeAdded" },
308 { "timestamp", Utility::GetTime() },
309 { "downtime", Serialize(downtime, FAConfig | FAState) }
312 for (const EventQueue::Ptr& queue : queues) {
313 queue->ProcessEvent(result);
317 void ApiEvents::DowntimeRemovedHandler(const Downtime::Ptr& downtime)
319 std::vector<EventQueue::Ptr> queues = EventQueue::GetQueuesForType("DowntimeRemoved");
324 Log(LogDebug, "ApiEvents", "Processing event type 'DowntimeRemoved'.");
326 Dictionary::Ptr result = new Dictionary({
327 { "type", "DowntimeRemoved" },
328 { "timestamp", Utility::GetTime() },
329 { "downtime", Serialize(downtime, FAConfig | FAState) }
332 for (const EventQueue::Ptr& queue : queues) {
333 queue->ProcessEvent(result);
337 void ApiEvents::DowntimeStartedHandler(const Downtime::Ptr& downtime)
339 std::vector<EventQueue::Ptr> queues = EventQueue::GetQueuesForType("DowntimeStarted");
344 Log(LogDebug, "ApiEvents", "Processing event type 'DowntimeStarted'.");
346 Dictionary::Ptr result = new Dictionary({
347 { "type", "DowntimeStarted" },
348 { "timestamp", Utility::GetTime() },
349 { "downtime", Serialize(downtime, FAConfig | FAState) }
352 for (const EventQueue::Ptr& queue : queues) {
353 queue->ProcessEvent(result);
357 void ApiEvents::DowntimeTriggeredHandler(const Downtime::Ptr& downtime)
359 std::vector<EventQueue::Ptr> queues = EventQueue::GetQueuesForType("DowntimeTriggered");
364 Log(LogDebug, "ApiEvents", "Processing event type 'DowntimeTriggered'.");
366 Dictionary::Ptr result = new Dictionary({
367 { "type", "DowntimeTriggered" },
368 { "timestamp", Utility::GetTime() },
369 { "downtime", Serialize(downtime, FAConfig | FAState) }
372 for (const EventQueue::Ptr& queue : queues) {
373 queue->ProcessEvent(result);