Implement support for performance data unit prefixes.

Fixes #5026
This commit is contained in:
Gunnar Beutner 2013-11-08 08:39:05 +01:00
parent e246170374
commit ceb0a54baf
2 changed files with 38 additions and 10 deletions

View File

@ -1,5 +1,6 @@
#include "icinga/perfdatavalue.h"
#include "base/convert.h"
#include <boost/algorithm/string/case_conv.hpp>
#include <boost/algorithm/string/split.hpp>
#include <boost/algorithm/string/classification.hpp>
@ -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;

View File

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