Implement performance data parser.

Refs #2710
This commit is contained in:
Gunnar Beutner 2013-11-07 13:37:58 +01:00
parent 038be974e4
commit 2af388e4ef
8 changed files with 312 additions and 7 deletions

View File

@ -24,6 +24,7 @@ mkclass_target(host.ti host.th)
mkclass_target(icingaapplication.ti icingaapplication.th)
mkclass_target(notificationcommand.ti notificationcommand.th)
mkclass_target(notification.ti notification.th)
mkclass_target(perfdatavalue.ti perfdatavalue.th)
mkclass_target(servicegroup.ti servicegroup.th)
mkclass_target(service.ti service.th)
mkclass_target(timeperiod.ti timeperiod.th)
@ -38,11 +39,11 @@ add_library(icinga SHARED
externalcommandprocessor.cpp host.cpp host.th hostgroup.cpp hostgroup.th
icingaapplication.cpp icingaapplication.th macroprocessor.cpp
macroresolver.cpp notificationcommand.cpp notificationcommand.th
notification.cpp notification.th pluginutility.cpp service-check.cpp
service-comment.cpp service.cpp service-downtime.cpp service-event.cpp
service-flapping.cpp service.th servicegroup.cpp servicegroup.th
service-notification.cpp timeperiod.cpp timeperiod.th user.cpp user.th
usergroup.cpp usergroup.th icinga-type.cpp
notification.cpp notification.th perfdatavalue.cpp perfdatavalue.th
pluginutility.cpp service-check.cpp service-comment.cpp service.cpp
service-downtime.cpp service-event.cpp service-flapping.cpp service.th
servicegroup.cpp servicegroup.th service-notification.cpp timeperiod.cpp
timeperiod.th user.cpp user.th usergroup.cpp usergroup.th icinga-type.cpp
)
target_link_libraries(icinga ${Boost_LIBRARIES} base config)

View File

@ -0,0 +1,105 @@
#include "icinga/perfdatavalue.h"
#include "base/convert.h"
#include <boost/algorithm/string/split.hpp>
#include <boost/algorithm/string/classification.hpp>
using namespace icinga;
PerfdataValue::PerfdataValue(double value, bool counter, const String& unit,
const Value& warn, const Value& crit, const Value& min, const Value& max)
{
SetValue(value);
SetCounter(counter);
SetUnit(unit);
SetWarn(warn);
SetCrit(crit);
SetMin(min);
SetMax(max);
}
Value PerfdataValue::Parse(const String& perfdata)
{
size_t pos = perfdata.FindFirstNotOf("+-0123456789.e");
double value = Convert::ToDouble(perfdata.SubStr(0, pos));
if (pos == String::NPos)
return value;
std::vector<String> tokens;
boost::algorithm::split(tokens, perfdata, boost::is_any_of(";"));
bool counter = false;
String unit;
Value warn, crit, min, max;
unit = perfdata.SubStr(pos, tokens[0].GetLength() - pos);
if (unit == "s")
unit = "seconds";
else if (unit == "b")
unit = "bytes";
else if (unit == "%")
unit = "percent";
else if (unit == "c") {
counter = true;
unit = "";
} else
BOOST_THROW_EXCEPTION(std::invalid_argument("Invalid performance data unit: " + unit));
if (tokens.size() > 1 && tokens[1] != "U")
warn = Convert::ToDouble(tokens[1]);
if (tokens.size() > 2 && tokens[2] != "U")
crit = Convert::ToDouble(tokens[2]);
if (tokens.size() > 3 && tokens[3] != "U")
min = Convert::ToDouble(tokens[3]);
if (tokens.size() > 4 && tokens[4] != "U")
max = Convert::ToDouble(tokens[4]);
return make_shared<PerfdataValue>(value, counter, unit, warn, crit, min, max);
}
String PerfdataValue::Format(const Value& perfdata)
{
if (perfdata.IsObjectType<PerfdataValue>()) {
PerfdataValue::Ptr pdv = perfdata;
String output = Convert::ToString(pdv->GetValue());
String unit;
if (pdv->GetCounter())
unit = "c";
else if (pdv->GetUnit() == "seconds")
unit = "s";
else if (pdv->GetUnit() == "percent")
unit = "%";
else if (pdv->GetUnit() == "bytes")
unit = "b";
output += unit;
if (!pdv->GetWarn().IsEmpty()) {
output += ";" + pdv->GetWarn();
if (!pdv->GetCrit().IsEmpty()) {
output += ";" + pdv->GetCrit();
if (!pdv->GetMin().IsEmpty()) {
output += ";" + pdv->GetMin();
if (!pdv->GetMax().IsEmpty()) {
output += ";" + pdv->GetMax();
}
}
}
}
return output;
} else {
return perfdata;
}
}

