1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
|
/* Icinga 2 | (c) 2012 Icinga GmbH | GPLv2+ */
#include "icinga/pluginutility.hpp"
#include "icinga/macroprocessor.hpp"
#include "base/logger.hpp"
#include "base/utility.hpp"
#include "base/perfdatavalue.hpp"
#include "base/convert.hpp"
#include "base/process.hpp"
#include "base/objectlock.hpp"
#include "base/exception.hpp"
#include <boost/algorithm/string/trim.hpp>
using namespace icinga;
void PluginUtility::ExecuteCommand(const Command::Ptr& commandObj, const Checkable::Ptr& checkable,
const CheckResult::Ptr& cr, const MacroProcessor::ResolverList& macroResolvers,
const Dictionary::Ptr& resolvedMacros, bool useResolvedMacros, int timeout,
const std::function<void(const Value& commandLine, const ProcessResult&)>& callback)
{
Value raw_command = commandObj->GetCommandLine();
Dictionary::Ptr raw_arguments = commandObj->GetArguments();
Value command;
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);
}
return;
}
Dictionary::Ptr envMacros = new Dictionary();
Dictionary::Ptr env = commandObj->GetEnv();
if (env) {
ObjectLock olock(env);
for (const Dictionary::Pair& kv : env) {
String name = kv.second;
String missingMacro;
Value value = MacroProcessor::ResolveMacros(name, macroResolvers, cr,
&missingMacro, MacroProcessor::EscapeCallback(), resolvedMacros,
useResolvedMacros);
#ifdef I2_DEBUG
if (!missingMacro.IsEmpty())
Log(LogDebug, "PluginUtility")
<< "Macro '" << name << "' is not defined.";
#endif /* I2_DEBUG */
if (value.IsObjectType<Array>())
value = Utility::Join(value, ';');
envMacros->Set(kv.first, value);
}
}
if (resolvedMacros && !useResolvedMacros)
return;
Process::Ptr process = new Process(Process::PrepareCommand(command), envMacros);
process->SetTimeout(timeout);
process->SetAdjustPriority(true);
process->Run([callback, command](const ProcessResult& pr) { callback(command, pr); });
}
ServiceState PluginUtility::ExitStatusToState(int exitStatus)
{
switch (exitStatus) {
case 0:
return ServiceOK;
case 1:
return ServiceWarning;
case 2:
return ServiceCritical;
default:
return ServiceUnknown;
}
}
std::pair<String, String> PluginUtility::ParseCheckOutput(const String& output)
{
String text;
String perfdata;
std::vector<String> lines = output.Split("\r\n");
for (const String& line : lines) {
size_t delim = line.FindFirstOf("|");
if (!text.IsEmpty())
text += "\n";
if (delim != String::NPos && line.FindFirstOf("=", delim) != String::NPos) {
text += line.SubStr(0, delim);
if (!perfdata.IsEmpty())
perfdata += " ";
perfdata += line.SubStr(delim + 1, line.GetLength());
} else {
text += line;
}
}
boost::algorithm::trim(perfdata);
return std::make_pair(text, perfdata);
}
Array::Ptr PluginUtility::SplitPerfdata(const String& perfdata)
{
ArrayData result;
size_t begin = 0;
String multi_prefix;
for (;;) {
size_t eqp = perfdata.FindFirstOf('=', begin);
if (eqp == String::NPos)
break;
String label = perfdata.SubStr(begin, eqp - begin);
if (label.GetLength() > 2 && label[0] == '\'' && label[label.GetLength() - 1] == '\'')
label = label.SubStr(1, label.GetLength() - 2);
size_t multi_index = label.RFind("::");
if (multi_index != String::NPos)
multi_prefix = "";
size_t spq = perfdata.FindFirstOf(' ', eqp);
if (spq == String::NPos)
spq = perfdata.GetLength();
String value = perfdata.SubStr(eqp + 1, spq - eqp - 1);
if (!multi_prefix.IsEmpty())
label = multi_prefix + "::" + label;
String pdv;
if (label.FindFirstOf(" ") != String::NPos)
pdv = "'" + label + "'=" + value;
else
pdv = label + "=" + value;
result.emplace_back(std::move(pdv));
if (multi_index != String::NPos)
multi_prefix = label.SubStr(0, multi_index);
begin = spq + 1;
}
return new Array(std::move(result));
}
String PluginUtility::FormatPerfdata(const Array::Ptr& perfdata, bool normalize)
{
if (!perfdata)
return "";
std::ostringstream result;
ObjectLock olock(perfdata);
bool first = true;
for (const Value& pdv : perfdata) {
if (!first)
result << " ";
else
first = false;
if (pdv.IsObjectType<PerfdataValue>()) {
result << static_cast<PerfdataValue::Ptr>(pdv)->Format();
} 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 {
result << pdv;
}
}
return result.str();
}
|