2013-11-08 16:06:59 +01:00
|
|
|
/******************************************************************************
|
|
|
|
* Icinga 2 *
|
2014-03-19 01:02:29 +01:00
|
|
|
* Copyright (C) 2012-2014 Icinga Development Team (http://www.icinga.org) *
|
2013-11-08 16:06:59 +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-05-25 16:23:35 +02:00
|
|
|
#include "icinga/perfdatavalue.hpp"
|
|
|
|
#include "base/convert.hpp"
|
2014-08-05 00:48:32 +02:00
|
|
|
#include "base/exception.hpp"
|
2013-11-08 08:39:05 +01:00
|
|
|
#include <boost/algorithm/string/case_conv.hpp>
|
2013-11-07 13:37:58 +01:00
|
|
|
#include <boost/algorithm/string/split.hpp>
|
|
|
|
#include <boost/algorithm/string/classification.hpp>
|
|
|
|
|
|
|
|
using namespace icinga;
|
|
|
|
|
2013-11-08 16:07:21 +01:00
|
|
|
REGISTER_TYPE(PerfdataValue);
|
2013-11-08 11:17:46 +01:00
|
|
|
|
|
|
|
PerfdataValue::PerfdataValue(void)
|
|
|
|
{ }
|
|
|
|
|
2013-11-07 13:37:58 +01:00
|
|
|
PerfdataValue::PerfdataValue(double value, bool counter, const String& unit,
|
|
|
|
const Value& warn, const Value& crit, const Value& min, const Value& max)
|
|
|
|
{
|
|
|
|
SetValue(value);
|
|
|
|
SetCounter(counter);
|
|
|
|
SetUnit(unit);
|
|
|
|
SetWarn(warn);
|
|
|
|
SetCrit(crit);
|
|
|
|
SetMin(min);
|
|
|
|
SetMax(max);
|
|
|
|
}
|
|
|
|
|
|
|
|
Value PerfdataValue::Parse(const String& perfdata)
|
|
|
|
{
|
|
|
|
size_t pos = perfdata.FindFirstNotOf("+-0123456789.e");
|
|
|
|
|
|
|
|
double value = Convert::ToDouble(perfdata.SubStr(0, pos));
|
|
|
|
|
|
|
|
if (pos == String::NPos)
|
|
|
|
return value;
|
|
|
|
|
|
|
|
std::vector<String> tokens;
|
|
|
|
boost::algorithm::split(tokens, perfdata, boost::is_any_of(";"));
|
|
|
|
|
|
|
|
bool counter = false;
|
|
|
|
String unit;
|
|
|
|
Value warn, crit, min, max;
|
|
|
|
|
|
|
|
unit = perfdata.SubStr(pos, tokens[0].GetLength() - pos);
|
|
|
|
|
2013-11-08 08:39:05 +01:00
|
|
|
boost::algorithm::to_lower(unit);
|
|
|
|
|
2013-12-16 16:36:54 +01:00
|
|
|
double base = 1.0;
|
|
|
|
|
2013-11-08 08:39:05 +01:00
|
|
|
if (unit == "us") {
|
2013-12-17 14:47:19 +01:00
|
|
|
base /= 1000.0 * 1000.0;
|
2013-11-08 08:39:05 +01:00
|
|
|
unit = "seconds";
|
|
|
|
} else if (unit == "ms") {
|
2013-12-16 16:36:54 +01:00
|
|
|
base /= 1000.0;
|
2013-11-07 13:37:58 +01:00
|
|
|
unit = "seconds";
|
2013-11-08 08:39:05 +01:00
|
|
|
} else if (unit == "s") {
|
|
|
|
unit = "seconds";
|
|
|
|
} else if (unit == "tb") {
|
2013-12-16 16:36:54 +01:00
|
|
|
base *= 1024.0 * 1024.0 * 1024.0 * 1024.0;
|
2013-11-08 08:39:05 +01:00
|
|
|
unit = "bytes";
|
|
|
|
} else if (unit == "gb") {
|
2013-12-16 16:36:54 +01:00
|
|
|
base *= 1024.0 * 1024.0 * 1024.0;
|
2013-11-07 13:37:58 +01:00
|
|
|
unit = "bytes";
|
2013-11-08 08:39:05 +01:00
|
|
|
} else if (unit == "mb") {
|
2013-12-16 16:36:54 +01:00
|
|
|
base *= 1024.0 * 1024.0;
|
2013-11-08 08:39:05 +01:00
|
|
|
unit = "bytes";
|
|
|
|
} else if (unit == "kb") {
|
2013-12-16 16:36:54 +01:00
|
|
|
base *= 1024.0;
|
2013-11-08 08:39:05 +01:00
|
|
|
unit = "bytes";
|
|
|
|
} else if (unit == "b") {
|
|
|
|
unit = "bytes";
|
|
|
|
} else if (unit == "%") {
|
2013-11-07 13:37:58 +01:00
|
|
|
unit = "percent";
|
2013-11-08 08:39:05 +01:00
|
|
|
} else if (unit == "c") {
|
2013-11-07 13:37:58 +01:00
|
|
|
counter = true;
|
|
|
|
unit = "";
|
2013-11-08 08:39:05 +01:00
|
|
|
} else if (unit != "") {
|
2013-11-07 13:37:58 +01:00
|
|
|
BOOST_THROW_EXCEPTION(std::invalid_argument("Invalid performance data unit: " + unit));
|
2013-11-08 08:39:05 +01:00
|
|
|
}
|
2013-11-07 13:37:58 +01:00
|
|
|
|
2013-11-17 00:05:31 +01:00
|
|
|
if (tokens.size() > 1 && tokens[1] != "U" && tokens[1] != "")
|
2013-11-07 13:37:58 +01:00
|
|
|
warn = Convert::ToDouble(tokens[1]);
|
|
|
|
|
2013-11-17 00:05:31 +01:00
|
|
|
if (tokens.size() > 2 && tokens[2] != "U" && tokens[2] != "")
|
2013-11-07 13:37:58 +01:00
|
|
|
crit = Convert::ToDouble(tokens[2]);
|
|
|
|
|
2013-11-17 00:05:31 +01:00
|
|
|
if (tokens.size() > 3 && tokens[3] != "U" && tokens[3] != "")
|
2013-11-07 13:37:58 +01:00
|
|
|
min = Convert::ToDouble(tokens[3]);
|
|
|
|
|
2013-11-17 00:05:31 +01:00
|
|
|
if (tokens.size() > 4 && tokens[4] != "U" && tokens[4] != "")
|
2013-11-07 13:37:58 +01:00
|
|
|
max = Convert::ToDouble(tokens[4]);
|
|
|
|
|
2013-12-17 14:47:19 +01:00
|
|
|
value = value * base;
|
|
|
|
|
|
|
|
if (!warn.IsEmpty())
|
2013-12-16 16:36:54 +01:00
|
|
|
warn = warn * base;
|
2013-12-17 14:47:19 +01:00
|
|
|
|
|
|
|
if (!crit.IsEmpty())
|
2013-12-16 16:36:54 +01:00
|
|
|
crit = crit * base;
|
2013-12-17 14:47:19 +01:00
|
|
|
|
|
|
|
if (!min.IsEmpty())
|
2013-12-16 16:36:54 +01:00
|
|
|
min = min * base;
|
2013-12-17 14:47:19 +01:00
|
|
|
|
|
|
|
if (!max.IsEmpty())
|
2013-12-16 16:36:54 +01:00
|
|
|
max = max * base;
|
|
|
|
|
2013-11-07 13:37:58 +01:00
|
|
|
return make_shared<PerfdataValue>(value, counter, unit, warn, crit, min, max);
|
|
|
|
}
|
|
|
|
|
|
|
|
String PerfdataValue::Format(const Value& perfdata)
|
|
|
|
{
|
|
|
|
if (perfdata.IsObjectType<PerfdataValue>()) {
|
|
|
|
PerfdataValue::Ptr pdv = perfdata;
|
2013-12-02 12:55:35 +01:00
|
|
|
std::ostringstream result;
|
2013-11-07 13:37:58 +01:00
|
|
|
|
2013-12-16 16:57:30 +01:00
|
|
|
result << Convert::ToString(pdv->GetValue());
|
2013-11-07 13:37:58 +01:00
|
|
|
|
|
|
|
String unit;
|
|
|
|
|
|
|
|
if (pdv->GetCounter())
|
|
|
|
unit = "c";
|
|
|
|
else if (pdv->GetUnit() == "seconds")
|
|
|
|
unit = "s";
|
|
|
|
else if (pdv->GetUnit() == "percent")
|
|
|
|
unit = "%";
|
|
|
|
else if (pdv->GetUnit() == "bytes")
|
2013-11-08 08:39:05 +01:00
|
|
|
unit = "B";
|
2013-11-07 13:37:58 +01:00
|
|
|
|
2013-12-02 12:55:35 +01:00
|
|
|
result << unit;
|
2013-11-07 13:37:58 +01:00
|
|
|
|
|
|
|
if (!pdv->GetWarn().IsEmpty()) {
|
2013-12-02 12:55:35 +01:00
|
|
|
result << ";" << pdv->GetWarn();
|
2013-11-07 13:37:58 +01:00
|
|
|
|
|
|
|
if (!pdv->GetCrit().IsEmpty()) {
|
2013-12-02 12:55:35 +01:00
|
|
|
result << ";" << pdv->GetCrit();
|
2013-11-07 13:37:58 +01:00
|
|
|
|
|
|
|
if (!pdv->GetMin().IsEmpty()) {
|
2013-12-02 12:55:35 +01:00
|
|
|
result << ";" << pdv->GetMin();
|
2013-11-07 13:37:58 +01:00
|
|
|
|
|
|
|
if (!pdv->GetMax().IsEmpty()) {
|
2013-12-02 12:55:35 +01:00
|
|
|
result << ";" << pdv->GetMax();
|
2013-11-07 13:37:58 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-12-02 12:55:35 +01:00
|
|
|
return result.str();
|
2013-11-07 13:37:58 +01:00
|
|
|
} else {
|
|
|
|
return perfdata;
|
|
|
|
}
|
|
|
|
}
|