]> granicus.if.org Git - icinga2/blobdiff - lib/icinga/apievents.cpp
Merge pull request #6727 from Icinga/feature/cluster-config-sync-stage
[icinga2] / lib / icinga / apievents.cpp
index 2dfeda7e5c5ee1d5e38b544a385e56c36bb22966..226afc1adfe1daf468d8e64d0f972c08ff24dc32 100644 (file)
-/******************************************************************************
- * Icinga 2                                                                   *
- * Copyright (C) 2012-2015 Icinga Development Team (http://www.icinga.org)    *
- *                                                                            *
- * This program is free software; you can redistribute it and/or              *
- * modify it under the terms of the GNU General Public License                *
- * as published by the Free Software Foundation; either version 2             *
- * of the License, or (at your option) any later version.                     *
- *                                                                            *
- * This program is distributed in the hope that it will be useful,            *
- * but WITHOUT ANY WARRANTY; without even the implied warranty of             *
- * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the              *
- * GNU General Public License for more details.                               *
- *                                                                            *
- * You should have received a copy of the GNU General Public License          *
- * along with this program; if not, write to the Free Software Foundation     *
- * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA.             *
- ******************************************************************************/
+/* Icinga 2 | (c) 2012 Icinga GmbH | GPLv2+ */
 
 #include "icinga/apievents.hpp"
 #include "icinga/service.hpp"
-#include "icinga/perfdatavalue.hpp"
-#include "remote/apilistener.hpp"
-#include "remote/endpoint.hpp"
-#include "remote/messageorigin.hpp"
-#include "remote/zone.hpp"
-#include "remote/apifunction.hpp"
-#include "base/application.hpp"
-#include "base/configtype.hpp"
-#include "base/utility.hpp"
-#include "base/exception.hpp"
+#include "icinga/notificationcommand.hpp"
+#include "remote/eventqueue.hpp"
 #include "base/initialize.hpp"
 #include "base/serializer.hpp"
-#include "base/json.hpp"
-#include <fstream>
+#include "base/logger.hpp"
 
 using namespace icinga;
 
 INITIALIZE_ONCE(&ApiEvents::StaticInitialize);
 
-REGISTER_APIFUNCTION(CheckResult, event, &ApiEvents::CheckResultAPIHandler);
-REGISTER_APIFUNCTION(SetNextCheck, event, &ApiEvents::NextCheckChangedAPIHandler);
-REGISTER_APIFUNCTION(SetNextNotification, event, &ApiEvents::NextNotificationChangedAPIHandler);
-REGISTER_APIFUNCTION(SetForceNextCheck, event, &ApiEvents::ForceNextCheckChangedAPIHandler);
-REGISTER_APIFUNCTION(SetForceNextNotification, event, &ApiEvents::ForceNextNotificationChangedAPIHandler);
-REGISTER_APIFUNCTION(AddComment, event, &ApiEvents::CommentAddedAPIHandler);
-REGISTER_APIFUNCTION(RemoveComment, event, &ApiEvents::CommentRemovedAPIHandler);
-REGISTER_APIFUNCTION(AddDowntime, event, &ApiEvents::DowntimeAddedAPIHandler);
-REGISTER_APIFUNCTION(RemoveDowntime, event, &ApiEvents::DowntimeRemovedAPIHandler);
-REGISTER_APIFUNCTION(SetAcknowledgement, event, &ApiEvents::AcknowledgementSetAPIHandler);
-REGISTER_APIFUNCTION(ClearAcknowledgement, event, &ApiEvents::AcknowledgementClearedAPIHandler);
-REGISTER_APIFUNCTION(UpdateRepository, event, &ApiEvents::UpdateRepositoryAPIHandler);
-REGISTER_APIFUNCTION(ExecuteCommand, event, &ApiEvents::ExecuteCommandAPIHandler);
-
-static Timer::Ptr l_RepositoryTimer;
-
-void ApiEvents::StaticInitialize(void)
+void ApiEvents::StaticInitialize()
 {
        Checkable::OnNewCheckResult.connect(&ApiEvents::CheckResultHandler);
-       Checkable::OnNextCheckChanged.connect(&ApiEvents::NextCheckChangedHandler);
-       Notification::OnNextNotificationChanged.connect(&ApiEvents::NextNotificationChangedHandler);
-       Checkable::OnForceNextCheckChanged.connect(&ApiEvents::ForceNextCheckChangedHandler);
-       Checkable::OnForceNextNotificationChanged.connect(&ApiEvents::ForceNextNotificationChangedHandler);
-
-       Checkable::OnCommentAdded.connect(&ApiEvents::CommentAddedHandler);
-       Checkable::OnCommentRemoved.connect(&ApiEvents::CommentRemovedHandler);
-       Checkable::OnDowntimeAdded.connect(&ApiEvents::DowntimeAddedHandler);
-       Checkable::OnDowntimeRemoved.connect(&ApiEvents::DowntimeRemovedHandler);
-       Checkable::OnAcknowledgementSet.connect(&ApiEvents::AcknowledgementSetHandler);
-       Checkable::OnAcknowledgementCleared.connect(&ApiEvents::AcknowledgementClearedHandler);
-
-       l_RepositoryTimer = new Timer();
-       l_RepositoryTimer->SetInterval(30);
-       l_RepositoryTimer->OnTimerExpired.connect(boost::bind(&ApiEvents::RepositoryTimerHandler));
-       l_RepositoryTimer->Start();
-       l_RepositoryTimer->Reschedule(0);
-}
+       Checkable::OnStateChange.connect(&ApiEvents::StateChangeHandler);
+       Checkable::OnNotificationSentToAllUsers.connect(&ApiEvents::NotificationSentToAllUsersHandler);
 
