-
Notifications
You must be signed in to change notification settings - Fork 577
/
pluginchecktask.cpp
97 lines (74 loc) · 3.26 KB
/
pluginchecktask.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
/* Icinga 2 | (c) 2012 Icinga GmbH | GPLv2+ */
#include "methods/pluginchecktask.hpp"
#include "icinga/pluginutility.hpp"
#include "icinga/checkcommand.hpp"
#include "icinga/macroprocessor.hpp"
#include "base/configtype.hpp"
#include "base/logger.hpp"
#include "base/function.hpp"
#include "base/utility.hpp"
#include "base/process.hpp"
#include "base/convert.hpp"
#include <sstream>
using namespace icinga;
REGISTER_FUNCTION_NONCONST(Internal, PluginCheck, &PluginCheckTask::ScriptFunc, "checkable:cr:resolvedMacros:useResolvedMacros");
void PluginCheckTask::ScriptFunc(const Checkable::Ptr& checkable, const CheckResult::Ptr& cr,
const Dictionary::Ptr& resolvedMacros, bool useResolvedMacros)
{
REQUIRE_NOT_NULL(checkable);
REQUIRE_NOT_NULL(cr);
CheckCommand::Ptr commandObj = CheckCommand::ExecuteOverride ? CheckCommand::ExecuteOverride : checkable->GetCheckCommand();
Host::Ptr host;
Service::Ptr service;
tie(host, service) = GetHostService(checkable);
MacroProcessor::ResolverList resolvers;
if (MacroResolver::OverrideMacros)
resolvers.emplace_back("override", MacroResolver::OverrideMacros);
if (service)
resolvers.emplace_back("service", service);
resolvers.emplace_back("host", host);
resolvers.emplace_back("command", commandObj);
int timeout = commandObj->GetTimeout();
if (!checkable->GetCheckTimeout().IsEmpty())
timeout = checkable->GetCheckTimeout();
std::function<void(const Value& commandLine, const ProcessResult&)> callback;
if (Checkable::ExecuteCommandProcessFinishedHandler) {
callback = Checkable::ExecuteCommandProcessFinishedHandler;
} else {
callback = [checkable, cr](const Value& commandLine, const ProcessResult& pr) {
ProcessFinishedHandler(checkable, cr, commandLine, pr);
};
}
PluginUtility::ExecuteCommand(commandObj, checkable, checkable->GetLastCheckResult(),
resolvers, resolvedMacros, useResolvedMacros, timeout, callback);
if (!resolvedMacros || useResolvedMacros) {
Checkable::CurrentConcurrentChecks.fetch_add(1);
Checkable::IncreasePendingChecks();
}
}
void PluginCheckTask::ProcessFinishedHandler(const Checkable::Ptr& checkable, const CheckResult::Ptr& cr, const Value& commandLine, const ProcessResult& pr)
{
Checkable::CurrentConcurrentChecks.fetch_sub(1);
Checkable::DecreasePendingChecks();
String output = pr.Output.Trim();
if (pr.ExitStatus > 3) {
Process::Arguments parguments = Process::PrepareCommand(commandLine);
Log(LogWarning, "PluginCheckTask")
<< "Check command for object '" << checkable->GetName() << "' (PID: " << pr.PID
<< ", arguments: " << Process::PrettyPrintArguments(parguments) << ") terminated with exit code "
<< pr.ExitStatus << ", output: " << pr.Output;
std::stringstream crOutput;
crOutput << "<Terminated with exit code " << pr.ExitStatus
<< " (0x" << std::noshowbase << std::hex << std::uppercase << pr.ExitStatus << ").>";
output += crOutput.str();
}
std::pair<String, String> co = PluginUtility::ParseCheckOutput(output);
cr->SetCommand(commandLine);
cr->SetOutput(co.first);
cr->SetPerformanceData(PluginUtility::SplitPerfdata(co.second));
cr->SetState(PluginUtility::ExitStatusToState(pr.ExitStatus));
cr->SetExitStatus(pr.ExitStatus);
cr->SetExecutionStart(pr.ExecutionStart);
cr->SetExecutionEnd(pr.ExecutionEnd);
checkable->ProcessCheckResult(cr);
}