Skip to content

Commit a0ad767

Browse files
committed
TODO
1 parent 4a0365a commit a0ad767

7 files changed

+45
-31
lines changed

lib/compat/externalcommandlistener.cpp

+1-1
Original file line numberDiff line numberDiff line change
@@ -139,7 +139,7 @@ void ExternalCommandListener::CommandPipeThread(const String& commandPath)
139139
Log(LogInformation, "ExternalCommandListener")
140140
<< "Executing external command: " << command;
141141

142-
ExternalCommandProcessor::Execute(command);
142+
ExternalCommandProcessor::Execute(this, command);
143143
} catch (const std::exception& ex) {
144144
Log(LogWarning, "ExternalCommandListener")
145145
<< "External command failed: " << DiagnosticInformation(ex, false);

lib/icinga/externalcommandprocessor.cpp

+22-12
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ using namespace icinga;
2727

2828
boost::signals2::signal<void(double, const String&, const std::vector<String>&)> ExternalCommandProcessor::OnNewExternalCommand;
2929

30-
void ExternalCommandProcessor::Execute(const String& line)
30+
void ExternalCommandProcessor::Execute(const CheckResultProducer::Ptr& producer, const String& line)
3131
{
3232
if (line.IsEmpty())
3333
return;
@@ -54,10 +54,10 @@ void ExternalCommandProcessor::Execute(const String& line)
5454
BOOST_THROW_EXCEPTION(std::invalid_argument("Missing arguments in command: " + line));
5555

5656
std::vector<String> argvExtra(argv.begin() + 1, argv.end());
57-
Execute(ts, argv[0], argvExtra);
57+
Execute(producer, ts, argv[0], argvExtra);
5858
}
5959

60-
void ExternalCommandProcessor::Execute(double time, const String& command, const std::vector<String>& arguments)
60+
void ExternalCommandProcessor::Execute(const CheckResultProducer::Ptr& producer, double time, const String& command, const std::vector<String>& arguments)
6161
{
6262
ExternalCommandInfo eci;
6363

@@ -102,7 +102,7 @@ void ExternalCommandProcessor::Execute(double time, const String& command, const
102102

103103
OnNewExternalCommand(time, command, realArguments);
104104

105-
eci.Callback(time, realArguments);
105+
eci.Callback(producer, time, realArguments);
106106
}
107107

108108
void ExternalCommandProcessor::RegisterCommand(const String& command, const ExternalCommandCallback& callback, size_t minArgs, size_t maxArgs)
@@ -115,6 +115,16 @@ void ExternalCommandProcessor::RegisterCommand(const String& command, const Exte
115115
GetCommands()[command] = eci;
116116
}
117117

118+
void ExternalCommandProcessor::RegisterCommand(const String& command, const ExternalCommandCallbackLite& callback, size_t minArgs, size_t maxArgs)
119+
{
120+
RegisterCommand(
121+
command,
122+
[callback](const CheckResultProducer::Ptr&, double time, const std::vector<String>& args) { callback(time, args); },
123+
minArgs,
124+
maxArgs
125+
);
126+
}
127+
118128
void ExternalCommandProcessor::RegisterCommands()
119129
{
120130
RegisterCommand("PROCESS_HOST_CHECK_RESULT", &ExternalCommandProcessor::ProcessHostCheckResult, 3);
@@ -237,7 +247,7 @@ void ExternalCommandProcessor::RegisterCommands()
237247
RegisterCommand("DISABLE_SERVICEGROUP_SVC_NOTIFICATIONS", &ExternalCommandProcessor::DisableServicegroupSvcNotifications, 1);
238248
}
239249

240-
void ExternalCommandProcessor::ExecuteFromFile(const String& line, std::deque< std::vector<String> >& file_queue)
250+
void ExternalCommandProcessor::ExecuteFromFile(const CheckResultProducer::Ptr& producer, const String& line, std::deque< std::vector<String> >& file_queue)
241251
{
242252
if (line.IsEmpty())
243253
return;
@@ -270,11 +280,11 @@ void ExternalCommandProcessor::ExecuteFromFile(const String& line, std::deque< s
270280
<< "Enqueing external command file " << argvExtra[0];
271281
file_queue.push_back(argvExtra);
272282
} else {
273-
Execute(ts, argv[0], argvExtra);
283+
Execute(producer, ts, argv[0], argvExtra);
274284
}
275285
}
276286

277-
void ExternalCommandProcessor::ProcessHostCheckResult(double time, const std::vector<String>& arguments)
287+
void ExternalCommandProcessor::ProcessHostCheckResult(const CheckResultProducer::Ptr& producer, double time, const std::vector<String>& arguments)
278288
{
279289
Host::Ptr host = Host::GetByName(arguments[0]);
280290

@@ -318,10 +328,10 @@ void ExternalCommandProcessor::ProcessHostCheckResult(double time, const std::ve
318328
Log(LogNotice, "ExternalCommandProcessor")
319329
<< "Processing passive check result for host '" << arguments[0] << "'";
320330

321-
host->ProcessCheckResult(result);
331+
host->ProcessCheckResult(result, producer);
322332
}
323333

324-
void ExternalCommandProcessor::ProcessServiceCheckResult(double time, const std::vector<String>& arguments)
334+
void ExternalCommandProcessor::ProcessServiceCheckResult(const CheckResultProducer::Ptr& producer, double time, const std::vector<String>& arguments)
325335
{
326336
Service::Ptr service = Service::GetByNamePair(arguments[0], arguments[1]);
327337

@@ -356,7 +366,7 @@ void ExternalCommandProcessor::ProcessServiceCheckResult(double time, const std:
356366
Log(LogNotice, "ExternalCommandProcessor")
357367
<< "Processing passive check result for service '" << arguments[1] << "'";
358368

359-
service->ProcessCheckResult(result);
369+
service->ProcessCheckResult(result, producer);
360370
}
361371

362372
void ExternalCommandProcessor::ScheduleHostCheck(double, const std::vector<String>& arguments)
@@ -921,7 +931,7 @@ void ExternalCommandProcessor::DisableHostgroupPassiveSvcChecks(double, const st
921931
}
922932
}
923933

924-
void ExternalCommandProcessor::ProcessFile(double, const std::vector<String>& arguments)
934+
void ExternalCommandProcessor::ProcessFile(const CheckResultProducer::Ptr& producer, double, const std::vector<String>& arguments)
925935
{
926936
std::deque< std::vector<String> > file_queue;
927937
file_queue.push_back(arguments);
@@ -946,7 +956,7 @@ void ExternalCommandProcessor::ProcessFile(double, const std::vector<String>& ar
946956
Log(LogNotice, "compat")
947957
<< "Executing external command: " << line;
948958

949-
ExecuteFromFile(line, file_queue);
959+
ExecuteFromFile(producer, line, file_queue);
950960
} catch (const std::exception& ex) {
951961
Log(LogWarning, "ExternalCommandProcessor")
952962
<< "External command failed: " << DiagnosticInformation(ex);

lib/icinga/externalcommandprocessor.hpp

+10-7
Original file line numberDiff line numberDiff line change
@@ -5,14 +5,16 @@
55

66
#include "icinga/i2-icinga.hpp"
77
#include "icinga/command.hpp"
8+
#include "remote/crproducer.hpp"
89
#include "base/string.hpp"
910
#include <boost/signals2.hpp>
1011
#include <vector>
1112

1213
namespace icinga
1314
{
1415

15-
typedef std::function<void (double, const std::vector<String>& arguments)> ExternalCommandCallback;
16+
typedef std::function<void(const CheckResultProducer::Ptr&, double, const std::vector<String>& arguments)> ExternalCommandCallback;
17+
typedef std::function<void(double, const std::vector<String>& arguments)> ExternalCommandCallbackLite;
1618

1719
struct ExternalCommandInfo
1820
{
@@ -23,18 +25,18 @@ struct ExternalCommandInfo
2325

2426
class ExternalCommandProcessor {
2527
public:
26-
static void Execute(const String& line);
27-
static void Execute(double time, const String& command, const std::vector<String>& arguments);
28+
static void Execute(const CheckResultProducer::Ptr& producer, const String& line);
29+
static void Execute(const CheckResultProducer::Ptr& producer, double time, const String& command, const std::vector<String>& arguments);
2830

2931
static boost::signals2::signal<void(double, const String&, const std::vector<String>&)> OnNewExternalCommand;
3032

3133
private:
3234
ExternalCommandProcessor();
3335

34-
static void ExecuteFromFile(const String& line, std::deque< std::vector<String> >& file_queue);
36+
static void ExecuteFromFile(const CheckResultProducer::Ptr& producer, const String& line, std::deque< std::vector<String> >& file_queue);
3537

36-
static void ProcessHostCheckResult(double time, const std::vector<String>& arguments);
37-
static void ProcessServiceCheckResult(double time, const std::vector<String>& arguments);
38+
static void ProcessHostCheckResult(const CheckResultProducer::Ptr& producer, double time, const std::vector<String>& arguments);
39+
static void ProcessServiceCheckResult(const CheckResultProducer::Ptr& producer, double time, const std::vector<String>& arguments);
3840
static void ScheduleHostCheck(double time, const std::vector<String>& arguments);
3941
static void ScheduleForcedHostCheck(double time, const std::vector<String>& arguments);
4042
static void ScheduleSvcCheck(double time, const std::vector<String>& arguments);
@@ -67,7 +69,7 @@ class ExternalCommandProcessor {
6769
static void DisableServicegroupPassiveSvcChecks(double time, const std::vector<String>& arguments);
6870
static void EnableHostgroupPassiveSvcChecks(double time, const std::vector<String>& arguments);
6971
static void DisableHostgroupPassiveSvcChecks(double time, const std::vector<String>& arguments);
70-
static void ProcessFile(double time, const std::vector<String>& arguments);
72+
static void ProcessFile(const CheckResultProducer::Ptr& producer, double time, const std::vector<String>& arguments);
7173
static void ScheduleSvcDowntime(double time, const std::vector<String>& arguments);
7274
static void DelSvcDowntime(double time, const std::vector<String>& arguments);
7375
static void ScheduleHostDowntime(double time, const std::vector<String>& arguments);
@@ -157,6 +159,7 @@ class ExternalCommandProcessor {
157159
static void ChangeCustomCommandVarInternal(const Command::Ptr& command, const String& name, const Value& value);
158160

159161
static void RegisterCommand(const String& command, const ExternalCommandCallback& callback, size_t minArgs = 0, size_t maxArgs = UINT_MAX);
162+
static void RegisterCommand(const String& command, const ExternalCommandCallbackLite& callback, size_t minArgs = 0, size_t maxArgs = UINT_MAX);
160163
static void RegisterCommands();
161164

162165
static std::mutex& GetMutex();

lib/livestatus/livestatuslistener.cpp

+1-1
Original file line numberDiff line numberDiff line change
@@ -193,7 +193,7 @@ void LivestatusListener::ClientHandler(const Socket::Ptr& client)
193193
break;
194194

195195
LivestatusQuery::Ptr query = new LivestatusQuery(lines, GetCompatLogPath());
196-
if (!query->Execute(stream))
196+
if (!query->Execute(this, stream))
197197
break;
198198
}
199199

lib/livestatus/livestatusquery.cpp

+4-4
Original file line numberDiff line numberDiff line change
@@ -570,7 +570,7 @@ void LivestatusQuery::ExecuteGetHelper(const Stream::Ptr& stream)
570570
SendResponse(stream, LivestatusErrorOK, result.str());
571571
}
572572

573-
void LivestatusQuery::ExecuteCommandHelper(const Stream::Ptr& stream)
573+
void LivestatusQuery::ExecuteCommandHelper(const CheckResultProducer::Ptr& producer, const Stream::Ptr& stream)
574574
{
575575
{
576576
std::unique_lock<std::mutex> lock(l_QueryMutex);
@@ -580,7 +580,7 @@ void LivestatusQuery::ExecuteCommandHelper(const Stream::Ptr& stream)
580580

581581
Log(LogNotice, "LivestatusQuery")
582582
<< "Executing command: " << m_Command;
583-
ExternalCommandProcessor::Execute(m_Command);
583+
ExternalCommandProcessor::Execute(producer, m_Command);
584584
SendResponse(stream, LivestatusErrorOK, "");
585585
}
586586

@@ -621,7 +621,7 @@ void LivestatusQuery::PrintFixed16(const Stream::Ptr& stream, int code, const St
621621
}
622622
}
623623

624-
bool LivestatusQuery::Execute(const Stream::Ptr& stream)
624+
bool LivestatusQuery::Execute(const CheckResultProducer::Ptr& producer, const Stream::Ptr& stream)
625625
{
626626
try {
627627
Log(LogNotice, "LivestatusQuery")
@@ -630,7 +630,7 @@ bool LivestatusQuery::Execute(const Stream::Ptr& stream)
630630
if (m_Verb == "GET")
631631
ExecuteGetHelper(stream);
632632
else if (m_Verb == "COMMAND")
633-
ExecuteCommandHelper(stream);
633+
ExecuteCommandHelper(producer, stream);
634634
else if (m_Verb == "ERROR")
635635
ExecuteErrorHelper(stream);
636636
else

lib/livestatus/livestatusquery.hpp

+3-2
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55

66
#include "livestatus/filter.hpp"
77
#include "livestatus/aggregator.hpp"
8+
#include "remote/crproducer.hpp"
89
#include "base/object.hpp"
910
#include "base/array.hpp"
1011
#include "base/stream.hpp"
@@ -33,7 +34,7 @@ class LivestatusQuery final : public Object
3334

3435
LivestatusQuery(const std::vector<String>& lines, const String& compat_log_path);
3536

36-
bool Execute(const Stream::Ptr& stream);
37+
bool Execute(const CheckResultProducer::Ptr& producer, const Stream::Ptr& stream);
3738

3839
static int GetExternalCommands();
3940

@@ -76,7 +77,7 @@ class LivestatusQuery final : public Object
7677
static String QuoteStringPython(const String& str);
7778

7879
void ExecuteGetHelper(const Stream::Ptr& stream);
79-
void ExecuteCommandHelper(const Stream::Ptr& stream);
80+
void ExecuteCommandHelper(const CheckResultProducer::Ptr& producer, const Stream::Ptr& stream);
8081
void ExecuteErrorHelper(const Stream::Ptr& stream);
8182

8283
void SendResponse(const Stream::Ptr& stream, int code, const String& data);

test/livestatus.cpp

+4-4
Original file line numberDiff line numberDiff line change
@@ -8,14 +8,14 @@
88

99
using namespace icinga;
1010

11-
String LivestatusQueryHelper(const std::vector<String>& lines)
11+
String LivestatusQueryHelper(const CheckResultProducer::Ptr& producer, const std::vector<String>& lines)
1212
{
1313
LivestatusQuery::Ptr query = new LivestatusQuery(lines, "");
1414

1515
std::stringstream stream;
1616
StdioStream::Ptr sstream = new StdioStream(&stream, false);
1717

18-
query->Execute(sstream);
18+
query->Execute(producer, sstream);
1919

2020
String output;
2121
String result;
@@ -56,7 +56,7 @@ BOOST_AUTO_TEST_CASE(hosts)
5656
lines.emplace_back("\n");
5757

5858
/* use our query helper */
59-
String output = LivestatusQueryHelper(lines);
59+
String output = LivestatusQueryHelper(new UnitTestCRP(), lines);
6060

6161
Array::Ptr query_result = JsonDecode(output);
6262

@@ -86,7 +86,7 @@ BOOST_AUTO_TEST_CASE(services)
8686
lines.emplace_back("\n");
8787

8888
/* use our query helper */
89-
String output = LivestatusQueryHelper(lines);
89+
String output = LivestatusQueryHelper(new UnitTestCRP(), lines);
9090

9191
Array::Ptr query_result = JsonDecode(output);
9292

0 commit comments

Comments
 (0)