2012-07-09 20:32:02 +02:00
|
|
|
/******************************************************************************
|
|
|
|
* Icinga 2 *
|
2016-01-12 08:29:59 +01:00
|
|
|
* Copyright (C) 2012-2016 Icinga Development Team (https://www.icinga.org/) *
|
2012-07-09 20:32:02 +02:00
|
|
|
* *
|
|
|
|
* This program is free software; you can redistribute it and/or *
|
|
|
|
* modify it under the terms of the GNU General Public License *
|
|
|
|
* as published by the Free Software Foundation; either version 2 *
|
|
|
|
* of the License, or (at your option) any later version. *
|
|
|
|
* *
|
|
|
|
* This program is distributed in the hope that it will be useful, *
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of *
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
|
|
|
|
* GNU General Public License for more details. *
|
|
|
|
* *
|
|
|
|
* You should have received a copy of the GNU General Public License *
|
|
|
|
* along with this program; if not, write to the Free Software Foundation *
|
|
|
|
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA. *
|
|
|
|
******************************************************************************/
|
|
|
|
|
2014-05-25 16:23:35 +02:00
|
|
|
#include "icinga/pluginutility.hpp"
|
|
|
|
#include "icinga/macroprocessor.hpp"
|
|
|
|
#include "icinga/perfdatavalue.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"
|
|
|
|
#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-03-15 18:21:29 +01:00
|
|
|
#include <boost/algorithm/string/classification.hpp>
|
2013-03-18 11:02:18 +01:00
|
|
|
#include <boost/algorithm/string/split.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,
|
2014-05-22 08:59:46 +02:00
|
|
|
const CheckResult::Ptr& cr, const MacroProcessor::ResolverList& macroResolvers,
|
2014-11-13 11:23:57 +01:00
|
|
|
const Dictionary::Ptr& resolvedMacros, bool useResolvedMacros,
|
2014-04-26 13:16:08 +02:00
|
|
|
const boost::function<void(const Value& commandLine, const ProcessResult&)>& callback)
|
|
|
|
{
|
|
|
|
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,
|
|
|
|
macroResolvers, cr, resolvedMacros, useResolvedMacros);
|
|
|
|
} 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;
|
|
|
|
|
2014-11-13 11:23:57 +01:00
|
|
|
Value value = MacroProcessor::ResolveMacros(name, macroResolvers, cr,
|
|
|
|
NULL, MacroProcessor::EscapeCallback(), resolvedMacros,
|
|
|
|
useResolvedMacros);
|
2014-04-26 13:16:08 +02:00
|
|
|
|
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
|
|
|
|
|
|
|
if (checkable->GetCheckTimeout().IsEmpty())
|
|
|
|
process->SetTimeout(commandObj->GetTimeout());
|
|
|
|
else
|
|
|
|
process->SetTimeout(checkable->GetCheckTimeout());
|
|
|
|
|
2014-04-26 13:16:08 +02:00
|
|
|
process->Run(boost::bind(callback, command, _1));
|
|
|
|
}
|
|
|
|
|
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
|
|
|
|
2013-03-18 11:02:18 +01:00
|
|
|
std::vector<String> lines;
|
|
|
|
boost::algorithm::split(lines, output, boost::is_any_of("\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
|
|
|
|
2012-08-02 09:38:08 +02:00
|
|
|
if (delim != String::NPos) {
|
|
|
|
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
|
|
|
{
|
2014-11-08 21:17:16 +01:00
|
|
|
Array::Ptr result = new Array();
|
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
|
|
|
|
2014-09-17 15:38:39 +02:00
|
|
|
result->Add(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
|
|
|
|
|
|
|
return result;
|
2013-11-07 13:37:58 +01:00
|
|
|
}
|
|
|
|
|
2014-09-17 15:38:39 +02:00
|
|
|
String PluginUtility::FormatPerfdata(const Array::Ptr& perfdata)
|
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
|
|
|
|
2014-09-17 15:38:39 +02:00
|
|
|
if (pdv.IsObjectType<PerfdataValue>())
|
|
|
|
result << static_cast<PerfdataValue::Ptr>(pdv)->Format();
|
|
|
|
else
|
|
|
|
result << pdv;
|
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
|
|
|
}
|