PluginUtility::FormatPerfdata(): normalize UoMs if desired

This commit is contained in:
Alexander A. Klimov 2021-05-11 16:47:03 +02:00
parent 83cfb84fe0
commit ea5411a6e0
4 changed files with 29 additions and 4 deletions

View File

@ -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<PerfdataValue>())
if (pdv.IsObjectType<PerfdataValue>()) {
result << static_cast<PerfdataValue::Ptr>(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();

View File

@ -31,7 +31,7 @@ public:
static std::pair<String, String> 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();

View File

@ -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

View File

@ -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");