From ceb0a54bafd77cb3d8997205f3455adf74db3c1a Mon Sep 17 00:00:00 2001 From: Gunnar Beutner Date: Fri, 8 Nov 2013 08:39:05 +0100 Subject: [PATCH] Implement support for performance data unit prefixes. Fixes #5026 --- lib/icinga/perfdatavalue.cpp | 34 ++++++++++++++++++++++++++++------ test/icinga-perfdata.cpp | 14 ++++++++++---- 2 files changed, 38 insertions(+), 10 deletions(-) diff --git a/lib/icinga/perfdatavalue.cpp b/lib/icinga/perfdatavalue.cpp index 3769b072f..0333a23f9 100644 --- a/lib/icinga/perfdatavalue.cpp +++ b/lib/icinga/perfdatavalue.cpp @@ -1,5 +1,6 @@ #include "icinga/perfdatavalue.h" #include "base/convert.h" +#include #include #include @@ -35,17 +36,38 @@ Value PerfdataValue::Parse(const String& perfdata) unit = perfdata.SubStr(pos, tokens[0].GetLength() - pos); - if (unit == "s") + boost::algorithm::to_lower(unit); + + if (unit == "us") { + value /= 1000 * 1000; unit = "seconds"; - else if (unit == "b") + } else if (unit == "ms") { + value /= 1000; + unit = "seconds"; + } else if (unit == "s") { + unit = "seconds"; + } else if (unit == "tb") { + value *= 1024 * 1024 * 1024 * 1024; unit = "bytes"; - else if (unit == "%") + } else if (unit == "gb") { + value *= 1024 * 1024 * 1024; + unit = "bytes"; + } else if (unit == "mb") { + value *= 1024 * 1024; + unit = "bytes"; + } else if (unit == "kb") { + value *= 1024; + unit = "bytes"; + } else if (unit == "b") { + unit = "bytes"; + } else if (unit == "%") { unit = "percent"; - else if (unit == "c") { + } else if (unit == "c") { counter = true; unit = ""; - } else + } else if (unit != "") { BOOST_THROW_EXCEPTION(std::invalid_argument("Invalid performance data unit: " + unit)); + } if (tokens.size() > 1 && tokens[1] != "U") warn = Convert::ToDouble(tokens[1]); @@ -78,7 +100,7 @@ String PerfdataValue::Format(const Value& perfdata) else if (pdv->GetUnit() == "percent") unit = "%"; else if (pdv->GetUnit() == "bytes") - unit = "b"; + unit = "B"; output += unit; diff --git a/test/icinga-perfdata.cpp b/test/icinga-perfdata.cpp index bac094e0a..04812983c 100644 --- a/test/icinga-perfdata.cpp +++ b/test/icinga-perfdata.cpp @@ -24,6 +24,12 @@ using namespace icinga; BOOST_AUTO_TEST_SUITE(icinga_perfdata) +BOOST_AUTO_TEST_CASE(empty) +{ + Dictionary::Ptr pd = PluginUtility::ParsePerfdata(""); + BOOST_CHECK(pd->GetLength() == 0); +} + BOOST_AUTO_TEST_CASE(simple) { Dictionary::Ptr pd = PluginUtility::ParsePerfdata("test=123456"); @@ -45,7 +51,7 @@ BOOST_AUTO_TEST_CASE(multiple) BOOST_AUTO_TEST_CASE(uom) { - Dictionary::Ptr pd = PluginUtility::ParsePerfdata("test=123456b"); + Dictionary::Ptr pd = PluginUtility::ParsePerfdata("test=123456B"); PerfdataValue::Ptr pv = pd->Get("test"); BOOST_CHECK(pv); @@ -59,12 +65,12 @@ BOOST_AUTO_TEST_CASE(uom) BOOST_CHECK(pv->GetMax() == Empty); String str = PluginUtility::FormatPerfdata(pd); - BOOST_CHECK(str == "test=123456b"); + BOOST_CHECK(str == "test=123456B"); } BOOST_AUTO_TEST_CASE(warncritminmax) { - Dictionary::Ptr pd = PluginUtility::ParsePerfdata("test=123456b;1000;2000;3000;4000"); + Dictionary::Ptr pd = PluginUtility::ParsePerfdata("test=123456B;1000;2000;3000;4000"); PerfdataValue::Ptr pv = pd->Get("test"); BOOST_CHECK(pv); @@ -78,7 +84,7 @@ BOOST_AUTO_TEST_CASE(warncritminmax) BOOST_CHECK(pv->GetMax() == 4000); String str = PluginUtility::FormatPerfdata(pd); - BOOST_CHECK(str == "test=123456b;1000;2000;3000;4000"); + BOOST_CHECK(str == "test=123456B;1000;2000;3000;4000"); } BOOST_AUTO_TEST_CASE(invalid)