2013-11-08 16:06:59 +01:00
|
|
|
/******************************************************************************
|
|
|
|
* Icinga 2 *
|
2018-10-18 09:27:04 +02:00
|
|
|
* Copyright (C) 2012-2018 Icinga Development Team (https://icinga.com/) *
|
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. *
|
|
|
|
******************************************************************************/
|
|
|
|
|
2017-05-15 15:51:39 +02:00
|
|
|
#include "base/perfdatavalue.hpp"
|
2018-01-18 13:50:38 +01:00
|
|
|
#include "base/perfdatavalue-ti.cpp"
|
2014-05-25 16:23:35 +02:00
|
|
|
#include "base/convert.hpp"
|
2014-08-05 00:48:32 +02:00
|
|
|
#include "base/exception.hpp"
|
2015-03-01 17:04:48 +01:00
|
|
|
#include "base/logger.hpp"
|
2015-03-11 16:29:20 +01:00
|
|
|
#include "base/function.hpp"
|
2013-11-07 13:37:58 +01:00
|
|
|
|
|
|
|
using namespace icinga;
|
|
|
|
|
2013-11-08 16:07:21 +01:00
|
|
|
REGISTER_TYPE(PerfdataValue);
|
2018-08-07 13:55:41 +02:00
|
|
|
REGISTER_FUNCTION(System, parse_performance_data, PerfdataValue::Parse, "perfdata");
|
2013-11-08 11:17:46 +01:00
|
|
|
|
2018-01-04 08:54:18 +01:00
|
|
|
PerfdataValue::PerfdataValue(const String& label, double value, bool counter,
|
2017-12-19 15:50:05 +01:00
|
|
|
const String& unit, const Value& warn, const Value& crit, const Value& min,
|
|
|
|
const Value& max)
|
2013-11-07 13:37:58 +01:00
|
|
|
{
|
2015-08-04 14:47:44 +02:00
|
|
|
SetLabel(label, true);
|
|
|
|
SetValue(value, true);
|
|
|
|
SetCounter(counter, true);
|
|
|
|
SetUnit(unit, true);
|
|
|
|
SetWarn(warn, true);
|
|
|
|
SetCrit(crit, true);
|
|
|
|
SetMin(min, true);
|
|
|
|
SetMax(max, true);
|
2013-11-07 13:37:58 +01:00
|
|
|
}
|
|
|
|
|
2014-09-17 15:38:39 +02:00
|
|
|
PerfdataValue::Ptr PerfdataValue::Parse(const String& perfdata)
|
2013-11-07 13:37:58 +01:00
|
|
|
{
|
2015-10-13 09:38:31 +02:00
|
|
|
size_t eqp = perfdata.FindLastOf('=');
|
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);
|
2017-05-15 15:51:39 +02:00
|
|
|
|
2014-09-17 15:38:39 +02:00
|
|
|
size_t pos = valueStr.FindFirstNotOf("+-0123456789.e");
|
|
|
|
|
|
|
|
double value = Convert::ToDouble(valueStr.SubStr(0, pos));
|
2013-11-07 13:37:58 +01:00
|
|
|
|
2018-01-04 18:24:45 +01:00
|
|
|
std::vector<String> tokens = valueStr.Split(";");
|
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
|
|
|
|
2018-01-04 18:24:45 +01:00
|
|
|
unit = unit.ToLower();
|
2013-11-08 08:39:05 +01:00
|
|
|
|
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
|
|
|
|
2015-03-01 17:04:48 +01:00
|
|
|
warn = ParseWarnCritMinMaxToken(tokens, 1, "warning");
|
|
|
|
crit = ParseWarnCritMinMaxToken(tokens, 2, "critical");
|
|
|
|
min = ParseWarnCritMinMaxToken(tokens, 3, "minimum");
|
|
|
|
max = ParseWarnCritMinMaxToken(tokens, 4, "maximum");
|
2013-11-07 13:37:58 +01:00
|
|
|
|
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-11-08 21:17:16 +01:00
|
|
|
return new PerfdataValue(label, value, counter, unit, warn, crit, min, max);
|
2013-11-07 13:37:58 +01:00
|
|
|
}
|
|
|
|
|
2018-01-04 04:25:35 +01:00
|
|
|
String PerfdataValue::Format() 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
|
|
|
}
|
2015-03-01 17:04:48 +01:00
|
|
|
|
|
|
|
Value PerfdataValue::ParseWarnCritMinMaxToken(const std::vector<String>& tokens, std::vector<String>::size_type index, const String& description)
|
|
|
|
{
|
|
|
|
if (tokens.size() > index && tokens[index] != "U" && tokens[index] != "" && tokens[index].FindFirstNotOf("+-0123456789.e") == String::NPos)
|
|
|
|
return Convert::ToDouble(tokens[index]);
|
|
|
|
else {
|
|
|
|
if (tokens.size() > index && tokens[index] != "")
|
|
|
|
Log(LogDebug, "PerfdataValue")
|
2017-12-19 15:50:05 +01:00
|
|
|
<< "Ignoring unsupported perfdata " << description << " range, value: '" << tokens[index] << "'.";
|
2015-03-01 17:04:48 +01:00
|
|
|
return Empty;
|
|
|
|
}
|
|
|
|
}
|