View File

@ -0,0 +1,25 @@
#ifndef PERFDATAVALUE_H
#define PERFDATAVALUE_H
#include "icinga/i2-icinga.h"
#include "icinga/perfdatavalue.th"
namespace icinga
{
class I2_ICINGA_API PerfdataValue : public ObjectImpl<PerfdataValue>
{
public:
DECLARE_PTR_TYPEDEFS(PerfdataValue);
PerfdataValue(double value, bool counter, const String& unit,
const Value& warn = Empty, const Value& crit = Empty,
const Value& min = Empty, const Value& max = Empty);
static Value Parse(const String& perfdata);
static String Format(const Value& perfdata);
};
}
#endif /* PERFDATA_VALUE */

View File

@ -0,0 +1,17 @@
#include "base/dynamicobject.h"
namespace icinga
{
class PerfdataValue
{
[state] double Value;
[state] bool Counter;
[state] String Unit;
[state] Value Crit;
[state] Value Warn;
[state] Value Min;
[state] Value Max;
};
}

View File

@ -21,6 +21,7 @@
#include "icinga/checkcommand.h"
#include "icinga/macroprocessor.h"
#include "icinga/icingaapplication.h"
#include "icinga/perfdatavalue.h"
#include "base/dynamictype.h"
#include "base/logger_fwd.h"
#include "base/scriptfunction.h"
@ -79,3 +80,57 @@ Dictionary::Ptr PluginUtility::ParseCheckOutput(const String& output)
return result;
}
Value PluginUtility::ParsePerfdata(const String& perfdata)
{
try {
Dictionary::Ptr result = make_shared<Dictionary>();
size_t begin = 0;
for (;;) {
size_t eqp = perfdata.FindFirstOf('=', begin);
if (eqp == String::NPos)
break;
String key = perfdata.SubStr(begin, eqp - begin);
size_t spq = perfdata.FindFirstOf(' ', eqp);
if (spq == String::NPos)
spq = perfdata.GetLength();
String value = perfdata.SubStr(eqp + 1, spq - eqp - 1);
result->Set(key, PerfdataValue::Parse(value));
begin = spq + 1;
}
return result;
} catch (const std::exception&) {
return perfdata;
}
}
String PluginUtility::FormatPerfdata(const Value& perfdata)
{
String output;
if (!perfdata.IsObjectType<Dictionary>())
return perfdata;
Dictionary::Ptr dict = perfdata;
String key;
Value value;
BOOST_FOREACH(boost::tie(key, value), dict) {
if (!output.IsEmpty())
output += " ";
output += key + "=" + PerfdataValue::Format(value);
}
return output;
}

View File

@ -41,6 +41,9 @@ public:
static ServiceState ExitStatusToState(int exitStatus);
static Dictionary::Ptr ParseCheckOutput(const String& output);
static Value ParsePerfdata(const String& perfdata);
static String FormatPerfdata(const Value& perfdata);
private:
PluginUtility(void);
};

View File

