From 56116d2218465e8fdda89ae2c014c5588282efa0 Mon Sep 17 00:00:00 2001 From: Maciej Dems Date: Tue, 26 Mar 2024 12:49:24 +0100 Subject: [PATCH] Fix missing values in PerfData normalization --- AUTHORS | 1 + lib/base/perfdatavalue.cpp | 27 +++++++++++++++++---------- test/CMakeLists.txt | 3 ++- test/icinga-perfdata.cpp | 13 +++++++++++-- 4 files changed, 31 insertions(+), 13 deletions(-) diff --git a/AUTHORS b/AUTHORS index 70c8126f0..bbd5aa946 100644 --- a/AUTHORS +++ b/AUTHORS @@ -163,6 +163,7 @@ Luca Lesinigo Lucas Bremgartner Lucas Fairchild-Madar Luiz Amaral +Maciej Dems Magnus Bäck Maik Stuebner Malte Rabenseifner diff --git a/lib/base/perfdatavalue.cpp b/lib/base/perfdatavalue.cpp index 60a39e4e8..aca8c0f3a 100644 --- a/lib/base/perfdatavalue.cpp +++ b/lib/base/perfdatavalue.cpp @@ -363,20 +363,27 @@ String PerfdataValue::Format() const result << unit; + std::string interm(";"); if (!GetWarn().IsEmpty()) { - result << ";" << Convert::ToString(GetWarn()); + result << interm << Convert::ToString(GetWarn()); + interm.clear(); + } - if (!GetCrit().IsEmpty()) { - result << ";" << Convert::ToString(GetCrit()); + interm += ";"; + if (!GetCrit().IsEmpty()) { + result << interm << Convert::ToString(GetCrit()); + interm.clear(); + } - if (!GetMin().IsEmpty()) { - result << ";" << Convert::ToString(GetMin()); + interm += ";"; + if (!GetMin().IsEmpty()) { + result << interm << Convert::ToString(GetMin()); + interm.clear(); + } - if (!GetMax().IsEmpty()) { - result << ";" << Convert::ToString(GetMax()); - } - } - } + interm += ";"; + if (!GetMax().IsEmpty()) { + result << interm << Convert::ToString(GetMax()); } return result.str(); diff --git a/test/CMakeLists.txt b/test/CMakeLists.txt index 8cdea977e..3128a26d6 100644 --- a/test/CMakeLists.txt +++ b/test/CMakeLists.txt @@ -207,11 +207,12 @@ add_boost_test(base icinga_perfdata/normalize icinga_perfdata/uom icinga_perfdata/warncritminmax - icinga_perfdata/ignore_invalid_warn_crit_min_max + icinga_perfdata/ignore_warn_crit_ranges icinga_perfdata/invalid icinga_perfdata/multi icinga_perfdata/scientificnotation icinga_perfdata/parse_edgecases + icinga_perfdata/empty_warn_crit_min_max methods_pluginnotificationtask/truncate_long_output remote_configpackageutility/ValidateName remote_url/id_and_path diff --git a/test/icinga-perfdata.cpp b/test/icinga-perfdata.cpp index 6a9ccd864..1318c08af 100644 --- a/test/icinga-perfdata.cpp +++ b/test/icinga-perfdata.cpp @@ -303,7 +303,7 @@ BOOST_AUTO_TEST_CASE(warncritminmax) BOOST_CHECK_EQUAL(pv->Format(), "test=123456B;1000;2000;3000;4000"); } -BOOST_AUTO_TEST_CASE(ignore_invalid_warn_crit_min_max) +BOOST_AUTO_TEST_CASE(ignore_warn_crit_ranges) { PerfdataValue::Ptr pv = PerfdataValue::Parse("test=123456;1000:2000;0:3000;3000;4000"); BOOST_CHECK(pv); @@ -313,7 +313,16 @@ BOOST_AUTO_TEST_CASE(ignore_invalid_warn_crit_min_max) BOOST_CHECK_EQUAL(pv->GetMin(), 3000); BOOST_CHECK_EQUAL(pv->GetMax(), 4000); - BOOST_CHECK_EQUAL(pv->Format(), "test=123456"); + BOOST_CHECK_EQUAL(pv->Format(), "test=123456;;;3000;4000"); +} + +BOOST_AUTO_TEST_CASE(empty_warn_crit_min_max) +{ + Array::Ptr pd = PluginUtility::SplitPerfdata("testA=5;;7;1;9 testB=5;7;;1;9 testC=5;;;1;9 testD=2m;;;1 testE=5;;7;;"); + BOOST_CHECK_EQUAL(pd->GetLength(), 5); + + String str = PluginUtility::FormatPerfdata(pd, true); + BOOST_CHECK_EQUAL(str, "testA=5;;7;1;9 testB=5;7;;1;9 testC=5;;;1;9 testD=120s;;;60 testE=5;;7"); } BOOST_AUTO_TEST_CASE(invalid)