Merge pull request #8492 from Icinga/bugfix/perfdata-scientific-notation

Fix perfdata parser not recognize scientific notation
This commit is contained in:
Julian Brost 2021-06-07 15:29:52 +02:00 committed by GitHub
commit 62cbe72cdb
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 52 additions and 2 deletions

View File

@ -244,7 +244,7 @@ PerfdataValue::Ptr PerfdataValue::Parse(const String& perfdata)
String valueStr = perfdata.SubStr(eqp + 1, spq - eqp - 1);
size_t pos = valueStr.FindFirstNotOf("+-0123456789.e");
size_t pos = valueStr.FindFirstNotOf("+-0123456789.eE");
if (pos != String::NPos && valueStr[pos] == ',') {
BOOST_THROW_EXCEPTION(std::invalid_argument("Invalid performance data value: " + perfdata));
@ -379,7 +379,7 @@ String PerfdataValue::Format() const
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)
if (tokens.size() > index && tokens[index] != "U" && tokens[index] != "" && tokens[index].FindFirstNotOf("+-0123456789.eE") == String::NPos)
return Convert::ToDouble(tokens[index]);
else {
if (tokens.size() > index && tokens[index] != "")

View File

@ -146,6 +146,7 @@ add_boost_test(base
icinga_perfdata/ignore_invalid_warn_crit_min_max
icinga_perfdata/invalid
icinga_perfdata/multi
icinga_perfdata/scientificnotation
remote_url/id_and_path
remote_url/parameters
remote_url/get_and_set

View File

@ -305,4 +305,53 @@ BOOST_AUTO_TEST_CASE(multi)
BOOST_CHECK(pd->Get(1) == "test::b=4");
}
BOOST_AUTO_TEST_CASE(scientificnotation)
{
PerfdataValue::Ptr pdv = PerfdataValue::Parse("test=1.1e+1");
BOOST_CHECK(pdv->GetLabel() == "test");
BOOST_CHECK(pdv->GetValue() == 11);
String str = pdv->Format();
BOOST_CHECK(str == "test=11");
pdv = PerfdataValue::Parse("test=1.1e1");
BOOST_CHECK(pdv->GetLabel() == "test");
BOOST_CHECK(pdv->GetValue() == 11);
str = pdv->Format();
BOOST_CHECK(str == "test=11");
pdv = PerfdataValue::Parse("test=1.1e-1");
BOOST_CHECK(pdv->GetLabel() == "test");
BOOST_CHECK(pdv->GetValue() == 0.11);
str = pdv->Format();
BOOST_CHECK(str == "test=0.110000");
pdv = PerfdataValue::Parse("test=1.1E1");
BOOST_CHECK(pdv->GetLabel() == "test");
BOOST_CHECK(pdv->GetValue() == 11);
str = pdv->Format();
BOOST_CHECK(str == "test=11");
pdv = PerfdataValue::Parse("test=1.1E-1");
BOOST_CHECK(pdv->GetLabel() == "test");
BOOST_CHECK(pdv->GetValue() == 0.11);
str = pdv->Format();
BOOST_CHECK(str == "test=0.110000");
pdv = PerfdataValue::Parse("test=1.1E-1;1.2e+1;1.3E-1;1.4e-2;1.5E2");
BOOST_CHECK(pdv->GetLabel() == "test");
BOOST_CHECK(pdv->GetValue() == 0.11);
BOOST_CHECK(pdv->GetWarn() == 12);
BOOST_CHECK(pdv->GetCrit() == 0.13);
BOOST_CHECK(pdv->GetMin() == 0.014);
BOOST_CHECK(pdv->GetMax() == 150);
str = pdv->Format();
BOOST_CHECK(str == "test=0.110000;12;0.130000;0.014000;150");
}
BOOST_AUTO_TEST_SUITE_END()