@ -18,8 +18,11 @@
include(BoostTestTargets)
add_boost_test(base
SOURCES base-array.cpp base-convert.cpp base-dictionary.cpp base-fifo.cpp base-match.cpp base-netstring.cpp base-object.cpp base-shellescape.cpp base-stacktrace.cpp base-string.cpp base-timer.cpp base-value.cpp test.cpp
LIBRARIES base
SOURCES base-array.cpp base-convert.cpp base-dictionary.cpp base-fifo.cpp
base-match.cpp base-netstring.cpp base-object.cpp base-shellescape.cpp
base-stacktrace.cpp base-string.cpp base-timer.cpp base-value.cpp
icinga-perfdata.cpp test.cpp
LIBRARIES base config icinga
TESTS base_array/construct
base_array/getset
base_array/remove
@ -62,5 +65,11 @@ add_boost_test(base
base_value/scalar
base_value/convert
base_value/format
icinga_perfdata/simple
icinga_perfdata/multiple
icinga_perfdata/uom
icinga_perfdata/warncrit
icinga_perfdata/minmax
icinga_perfdata/invalid
)

90
test/icinga-perfdata.cpp Normal file
View File

@ -0,0 +1,90 @@
/******************************************************************************
* Icinga 2 *
* Copyright (C) 2012-2013 Icinga Development Team (http://www.icinga.org/) *
* *
* This program is free software; you can redistribute it and/or *
* modify it under the terms of the GNU General Public License *
* as published by the Free Software Foundation; either version 2 *
* of the License, or (at your option) any later version. *
* *
* This program is distributed in the hope that it will be useful, *
* but WITHOUT ANY WARRANTY; without even the implied warranty of *
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
* GNU General Public License for more details. *
* *
* You should have received a copy of the GNU General Public License *
* along with this program; if not, write to the Free Software Foundation *
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA. *
******************************************************************************/
#include "icinga/pluginutility.cpp"
#include <boost/test/unit_test.hpp>
using namespace icinga;
BOOST_AUTO_TEST_SUITE(icinga_perfdata)
BOOST_AUTO_TEST_CASE(simple)
{
Dictionary::Ptr pd = PluginUtility::ParsePerfdata("test=123456");
BOOST_CHECK(pd->Get("test") == 123456);
String str = PluginUtility::FormatPerfdata(pd);
BOOST_CHECK(str == "test=123456");
}
BOOST_AUTO_TEST_CASE(multiple)
{
Dictionary::Ptr pd = PluginUtility::ParsePerfdata("testA=123456 testB=123456");
BOOST_CHECK(pd->Get("testA") == 123456);
BOOST_CHECK(pd->Get("testB") == 123456);
String str = PluginUtility::FormatPerfdata(pd);
BOOST_CHECK(str == "testA=123456 testB=123456");
}
BOOST_AUTO_TEST_CASE(uom)
{
Dictionary::Ptr pd = PluginUtility::ParsePerfdata("test=123456b");
PerfdataValue::Ptr pv = pd->Get("test");
BOOST_CHECK(pv);
BOOST_CHECK(pv->GetValue() == 123456);
BOOST_CHECK(!pv->GetCounter());
BOOST_CHECK(pv->GetUnit() == "bytes");
BOOST_CHECK(pv->GetCrit() == Empty);
BOOST_CHECK(pv->GetWarn() == Empty);
BOOST_CHECK(pv->GetMin() == Empty);
BOOST_CHECK(pv->GetMax() == Empty);
String str = PluginUtility::FormatPerfdata(pd);
BOOST_CHECK(str == "test=123456b");
}
BOOST_AUTO_TEST_CASE(warncrit)
{
Dictionary::Ptr pd = PluginUtility::ParsePerfdata("test=123456b;1000;2000");
PerfdataValue::Ptr pv = pd->Get("test");
BOOST_CHECK(pv);
BOOST_CHECK(pv->GetValue() == 123456);
BOOST_CHECK(!pv->GetCounter());
BOOST_CHECK(pv->GetUnit() == "bytes");
BOOST_CHECK(pv->GetWarn() == 1000);
BOOST_CHECK(pv->GetCrit() == 2000);
BOOST_CHECK(pv->GetMin() == Empty);
BOOST_CHECK(pv->GetMax() == Empty);
String str = PluginUtility::FormatPerfdata(pd);
BOOST_CHECK(str == "test=123456b;1000;2000");
}
BOOST_AUTO_TEST_CASE(invalid)
{
BOOST_CHECK(PluginUtility::ParsePerfdata("test=1,23456") == "test=1,23456");
BOOST_CHECK(PluginUtility::ParsePerfdata("test=123456;10%;20%") == "test=123456;10%;20%");
}
BOOST_AUTO_TEST_SUITE_END()