icinga2/plugins/thresholds.cpp

284 lines
7.3 KiB
C++
Raw Normal View History

2014-11-06 16:36:42 +01:00
/******************************************************************************
* Icinga 2 *
2018-10-18 09:27:04 +02:00
* Copyright (C) 2012-2018 Icinga Development Team (https://icinga.com/) *
2014-11-06 16:36:42 +01:00
* *
* 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
2018-02-01 08:45:09 +01:00
#include "plugins/thresholds.hpp"
2014-11-19 06:40:58 +01:00
#include <boost/algorithm/string.hpp>
#include <boost/lexical_cast.hpp>
#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
2014-12-17 14:23:20 +01:00
threshold::threshold()
: set(false)
2014-11-14 14:36:10 +01:00
{}
threshold::threshold(const double v, const double c, bool l , bool p ) {
lower = v;
upper = c;
legal = l;
perc = p;
}
2018-02-01 08:45:09 +01:00
threshold::threshold(const std::wstring& stri)
2014-11-06 15:17:08 +01:00
{
if (stri.empty())
throw std::invalid_argument("Threshold must not be empty");
2014-11-06 15:17:08 +01:00
2015-03-23 13:07:02 +01:00
std::wstring str = stri;
2014-11-06 15:17:08 +01:00
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)
2015-03-23 13:07:02 +01:00
str = std::wstring(str.begin() + 1, str.end());
2014-11-06 15:17:08 +01:00
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
2015-03-23 13:07:02 +01:00
str = std::wstring(str.begin() + 1, str.end() - 1);
std::vector<std::wstring> svec;
2014-11-06 15:17:08 +01:00
boost::split(svec, str, boost::is_any_of(L"-"));
if (svec.size() != 2)
throw std::invalid_argument("Threshold range requires two arguments");
2015-03-23 13:07:02 +01:00
std::wstring str1 = svec.at(0), str2 = svec.at(1);
2014-11-06 15:17:08 +01:00
if (str1.at(str1.length() - 1) == L'%' && str2.at(str2.length() - 1) == L'%') {
2014-12-17 14:23:20 +01:00
pc = true;
2015-03-23 13:07:02 +01:00
str1 = std::wstring(str1.begin(), str1.end() - 1);
str2 = std::wstring(str2.begin(), str2.end() - 1);
2014-11-06 15:17:08 +01:00
}
2014-12-17 14:23:20 +01:00
2014-11-06 15:17:08 +01:00
try {
2014-11-19 06:40:58 +01:00
boost::algorithm::trim(str1);
2015-03-23 13:07:02 +01:00
lower = boost::lexical_cast<DOUBLE>(str1);
2014-11-19 06:40:58 +01:00
boost::algorithm::trim(str2);
2015-03-23 13:07:02 +01:00
upper = boost::lexical_cast<DOUBLE>(str2);
2014-11-19 06:40:58 +01:00
legal = !low; perc = pc; set = true;
2018-02-01 08:45:09 +01:00
} catch (const boost::bad_lexical_cast&) {
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'%') {
2014-12-17 14:23:20 +01:00
pc = true;
2015-03-23 13:07:02 +01:00
str = std::wstring(str.begin(), str.end() - 1);
2014-11-06 15:17:08 +01:00
}
try {
2014-11-19 06:40:58 +01:00
boost::algorithm::trim(str);
2015-03-23 13:07:02 +01:00
lower = upper = boost::lexical_cast<DOUBLE>(str);
2014-11-19 06:40:58 +01:00
legal = !low; perc = pc; set = true;
2018-02-01 08:45:09 +01:00
} catch (const boost::bad_lexical_cast&) {
throw std::invalid_argument("Unknown Threshold type");
2014-11-06 15:17:08 +01:00
}
}
}
2014-12-17 14:23:20 +01:00
//return TRUE if the threshold is broken
2018-02-01 08:45:09 +01:00
bool threshold::rend(const double val, const double max)
2014-12-17 14:23:20 +01:00
{
2018-02-01 08:45:09 +01:00
double upperAbs = upper;
double lowerAbs = lower;
if (perc) {
2015-03-23 13:07:02 +01:00
upperAbs = upper / 100.0 * max;
lowerAbs = lower / 100.0 * max;
}
2014-12-17 14:23:20 +01:00
if (!set)
return set;
if (lowerAbs == upperAbs)
return val > upperAbs == legal;
2014-12-17 14:23:20 +01:00
else
return (val < lowerAbs || upperAbs < val) != legal;
2014-12-17 14:23:20 +01:00
}
//returns a printable string of the threshold
2018-02-01 08:45:09 +01:00
std::wstring threshold::pString(const double max)
2014-12-17 14:23:20 +01:00
{
if (!set)
return L"";
//transform percentages to abolute values
2018-02-01 08:45:09 +01:00
double lowerAbs = lower;
double upperAbs = upper;
if (perc) {
2015-03-23 13:07:02 +01:00
lowerAbs = lower / 100.0 * max;
upperAbs = upper / 100.0 * max;
}
2014-12-17 14:23:20 +01:00
2017-12-21 19:13:48 +01:00
std::wstring s, lowerStr = removeZero(lowerAbs),
upperStr = removeZero(upperAbs);
2014-12-17 14:23:20 +01:00
if (lower != upper) {
s.append(L"[").append(lowerStr).append(L"-")
.append(upperStr).append(L"]");
} else
s.append(lowerStr);
2017-12-13 12:54:14 +01:00
2014-12-17 14:23:20 +01:00
return s;
}
threshold threshold::toSeconds(const Tunit& fromUnit) {
if (!set)
return *this;
double lowerAbs = lower;
double upperAbs = upper;
switch (fromUnit) {
case TunitMS:
lowerAbs = lowerAbs / 1000;
upperAbs = upperAbs / 1000;
break;
case TunitS:
lowerAbs = lowerAbs ;
upperAbs = upperAbs ;
break;
case TunitM:
lowerAbs = lowerAbs * 60;
upperAbs = upperAbs * 60;
break;
case TunitH:
lowerAbs = lowerAbs * 60 * 60;
upperAbs = upperAbs * 60 * 60;
break;
}
return threshold(lowerAbs, upperAbs, legal, perc);
}
2018-02-01 08:45:09 +01:00
std::wstring removeZero(double val)
2014-12-17 14:23:20 +01:00
{
std::wstring ret = boost::lexical_cast<std::wstring>(val);
2017-12-14 08:47:04 +01:00
std::wstring::size_type pos = ret.length();
2014-12-17 14:23:20 +01:00
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--;
}
2014-12-19 09:36:59 +01:00
return L"0";
2014-12-17 14:23:20 +01:00
}
2018-02-01 08:45:09 +01:00
std::vector<std::wstring> splitMultiOptions(const std::wstring& str)
2014-12-17 14:23:20 +01:00
{
2015-03-23 13:07:02 +01:00
std::vector<std::wstring> sVec;
2014-12-17 14:23:20 +01:00
boost::split(sVec, str, boost::is_any_of(L","));
return sVec;
}
2018-02-01 08:45:09 +01:00
Bunit parseBUnit(const std::wstring& str)
2014-11-06 15:17:08 +01:00
{
2015-03-23 13:07:02 +01:00
std::wstring wstr = to_upper_copy(str);
2014-11-13 15:57:10 +01:00
if (wstr == L"B")
2014-11-06 15:17:08 +01:00
return BunitB;
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");
}
2018-02-01 08:45:09 +01:00
std::wstring BunitStr(const Bunit& unit)
2014-11-13 15:57:10 +01:00
{
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;
}
2018-02-01 08:45:09 +01:00
Tunit parseTUnit(const std::wstring& str) {
2015-03-23 13:07:02 +01:00
std::wstring wstr = to_lower_copy(str);
2014-11-13 15:57:10 +01:00
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");
}
2018-02-01 08:45:09 +01:00
std::wstring TunitStr(const Tunit& unit)
2014-11-13 15:57:10 +01:00
{
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
}
2018-02-01 08:45:09 +01:00
void printErrorInfo(unsigned long err)
2014-11-14 14:36:10 +01:00
{
if (!err)
err = GetLastError();
LPWSTR mBuf = NULL;
2015-03-23 13:07:02 +01:00
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 {
boost::trim_right(std::wstring(mBuf));
2015-03-23 13:07:02 +01:00
std::wcout << mBuf << std::endl;
}
2015-09-18 13:04:09 +02:00
}
std::wstring formatErrorInfo(unsigned long err) {
std::wostringstream out;
if (!err)
err = GetLastError();
LPWSTR mBuf = NULL;
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))
out << "Failed to format error message, last error was: " << err;
else {
std::wstring tempOut = std::wstring(mBuf);
boost::trim_right(tempOut);
out << tempOut;
}
return out.str();
}