1 /* Icinga 2 | (c) 2012 Icinga GmbH | GPLv2+ */
3 #include "icinga/checkable.hpp"
4 #include "icinga/eventcommand.hpp"
5 #include "icinga/icingaapplication.hpp"
6 #include "icinga/service.hpp"
7 #include "remote/apilistener.hpp"
8 #include "base/logger.hpp"
9 #include "base/context.hpp"
11 using namespace icinga;
13 boost::signals2::signal<void (const Checkable::Ptr&)> Checkable::OnEventCommandExecuted;
15 EventCommand::Ptr Checkable::GetEventCommand() const
17 return EventCommand::GetByName(GetEventCommandRaw());
20 void Checkable::ExecuteEventHandler(const Dictionary::Ptr& resolvedMacros, bool useResolvedMacros)
22 CONTEXT("Executing event handler for object '" + GetName() + "'");
24 if (!IcingaApplication::GetInstance()->GetEnableEventHandlers() || !GetEnableEventHandler())
27 /* HA enabled zones. */
28 if (IsActive() && IsPaused()) {
29 Log(LogNotice, "Checkable")
30 << "Skipping event handler for HA-paused checkable '" << GetName() << "'";
34 EventCommand::Ptr ec = GetEventCommand();
39 Log(LogNotice, "Checkable")
40 << "Executing event handler '" << ec->GetName() << "' for checkable '" << GetName() << "'";
42 Dictionary::Ptr macros;
43 Endpoint::Ptr endpoint = GetCommandEndpoint();
45 if (endpoint && !useResolvedMacros)
46 macros = new Dictionary();
48 macros = resolvedMacros;
50 ec->Execute(this, macros, useResolvedMacros);
53 Dictionary::Ptr message = new Dictionary();
54 message->Set("jsonrpc", "2.0");
55 message->Set("method", "event::ExecuteCommand");
59 tie(host, service) = GetHostService(this);
61 Dictionary::Ptr params = new Dictionary();
62 message->Set("params", params);
63 params->Set("command_type", "event_command");
64 params->Set("command", GetEventCommand()->GetName());
65 params->Set("host", host->GetName());
68 params->Set("service", service->GetShortName());
70 params->Set("macros", macros);
72 ApiListener::Ptr listener = ApiListener::GetInstance();
75 listener->SyncSendMessage(endpoint, message);
80 OnEventCommandExecuted(this);