diff --git a/lib/icinga/pluginutility.cpp b/lib/icinga/pluginutility.cpp index 5c7f4b924..2197d1693 100644 --- a/lib/icinga/pluginutility.cpp +++ b/lib/icinga/pluginutility.cpp @@ -176,7 +176,7 @@ Array::Ptr PluginUtility::SplitPerfdata(const String& perfdata) return new Array(std::move(result)); } -String PluginUtility::FormatPerfdata(const Array::Ptr& perfdata) +String PluginUtility::FormatPerfdata(const Array::Ptr& perfdata, bool normalize) { if (!perfdata) return ""; @@ -192,10 +192,25 @@ String PluginUtility::FormatPerfdata(const Array::Ptr& perfdata) else first = false; - if (pdv.IsObjectType()) + if (pdv.IsObjectType()) { result << static_cast(pdv)->Format(); - else + } else if (normalize) { + PerfdataValue::Ptr normalized; + + try { + normalized = PerfdataValue::Parse(pdv); + } catch (const std::invalid_argument& ex) { + Log(LogDebug, "PerfdataValue") << ex.what(); + } + + if (normalized) { + result << normalized->Format(); + } else { + result << pdv; + } + } else { result << pdv; + } } return result.str(); diff --git a/lib/icinga/pluginutility.hpp b/lib/icinga/pluginutility.hpp index 0ea56e2c5..3f6a8444a 100644 --- a/lib/icinga/pluginutility.hpp +++ b/lib/icinga/pluginutility.hpp @@ -31,7 +31,7 @@ public: static std::pair ParseCheckOutput(const String& output); static Array::Ptr SplitPerfdata(const String& perfdata); - static String FormatPerfdata(const Array::Ptr& perfdata); + static String FormatPerfdata(const Array::Ptr& perfdata, bool normalize = false); private: PluginUtility(); diff --git a/test/CMakeLists.txt b/test/CMakeLists.txt index 6a5a4bb27..3d19eb83d 100644 --- a/test/CMakeLists.txt +++ b/test/CMakeLists.txt @@ -143,6 +143,7 @@ add_boost_test(base icinga_perfdata/simple icinga_perfdata/quotes icinga_perfdata/multiple + icinga_perfdata/normalize icinga_perfdata/uom icinga_perfdata/warncritminmax icinga_perfdata/ignore_invalid_warn_crit_min_max diff --git a/test/icinga-perfdata.cpp b/test/icinga-perfdata.cpp index 288161486..f763ac043 100644 --- a/test/icinga-perfdata.cpp +++ b/test/icinga-perfdata.cpp @@ -43,6 +43,15 @@ BOOST_AUTO_TEST_CASE(multiple) BOOST_CHECK(str == "testA=123456 testB=123456"); } +BOOST_AUTO_TEST_CASE(normalize) +{ + Array::Ptr pd = PluginUtility::SplitPerfdata("testA=2m;3;4;1;5 testB=2foobar"); + BOOST_CHECK(pd->GetLength() == 2); + + String str = PluginUtility::FormatPerfdata(pd, true); + BOOST_CHECK(str == "testA=120s;180;240;60;300 testB=2"); +} + BOOST_AUTO_TEST_CASE(uom) { PerfdataValue::Ptr pv = PerfdataValue::Parse("test=123456B");