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
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
|
/* Icinga 2 | (c) 2012 Icinga GmbH | GPLv2+ */
#include "plugins/thresholds.hpp"
#include <boost/algorithm/string.hpp>
#include <boost/lexical_cast.hpp>
#include <iostream>
using namespace boost::algorithm;
threshold::threshold()
: set(false)
{}
threshold::threshold(const double v, const double c, bool l , bool p ) {
lower = v;
upper = c;
legal = l;
perc = p;
}
threshold::threshold(const std::wstring& stri)
{
if (stri.empty())
throw std::invalid_argument("Threshold must not be empty");
std::wstring str = stri;
//kill whitespace
boost::algorithm::trim(str);
bool low = (str.at(0) == L'!');
if (low)
str = std::wstring(str.begin() + 1, str.end());
bool pc = false;
if (str.at(0) == L'[' && str.at(str.length() - 1) == L']') {//is range
str = std::wstring(str.begin() + 1, str.end() - 1);
std::vector<std::wstring> svec;
boost::split(svec, str, boost::is_any_of(L"-"));
if (svec.size() != 2)
throw std::invalid_argument("Threshold range requires two arguments");
std::wstring str1 = svec.at(0), str2 = svec.at(1);
if (str1.at(str1.length() - 1) == L'%' && str2.at(str2.length() - 1) == L'%') {
pc = true;
str1 = std::wstring(str1.begin(), str1.end() - 1);
str2 = std::wstring(str2.begin(), str2.end() - 1);
}
try {
boost::algorithm::trim(str1);
lower = boost::lexical_cast<DOUBLE>(str1);
boost::algorithm::trim(str2);
upper = boost::lexical_cast<DOUBLE>(str2);
legal = !low; perc = pc; set = true;
} catch (const boost::bad_lexical_cast&) {
throw std::invalid_argument("Unknown Threshold type");
}
} else { //not range
if (str.at(str.length() - 1) == L'%') {
pc = true;
str = std::wstring(str.begin(), str.end() - 1);
}
try {
boost::algorithm::trim(str);
lower = upper = boost::lexical_cast<DOUBLE>(str);
legal = !low; perc = pc; set = true;
} catch (const boost::bad_lexical_cast&) {
throw std::invalid_argument("Unknown Threshold type");
}
}
}
//return TRUE if the threshold is broken
bool threshold::rend(const double val, const double max)
{
double upperAbs = upper;
double lowerAbs = lower;
if (perc) {
upperAbs = upper / 100.0 * max;
lowerAbs = lower / 100.0 * max;
}
if (!set)
return set;
if (lowerAbs == upperAbs)
return val > upperAbs == legal;
else
return (val < lowerAbs || upperAbs < val) != legal;
}
//returns a printable string of the threshold
std::wstring threshold::pString(const double max)
{
if (!set)
return L"";
//transform percentages to abolute values
double lowerAbs = lower;
double upperAbs = upper;
if (perc) {
lowerAbs = lower / 100.0 * max;
upperAbs = upper / 100.0 * max;
}
std::wstring s, lowerStr = removeZero(lowerAbs),
upperStr = removeZero(upperAbs);
if (lower != upper) {
s.append(L"[").append(lowerStr).append(L"-")
.append(upperStr).append(L"]");
} else
s.append(lowerStr);
return s;
}
threshold threshold::toSeconds(const Tunit& fromUnit) {
if (!set)
return *this;
double lowerAbs = lower;
double upperAbs = upper;
switch (fromUnit) {
case TunitMS:
lowerAbs = lowerAbs / 1000;
upperAbs = upperAbs / 1000;
break;
case TunitS:
lowerAbs = lowerAbs ;
upperAbs = upperAbs ;
break;
case TunitM:
lowerAbs = lowerAbs * 60;
upperAbs = upperAbs * 60;
break;
case TunitH:
lowerAbs = lowerAbs * 60 * 60;
upperAbs = upperAbs * 60 * 60;
break;
}
return threshold(lowerAbs, upperAbs, legal, perc);
}
std::wstring removeZero(double val)
{
std::wstring ret = boost::lexical_cast<std::wstring>(val);
std::wstring::size_type pos = ret.length();
if (ret.find_first_of(L".") == std::string::npos)
return ret;
for (std::wstring::reverse_iterator rit = ret.rbegin(); rit != ret.rend(); ++rit) {
if (*rit == L'.') {
return ret.substr(0, pos - 1);
}
if (*rit != L'0') {
return ret.substr(0, pos);
}
pos--;
}
return L"0";
}
std::vector<std::wstring> splitMultiOptions(const std::wstring& str)
{
std::vector<std::wstring> sVec;
boost::split(sVec, str, boost::is_any_of(L","));
return sVec;
}
Bunit parseBUnit(const std::wstring& str)
{
std::wstring wstr = to_upper_copy(str);
if (wstr == L"B")
return BunitB;
if (wstr == L"KB")
return BunitkB;
if (wstr == L"MB")
return BunitMB;
if (wstr == L"GB")
return BunitGB;
if (wstr == L"TB")
return BunitTB;
throw std::invalid_argument("Unknown unit type");
}
std::wstring BunitStr(const Bunit& unit)
{
switch (unit) {
case BunitB:
return L"B";
case BunitkB:
return L"kB";
case BunitMB:
return L"MB";
case BunitGB:
return L"GB";
case BunitTB:
return L"TB";
}
return NULL;
}
Tunit parseTUnit(const std::wstring& str) {
std::wstring wstr = to_lower_copy(str);
if (wstr == L"ms")
return TunitMS;
if (wstr == L"s")
return TunitS;
if (wstr == L"m")
return TunitM;
if (wstr == L"h")
return TunitH;
throw std::invalid_argument("Unknown unit type");
}
std::wstring TunitStr(const Tunit& unit)
{
switch (unit) {
case TunitMS:
return L"ms";
case TunitS:
return L"s";
case TunitM:
return L"m";
case TunitH:
return L"h";
}
return NULL;
}
void printErrorInfo(unsigned long err)
{
if (!err)
err = GetLastError();
LPWSTR mBuf = NULL;
if (!FormatMessage(FORMAT_MESSAGE_ALLOCATE_BUFFER | FORMAT_MESSAGE_FROM_SYSTEM | FORMAT_MESSAGE_IGNORE_INSERTS,
NULL, err, MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT), (LPWSTR)&mBuf, 0, NULL))
std::wcout << "Failed to format error message, last error was: " << err << '\n';
else {
boost::trim_right(std::wstring(mBuf));
std::wcout << mBuf << std::endl;
}
}
std::wstring formatErrorInfo(unsigned long err) {
std::wostringstream out;
if (!err)
err = GetLastError();
LPWSTR mBuf = NULL;
if (!FormatMessage(FORMAT_MESSAGE_ALLOCATE_BUFFER | FORMAT_MESSAGE_FROM_SYSTEM | FORMAT_MESSAGE_IGNORE_INSERTS,
NULL, err, MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT), (LPWSTR)&mBuf, 0, NULL))
out << "Failed to format error message, last error was: " << err;
else {
std::wstring tempOut = std::wstring(mBuf);
boost::trim_right(tempOut);
out << tempOut;
}
return out.str();
}
std::wstring stateToString(const state& state) {
switch (state) {
case OK: return L"OK";
case WARNING: return L"WARNING";
case CRITICAL: return L"CRITICAL";
default: return L"UNKNOWN";
}
}
|