2019-02-25 14:48:22 +01:00
|
|
|
/* Icinga 2 | (c) 2012 Icinga GmbH | GPLv2+ */
|
2012-07-09 20:32:02 +02:00
|
|
|
|
2014-05-25 16:23:35 +02:00
|
|
|
#include "icinga/pluginutility.hpp"
|
|
|
|
#include "icinga/macroprocessor.hpp"
|
2014-10-19 14:21:12 +02:00
|
|
|
#include "base/logger.hpp"
|
2014-05-25 16:23:35 +02:00
|
|
|
#include "base/utility.hpp"
|
2017-05-15 15:51:39 +02:00
|
|
|
#include "base/perfdatavalue.hpp"
|
2014-05-25 16:23:35 +02:00
|
|
|
#include "base/convert.hpp"
|
|
|
|
#include "base/process.hpp"
|
|
|
|
#include "base/objectlock.hpp"
|
2015-08-27 08:22:35 +02:00
|
|
|
#include "base/exception.hpp"
|
2013-11-17 01:00:03 +01:00
|
|
|
#include <boost/algorithm/string/trim.hpp>
|
2012-06-13 13:42:55 +02:00
|
|
|
|
|
|
|
using namespace icinga;
|
|
|
|
|
2014-04-26 13:16:08 +02:00
|
|
|
void PluginUtility::ExecuteCommand(const Command::Ptr& commandObj, const Checkable::Ptr& checkable,
|
2017-12-19 15:50:05 +01:00
|
|
|
const CheckResult::Ptr& cr, const MacroProcessor::ResolverList& macroResolvers,
|
2018-09-28 14:32:57 +02:00
|
|
|
const Dictionary::Ptr& resolvedMacros, bool useResolvedMacros, int timeout,
|
2017-12-19 15:50:05 +01:00
|
|
|
const std::function<void(const Value& commandLine, const ProcessResult&)>& callback)
|
2014-04-26 13:16:08 +02:00
|
|
|
{
|
|
|
|
Value raw_command = commandObj->GetCommandLine();
|
|
|
|
Dictionary::Ptr raw_arguments = commandObj->GetArguments();
|
|
|
|
|
|
|
|
Value command;
|
|
|
|
|
2015-08-27 08:22:35 +02:00
|
|
|
try {
|
|
|
|
command = MacroProcessor::ResolveArguments(raw_command, raw_arguments,
|
2017-12-19 15:50:05 +01:00
|
|
|
macroResolvers, cr, resolvedMacros, useResolvedMacros);
|
2015-08-27 08:22:35 +02:00
|
|
|
} catch (const std::exception& ex) {
|
|
|
|
String message = DiagnosticInformation(ex);
|
|
|
|
|
|
|
|
Log(LogWarning, "PluginUtility", message);
|
|
|
|
|
|
|
|
if (callback) {
|
|
|
|
ProcessResult pr;
|
|
|
|
pr.PID = -1;
|
|
|
|
pr.ExecutionStart = Utility::GetTime();
|
|
|
|
pr.ExecutionEnd = pr.ExecutionStart;
|
|
|
|
pr.ExitStatus = 3; /* Unknown */
|
|
|
|
pr.Output = message;
|
|
|
|
callback(Empty, pr);
|
2014-04-29 10:32:06 +02:00
|
|
|
}
|
|
|
|
|
2015-08-27 08:22:35 +02:00
|
|
|
return;
|
2014-04-26 13:16:08 +02:00
|
|
|
}
|
|
|
|
|
2014-11-08 21:17:16 +01:00
|
|
|
Dictionary::Ptr envMacros = new Dictionary();
|
2014-04-26 13:16:08 +02:00
|
|
|
|
|
|
|
Dictionary::Ptr env = commandObj->GetEnv();
|
|
|
|
|
|
|
|
if (env) {
|
|
|
|
ObjectLock olock(env);
|
2016-08-25 06:19:44 +02:00
|
|
|
for (const Dictionary::Pair& kv : env) {
|
2014-04-26 13:16:08 +02:00
|
|
|
String name = kv.second;
|
|
|
|
|
2018-07-29 15:39:02 +02:00
|
|
|
String missingMacro;
|
2014-11-13 11:23:57 +01:00
|
|
|
Value value = MacroProcessor::ResolveMacros(name, macroResolvers, cr,
|
2018-07-29 15:08:12 +02:00
|
|
|
&missingMacro, MacroProcessor::EscapeCallback(), resolvedMacros,
|
2017-12-19 15:50:05 +01:00
|
|
|
useResolvedMacros);
|
2014-04-26 13:16:08 +02:00
|
|
|
|
2018-07-29 15:08:12 +02:00
|
|
|
#ifdef I2_DEBUG
|
2018-07-29 15:39:02 +02:00
|
|
|
if (!missingMacro.IsEmpty())
|
2018-07-29 15:08:12 +02:00
|
|
|
Log(LogDebug, "PluginUtility")
|
|
|
|
<< "Macro '" << name << "' is not defined.";
|
|
|
|
#endif /* I2_DEBUG */
|
|
|
|
|
2014-11-26 20:43:42 +01:00
|
|
|
if (value.IsObjectType<Array>())
|
|
|
|
value = Utility::Join(value, ';');
|
|
|
|
|
2014-04-26 13:16:08 +02:00
|
|
|
envMacros->Set(kv.first, value);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-11-13 11:23:57 +01:00
|
|
|
if (resolvedMacros && !useResolvedMacros)
|
|
|
|
return;
|
|
|
|
|
2014-11-08 21:17:16 +01:00
|
|
|
Process::Ptr process = new Process(Process::PrepareCommand(command), envMacros);
|
2016-04-07 15:07:17 +02:00
|
|
|
|
2018-09-28 14:32:57 +02:00
|
|
|
process->SetTimeout(timeout);
|
2017-02-21 11:31:07 +01:00
|
|
|
process->SetAdjustPriority(true);
|
|
|
|
|
2021-01-18 14:29:05 +01:00
|
|
|
process->Run([callback, command](const ProcessResult& pr) { callback(command, pr); });
|
2014-04-26 13:16:08 +02:00
|
|
|
}
|
|
|
|
|
2013-11-04 14:41:24 +01:00
|
|
|
ServiceState PluginUtility::ExitStatusToState(int exitStatus)
|
2013-01-22 12:05:36 +01:00
|
|
|
{
|
|
|
|
switch (exitStatus) {
|
2012-07-13 21:00:54 +02:00
|
|
|
case 0:
|
2014-04-08 09:11:54 +02:00
|
|
|
return ServiceOK;
|
2012-07-13 21:00:54 +02:00
|
|
|
case 1:
|
2014-04-08 09:11:54 +02:00
|
|
|
return ServiceWarning;
|
2012-07-13 21:00:54 +02:00
|
|
|
case 2:
|
2014-04-08 09:11:54 +02:00
|
|
|
return ServiceCritical;
|
2012-07-13 21:00:54 +02:00
|
|
|
default:
|
2014-04-08 09:11:54 +02:00
|
|
|
return ServiceUnknown;
|
2012-06-19 09:38:20 +02:00
|
|
|
}
|
2012-06-24 16:30:16 +02:00
|
|
|
}
|
2012-06-13 13:42:55 +02:00
|
|
|
|
2014-09-07 12:27:06 +02:00
|
|
|
std::pair<String, String> PluginUtility::ParseCheckOutput(const String& output)
|
2012-06-29 14:14:51 +02:00
|
|
|
{
|
2012-08-02 09:38:08 +02:00
|
|
|
String text;
|
|
|
|
String perfdata;
|
2012-06-29 14:14:51 +02:00
|
|
|
|
2018-01-04 18:24:45 +01:00
|
|
|
std::vector<String> lines = output.Split("\r\n");
|
2012-06-29 14:14:51 +02:00
|
|
|
|
2016-08-25 06:19:44 +02:00
|
|
|
for (const String& line : lines) {
|
2012-08-02 09:38:08 +02:00
|
|
|
size_t delim = line.FindFirstOf("|");
|
2012-06-29 14:14:51 +02:00
|
|
|
|
2012-08-02 09:38:08 +02:00
|
|
|
if (!text.IsEmpty())
|
|
|
|
text += "\n";
|
2012-06-29 14:14:51 +02:00
|
|
|
|
2020-05-14 10:53:13 +02:00
|
|
|
if (delim != String::NPos && line.FindFirstOf("=", delim) != String::NPos) {
|
2012-08-02 09:38:08 +02:00
|
|
|
text += line.SubStr(0, delim);
|
2012-06-29 14:14:51 +02:00
|
|
|
|
2012-08-02 09:38:08 +02:00
|
|
|
if (!perfdata.IsEmpty())
|
|
|
|
perfdata += " ";
|
2012-06-29 14:14:51 +02:00
|
|
|
|
2012-08-02 09:38:08 +02:00
|
|
|
perfdata += line.SubStr(delim + 1, line.GetLength());
|
2012-06-29 14:14:51 +02:00
|
|
|
} else {
|
2012-08-02 09:38:08 +02:00
|
|
|
text += line;
|
2012-06-29 14:14:51 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-11-17 01:00:03 +01:00
|
|
|
boost::algorithm::trim(perfdata);
|
|
|
|
|
2014-09-07 12:27:06 +02:00
|
|
|
return std::make_pair(text, perfdata);
|
2012-06-13 13:42:55 +02:00
|
|
|
}
|
2013-11-07 13:37:58 +01:00
|
|
|
|
2014-09-17 15:38:39 +02:00
|
|
|
Array::Ptr PluginUtility::SplitPerfdata(const String& perfdata)
|
2013-11-07 13:37:58 +01:00
|
|
|
{
|
2018-01-11 11:17:38 +01:00
|
|
|
ArrayData result;
|
2014-08-27 18:29:08 +02:00
|
|
|
|
2014-09-17 15:38:39 +02:00
|
|
|
size_t begin = 0;
|
|
|
|
String multi_prefix;
|
2014-08-27 18:29:08 +02:00
|
|
|
|
2014-09-17 15:38:39 +02:00
|
|
|
for (;;) {
|
|
|
|
size_t eqp = perfdata.FindFirstOf('=', begin);
|
2013-11-07 13:37:58 +01:00
|
|
|
|
2014-09-17 15:38:39 +02:00
|
|
|
if (eqp == String::NPos)
|
|
|
|
break;
|
2013-11-07 13:37:58 +01:00
|
|
|
|
2014-09-17 15:38:39 +02:00
|
|
|
String label = perfdata.SubStr(begin, eqp - begin);
|
2013-11-07 13:37:58 +01:00
|
|
|
|
2014-09-17 15:38:39 +02:00
|
|
|
if (label.GetLength() > 2 && label[0] == '\'' && label[label.GetLength() - 1] == '\'')
|
|
|
|
label = label.SubStr(1, label.GetLength() - 2);
|
2013-11-13 15:42:58 +01:00
|
|
|
|
2014-09-17 15:38:39 +02:00
|
|
|
size_t multi_index = label.RFind("::");
|
2013-11-17 03:29:43 +01:00
|
|
|
|
2014-09-17 15:38:39 +02:00
|
|
|
if (multi_index != String::NPos)
|
|
|
|
multi_prefix = "";
|
2013-11-17 03:29:43 +01:00
|
|
|
|
2014-09-17 15:38:39 +02:00
|
|
|
size_t spq = perfdata.FindFirstOf(' ', eqp);
|
2013-11-07 13:37:58 +01:00
|
|
|
|
2014-09-17 15:38:39 +02:00
|
|
|
if (spq == String::NPos)
|
|
|
|
spq = perfdata.GetLength();
|
2013-11-07 13:37:58 +01:00
|
|
|
|
2014-09-17 15:38:39 +02:00
|
|
|
String value = perfdata.SubStr(eqp + 1, spq - eqp - 1);
|
2013-11-07 13:37:58 +01:00
|
|
|
|
2014-09-17 15:38:39 +02:00
|
|
|
if (!multi_prefix.IsEmpty())
|
|
|
|
label = multi_prefix + "::" + label;
|
2013-11-17 03:29:43 +01:00
|
|
|
|
2014-09-17 15:38:39 +02:00
|
|
|
String pdv;
|
|
|
|
if (label.FindFirstOf(" ") != String::NPos)
|
|
|
|
pdv = "'" + label + "'=" + value;
|
|
|
|
else
|
|
|
|
pdv = label + "=" + value;
|
2013-11-07 13:37:58 +01:00
|
|
|
|
2018-01-11 11:17:38 +01:00
|
|
|
result.emplace_back(std::move(pdv));
|
2013-11-17 03:29:43 +01:00
|
|
|
|
2014-09-17 15:38:39 +02:00
|
|
|
if (multi_index != String::NPos)
|
|
|
|
multi_prefix = label.SubStr(0, multi_index);
|
2013-11-07 13:37:58 +01:00
|
|
|
|
2014-09-17 15:38:39 +02:00
|
|
|
begin = spq + 1;
|
2013-11-07 13:37:58 +01:00
|
|
|
}
|
2014-09-17 15:38:39 +02:00
|
|
|
|
2018-01-11 11:17:38 +01:00
|
|
|
return new Array(std::move(result));
|
2013-11-07 13:37:58 +01:00
|
|
|
}
|
|
|
|
|
2021-05-11 16:47:03 +02:00
|
|
|
String PluginUtility::FormatPerfdata(const Array::Ptr& perfdata, bool normalize)
|
2013-11-07 13:37:58 +01:00
|
|
|
{
|
2014-09-17 16:07:12 +02:00
|
|
|
if (!perfdata)
|
|
|
|
return "";
|
|
|
|
|
2013-11-30 17:42:50 +01:00
|
|
|
std::ostringstream result;
|
2013-11-07 13:37:58 +01:00
|
|
|
|
2014-09-17 15:38:39 +02:00
|
|
|
ObjectLock olock(perfdata);
|
2013-11-13 09:08:17 +01:00
|
|
|
|
2013-11-30 17:42:50 +01:00
|
|
|
bool first = true;
|
2016-08-25 06:19:44 +02:00
|
|
|
for (const Value& pdv : perfdata) {
|
2013-11-30 17:42:50 +01:00
|
|
|
if (!first)
|
|
|
|
result << " ";
|
|
|
|
else
|
|
|
|
first = false;
|
2013-11-07 13:37:58 +01:00
|
|
|
|
2021-05-11 16:47:03 +02:00
|
|
|
if (pdv.IsObjectType<PerfdataValue>()) {
|
2014-09-17 15:38:39 +02:00
|
|
|
result << static_cast<PerfdataValue::Ptr>(pdv)->Format();
|
2021-05-11 16:47:03 +02:00
|
|
|
} else if (normalize) {
|
|
|
|
PerfdataValue::Ptr normalized;
|
|
|
|
|
|
|
|
try {
|
|
|
|
normalized = PerfdataValue::Parse(pdv);
|
|
|
|
} catch (const std::invalid_argument& ex) {
|
|
|
|
Log(LogDebug, "PerfdataValue") << ex.what();
|
|
|
|
}
|
|
|
|
|
|
|
|
if (normalized) {
|
|
|
|
result << normalized->Format();
|
|
|
|
} else {
|
|
|
|
result << pdv;
|
|
|
|
}
|
|
|
|
} else {
|
2014-09-17 15:38:39 +02:00
|
|
|
result << pdv;
|
2021-05-11 16:47:03 +02:00
|
|
|
}
|
2013-11-07 13:37:58 +01:00
|
|
|
}
|
|
|
|
|
2013-11-30 17:42:50 +01:00
|
|
|
return result.str();
|
2013-11-07 13:37:58 +01:00
|
|
|
}
|