2014-11-06 16:36:42 +01:00
|
|
|
/******************************************************************************
|
|
|
|
* Icinga 2 *
|
|
|
|
* Copyright (C) 2012-2014 Icinga Development Team (http://www.icinga.org) *
|
|
|
|
* *
|
|
|
|
* This program is free software; you can redistribute it and/or *
|
|
|
|
* modify it under the terms of the GNU General Public License *
|
|
|
|
* as published by the Free Software Foundation; either version 2 *
|
|
|
|
* of the License, or (at your option) any later version. *
|
|
|
|
* *
|
|
|
|
* This program is distributed in the hope that it will be useful, *
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of *
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
|
|
|
|
* GNU General Public License for more details. *
|
|
|
|
* *
|
|
|
|
* You should have received a copy of the GNU General Public License *
|
|
|
|
* along with this program; if not, write to the Free Software Foundation *
|
|
|
|
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA. *
|
|
|
|
******************************************************************************/
|
2014-11-06 15:17:08 +01:00
|
|
|
|
|
|
|
#include "thresholds.h"
|
2014-11-19 06:40:58 +01:00
|
|
|
#include <boost/algorithm/string.hpp>
|
|
|
|
#include <boost/lexical_cast.hpp>
|
|
|
|
#include <vector>
|
|
|
|
#include <iostream>
|
2014-11-06 15:17:08 +01:00
|
|
|
|
2014-11-13 15:57:10 +01:00
|
|
|
using namespace boost::algorithm;
|
2014-11-06 15:17:08 +01:00
|
|
|
|
|
|
|
using std::wstring;
|
|
|
|
|
2014-11-14 14:36:10 +01:00
|
|
|
threshold::threshold(bool l)
|
|
|
|
: set(false), legal(l)
|
|
|
|
{}
|
|
|
|
|
|
|
|
//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)
|
2014-11-06 15:17:08 +01:00
|
|
|
{
|
|
|
|
if (stri.empty())
|
2014-11-10 14:39:12 +01:00
|
|
|
throw std::invalid_argument("Threshold must not be empty");
|
2014-11-06 15:17:08 +01:00
|
|
|
|
|
|
|
wstring str = stri;
|
|
|
|
|
2014-11-13 15:57:10 +01:00
|
|
|
//kill whitespace
|
2014-11-13 20:11:17 +01:00
|
|
|
boost::algorithm::trim(str);
|
2014-11-13 15:57:10 +01:00
|
|
|
|
2014-11-06 15:17:08 +01:00
|
|
|
bool low = (str.at(0) == L'!');
|
|
|
|
if (low)
|
|
|
|
str = wstring(str.begin() + 1, str.end());
|
|
|
|
|
2014-11-14 14:36:10 +01:00
|
|
|
bool pc = false;
|
2014-11-06 15:17:08 +01:00
|
|
|
|
|
|
|
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;
|
|
|
|
boost::split(svec, str, boost::is_any_of(L"-"));
|
|
|
|
if (svec.size() != 2)
|
2014-11-10 14:39:12 +01:00
|
|
|
throw std::invalid_argument("Threshold range requires two arguments");
|
2014-11-06 15:17:08 +01:00
|
|
|
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);
|
|
|
|
}
|
|
|
|
|
|
|
|
try {
|
2014-11-19 06:40:58 +01:00
|
|
|
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;
|
2014-11-06 15:17:08 +01:00
|
|
|
} catch (const boost::bad_lexical_cast&) {
|
2014-11-10 14:39:12 +01:00
|
|
|
throw std::invalid_argument("Unknown Threshold type");
|
2014-11-06 15:17:08 +01:00
|
|
|
}
|
|
|
|
} else { //not range
|
|
|
|
if (str.at(str.length() - 1) == L'%') {
|
|
|
|
perc = true;
|
|
|
|
str = wstring(str.begin(), str.end() - 1);
|
|
|
|
}
|
|
|
|
try {
|
2014-11-19 06:40:58 +01:00
|
|
|
boost::algorithm::trim(str);
|
|
|
|
lower = upper = boost::lexical_cast<double>(str);
|
|
|
|
legal = !low; perc = pc; set = true;
|
2014-11-06 15:17:08 +01:00
|
|
|
} catch (const boost::bad_lexical_cast&) {
|
2014-11-10 14:39:12 +01:00
|
|
|
throw std::invalid_argument("Unknown Threshold type");
|
2014-11-06 15:17:08 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-11-13 15:57:10 +01:00
|
|
|
Bunit parseBUnit(const wstring& str)
|
2014-11-06 15:17:08 +01:00
|
|
|
{
|
2014-11-13 15:57:10 +01:00
|
|
|
wstring wstr = to_upper_copy(str);
|
|
|
|
|
|
|
|
if (wstr == L"B")
|
2014-11-06 15:17:08 +01:00
|
|
|
return BunitB;
|
2014-11-13 15:57:10 +01:00
|
|
|
if (wstr == L"kB")
|
2014-11-06 15:17:08 +01:00
|
|
|
return BunitkB;
|
2014-11-13 15:57:10 +01:00
|
|
|
if (wstr == L"MB")
|
2014-11-06 15:17:08 +01:00
|
|
|
return BunitMB;
|
2014-11-13 15:57:10 +01:00
|
|
|
if (wstr == L"GB")
|
2014-11-06 15:17:08 +01:00
|
|
|
return BunitGB;
|
2014-11-13 15:57:10 +01:00
|
|
|
if (wstr == L"TB")
|
2014-11-06 15:17:08 +01:00
|
|
|
return BunitTB;
|
|
|
|
|
|
|
|
throw std::invalid_argument("Unknown unit type");
|
|
|
|
}
|
|
|
|
|
2014-11-13 15:57:10 +01:00
|
|
|
wstring BunitStr(const Bunit& unit)
|
|
|
|
{
|
2014-11-06 15:17:08 +01:00
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
2014-11-13 15:57:10 +01:00
|
|
|
Tunit parseTUnit(const wstring& str) {
|
|
|
|
wstring wstr = to_lower_copy(str);
|
|
|
|
|
|
|
|
if (wstr == L"ms")
|
2014-11-06 15:17:08 +01:00
|
|
|
return TunitMS;
|
2014-11-13 15:57:10 +01:00
|
|
|
if (wstr == L"s")
|
2014-11-06 15:17:08 +01:00
|
|
|
return TunitS;
|
2014-11-13 15:57:10 +01:00
|
|
|
if (wstr == L"m")
|
2014-11-06 15:17:08 +01:00
|
|
|
return TunitM;
|
2014-11-13 15:57:10 +01:00
|
|
|
if (wstr == L"h")
|
2014-11-06 15:17:08 +01:00
|
|
|
return TunitH;
|
|
|
|
|
|
|
|
throw std::invalid_argument("Unknown unit type");
|
|
|
|
}
|
|
|
|
|
2014-11-13 15:57:10 +01:00
|
|
|
wstring TunitStr(const Tunit& unit)
|
|
|
|
{
|
2014-11-06 15:17:08 +01:00
|
|
|
switch (unit) {
|
|
|
|
case TunitMS:
|
|
|
|
return L"ms";
|
|
|
|
case TunitS:
|
|
|
|
return L"s";
|
|
|
|
case TunitM:
|
|
|
|
return L"m";
|
|
|
|
case TunitH:
|
|
|
|
return L"h";
|
|
|
|
}
|
|
|
|
return NULL;
|
2014-11-14 14:36:10 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
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;
|
2014-11-06 15:17:08 +01:00
|
|
|
}
|