1 /******************************************************************************
3 * Copyright (C) 2012-2018 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/trim.hpp>
31 using namespace icinga;
33 void PluginUtility::ExecuteCommand(const Command::Ptr& commandObj, const Checkable::Ptr& checkable,
34 const CheckResult::Ptr& cr, const MacroProcessor::ResolverList& macroResolvers,
35 const Dictionary::Ptr& resolvedMacros, bool useResolvedMacros,
36 const std::function<void(const Value& commandLine, const ProcessResult&)>& callback)
38 Value raw_command = commandObj->GetCommandLine();
39 Dictionary::Ptr raw_arguments = commandObj->GetArguments();
44 command = MacroProcessor::ResolveArguments(raw_command, raw_arguments,
45 macroResolvers, cr, resolvedMacros, useResolvedMacros);
46 } catch (const std::exception& ex) {
47 String message = DiagnosticInformation(ex);
49 Log(LogWarning, "PluginUtility", message);
54 pr.ExecutionStart = Utility::GetTime();
55 pr.ExecutionEnd = pr.ExecutionStart;
56 pr.ExitStatus = 3; /* Unknown */
64 Dictionary::Ptr envMacros = new Dictionary();
66 Dictionary::Ptr env = commandObj->GetEnv();
69 ObjectLock olock(env);
70 for (const Dictionary::Pair& kv : env) {
71 String name = kv.second;
73 Value value = MacroProcessor::ResolveMacros(name, macroResolvers, cr,
74 nullptr, MacroProcessor::EscapeCallback(), resolvedMacros,
77 if (value.IsObjectType<Array>())
78 value = Utility::Join(value, ';');
80 envMacros->Set(kv.first, value);
84 if (resolvedMacros && !useResolvedMacros)
87 Process::Ptr process = new Process(Process::PrepareCommand(command), envMacros);
89 if (checkable->GetCheckTimeout().IsEmpty())
90 process->SetTimeout(commandObj->GetTimeout());
92 process->SetTimeout(checkable->GetCheckTimeout());
94 process->SetAdjustPriority(true);
96 process->Run(std::bind(callback, command, _1));
99 ServiceState PluginUtility::ExitStatusToState(int exitStatus)
101 switch (exitStatus) {
105 return ServiceWarning;
107 return ServiceCritical;
109 return ServiceUnknown;
113 std::pair<String, String> PluginUtility::ParseCheckOutput(const String& output)
118 std::vector<String> lines = output.Split("\r\n");
120 for (const String& line : lines) {
121 size_t delim = line.FindFirstOf("|");
126 if (delim != String::NPos) {
127 text += line.SubStr(0, delim);
129 if (!perfdata.IsEmpty())
132 perfdata += line.SubStr(delim + 1, line.GetLength());
138 boost::algorithm::trim(perfdata);
140 return std::make_pair(text, perfdata);
143 Array::Ptr PluginUtility::SplitPerfdata(const String& perfdata)
151 size_t eqp = perfdata.FindFirstOf('=', begin);
153 if (eqp == String::NPos)
156 String label = perfdata.SubStr(begin, eqp - begin);
158 if (label.GetLength() > 2 && label[0] == '\'' && label[label.GetLength() - 1] == '\'')
159 label = label.SubStr(1, label.GetLength() - 2);
161 size_t multi_index = label.RFind("::");
163 if (multi_index != String::NPos)
166 size_t spq = perfdata.FindFirstOf(' ', eqp);
168 if (spq == String::NPos)
169 spq = perfdata.GetLength();
171 String value = perfdata.SubStr(eqp + 1, spq - eqp - 1);
173 if (!multi_prefix.IsEmpty())
174 label = multi_prefix + "::" + label;
177 if (label.FindFirstOf(" ") != String::NPos)
178 pdv = "'" + label + "'=" + value;
180 pdv = label + "=" + value;
182 result.emplace_back(std::move(pdv));
184 if (multi_index != String::NPos)
185 multi_prefix = label.SubStr(0, multi_index);
190 return new Array(std::move(result));
193 String PluginUtility::FormatPerfdata(const Array::Ptr& perfdata)
198 std::ostringstream result;
200 ObjectLock olock(perfdata);
203 for (const Value& pdv : perfdata) {
209 if (pdv.IsObjectType<PerfdataValue>())
210 result << static_cast<PerfdataValue::Ptr>(pdv)->Format();