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)
|
|
|
|
{ }
|
|
|
|
|
2014-09-17 15:38:39 +02:00
|
|
|
PerfdataValue::PerfdataValue(String label, double value, bool counter,
|
|
|
|
const String& unit, const Value& warn, const Value& crit, const Value& min,
|
|
|
|
const Value& max)
|
2013-11-07 13:37:58 +01:00
|
|
|
{
|
2014-09-17 15:38:39 +02:00
|
|
|
SetLabel(label);
|
2013-11-07 13:37:58 +01:00
|
|
|
SetValue(value);
|
|
|
|
SetCounter(counter);
|
|
|
|
SetUnit(unit);
|
|
|
|
SetWarn(warn);
|
|
|
|
SetCrit(crit);
|
|
|
|
SetMin(min);
|
|
|
|
SetMax(max);
|
|
|
|
}
|
|
|
|
|
2014-09-17 15:38:39 +02:00
|
|
|
PerfdataValue::Ptr PerfdataValue::Parse(const String& perfdata)
|
2013-11-07 13:37:58 +01:00
|
|
|
{
|
2014-09-17 15:38:39 +02:00
|
|
|
size_t eqp = perfdata.FindFirstOf('=');
|
2013-11-07 13:37:58 +01:00
|
|
|
|
2014-09-17 15:38:39 +02:00
|
|
|
if (eqp == String::NPos)
|
|
|
|
BOOST_THROW_EXCEPTION(std::invalid_argument("Invalid performance data value: " + perfdata));
|
2013-11-07 13:37:58 +01:00
|
|
|
|
2014-09-17 15:38:39 +02:00
|
|
|
String label = perfdata.SubStr(0, eqp);
|
|
|
|
|
|
|
|
if (label.GetLength() > 2 && label[0] == '\'' && label[label.GetLength() - 1] == '\'')
|
|
|
|
label = label.SubStr(1, label.GetLength() - 2);
|
|
|
|
|
|
|
|
size_t spq = perfdata.FindFirstOf(' ', eqp);
|
|
|
|
|
|
|
|
if (spq == String::NPos)
|
|
|
|
spq = perfdata.GetLength();
|
|
|
|
|
|
|
|
String valueStr = perfdata.SubStr(eqp + 1, spq - eqp - 1);
|
|
|
|
|
|
|
|
size_t pos = valueStr.FindFirstNotOf("+-0123456789.e");
|
|
|
|
|
|
|
|
double value = Convert::ToDouble(valueStr.SubStr(0, pos));
|
2013-11-07 13:37:58 +01:00
|
|
|
|
|
|
|
std::vector<String> tokens;
|
2014-09-17 15:38:39 +02:00
|
|
|
boost::algorithm::split(tokens, valueStr, boost::is_any_of(";"));
|
2013-11-07 13:37:58 +01:00
|
|
|
|
|
|
|
bool counter = false;
|
|
|
|
String unit;
|
|
|
|
Value warn, crit, min, max;
|
|
|
|
|
2014-09-17 15:38:39 +02:00
|
|
|
if (pos != String::NPos)
|
|
|
|
unit = valueStr.SubStr(pos, tokens[0].GetLength() - pos);
|
2013-11-07 13:37:58 +01:00
|
|
|
|
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;
|
|
|
|
|
2014-09-17 15:38:39 +02:00
|
|
|
return make_shared<PerfdataValue>(label, value, counter, unit, warn, crit, min, max);
|
2013-11-07 13:37:58 +01:00
|
|
|
}
|
|
|
|
|
2014-09-17 15:38:39 +02:00
|
|
|
String PerfdataValue::Format(void) const
|
2013-11-07 13:37:58 +01:00
|
|
|
{
|
2014-09-17 15:38:39 +02:00
|
|
|
std::ostringstream result;
|
2013-11-07 13:37:58 +01:00
|
|
|
|
2014-09-17 15:38:39 +02:00
|
|
|
if (GetLabel().FindFirstOf(" ") != String::NPos)
|
|
|
|
result << "'" << GetLabel() << "'";
|
|
|
|
else
|
|
|
|
result << GetLabel();
|
2013-11-07 13:37:58 +01:00
|
|
|
|
2014-09-17 15:38:39 +02:00
|
|
|
result << "=" << Convert::ToString(GetValue());
|
2013-11-07 13:37:58 +01:00
|
|
|
|
2014-09-17 15:38:39 +02:00
|
|
|
String unit;
|
|
|
|
|
|
|
|
if (GetCounter())
|
|
|
|
unit = "c";
|
|
|
|
else if (GetUnit() == "seconds")
|
|
|
|
unit = "s";
|
|
|
|
else if (GetUnit() == "percent")
|
|
|
|
unit = "%";
|
|
|
|
else if (GetUnit() == "bytes")
|
|
|
|
unit = "B";
|
2013-11-07 13:37:58 +01:00
|
|
|
|
2014-09-17 15:38:39 +02:00
|
|
|
result << unit;
|
2013-11-07 13:37:58 +01:00
|
|
|
|
2014-09-17 15:38:39 +02:00
|
|
|
if (!GetWarn().IsEmpty()) {
|
|
|
|
result << ";" << Convert::ToString(GetWarn());
|
2013-11-07 13:37:58 +01:00
|
|
|
|
2014-09-17 15:38:39 +02:00
|
|
|
if (!GetCrit().IsEmpty()) {
|
|
|
|
result << ";" << Convert::ToString(GetCrit());
|
2013-11-07 13:37:58 +01:00
|
|
|
|
2014-09-17 15:38:39 +02:00
|
|
|
if (!GetMin().IsEmpty()) {
|
|
|
|
result << ";" << Convert::ToString(GetMin());
|
2013-11-07 13:37:58 +01:00
|
|
|
|
2014-09-17 15:38:39 +02:00
|
|
|
if (!GetMax().IsEmpty()) {
|
|
|
|
result << ";" << Convert::ToString(GetMax());
|
2013-11-07 13:37:58 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2014-09-17 15:38:39 +02:00
|
|
|
|
|
|
|
return result.str();
|
2013-11-07 13:37:58 +01:00
|
|
|
}
|