MCPcopy Create free account
hub / github.com/Icinga/icinga2 / ExecuteCommand

Method ExecuteCommand

lib/icinga/pluginutility.cpp:17–84  ·  view source on GitHub ↗

Source from the content-addressed store, hash-verified

15using namespace icinga;
16
17void PluginUtility::ExecuteCommand(const Command::Ptr& commandObj,
18 const CheckResult::Ptr& cr, const MacroProcessor::ResolverList& macroResolvers,
19 const Dictionary::Ptr& resolvedMacros, bool useResolvedMacros, int timeout,
20 const std::function<void(const Value& commandLine, const ProcessResult&)>& callback)
21{
22 Value raw_command = commandObj->GetCommandLine();
23 Dictionary::Ptr raw_arguments = commandObj->GetArguments();
24
25 Value command;
26
27 try {
28 command = MacroProcessor::ResolveArguments(raw_command, raw_arguments,
29 macroResolvers, cr, resolvedMacros, useResolvedMacros);
30 } catch (const std::exception& ex) {
31 String message = DiagnosticInformation(ex);
32
33 Log(LogWarning, "PluginUtility", message);
34
35 if (callback) {
36 ProcessResult pr;
37 pr.PID = -1;
38 pr.ExecutionStart = Utility::GetTime();
39 pr.ExecutionEnd = pr.ExecutionStart;
40 pr.ExitStatus = 3; /* Unknown */
41 pr.Output = message;
42 callback(Empty, pr);
43 }
44
45 return;
46 }
47
48 Dictionary::Ptr envMacros = new Dictionary();
49
50 Dictionary::Ptr env = commandObj->GetEnv();
51
52 if (env) {
53 ObjectLock olock(env);
54 for (const Dictionary::Pair& kv : env) {
55 String name = kv.second;
56
57 String missingMacro;
58 Value value = MacroProcessor::ResolveMacros(name, macroResolvers, cr,
59 &missingMacro, MacroProcessor::EscapeCallback(), resolvedMacros,
60 useResolvedMacros);
61
62#ifdef I2_DEBUG
63 if (!missingMacro.IsEmpty())
64 Log(LogDebug, "PluginUtility")
65 << "Macro '" << name << "' is not defined.";
66#endif /* I2_DEBUG */
67
68 if (value.IsObjectType<Array>())
69 value = Utility::Join(value, ';');
70
71 envMacros->Set(kv.first, value);
72 }
73 }
74

Callers

nothing calls this directly

Calls 8

LogClass · 0.85
GetCommandLineMethod · 0.80
GetEnvMethod · 0.80
SetTimeoutMethod · 0.80
SetAdjustPriorityMethod · 0.80
IsEmptyMethod · 0.45
SetMethod · 0.45
RunMethod · 0.45

Tested by

no test coverage detected