1 /******************************************************************************
3 * Copyright (C) 2012-2017 Icinga Development Team (https://www.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 "thresholds.h"
21 #include <boost/algorithm/string.hpp>
22 #include <boost/lexical_cast.hpp>
25 using namespace boost::algorithm;
27 threshold::threshold()
31 threshold::threshold(CONST std::wstring& stri)
34 throw std::invalid_argument("Threshold must not be empty");
36 std::wstring str = stri;
39 boost::algorithm::trim(str);
41 bool low = (str.at(0) == L'!');
43 str = std::wstring(str.begin() + 1, str.end());
47 if (str.at(0) == L'[' && str.at(str.length() - 1) == L']') {//is range
48 str = std::wstring(str.begin() + 1, str.end() - 1);
49 std::vector<std::wstring> svec;
50 boost::split(svec, str, boost::is_any_of(L"-"));
52 throw std::invalid_argument("Threshold range requires two arguments");
53 std::wstring str1 = svec.at(0), str2 = svec.at(1);
55 if (str1.at(str1.length() - 1) == L'%' && str2.at(str2.length() - 1) == L'%') {
57 str1 = std::wstring(str1.begin(), str1.end() - 1);
58 str2 = std::wstring(str2.begin(), str2.end() - 1);
62 boost::algorithm::trim(str1);
63 lower = boost::lexical_cast<DOUBLE>(str1);
64 boost::algorithm::trim(str2);
65 upper = boost::lexical_cast<DOUBLE>(str2);
66 legal = !low; perc = pc; set = true;
67 } catch (CONST boost::bad_lexical_cast&) {
68 throw std::invalid_argument("Unknown Threshold type");
71 if (str.at(str.length() - 1) == L'%') {
73 str = std::wstring(str.begin(), str.end() - 1);
76 boost::algorithm::trim(str);
77 lower = upper = boost::lexical_cast<DOUBLE>(str);
78 legal = !low; perc = pc; set = true;
79 } catch (CONST boost::bad_lexical_cast&) {
80 throw std::invalid_argument("Unknown Threshold type");
85 //return TRUE if the threshold is broken
86 BOOL threshold::rend(CONST DOUBLE val, CONST DOUBLE max)
88 DOUBLE upperAbs = upper;
89 DOUBLE lowerAbs = lower;
92 upperAbs = upper / 100.0 * max;
93 lowerAbs = lower / 100.0 * max;
98 if (lowerAbs == upperAbs)
99 return val > upperAbs == legal;
101 return (val < lowerAbs || upperAbs < val) != legal;
104 //returns a printable string of the threshold
105 std::wstring threshold::pString(CONST DOUBLE max)
109 //transform percentages to abolute values
110 DOUBLE lowerAbs = lower;
111 DOUBLE upperAbs = upper;
113 lowerAbs = lower / 100.0 * max;
114 upperAbs = upper / 100.0 * max;
117 std::wstring s, lowerStr = removeZero(lowerAbs),
118 upperStr = removeZero(upperAbs);
120 if (lower != upper) {
121 s.append(L"[").append(lowerStr).append(L"-")
122 .append(upperStr).append(L"]");
129 std::wstring removeZero(DOUBLE val)
131 std::wstring ret = boost::lexical_cast<std::wstring>(val);
132 INT pos = ret.length();
133 if (ret.find_first_of(L".") == std::string::npos)
135 for (std::wstring::reverse_iterator rit = ret.rbegin(); rit != ret.rend(); ++rit) {
137 return ret.substr(0, pos - 1);
140 return ret.substr(0, pos);
147 std::vector<std::wstring> splitMultiOptions(std::wstring str)
149 std::vector<std::wstring> sVec;
150 boost::split(sVec, str, boost::is_any_of(L","));
154 Bunit parseBUnit(CONST std::wstring& str)
156 std::wstring wstr = to_upper_copy(str);
169 throw std::invalid_argument("Unknown unit type");
172 std::wstring BunitStr(CONST Bunit& unit)
189 Tunit parseTUnit(CONST std::wstring& str) {
190 std::wstring wstr = to_lower_copy(str);
201 throw std::invalid_argument("Unknown unit type");
204 std::wstring TunitStr(CONST Tunit& unit)
222 err = GetLastError();
224 if (!FormatMessage(FORMAT_MESSAGE_ALLOCATE_BUFFER | FORMAT_MESSAGE_FROM_SYSTEM | FORMAT_MESSAGE_IGNORE_INSERTS,
225 NULL, err, MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT), (LPWSTR)&mBuf, 0, NULL))
226 std::wcout << "Failed to format error message, last error was: " << err << '\n';
228 std::wcout << mBuf << std::endl;