Update PerfdataValue::Parse to ignore invalid warn, crit, min, and max values

... instead of generating exception so that metric values can
still be passed to the graphing backend.
Also update icinga-perfdata tests to reflect these changes.

refs #5043

Signed-off-by: Michael Friedrich <michael.friedrich@netways.de>
This commit is contained in:
Jason Young 2015-03-01 11:04:48 -05:00 committed by Michael Friedrich
parent 5df3010826
commit faf6ce1a5f
4 changed files with 36 additions and 12 deletions

View File

@ -20,6 +20,7 @@
#include "icinga/perfdatavalue.hpp"
#include "base/convert.hpp"
#include "base/exception.hpp"
#include "base/logger.hpp"
#include <boost/algorithm/string/case_conv.hpp>
#include <boost/algorithm/string/split.hpp>
#include <boost/algorithm/string/classification.hpp>
@ -113,17 +114,10 @@ PerfdataValue::Ptr PerfdataValue::Parse(const String& perfdata)
BOOST_THROW_EXCEPTION(std::invalid_argument("Invalid performance data unit: " + unit));
}
if (tokens.size() > 1 && tokens[1] != "U" && tokens[1] != "")
warn = Convert::ToDouble(tokens[1]);
if (tokens.size() > 2 && tokens[2] != "U" && tokens[2] != "")
crit = Convert::ToDouble(tokens[2]);
if (tokens.size() > 3 && tokens[3] != "U" && tokens[3] != "")
min = Convert::ToDouble(tokens[3]);
if (tokens.size() > 4 && tokens[4] != "U" && tokens[4] != "")
max = Convert::ToDouble(tokens[4]);
warn = ParseWarnCritMinMaxToken(tokens, 1, "warning");
crit = ParseWarnCritMinMaxToken(tokens, 2, "critical");
min = ParseWarnCritMinMaxToken(tokens, 3, "minimum");
max = ParseWarnCritMinMaxToken(tokens, 4, "maximum");
value = value * base;
@ -184,3 +178,15 @@ String PerfdataValue::Format(void) const
return result.str();
}
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")
<< "Ignoring unsupported perfdata " << description << " range, value: '" << tokens[index] << "'.";
return Empty;
}
}

View File

@ -39,6 +39,10 @@ public:
static PerfdataValue::Ptr Parse(const String& perfdata);
String Format(void) const;
private:
static Value ParseWarnCritMinMaxToken(const std::vector<String>& tokens,
std::vector<String>::size_type index, const String& description);
};
}

View File

@ -103,6 +103,7 @@ add_boost_test(base
icinga_perfdata/multiple
icinga_perfdata/uom
icinga_perfdata/warncritminmax
icinga_perfdata/ignore_invalid_warn_crit_min_max
icinga_perfdata/invalid
icinga_perfdata/multi
)

View File

@ -114,10 +114,23 @@ BOOST_AUTO_TEST_CASE(warncritminmax)
BOOST_CHECK(pv->Format() == "test=123456B;1000;2000;3000;4000");
}
BOOST_AUTO_TEST_CASE(ignore_invalid_warn_crit_min_max)
{
PerfdataValue::Ptr pv = PerfdataValue::Parse("test=123456;1000:2000;0:3000;3000;4000");
BOOST_CHECK(pv);
BOOST_CHECK(pv->GetValue() == 123456);
BOOST_CHECK(pv->GetWarn() == Empty);
BOOST_CHECK(pv->GetCrit() == Empty);
BOOST_CHECK(pv->GetMin() == 3000);
BOOST_CHECK(pv->GetMax() == 4000);
BOOST_CHECK(pv->Format() == "test=123456");
}
BOOST_AUTO_TEST_CASE(invalid)
{
BOOST_CHECK_THROW(PerfdataValue::Parse("123456"), boost::exception);
BOOST_CHECK_THROW(PerfdataValue::Parse("test=1,23456"), boost::exception);
BOOST_CHECK_THROW(PerfdataValue::Parse("test=123456;10%;20%"), boost::exception);
}
BOOST_AUTO_TEST_CASE(multi)