1 /******************************************************************************
3 * Copyright (C) 2012-2017 Icinga Development Team (https://www.icinga.com/) *
5 * This program is free software; you can redistribute it and/or *
6 * modify it under the terms of the GNU General Public License *
7 * as published by the Free Software Foundation; either version 2 *
8 * of the License, or (at your option) any later version. *
10 * This program is distributed in the hope that it will be useful, *
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of *
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
13 * GNU General Public License for more details. *
15 * You should have received a copy of the GNU General Public License *
16 * along with this program; if not, write to the Free Software Foundation *
17 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA. *
18 ******************************************************************************/
20 #include "icinga/pluginutility.hpp"
21 #include "icinga/macroprocessor.hpp"
22 #include "base/logger.hpp"
23 #include "base/utility.hpp"
24 #include "base/perfdatavalue.hpp"
25 #include "base/convert.hpp"
26 #include "base/process.hpp"
27 #include "base/objectlock.hpp"
28 #include "base/exception.hpp"
29 #include <boost/algorithm/string/classification.hpp>
30 #include <boost/algorithm/string/split.hpp>
31 #include <boost/algorithm/string/trim.hpp>
33 using namespace icinga;
35 void PluginUtility::ExecuteCommand(const Command::Ptr& commandObj, const Checkable::Ptr& checkable,
36 const CheckResult::Ptr& cr, const MacroProcessor::ResolverList& macroResolvers,
37 const Dictionary::Ptr& resolvedMacros, bool useResolvedMacros,
38 const std::function<void(const Value& commandLine, const ProcessResult&)>& callback)
40 Value raw_command = commandObj->GetCommandLine();
41 Dictionary::Ptr raw_arguments = commandObj->GetArguments();
46 command = MacroProcessor::ResolveArguments(raw_command, raw_arguments,
47 macroResolvers, cr, resolvedMacros, useResolvedMacros);
48 } catch (const std::exception& ex) {
49 String message = DiagnosticInformation(ex);
51 Log(LogWarning, "PluginUtility", message);
56 pr.ExecutionStart = Utility::GetTime();
57 pr.ExecutionEnd = pr.ExecutionStart;
58 pr.ExitStatus = 3; /* Unknown */
66 Dictionary::Ptr envMacros = new Dictionary();
68 Dictionary::Ptr env = commandObj->GetEnv();
71 ObjectLock olock(env);
72 for (const Dictionary::Pair& kv : env) {
73 String name = kv.second;
75 Value value = MacroProcessor::ResolveMacros(name, macroResolvers, cr,
76 nullptr, MacroProcessor::EscapeCallback(), resolvedMacros,
79 if (value.IsObjectType<Array>())
80 value = Utility::Join(value, ';');
82 envMacros->Set(kv.first, value);
86 if (resolvedMacros && !useResolvedMacros)
89 Process::Ptr process = new Process(Process::PrepareCommand(command), envMacros);
91 if (checkable->GetCheckTimeout().IsEmpty())
92 process->SetTimeout(commandObj->GetTimeout());
94 process->SetTimeout(checkable->GetCheckTimeout());
96 process->SetAdjustPriority(true);
98 process->Run(std::bind(callback, command, _1));
101 ServiceState PluginUtility::ExitStatusToState(int exitStatus)
103 switch (exitStatus) {
107 return ServiceWarning;
109 return ServiceCritical;
111 return ServiceUnknown;
115 std::pair<String, String> PluginUtility::ParseCheckOutput(const String& output)
120 std::vector<String> lines;
121 boost::algorithm::split(lines, output, boost::is_any_of("\r\n"));
123 for (const String& line : lines) {
124 size_t delim = line.FindFirstOf("|");
129 if (delim != String::NPos) {
130 text += line.SubStr(0, delim);
132 if (!perfdata.IsEmpty())
135 perfdata += line.SubStr(delim + 1, line.GetLength());
141 boost::algorithm::trim(perfdata);
143 return std::make_pair(text, perfdata);
146 Array::Ptr PluginUtility::SplitPerfdata(const String& perfdata)
148 Array::Ptr result = new Array();
154 size_t eqp = perfdata.FindFirstOf('=', begin);
156 if (eqp == String::NPos)
159 String label = perfdata.SubStr(begin, eqp - begin);
161 if (label.GetLength() > 2 && label[0] == '\'' && label[label.GetLength() - 1] == '\'')
162 label = label.SubStr(1, label.GetLength() - 2);
164 size_t multi_index = label.RFind("::");
166 if (multi_index != String::NPos)
169 size_t spq = perfdata.FindFirstOf(' ', eqp);
171 if (spq == String::NPos)
172 spq = perfdata.GetLength();
174 String value = perfdata.SubStr(eqp + 1, spq - eqp - 1);
176 if (!multi_prefix.IsEmpty())
177 label = multi_prefix + "::" + label;
180 if (label.FindFirstOf(" ") != String::NPos)
181 pdv = "'" + label + "'=" + value;
183 pdv = label + "=" + value;
187 if (multi_index != String::NPos)
188 multi_prefix = label.SubStr(0, multi_index);
196 String PluginUtility::FormatPerfdata(const Array::Ptr& perfdata)
201 std::ostringstream result;
203 ObjectLock olock(perfdata);
206 for (const Value& pdv : perfdata) {
212 if (pdv.IsObjectType<PerfdataValue>())
213 result << static_cast<PerfdataValue::Ptr>(pdv)->Format();