]> granicus.if.org Git - icinga2/blob - lib/icinga/checkable-event.cpp
Fix spelling errors.
[icinga2] / lib / icinga / checkable-event.cpp
1 /* Icinga 2 | (c) 2012 Icinga GmbH | GPLv2+ */
2
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"
10
11 using namespace icinga;
12
13 boost::signals2::signal<void (const Checkable::Ptr&)> Checkable::OnEventCommandExecuted;
14
15 EventCommand::Ptr Checkable::GetEventCommand() const
16 {
17         return EventCommand::GetByName(GetEventCommandRaw());
18 }
19
20 void Checkable::ExecuteEventHandler(const Dictionary::Ptr& resolvedMacros, bool useResolvedMacros)
21 {
22         CONTEXT("Executing event handler for object '" + GetName() + "'");
23
24         if (!IcingaApplication::GetInstance()->GetEnableEventHandlers() || !GetEnableEventHandler())
25                 return;
26
27         /* HA enabled zones. */
28         if (IsActive() && IsPaused()) {
29                 Log(LogNotice, "Checkable")
30                         << "Skipping event handler for HA-paused checkable '" << GetName() << "'";
31                 return;
32         }
33
34         EventCommand::Ptr ec = GetEventCommand();
35
36         if (!ec)
37                 return;
38
39         Log(LogNotice, "Checkable")
40                 << "Executing event handler '" << ec->GetName() << "' for checkable '" << GetName() << "'";
41
42         Dictionary::Ptr macros;
43         Endpoint::Ptr endpoint = GetCommandEndpoint();
44
45         if (endpoint && !useResolvedMacros)
46                 macros = new Dictionary();
47         else
48                 macros = resolvedMacros;
49
50         ec->Execute(this, macros, useResolvedMacros);
51
52         if (endpoint) {
53                 Dictionary::Ptr message = new Dictionary();
54                 message->Set("jsonrpc", "2.0");
55                 message->Set("method", "event::ExecuteCommand");
56
57                 Host::Ptr host;
58                 Service::Ptr service;
59                 tie(host, service) = GetHostService(this);
60
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());
66
67                 if (service)
68                         params->Set("service", service->GetShortName());
69
70                 params->Set("macros", macros);
71
72                 ApiListener::Ptr listener = ApiListener::GetInstance();
73
74                 if (listener)
75                         listener->SyncSendMessage(endpoint, message);
76
77                 return;
78         }
79
80         OnEventCommandExecuted(this);
81 }