1 /* Icinga 2 | (c) 2012 Icinga GmbH | GPLv2+ */
3 #include "plugins/thresholds.hpp"
4 #include <boost/algorithm/string.hpp>
5 #include <boost/lexical_cast.hpp>
8 using namespace boost::algorithm;
10 threshold::threshold()
14 threshold::threshold(const double v, const double c, bool l , bool p ) {
21 threshold::threshold(const std::wstring& stri)
24 throw std::invalid_argument("Threshold must not be empty");
26 std::wstring str = stri;
29 boost::algorithm::trim(str);
31 bool low = (str.at(0) == L'!');
33 str = std::wstring(str.begin() + 1, str.end());
37 if (str.at(0) == L'[' && str.at(str.length() - 1) == L']') {//is range
38 str = std::wstring(str.begin() + 1, str.end() - 1);
39 std::vector<std::wstring> svec;
40 boost::split(svec, str, boost::is_any_of(L"-"));
42 throw std::invalid_argument("Threshold range requires two arguments");
43 std::wstring str1 = svec.at(0), str2 = svec.at(1);
45 if (str1.at(str1.length() - 1) == L'%' && str2.at(str2.length() - 1) == L'%') {
47 str1 = std::wstring(str1.begin(), str1.end() - 1);
48 str2 = std::wstring(str2.begin(), str2.end() - 1);
52 boost::algorithm::trim(str1);
53 lower = boost::lexical_cast<DOUBLE>(str1);
54 boost::algorithm::trim(str2);
55 upper = boost::lexical_cast<DOUBLE>(str2);
56 legal = !low; perc = pc; set = true;
57 } catch (const boost::bad_lexical_cast&) {
58 throw std::invalid_argument("Unknown Threshold type");
61 if (str.at(str.length() - 1) == L'%') {
63 str = std::wstring(str.begin(), str.end() - 1);
66 boost::algorithm::trim(str);
67 lower = upper = boost::lexical_cast<DOUBLE>(str);
68 legal = !low; perc = pc; set = true;
69 } catch (const boost::bad_lexical_cast&) {
70 throw std::invalid_argument("Unknown Threshold type");
75 //return TRUE if the threshold is broken
76 bool threshold::rend(const double val, const double max)
78 double upperAbs = upper;
79 double lowerAbs = lower;
82 upperAbs = upper / 100.0 * max;
83 lowerAbs = lower / 100.0 * max;
88 if (lowerAbs == upperAbs)
89 return val > upperAbs == legal;
91 return (val < lowerAbs || upperAbs < val) != legal;
94 //returns a printable string of the threshold
95 std::wstring threshold::pString(const double max)
99 //transform percentages to abolute values
100 double lowerAbs = lower;
101 double upperAbs = upper;
103 lowerAbs = lower / 100.0 * max;
104 upperAbs = upper / 100.0 * max;
107 std::wstring s, lowerStr = removeZero(lowerAbs),
108 upperStr = removeZero(upperAbs);
110 if (lower != upper) {
111 s.append(L"[").append(lowerStr).append(L"-")
112 .append(upperStr).append(L"]");
119 threshold threshold::toSeconds(const Tunit& fromUnit) {
123 double lowerAbs = lower;
124 double upperAbs = upper;
128 lowerAbs = lowerAbs / 1000;
129 upperAbs = upperAbs / 1000;
132 lowerAbs = lowerAbs ;
133 upperAbs = upperAbs ;
136 lowerAbs = lowerAbs * 60;
137 upperAbs = upperAbs * 60;
140 lowerAbs = lowerAbs * 60 * 60;
141 upperAbs = upperAbs * 60 * 60;
145 return threshold(lowerAbs, upperAbs, legal, perc);
148 std::wstring removeZero(double val)
150 std::wstring ret = boost::lexical_cast<std::wstring>(val);
151 std::wstring::size_type pos = ret.length();
152 if (ret.find_first_of(L".") == std::string::npos)
154 for (std::wstring::reverse_iterator rit = ret.rbegin(); rit != ret.rend(); ++rit) {
156 return ret.substr(0, pos - 1);
159 return ret.substr(0, pos);
166 std::vector<std::wstring> splitMultiOptions(const std::wstring& str)
168 std::vector<std::wstring> sVec;
169 boost::split(sVec, str, boost::is_any_of(L","));
173 Bunit parseBUnit(const std::wstring& str)
175 std::wstring wstr = to_upper_copy(str);
188 throw std::invalid_argument("Unknown unit type");
191 std::wstring BunitStr(const Bunit& unit)
208 Tunit parseTUnit(const std::wstring& str) {
209 std::wstring wstr = to_lower_copy(str);
220 throw std::invalid_argument("Unknown unit type");
223 std::wstring TunitStr(const Tunit& unit)
238 void printErrorInfo(unsigned long err)
241 err = GetLastError();
243 if (!FormatMessage(FORMAT_MESSAGE_ALLOCATE_BUFFER | FORMAT_MESSAGE_FROM_SYSTEM | FORMAT_MESSAGE_IGNORE_INSERTS,
244 NULL, err, MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT), (LPWSTR)&mBuf, 0, NULL))
245 std::wcout << "Failed to format error message, last error was: " << err << '\n';
247 boost::trim_right(std::wstring(mBuf));
248 std::wcout << mBuf << std::endl;
252 std::wstring formatErrorInfo(unsigned long err) {
253 std::wostringstream out;
255 err = GetLastError();
257 if (!FormatMessage(FORMAT_MESSAGE_ALLOCATE_BUFFER | FORMAT_MESSAGE_FROM_SYSTEM | FORMAT_MESSAGE_IGNORE_INSERTS,
258 NULL, err, MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT), (LPWSTR)&mBuf, 0, NULL))
259 out << "Failed to format error message, last error was: " << err;
261 std::wstring tempOut = std::wstring(mBuf);
262 boost::trim_right(tempOut);
269 std::wstring stateToString(const state& state) {
271 case OK: return L"OK";
272 case WARNING: return L"WARNING";
273 case CRITICAL: return L"CRITICAL";
274 default: return L"UNKNOWN";