1 /******************************************************************************
3 * Copyright (C) 2012-2018 Icinga Development Team (https://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, int timeout,
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;
74 Value value = MacroProcessor::ResolveMacros(name, macroResolvers, cr,
75 &missingMacro, MacroProcessor::EscapeCallback(), resolvedMacros,
79 if (!missingMacro.IsEmpty())
80 Log(LogDebug, "PluginUtility")
81 << "Macro '" << name << "' is not defined.";
84 if (value.IsObjectType<Array>())
85 value = Utility::Join(value, ';');
87 envMacros->Set(kv.first, value);
91 if (resolvedMacros && !useResolvedMacros)
94 Process::Ptr process = new Process(Process::PrepareCommand(command), envMacros);
96 process->SetTimeout(timeout);
97 process->SetAdjustPriority(true);
99 process->Run(std::bind(callback, command, _1));
102 ServiceState PluginUtility::ExitStatusToState(int exitStatus)
104 switch (exitStatus) {
108 return ServiceWarning;
110 return ServiceCritical;
112 return ServiceUnknown;
116 std::pair<String, String> PluginUtility::ParseCheckOutput(const String& output)
121 std::vector<String> lines = output.Split("\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)
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;
185 result.emplace_back(std::move(pdv));
187 if (multi_index != String::NPos)
188 multi_prefix = label.SubStr(0, multi_index);
193 return new Array(std::move(result));
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();