/******************************************************************************
* Icinga 2 *
- * Copyright (C) 2012-2014 Icinga Development Team (http://www.icinga.org) *
+ * Copyright (C) 2012-2017 Icinga Development Team (https://www.icinga.com/) *
* *
* This program is free software; you can redistribute it and/or *
* modify it under the terms of the GNU General Public License *
#include "thresholds.h"
#include <boost/algorithm/string.hpp>
#include <boost/lexical_cast.hpp>
-#include <vector>
#include <iostream>
using namespace boost::algorithm;
-using std::wstring;
-
-threshold::threshold(bool l)
- : set(false), legal(l)
+threshold::threshold()
+ : set(false)
{}
-//return TRUE if the threshold is broken
-bool threshold::rend(const double b)
-{
- if (!set)
- return set;
- if (lower == upper)
- return b > upper == legal;
- else
- return (b < lower || upper < b) != legal;
-}
-
-//returns a printable string of the threshold
-std::wstring threshold::pString()
-{
- if (!set)
- return L"0";
-
- std::wstring s;
- if (!legal)
- s.append(L"!");
-
- if (lower != upper) {
- if (perc)
- s.append(L"[").append(std::to_wstring(lower)).append(L"%").append(L"-")
- .append(std::to_wstring(upper)).append(L"%").append(L"]");
- else
- s.append(L"[").append(std::to_wstring(lower)).append(L"-")
- .append(std::to_wstring(upper)).append(L"]");
- } else {
- if (perc)
- s = std::to_wstring(lower).append(L"%");
- else
- s = std::to_wstring(lower);
- }
- return s;
-}
-
-threshold::threshold(const wstring& stri)
+threshold::threshold(CONST std::wstring& stri)
{
if (stri.empty())
throw std::invalid_argument("Threshold must not be empty");
- wstring str = stri;
+ std::wstring str = stri;
//kill whitespace
boost::algorithm::trim(str);
bool low = (str.at(0) == L'!');
if (low)
- str = wstring(str.begin() + 1, str.end());
+ 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 = wstring(str.begin() + 1, str.end() - 1);
- std::vector<wstring> svec;
+ 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");
- wstring str1 = svec.at(0), str2 = svec.at(1);
+ std::wstring str1 = svec.at(0), str2 = svec.at(1);
if (str1.at(str1.length() - 1) == L'%' && str2.at(str2.length() - 1) == L'%') {
- perc = true;
- str1 = wstring(str1.begin(), str1.end() - 1);
- str2 = wstring(str2.begin(), str2.end() - 1);
+ 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);
+ lower = boost::lexical_cast<DOUBLE>(str1);
boost::algorithm::trim(str2);
- upper = boost::lexical_cast<double>(str2);
+ upper = boost::lexical_cast<DOUBLE>(str2);
legal = !low; perc = pc; set = true;
- } catch (const boost::bad_lexical_cast&) {
+ } catch (CONST boost::bad_lexical_cast&) {
throw std::invalid_argument("Unknown Threshold type");
}
} else { //not range
if (str.at(str.length() - 1) == L'%') {
- perc = true;
- str = wstring(str.begin(), str.end() - 1);
+ pc = true;
+ str = std::wstring(str.begin(), str.end() - 1);
}
try {
boost::algorithm::trim(str);
- lower = upper = boost::lexical_cast<double>(str);
+ lower = upper = boost::lexical_cast<DOUBLE>(str);
legal = !low; perc = pc; set = true;
- } catch (const boost::bad_lexical_cast&) {
+ } catch (CONST boost::bad_lexical_cast&) {
throw std::invalid_argument("Unknown Threshold type");
}
}
}
-Bunit parseBUnit(const wstring& str)
+//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;
+}
+
+std::wstring removeZero(DOUBLE val)
+{
+ std::wstring ret = boost::lexical_cast<std::wstring>(val);
+ INT 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(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)
{
- wstring wstr = to_upper_copy(str);
+ std::wstring wstr = to_upper_copy(str);
if (wstr == L"B")
return BunitB;
- if (wstr == L"kB")
+ if (wstr == L"KB")
return BunitkB;
if (wstr == L"MB")
return BunitMB;
throw std::invalid_argument("Unknown unit type");
}
-wstring BunitStr(const Bunit& unit)
+std::wstring BunitStr(CONST Bunit& unit)
{
switch (unit) {
case BunitB:
return NULL;
}
-Tunit parseTUnit(const wstring& str) {
- wstring wstr = to_lower_copy(str);
+Tunit parseTUnit(CONST std::wstring& str) {
+ std::wstring wstr = to_lower_copy(str);
if (wstr == L"ms")
return TunitMS;
throw std::invalid_argument("Unknown unit type");
}
-wstring TunitStr(const Tunit& unit)
+std::wstring TunitStr(CONST Tunit& unit)
{
switch (unit) {
case TunitMS:
return NULL;
}
-void die(DWORD err)
+VOID die(DWORD err)
{
if (!err)
err = GetLastError();
LPWSTR mBuf = NULL;
- size_t mS = 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 << mBuf << std::endl;
-}
\ No newline at end of file
+ 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
+ std::wcout << mBuf << std::endl;
+}