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 "plugins/thresholds.hpp"
21 #include <boost/algorithm/string.hpp>
22 #include <boost/lexical_cast.hpp>
25 using namespace boost::algorithm;
27 threshold::threshold()
31 threshold::threshold(const double v, const double c, bool l , bool p ) {
38 threshold::threshold(const std::wstring& stri)
41 throw std::invalid_argument("Threshold must not be empty");
43 std::wstring str = stri;
46 boost::algorithm::trim(str);
48 bool low = (str.at(0) == L'!');
50 str = std::wstring(str.begin() + 1, str.end());
54 if (str.at(0) == L'[' && str.at(str.length() - 1) == L']') {//is range
55 str = std::wstring(str.begin() + 1, str.end() - 1);
56 std::vector<std::wstring> svec;
57 boost::split(svec, str, boost::is_any_of(L"-"));
59 throw std::invalid_argument("Threshold range requires two arguments");
60 std::wstring str1 = svec.at(0), str2 = svec.at(1);
62 if (str1.at(str1.length() - 1) == L'%' && str2.at(str2.length() - 1) == L'%') {
64 str1 = std::wstring(str1.begin(), str1.end() - 1);
65 str2 = std::wstring(str2.begin(), str2.end() - 1);
69 boost::algorithm::trim(str1);
70 lower = boost::lexical_cast<DOUBLE>(str1);
71 boost::algorithm::trim(str2);
72 upper = boost::lexical_cast<DOUBLE>(str2);
73 legal = !low; perc = pc; set = true;
74 } catch (const boost::bad_lexical_cast&) {
75 throw std::invalid_argument("Unknown Threshold type");
78 if (str.at(str.length() - 1) == L'%') {
80 str = std::wstring(str.begin(), str.end() - 1);
83 boost::algorithm::trim(str);
84 lower = upper = boost::lexical_cast<DOUBLE>(str);
85 legal = !low; perc = pc; set = true;
86 } catch (const boost::bad_lexical_cast&) {
87 throw std::invalid_argument("Unknown Threshold type");
92 //return TRUE if the threshold is broken
93 bool threshold::rend(const double val, const double max)
95 double upperAbs = upper;
96 double lowerAbs = lower;
99 upperAbs = upper / 100.0 * max;
100 lowerAbs = lower / 100.0 * max;
105 if (lowerAbs == upperAbs)
106 return val > upperAbs == legal;
108 return (val < lowerAbs || upperAbs < val) != legal;
111 //returns a printable string of the threshold
112 std::wstring threshold::pString(const double max)
116 //transform percentages to abolute values
117 double lowerAbs = lower;
118 double upperAbs = upper;
120 lowerAbs = lower / 100.0 * max;
121 upperAbs = upper / 100.0 * max;
124 std::wstring s, lowerStr = removeZero(lowerAbs),
125 upperStr = removeZero(upperAbs);
127 if (lower != upper) {
128 s.append(L"[").append(lowerStr).append(L"-")
129 .append(upperStr).append(L"]");
136 threshold threshold::toSeconds(const Tunit& fromUnit) {
140 double lowerAbs = lower;
141 double upperAbs = upper;
145 lowerAbs = lowerAbs / 1000;
146 upperAbs = upperAbs / 1000;
149 lowerAbs = lowerAbs ;
150 upperAbs = upperAbs ;
153 lowerAbs = lowerAbs * 60;
154 upperAbs = upperAbs * 60;
157 lowerAbs = lowerAbs * 60 * 60;
158 upperAbs = upperAbs * 60 * 60;
162 return threshold(lowerAbs, upperAbs, legal, perc);
165 std::wstring removeZero(double val)
167 std::wstring ret = boost::lexical_cast<std::wstring>(val);
168 std::wstring::size_type pos = ret.length();
169 if (ret.find_first_of(L".") == std::string::npos)
171 for (std::wstring::reverse_iterator rit = ret.rbegin(); rit != ret.rend(); ++rit) {
173 return ret.substr(0, pos - 1);
176 return ret.substr(0, pos);
183 std::vector<std::wstring> splitMultiOptions(const std::wstring& str)
185 std::vector<std::wstring> sVec;
186 boost::split(sVec, str, boost::is_any_of(L","));
190 Bunit parseBUnit(const std::wstring& str)
192 std::wstring wstr = to_upper_copy(str);
205 throw std::invalid_argument("Unknown unit type");
208 std::wstring BunitStr(const Bunit& unit)
225 Tunit parseTUnit(const std::wstring& str) {
226 std::wstring wstr = to_lower_copy(str);
237 throw std::invalid_argument("Unknown unit type");
240 std::wstring TunitStr(const Tunit& unit)
255 void printErrorInfo(unsigned long err)
258 err = GetLastError();
260 if (!FormatMessage(FORMAT_MESSAGE_ALLOCATE_BUFFER | FORMAT_MESSAGE_FROM_SYSTEM | FORMAT_MESSAGE_IGNORE_INSERTS,
261 NULL, err, MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT), (LPWSTR)&mBuf, 0, NULL))
262 std::wcout << "Failed to format error message, last error was: " << err << '\n';
264 boost::trim_right(std::wstring(mBuf));
265 std::wcout << mBuf << std::endl;
269 std::wstring formatErrorInfo(unsigned long err) {
270 std::wostringstream out;
272 err = GetLastError();
274 if (!FormatMessage(FORMAT_MESSAGE_ALLOCATE_BUFFER | FORMAT_MESSAGE_FROM_SYSTEM | FORMAT_MESSAGE_IGNORE_INSERTS,
275 NULL, err, MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT), (LPWSTR)&mBuf, 0, NULL))
276 out << "Failed to format error message, last error was: " << err;
278 std::wstring tempOut = std::wstring(mBuf);
279 boost::trim_right(tempOut);
286 std::wstring stateToString(const state& state) {
288 case OK: return L"OK";
289 case WARNING: return L"WARNING";
290 case CRITICAL: return L"CRITICAL";
291 default: return L"UNKNOWN";