-Dictionary::Ptr ApiEvents::MakeCheckResultMessage(const Checkable::Ptr& checkable, const CheckResult::Ptr& cr)
-{
-       Dictionary::Ptr message = new Dictionary();
-       message->Set("jsonrpc", "2.0");
-       message->Set("method", "event::CheckResult");
+       Checkable::OnFlappingChanged.connect(&ApiEvents::FlappingChangedHandler);
 
-       Host::Ptr host;
-       Service::Ptr service;
-       tie(host, service) = GetHostService(checkable);
-
-       Dictionary::Ptr params = new Dictionary();
-       params->Set("host", host->GetName());
-       if (service)
-               params->Set("service", service->GetShortName());
-       else {
-               Value agent_service_name = checkable->GetExtension("agent_service_name");
-
-               if (!agent_service_name.IsEmpty())
-                       params->Set("service", agent_service_name);
-       }
-       params->Set("cr", Serialize(cr));
+       Checkable::OnAcknowledgementSet.connect(&ApiEvents::AcknowledgementSetHandler);
+       Checkable::OnAcknowledgementCleared.connect(&ApiEvents::AcknowledgementClearedHandler);
 
-       message->Set("params", params);
+       Comment::OnCommentAdded.connect(&ApiEvents::CommentAddedHandler);
+       Comment::OnCommentRemoved.connect(&ApiEvents::CommentRemovedHandler);
 
-       return message;
+       Downtime::OnDowntimeAdded.connect(&ApiEvents::DowntimeAddedHandler);
+       Downtime::OnDowntimeRemoved.connect(&ApiEvents::DowntimeRemovedHandler);
+       Downtime::OnDowntimeStarted.connect(&ApiEvents::DowntimeStartedHandler);
+       Downtime::OnDowntimeTriggered.connect(&ApiEvents::DowntimeTriggeredHandler);
 }
 
 void ApiEvents::CheckResultHandler(const Checkable::Ptr& checkable, const CheckResult::Ptr& cr, const MessageOrigin::Ptr& origin)
 {
-       ApiListener::Ptr listener = ApiListener::GetInstance();
+       std::vector<EventQueue::Ptr> queues = EventQueue::GetQueuesForType("CheckResult");
+       auto inboxes (EventsRouter::GetInstance().GetInboxes(EventType::CheckResult));
 
-       if (!listener)
+       if (queues.empty() && !inboxes)
                return;
 
-       Dictionary::Ptr message = MakeCheckResultMessage(checkable, cr);
-       listener->RelayMessage(origin, checkable, message, true);
-}
-
-Value ApiEvents::CheckResultAPIHandler(const MessageOrigin::Ptr& origin, const Dictionary::Ptr& params)
-{
-       Endpoint::Ptr endpoint = origin->FromClient->GetEndpoint();
-
-       if (!endpoint) {
-               Log(LogNotice, "ApiEvents")
-                   << "Discarding 'check result' message from '" << origin->FromClient->GetIdentity() << "': Invalid endpoint origin (client not allowed).";
-               return Empty;
-       }
-
-       if (!params)
-               return Empty;
-
-       CheckResult::Ptr cr = new CheckResult();
-
-       Dictionary::Ptr vcr = params->Get("cr");
-       Array::Ptr vperf = vcr->Get("performance_data");
-       vcr->Remove("performance_data");
-
-       Deserialize(cr, params->Get("cr"), true);
-
-       Array::Ptr rperf = new Array();
-
-       if (vperf) {
-               ObjectLock olock(vperf);
-               BOOST_FOREACH(const Value& vp, vperf) {
-                       Value p;
-
-                       if (vp.IsObjectType<Dictionary>()) {
-                               PerfdataValue::Ptr val = new PerfdataValue();
-                               Deserialize(val, vp, true);
-                               rperf->Add(val);
-                       } else
-                               rperf->Add(vp);
-               }
-       }
-
-       cr->SetPerformanceData(rperf);
-
-       Host::Ptr host = Host::GetByName(params->Get("host"));
+       Log(LogDebug, "ApiEvents", "Processing event type 'CheckResult'.");
 
-       if (!host)
-               return Empty;
-
-       Checkable::Ptr checkable;
-
-       if (params->Contains("service"))
-               checkable = host->GetServiceByShortName(params->Get("service"));
-       else
-               checkable = host;
-
-       if (!checkable)
-               return Empty;
-
-       if (origin->FromZone && !origin->FromZone->CanAccessObject(checkable) && endpoint != checkable->GetCommandEndpoint()) {
-               Log(LogNotice, "ApiEvents")
-                   << "Discarding 'check result' message from '" << origin->FromClient->GetIdentity() << "': Unauthorized access.";
-               return Empty;
-       }
-
-       if (endpoint == checkable->GetCommandEndpoint())
-               checkable->ProcessCheckResult(cr);
-       else
-               checkable->ProcessCheckResult(cr, origin);
-
-       return Empty;
-}
-
-void ApiEvents::NextCheckChangedHandler(const Checkable::Ptr& checkable, const MessageOrigin::Ptr& origin)
-{
-       ApiListener::Ptr listener = ApiListener::GetInstance();
-
-       if (!listener)
-               return;
+       Dictionary::Ptr result = new Dictionary();
+       result->Set("type", "CheckResult");
+       result->Set("timestamp", Utility::GetTime());
 
        Host::Ptr host;
        Service::Ptr service;
        tie(host, service) = GetHostService(checkable);
 
-       Dictionary::Ptr params = new Dictionary();
-       params->Set("host", host->GetName());
+       result->Set("host", host->GetName());
        if (service)
-               params->Set("service", service->GetShortName());
-       params->Set("next_check", checkable->GetNextCheck());
+               result->Set("service", service->GetShortName());
 
-       Dictionary::Ptr message = new Dictionary();
-       message->Set("jsonrpc", "2.0");
-       message->Set("method", "event::SetNextCheck");
-       message->Set("params", params);
+       result->Set("check_result", Serialize(cr));
 
-       listener->RelayMessage(origin, checkable, message, true);
-}
-
-Value ApiEvents::NextCheckChangedAPIHandler(const MessageOrigin::Ptr& origin, const Dictionary::Ptr& params)
-{
-       Endpoint::Ptr endpoint = origin->FromClient->GetEndpoint();
-
-       if (!endpoint) {
-               Log(LogNotice, "ApiEvents")
-                   << "Discarding 'next check changed' message from '" << origin->FromClient->GetIdentity() << "': Invalid endpoint origin (client not allowed).";
-               return Empty;
-       }
-
-       if (!params)
-               return Empty;
-
-       Host::Ptr host = Host::GetByName(params->Get("host"));
-
-       if (!host)
-               return Empty;
-
-       Checkable::Ptr checkable;
-
-       if (params->Contains("service"))
-               checkable = host->GetServiceByShortName(params->Get("service"));
-       else
-               checkable = host;
-
-       if (!checkable)
-               return Empty;
-
-       if (origin->FromZone && !origin->FromZone->CanAccessObject(checkable)) {
-               Log(LogNotice, "ApiEvents")
-                   << "Discarding 'next check changed' message from '" << origin->FromClient->GetIdentity() << "': Unauthorized access.";
-               return Empty;
+       for (const EventQueue::Ptr& queue : queues) {
+               queue->ProcessEvent(result);
        }
 
-       checkable->SetNextCheck(params->Get("next_check"), false, origin);
-
-       return Empty;
+       inboxes.Push(std::move(result));
 }
 
-void ApiEvents::NextNotificationChangedHandler(const Notification::Ptr& notification, const MessageOrigin::Ptr& origin)
+void ApiEvents::StateChangeHandler(const Checkable::Ptr& checkable, const CheckResult::Ptr& cr, StateType type, const MessageOrigin::Ptr& origin)
 {
-       ApiListener::Ptr listener = ApiListener::GetInstance();
+       std::vector<EventQueue::Ptr> queues = EventQueue::GetQueuesForType("StateChange");
+       auto inboxes (EventsRouter::GetInstance().GetInboxes(EventType::StateChange));
 
-       if (!listener)
+       if (queues.empty() && !inboxes)
                return;
 
-       Dictionary::Ptr params = new Dictionary();
-       params->Set("notification", notification->GetName());
-       params->Set("next_notification", notification->GetNextNotification());
-
-       Dictionary::Ptr message = new Dictionary();
-       message->Set("jsonrpc", "2.0");
-       message->Set("method", "event::SetNextNotification");
-       message->Set("params", params);
+       Log(LogDebug, "ApiEvents", "Processing event type 'StateChange'.");
 
-       listener->RelayMessage(origin, notification, message, true);
-}
-
-Value ApiEvents::NextNotificationChangedAPIHandler(const MessageOrigin::Ptr& origin, const Dictionary::Ptr& params)
-{
-       Endpoint::Ptr endpoint = origin->FromClient->GetEndpoint();
-
-       if (!endpoint) {
-               Log(LogNotice, "ApiEvents")
-                   << "Discarding 'next notification changed' message from '" << origin->FromClient->GetIdentity() << "': Invalid endpoint origin (client not allowed).";
-               return Empty;
-       }
-
-       if (!params)
-               return Empty;
-
-       Notification::Ptr notification = Notification::GetByName(params->Get("notification"));
-
-       if (!notification)
-               return Empty;
-
-       if (origin->FromZone && !origin->FromZone->CanAccessObject(notification)) {
-               Log(LogNotice, "ApiEvents")
-                   << "Discarding 'next notification changed' message from '" << origin->FromClient->GetIdentity() << "': Unauthorized access.";
-               return Empty;
-       }
-
-       notification->SetNextNotification(params->Get("next_notification"), false, origin);
-
-       return Empty;
-}
-
-void ApiEvents::ForceNextCheckChangedHandler(const Checkable::Ptr& checkable, const MessageOrigin::Ptr& origin)
-{
-       ApiListener::Ptr listener = ApiListener::GetInstance();
-
-       if (!listener)
-               return;
+       Dictionary::Ptr result = new Dictionary();
+       result->Set("type", "StateChange");
+       result->Set("timestamp", Utility::GetTime());
 
        Host::Ptr host;
        Service::Ptr service;
        tie(host, service) = GetHostService(checkable);
 
-       Dictionary::Ptr params = new Dictionary();
-       params->Set("host", host->GetName());
+       result->Set("host", host->GetName());
        if (service)
-               params->Set("service", service->GetShortName());
-       params->Set("forced", checkable->GetForceNextCheck());
+               result->Set("service", service->GetShortName());
 
-       Dictionary::Ptr message = new Dictionary();
-       message->Set("jsonrpc", "2.0");
-       message->Set("method", "event::SetForceNextCheck");
-       message->Set("params", params);
+       result->Set("state", service ? static_cast<int>(service->GetState()) : static_cast<int>(host->GetState()));
+       result->Set("state_type", checkable->GetStateType());
+       result->Set("check_result", Serialize(cr));
 
-       listener->RelayMessage(origin, checkable, message, true);
-}
-
-Value ApiEvents::ForceNextCheckChangedAPIHandler(const MessageOrigin::Ptr& origin, const Dictionary::Ptr& params)
-{
-       Endpoint::Ptr endpoint = origin->FromClient->GetEndpoint();
-
-       if (!endpoint) {
-               Log(LogNotice, "ApiEvents")
-                   << "Discarding 'force next check changed' message from '" << origin->FromClient->GetIdentity() << "': Invalid endpoint origin (client not allowed).";
-               return Empty;
-       }
-
-       if (!params)
-               return Empty;
-
-       Host::Ptr host = Host::GetByName(params->Get("host"));
-
-       if (!host)
-               return Empty;
-
-       Checkable::Ptr checkable;
-
-       if (params->Contains("service"))
-               checkable = host->GetServiceByShortName(params->Get("service"));
-       else
-               checkable = host;
-
-       if (!checkable)
-               return Empty;
-
-       if (origin->FromZone && !origin->FromZone->CanAccessObject(checkable)) {
-               Log(LogNotice, "ApiEvents")
-                   << "Discarding 'force next check' message from '" << origin->FromClient->GetIdentity() << "': Unauthorized access.";
-               return Empty;
+       for (const EventQueue::Ptr& queue : queues) {
+               queue->ProcessEvent(result);
        }
 
-       checkable->SetForceNextCheck(params->Get("forced"), false, origin);
-
-       return Empty;
+       inboxes.Push(std::move(result));
 }
 
-void ApiEvents::ForceNextNotificationChangedHandler(const Checkable::Ptr& checkable, const MessageOrigin::Ptr& origin)
+void ApiEvents::NotificationSentToAllUsersHandler(const Notification::Ptr& notification,
+       const Checkable::Ptr& checkable, const std::set<User::Ptr>& users, NotificationType type,
+       const CheckResult::Ptr& cr, const String& author, const String& text, const MessageOrigin::Ptr& origin)
 {
-       ApiListener::Ptr listener = ApiListener::GetInstance();
+       std::vector<EventQueue::Ptr> queues = EventQueue::GetQueuesForType("Notification");
+       auto inboxes (EventsRouter::GetInstance().GetInboxes(EventType::Notification));
 
-       if (!listener)
+       if (queues.empty() && !inboxes)
                return;
 
-       Host::Ptr host;
-       Service::Ptr service;
-       tie(host, service) = GetHostService(checkable);
-
-       Dictionary::Ptr params = new Dictionary();
-       params->Set("host", host->GetName());
-       if (service)
-               params->Set("service", service->GetShortName());
-       params->Set("forced", checkable->GetForceNextNotification());
-
-       Dictionary::Ptr message = new Dictionary();
-       message->Set("jsonrpc", "2.0");
-       message->Set("method", "event::SetForceNextNotification");
-       message->Set("params", params);
-
-       listener->RelayMessage(origin, checkable, message, true);
-}
-
-Value ApiEvents::ForceNextNotificationChangedAPIHandler(const MessageOrigin::Ptr& origin, const Dictionary::Ptr& params)
-{
-       Endpoint::Ptr endpoint = origin->FromClient->GetEndpoint();
-
-       if (!endpoint) {
-               Log(LogNotice, "ApiEvents")
-                   << "Discarding 'force next notification changed' message from '" << origin->FromClient->GetIdentity() << "': Invalid endpoint origin (client not allowed).";
-               return Empty;
-       }
-
-       if (!params)
-               return Empty;
-
-       Host::Ptr host = Host::GetByName(params->Get("host"));
-
-       if (!host)
-               return Empty;
-
-       Checkable::Ptr checkable;
+       Log(LogDebug, "ApiEvents", "Processing event type 'Notification'.");
 
-       if (params->Contains("service"))
-               checkable = host->GetServiceByShortName(params->Get("service"));
-       else
-               checkable = host;
-
-       if (!checkable)
-               return Empty;
-
-       if (origin->FromZone && !origin->FromZone->CanAccessObject(checkable)) {
-               Log(LogNotice, "ApiEvents")
-                   << "Discarding 'force next notification' message from '" << origin->FromClient->GetIdentity() << "': Unauthorized access.";
-               return Empty;
-       }
-
-       checkable->SetForceNextNotification(params->Get("forced"), false, origin);
-
-       return Empty;
-}
-
-void ApiEvents::CommentAddedHandler(const Checkable::Ptr& checkable, const Comment::Ptr& comment, const MessageOrigin::Ptr& origin)
-{
-       ApiListener::Ptr listener = ApiListener::GetInstance();
-
-       if (!listener)
-               return;
+       Dictionary::Ptr result = new Dictionary();
+       result->Set("type", "Notification");
+       result->Set("timestamp", Utility::GetTime());
 
        Host::Ptr host;
        Service::Ptr service;
        tie(host, service) = GetHostService(checkable);
 
-       Dictionary::Ptr params = new Dictionary();
-       params->Set("host", host->GetName());
+       result->Set("host", host->GetName());
        if (service)
-               params->Set("service", service->GetShortName());
-       params->Set("comment", Serialize(comment));
+               result->Set("service", service->GetShortName());
 
-       Dictionary::Ptr message = new Dictionary();
-       message->Set("jsonrpc", "2.0");
-       message->Set("method", "event::AddComment");
-       message->Set("params", params);
+       NotificationCommand::Ptr command = notification->GetCommand();
 
-       listener->RelayMessage(origin, checkable, message, true);
-}
+       if (command)
+               result->Set("command", command->GetName());
 
-Value ApiEvents::CommentAddedAPIHandler(const MessageOrigin::Ptr& origin, const Dictionary::Ptr& params)
-{
-       Endpoint::Ptr endpoint = origin->FromClient->GetEndpoint();
+       ArrayData userNames;
 
-       if (!endpoint) {
-               Log(LogNotice, "ApiEvents")
-                   << "Discarding 'comment added' message from '" << origin->FromClient->GetIdentity() << "': Invalid endpoint origin (client not allowed).";
-               return Empty;
+       for (const User::Ptr& user : users) {
+               userNames.push_back(user->GetName());
        }
 
-       if (!params)
-               return Empty;
-
-       Host::Ptr host = Host::GetByName(params->Get("host"));
-
-       if (!host)
-               return Empty;
+       result->Set("users", new Array(std::move(userNames)));
+       result->Set("notification_type", Notification::NotificationTypeToString(type));
+       result->Set("author", author);
+       result->Set("text", text);
+       result->Set("check_result", Serialize(cr));
 
-       Checkable::Ptr checkable;
-
-       if (params->Contains("service"))
-               checkable = host->GetServiceByShortName(params->Get("service"));
-       else
-               checkable = host;
-
-       if (!checkable)
-               return Empty;
-
-       if (origin->FromZone && !origin->FromZone->CanAccessObject(checkable)) {
-               Log(LogNotice, "ApiEvents")
-                   << "Discarding 'comment added' message from '" << origin->FromClient->GetIdentity() << "': Unauthorized access.";
-               return Empty;
+       for (const EventQueue::Ptr& queue : queues) {
+               queue->ProcessEvent(result);
        }
 
-       Comment::Ptr comment = new Comment();
-       Deserialize(comment, params->Get("comment"), true);
-
-       checkable->AddComment(comment->GetEntryType(), comment->GetAuthor(),
-           comment->GetText(), comment->GetExpireTime(), comment->GetId(), origin);
-
-       return Empty;
+       inboxes.Push(std::move(result));
 }
 
-void ApiEvents::CommentRemovedHandler(const Checkable::Ptr& checkable, const Comment::Ptr& comment, const MessageOrigin::Ptr& origin)
+void ApiEvents::FlappingChangedHandler(const Checkable::Ptr& checkable, const MessageOrigin::Ptr& origin)
 {
-       ApiListener::Ptr listener = ApiListener::GetInstance();
+       std::vector<EventQueue::Ptr> queues = EventQueue::GetQueuesForType("Flapping");
+       auto inboxes (EventsRouter::GetInstance().GetInboxes(EventType::Flapping));
 
-       if (!listener)
+       if (queues.empty() && !inboxes)
                return;
 
+       Log(LogDebug, "ApiEvents", "Processing event type 'Flapping'.");
+
+       Dictionary::Ptr result = new Dictionary();
+       result->Set("type", "Flapping");
+       result->Set("timestamp", Utility::GetTime());
+
        Host::Ptr host;
        Service::Ptr service;
        tie(host, service) = GetHostService(checkable);
 
-       Dictionary::Ptr params = new Dictionary();
-       params->Set("host", host->GetName());
+       result->Set("host", host->GetName());
        if (service)
-               params->Set("service", service->GetShortName());
-       params->Set("id", comment->GetId());
+               result->Set("service", service->GetShortName());
 
-       Dictionary::Ptr message = new Dictionary();
-       message->Set("jsonrpc", "2.0");
-       message->Set("method", "event::RemoveComment");
-       message->Set("params", params);
+       result->Set("state", service ? static_cast<int>(service->GetState()) : static_cast<int>(host->GetState()));
+       result->Set("state_type", checkable->GetStateType());
+       result->Set("is_flapping", checkable->IsFlapping());
+       result->Set("flapping_current", checkable->GetFlappingCurrent());
+       result->Set("threshold_low", checkable->GetFlappingThresholdLow());
+       result->Set("threshold_high", checkable->GetFlappingThresholdHigh());
 
-       listener->RelayMessage(origin, checkable, message, true);
-}
-
-Value ApiEvents::CommentRemovedAPIHandler(const MessageOrigin::Ptr& origin, const Dictionary::Ptr& params)
-{
-       Endpoint::Ptr endpoint = origin->FromClient->GetEndpoint();
-
-       if (!endpoint) {
-               Log(LogNotice, "ApiEvents")
-                   << "Discarding 'comment removed' message from '" << origin->FromClient->GetIdentity() << "': Invalid endpoint origin (client not allowed).";
-               return Empty;
+       for (const EventQueue::Ptr& queue : queues) {
+               queue->ProcessEvent(result);
        }
 
-       if (!params)
-               return Empty;
-
-       Host::Ptr host = Host::GetByName(params->Get("host"));
-
-       if (!host)
-               return Empty;
-
-       Checkable::Ptr checkable;
-
-       if (params->Contains("service"))
-               checkable = host->GetServiceByShortName(params->Get("service"));
-       else
-               checkable = host;
-
-       if (!checkable)
-               return Empty;
-
-       if (origin->FromZone && !origin->FromZone->CanAccessObject(checkable)) {
-               Log(LogNotice, "ApiEvents")
-                   << "Discarding 'comment removed' message from '" << origin->FromClient->GetIdentity() << "': Unauthorized access.";
-               return Empty;
-       }
-
-       checkable->RemoveComment(params->Get("id"), origin);
-
-       return Empty;
+       inboxes.Push(std::move(result));
 }
 
-void ApiEvents::DowntimeAddedHandler(const Checkable::Ptr& checkable, const Downtime::Ptr& downtime, const MessageOrigin::Ptr& origin)
+void ApiEvents::AcknowledgementSetHandler(const Checkable::Ptr& checkable,
+       const String& author, const String& comment, AcknowledgementType type,
+       bool notify, bool persistent, double expiry, const MessageOrigin::Ptr& origin)
 {
-       ApiListener::Ptr listener = ApiListener::GetInstance();
+       std::vector<EventQueue::Ptr> queues = EventQueue::GetQueuesForType("AcknowledgementSet");
+       auto inboxes (EventsRouter::GetInstance().GetInboxes(EventType::AcknowledgementSet));
 
-       if (!listener)
+       if (queues.empty() && !inboxes)
                return;
 
+       Log(LogDebug, "ApiEvents", "Processing event type 'AcknowledgementSet'.");
+
+       Dictionary::Ptr result = new Dictionary();
+       result->Set("type", "AcknowledgementSet");
+       result->Set("timestamp", Utility::GetTime());
+
        Host::Ptr host;
        Service::Ptr service;
        tie(host, service) = GetHostService(checkable);
 
-       Dictionary::Ptr params = new Dictionary();
-       params->Set("host", host->GetName());
+       result->Set("host", host->GetName());
        if (service)
-               params->Set("service", service->GetShortName());
-       params->Set("downtime", Serialize(downtime));
-
-       Dictionary::Ptr message = new Dictionary();
-       message->Set("jsonrpc", "2.0");
-       message->Set("method", "event::AddDowntime");
-       message->Set("params", params);
+               result->Set("service", service->GetShortName());
 
-       listener->RelayMessage(origin, checkable, message, true);
-}
+       result->Set("state", service ? static_cast<int>(service->GetState()) : static_cast<int>(host->GetState()));
+       result->Set("state_type", checkable->GetStateType());
 
-Value ApiEvents::DowntimeAddedAPIHandler(const MessageOrigin::Ptr& origin, const Dictionary::Ptr& params)
-{
-       Endpoint::Ptr endpoint = origin->FromClient->GetEndpoint();
+       result->Set("author", author);
+       result->Set("comment", comment);
+       result->Set("acknowledgement_type", type);
+       result->Set("notify", notify);
+       result->Set("persistent", persistent);
+       result->Set("expiry", expiry);
 
-       if (!endpoint) {
-               Log(LogNotice, "ApiEvents")
-                   << "Discarding 'downtime added' message from '" << origin->FromClient->GetIdentity() << "': Invalid endpoint origin (client not allowed).";
-               return Empty;
+       for (const EventQueue::Ptr& queue : queues) {
+               queue->ProcessEvent(result);
        }
 
-       if (!params)
-               return Empty;
-
-       Host::Ptr host = Host::GetByName(params->Get("host"));
-
-       if (!host)
-               return Empty;
-
-       Checkable::Ptr checkable;
-
-       if (params->Contains("service"))
-               checkable = host->GetServiceByShortName(params->Get("service"));
-       else
-               checkable = host;
-
-       if (!checkable)
-               return Empty;
-
-       if (origin->FromZone && !origin->FromZone->CanAccessObject(checkable)) {
-               Log(LogNotice, "ApiEvents")
-                   << "Discarding 'downtime added' message from '" << origin->FromClient->GetIdentity() << "': Unauthorized access.";
-               return Empty;
-       }
-
-       Downtime::Ptr downtime = new Downtime();
-       Deserialize(downtime, params->Get("downtime"), true);
-
-       checkable->AddDowntime(downtime->GetAuthor(), downtime->GetComment(),
-           downtime->GetStartTime(), downtime->GetEndTime(),
-           downtime->GetFixed(), downtime->GetTriggeredBy(),
-           downtime->GetDuration(), downtime->GetScheduledBy(),
-           downtime->GetId(), origin);
-
-       return Empty;
+       inboxes.Push(std::move(result));
 }
 
-void ApiEvents::DowntimeRemovedHandler(const Checkable::Ptr& checkable, const Downtime::Ptr& downtime, const MessageOrigin::Ptr& origin)
+void ApiEvents::AcknowledgementClearedHandler(const Checkable::Ptr& checkable, const MessageOrigin::Ptr& origin)
 {
-       ApiListener::Ptr listener = ApiListener::GetInstance();
+       std::vector<EventQueue::Ptr> queues = EventQueue::GetQueuesForType("AcknowledgementCleared");
+       auto inboxes (EventsRouter::GetInstance().GetInboxes(EventType::AcknowledgementCleared));
 
-       if (!listener)
+       if (queues.empty() && !inboxes)
                return;
 
+       Log(LogDebug, "ApiEvents", "Processing event type 'AcknowledgementCleared'.");
+
+       Dictionary::Ptr result = new Dictionary();
+       result->Set("type", "AcknowledgementCleared");
+       result->Set("timestamp", Utility::GetTime());
+
        Host::Ptr host;
        Service::Ptr service;
        tie(host, service) = GetHostService(checkable);
 
-       Dictionary::Ptr params = new Dictionary();
-       params->Set("host", host->GetName());
+       result->Set("host", host->GetName());
        if (service)
-               params->Set("service", service->GetShortName());
-       params->Set("id", downtime->GetId());
-
-       Dictionary::Ptr message = new Dictionary();
-       message->Set("jsonrpc", "2.0");
-       message->Set("method", "event::RemoveDowntime");
-       message->Set("params", params);
-
-       listener->RelayMessage(origin, checkable, message, true);
-}
-
-Value ApiEvents::DowntimeRemovedAPIHandler(const MessageOrigin::Ptr& origin, const Dictionary::Ptr& params)
-{
-       Endpoint::Ptr endpoint = origin->FromClient->GetEndpoint();
-
-       if (!endpoint) {
-               Log(LogNotice, "ApiEvents")
-                   << "Discarding 'downtime removed' message from '" << origin->FromClient->GetIdentity() << "': Invalid endpoint origin (client not allowed).";
-               return Empty;
-       }
-
-       if (!params)
-               return Empty;
-
-       Host::Ptr host = Host::GetByName(params->Get("host"));
-
-       if (!host)
-               return Empty;
-
-       Checkable::Ptr checkable;
+               result->Set("service", service->GetShortName());
 
-       if (params->Contains("service"))
-               checkable = host->GetServiceByShortName(params->Get("service"));
-       else
-               checkable = host;
+       result->Set("state", service ? static_cast<int>(service->GetState()) : static_cast<int>(host->GetState()));
+       result->Set("state_type", checkable->GetStateType());
 
-       if (!checkable)
-               return Empty;
-
-       if (origin->FromZone && !origin->FromZone->CanAccessObject(checkable)) {
-               Log(LogNotice, "ApiEvents")
-                   << "Discarding 'downtime removed' message from '" << origin->FromClient->GetIdentity() << "': Unauthorized access.";
-               return Empty;
+       for (const EventQueue::Ptr& queue : queues) {
+               queue->ProcessEvent(result);
        }
 
-       checkable->RemoveDowntime(params->Get("id"), false, origin);
+       result->Set("acknowledgement_type", AcknowledgementNone);
 
-       return Empty;
+       inboxes.Push(std::move(result));
 }
 
-void ApiEvents::AcknowledgementSetHandler(const Checkable::Ptr& checkable,
-    const String& author, const String& comment, AcknowledgementType type,
-    bool notify, double expiry, const MessageOrigin::Ptr& origin)
+void ApiEvents::CommentAddedHandler(const Comment::Ptr& comment)
 {
-       ApiListener::Ptr listener = ApiListener::GetInstance();
+       std::vector<EventQueue::Ptr> queues = EventQueue::GetQueuesForType("CommentAdded");
+       auto inboxes (EventsRouter::GetInstance().GetInboxes(EventType::CommentAdded));
 
-       if (!listener)
+       if (queues.empty() && !inboxes)
                return;
 
-       Host::Ptr host;
-       Service::Ptr service;
-       tie(host, service) = GetHostService(checkable);
-
-       Dictionary::Ptr params = new Dictionary();
-       params->Set("host", host->GetName());
-       if (service)
-               params->Set("service", service->GetShortName());
-       params->Set("author", author);
-       params->Set("comment", comment);
-       params->Set("acktype", type);
-       params->Set("notify", notify);
-       params->Set("expiry", expiry);
-
-       Dictionary::Ptr message = new Dictionary();
-       message->Set("jsonrpc", "2.0");
-       message->Set("method", "event::SetAcknowledgement");
-       message->Set("params", params);
-
-       listener->RelayMessage(origin, checkable, message, true);
-}
-
-Value ApiEvents::AcknowledgementSetAPIHandler(const MessageOrigin::Ptr& origin, const Dictionary::Ptr& params)
-{
-       Endpoint::Ptr endpoint = origin->FromClient->GetEndpoint();
-
-       if (!endpoint) {
-               Log(LogNotice, "ApiEvents")
-                   << "Discarding 'acknowledgement set' message from '" << origin->FromClient->GetIdentity() << "': Invalid endpoint origin (client not allowed).";
-               return Empty;
-       }
-
-       if (!params)
-               return Empty;
-
-       Host::Ptr host = Host::GetByName(params->Get("host"));
-
-       if (!host)
-               return Empty;
+       Log(LogDebug, "ApiEvents", "Processing event type 'CommentAdded'.");
 
-       Checkable::Ptr checkable;
+       Dictionary::Ptr result = new Dictionary({
+               { "type", "CommentAdded" },
+               { "timestamp", Utility::GetTime() },
+               { "comment", Serialize(comment, FAConfig | FAState) }
+       });
 
-       if (params->Contains("service"))
-               checkable = host->GetServiceByShortName(params->Get("service"));
-       else
-               checkable = host;
-
-       if (!checkable)
-               return Empty;
-
-       if (origin->FromZone && !origin->FromZone->CanAccessObject(checkable)) {
-               Log(LogNotice, "ApiEvents")
-                   << "Discarding 'acknowledgement set' message from '" << origin->FromClient->GetIdentity() << "': Unauthorized access.";
-               return Empty;
+       for (const EventQueue::Ptr& queue : queues) {
+               queue->ProcessEvent(result);
        }
 
-       checkable->AcknowledgeProblem(params->Get("author"), params->Get("comment"),
-           static_cast<AcknowledgementType>(static_cast<int>(params->Get("acktype"))),
-           params->Get("notify"), params->Get("expiry"), origin);
-
-       return Empty;
+       inboxes.Push(std::move(result));
 }
 
-void ApiEvents::AcknowledgementClearedHandler(const Checkable::Ptr& checkable, const MessageOrigin::Ptr& origin)
+void ApiEvents::CommentRemovedHandler(const Comment::Ptr& comment)
 {
-       ApiListener::Ptr listener = ApiListener::GetInstance();
+       std::vector<EventQueue::Ptr> queues = EventQueue::GetQueuesForType("CommentRemoved");
+       auto inboxes (EventsRouter::GetInstance().GetInboxes(EventType::CommentRemoved));
 
-       if (!listener)
+       if (queues.empty() && !inboxes)
                return;
 
-       Host::Ptr host;
-       Service::Ptr service;
-       tie(host, service) = GetHostService(checkable);
+       Log(LogDebug, "ApiEvents", "Processing event type 'CommentRemoved'.");
 
-       Dictionary::Ptr params = new Dictionary();
-       params->Set("host", host->GetName());
-       if (service)
-               params->Set("service", service->GetShortName());
+       Dictionary::Ptr result = new Dictionary({
+               { "type", "CommentRemoved" },
+               { "timestamp", Utility::GetTime() },
+               { "comment", Serialize(comment, FAConfig | FAState) }
+       });
 
-       Dictionary::Ptr message = new Dictionary();
-       message->Set("jsonrpc", "2.0");
-       message->Set("method", "event::ClearAcknowledgement");
-       message->Set("params", params);
+       for (const EventQueue::Ptr& queue : queues) {
+               queue->ProcessEvent(result);
+       }
 
-       listener->RelayMessage(origin, checkable, message, true);
+       inboxes.Push(std::move(result));
 }
 
-Value ApiEvents::AcknowledgementClearedAPIHandler(const MessageOrigin::Ptr& origin, const Dictionary::Ptr& params)
+void ApiEvents::DowntimeAddedHandler(const Downtime::Ptr& downtime)
 {
-       Endpoint::Ptr endpoint = origin->FromClient->GetEndpoint();
-
-       if (!endpoint) {
-               Log(LogNotice, "ApiEvents")
-                   << "Discarding 'acknowledgement cleared' message from '" << origin->FromClient->GetIdentity() << "': Invalid endpoint origin (client not allowed).";
-               return Empty;
-       }
-
-       if (!params)
-               return Empty;
-
-       Host::Ptr host = Host::GetByName(params->Get("host"));
-
-       if (!host)
-               return Empty;
+       std::vector<EventQueue::Ptr> queues = EventQueue::GetQueuesForType("DowntimeAdded");
+       auto inboxes (EventsRouter::GetInstance().GetInboxes(EventType::DowntimeAdded));
 
-       Checkable::Ptr checkable;
+       if (queues.empty() && !inboxes)
+               return;
 
-       if (params->Contains("service"))
-               checkable = host->GetServiceByShortName(params->Get("service"));
-       else
-               checkable = host;
+       Log(LogDebug, "ApiEvents", "Processing event type 'DowntimeAdded'.");
 
-       if (!checkable)
-               return Empty;
+       Dictionary::Ptr result = new Dictionary({
+               { "type", "DowntimeAdded" },
+               { "timestamp", Utility::GetTime() },
+               { "downtime", Serialize(downtime, FAConfig | FAState) }
+       });
 
-       if (origin->FromZone && !origin->FromZone->CanAccessObject(checkable)) {
-               Log(LogNotice, "ApiEvents")
-                   << "Discarding 'acknowledgement cleared' message from '" << origin->FromClient->GetIdentity() << "': Unauthorized access.";
-               return Empty;
+       for (const EventQueue::Ptr& queue : queues) {
+               queue->ProcessEvent(result);
        }
 
-       checkable->ClearAcknowledgement(origin);
-
-       return Empty;
+       inboxes.Push(std::move(result));
 }
 
-Value ApiEvents::ExecuteCommandAPIHandler(const MessageOrigin::Ptr& origin, const Dictionary::Ptr& params)
+void ApiEvents::DowntimeRemovedHandler(const Downtime::Ptr& downtime)
 {
-       Endpoint::Ptr sourceEndpoint = origin->FromClient->GetEndpoint();
-
-       if (!sourceEndpoint || (origin->FromZone && !Zone::GetLocalZone()->IsChildOf(origin->FromZone))) {
-               Log(LogNotice, "ApiEvents")
-                   << "Discarding 'execute command' message from '" << origin->FromClient->GetIdentity() << "': Invalid endpoint origin (client not allowed).";
-               return Empty;
-       }
-
-       ApiListener::Ptr listener = ApiListener::GetInstance();
-
-       if (!listener) {
-               Log(LogCritical, "ApiListener", "No instance available.");
-               return Empty;
-       }
+       std::vector<EventQueue::Ptr> queues = EventQueue::GetQueuesForType("DowntimeRemoved");
+       auto inboxes (EventsRouter::GetInstance().GetInboxes(EventType::DowntimeRemoved));
 
-       if (!listener->GetAcceptCommands()) {
-               Log(LogWarning, "ApiListener")
-                   << "Ignoring command. '" << listener->GetName() << "' does not accept commands.";
-
-               Host::Ptr host = new Host();
-               Dictionary::Ptr attrs = new Dictionary();
-
-               attrs->Set("__name", params->Get("host"));
-               attrs->Set("type", "Host");
-
-               Deserialize(host, attrs, false, FAConfig);
-
-               if (params->Contains("service"))
-                       host->SetExtension("agent_service_name", params->Get("service"));
+       if (queues.empty() && !inboxes)
+               return;
 
-               CheckResult::Ptr cr = new CheckResult();
-               cr->SetState(ServiceUnknown);
-               cr->SetOutput("Endpoint '" + Endpoint::GetLocalEndpoint()->GetName() + "' does not accept commands.");
-               Dictionary::Ptr message = MakeCheckResultMessage(host, cr);
-               listener->SyncSendMessage(sourceEndpoint, message);
+       Log(LogDebug, "ApiEvents", "Processing event type 'DowntimeRemoved'.");
 
-               return Empty;
-       }
+       Dictionary::Ptr result = new Dictionary({
+               { "type", "DowntimeRemoved" },
+               { "timestamp", Utility::GetTime() },
+               { "downtime", Serialize(downtime, FAConfig | FAState) }
+       });
 
-       /* use a virtual host object for executing the command */
-       Host::Ptr host = new Host();
-       Dictionary::Ptr attrs = new Dictionary();
-
-       attrs->Set("__name", params->Get("host"));
-       attrs->Set("type", "Host");
-
-       Deserialize(host, attrs, false, FAConfig);
-
-       if (params->Contains("service"))
-               host->SetExtension("agent_service_name", params->Get("service"));
-
-       String command = params->Get("command");
-       String command_type = params->Get("command_type");
-
-       if (command_type == "check_command") {
-               if (!CheckCommand::GetByName(command)) {
-                       CheckResult::Ptr cr = new CheckResult();
-                       cr->SetState(ServiceUnknown);
-                       cr->SetOutput("Check command '" + command + "' does not exist.");
-                       Dictionary::Ptr message = MakeCheckResultMessage(host, cr);
-                       listener->SyncSendMessage(sourceEndpoint, message);
-                       return Empty;
-               }
-       } else if (command_type == "event_command") {
-               if (!EventCommand::GetByName(command)) {
-                       Log(LogWarning, "ApiEvents")
-                           << "Event command '" << command << "' does not exist.";
-                       return Empty;
-               }
-       } else
-               return Empty;
-
-       attrs->Set(command_type, params->Get("command"));
-       attrs->Set("command_endpoint", sourceEndpoint->GetName());
-
-       Deserialize(host, attrs, false, FAConfig);
-
-       host->SetExtension("agent_check", true);
-
-       Dictionary::Ptr macros = params->Get("macros");
-
-       if (command_type == "check_command") {
-               try {
-                       host->ExecuteRemoteCheck(macros);
-               } catch (const std::exception& ex) {
-                       CheckResult::Ptr cr = new CheckResult();
-                       cr->SetState(ServiceUnknown);
-
-                       String output = "Exception occured while checking '" + host->GetName() + "': " + DiagnosticInformation(ex);
-                       cr->SetOutput(output);
-
-                       double now = Utility::GetTime();
-                       cr->SetScheduleStart(now);
-                       cr->SetScheduleEnd(now);
-                       cr->SetExecutionStart(now);
-                       cr->SetExecutionEnd(now);
-
-                       Dictionary::Ptr message = MakeCheckResultMessage(host, cr);
-                       listener->SyncSendMessage(sourceEndpoint, message);
-
-                       Log(LogCritical, "checker", output);
-               }
-       } else if (command_type == "event_command") {
-               host->ExecuteEventHandler(macros, true);
+       for (const EventQueue::Ptr& queue : queues) {
+               queue->ProcessEvent(result);
        }
 
-       return Empty;
+       inboxes.Push(std::move(result));
 }
 
-void ApiEvents::RepositoryTimerHandler(void)
+void ApiEvents::DowntimeStartedHandler(const Downtime::Ptr& downtime)
 {
-       ApiListener::Ptr listener = ApiListener::GetInstance();
+       std::vector<EventQueue::Ptr> queues = EventQueue::GetQueuesForType("DowntimeStarted");
+       auto inboxes (EventsRouter::GetInstance().GetInboxes(EventType::DowntimeStarted));
 
-       if (!listener)
+       if (queues.empty() && !inboxes)
                return;
 
-       Dictionary::Ptr repository = new Dictionary();
-
-       BOOST_FOREACH(const Host::Ptr& host, ConfigType::GetObjectsByType<Host>()) {
-               Array::Ptr services = new Array();
+       Log(LogDebug, "ApiEvents", "Processing event type 'DowntimeStarted'.");
 
-               BOOST_FOREACH(const Service::Ptr& service, host->GetServices()) {
-                       services->Add(service->GetShortName());
-               }
+       Dictionary::Ptr result = new Dictionary({
+               { "type", "DowntimeStarted" },
+               { "timestamp", Utility::GetTime() },
+               { "downtime", Serialize(downtime, FAConfig | FAState) }
+       });
 
-               repository->Set(host->GetName(), services);
+       for (const EventQueue::Ptr& queue : queues) {
+               queue->ProcessEvent(result);
        }
 
-       Endpoint::Ptr my_endpoint = Endpoint::GetLocalEndpoint();
-
-       if (!my_endpoint) {
-               Log(LogWarning, "ApiEvents", "No local endpoint defined. Bailing out.");
-               return;
-       }
-
-       Zone::Ptr my_zone = my_endpoint->GetZone();
-
-       if (!my_zone)
-               return;
-
-       Dictionary::Ptr params = new Dictionary();
-       params->Set("seen", Utility::GetTime());
-       params->Set("endpoint", my_endpoint->GetName());
-
-       Zone::Ptr parent_zone = my_zone->GetParent();
-       if (parent_zone)
-               params->Set("parent_zone", parent_zone->GetName());
-
-       params->Set("zone", my_zone->GetName());
-       params->Set("repository", repository);
-
-       Dictionary::Ptr message = new Dictionary();
-       message->Set("jsonrpc", "2.0");
-       message->Set("method", "event::UpdateRepository");
-       message->Set("params", params);
-
-       listener->RelayMessage(MessageOrigin::Ptr(), my_zone, message, false);
-}
-
-String ApiEvents::GetRepositoryDir(void)
-{
-       return Application::GetLocalStateDir() + "/lib/icinga2/api/repository/";
+       inboxes.Push(std::move(result));
 }
 
-Value ApiEvents::UpdateRepositoryAPIHandler(const MessageOrigin::Ptr& origin, const Dictionary::Ptr& params)
+void ApiEvents::DowntimeTriggeredHandler(const Downtime::Ptr& downtime)
 {
-       if (!params)
-               return Empty;
+       std::vector<EventQueue::Ptr> queues = EventQueue::GetQueuesForType("DowntimeTriggered");
+       auto inboxes (EventsRouter::GetInstance().GetInboxes(EventType::DowntimeTriggered));
 
-       Value vrepository = params->Get("repository");
-       if (vrepository.IsEmpty() || !vrepository.IsObjectType<Dictionary>())
-               return Empty;
-
-       String repositoryFile = GetRepositoryDir() + SHA256(params->Get("endpoint")) + ".repo";
-       String repositoryTempFile = repositoryFile + ".tmp";
+       if (queues.empty() && !inboxes)
+               return;
 
-       std::ofstream fp(repositoryTempFile.CStr(), std::ofstream::out | std::ostream::trunc);
-       fp << JsonEncode(params);
-       fp.close();
+       Log(LogDebug, "ApiEvents", "Processing event type 'DowntimeTriggered'.");
 
-#ifdef _WIN32
-       _unlink(repositoryFile.CStr());
-#endif /* _WIN32 */
+       Dictionary::Ptr result = new Dictionary({
+               { "type", "DowntimeTriggered" },
+               { "timestamp", Utility::GetTime() },
+               { "downtime", Serialize(downtime, FAConfig | FAState) }
+       });
 
-       if (rename(repositoryTempFile.CStr(), repositoryFile.CStr()) < 0) {
-               BOOST_THROW_EXCEPTION(posix_error()
-                   << boost::errinfo_api_function("rename")
-                   << boost::errinfo_errno(errno)
-                   << boost::errinfo_file_name(repositoryTempFile));
+       for (const EventQueue::Ptr& queue : queues) {
+               queue->ProcessEvent(result);
        }
 
-       ApiListener::Ptr listener = ApiListener::GetInstance();
-
-       if (!listener)
-               return Empty;
-
-       Dictionary::Ptr message = new Dictionary();
-       message->Set("jsonrpc", "2.0");
-       message->Set("method", "event::UpdateRepository");
-       message->Set("params", params);
-
-       listener->RelayMessage(origin, Zone::GetLocalZone(), message, true);
-
-       return Empty;
+       inboxes.Push(std::move(result));
 }
-