]> granicus.if.org Git - icinga2/commitdiff
Update PerfdataValue::Parse to ignore invalid warn, crit, min, and max values
authorJason Young <jyoung15@gmail.com>
Sun, 1 Mar 2015 16:04:48 +0000 (11:04 -0500)
committerMichael Friedrich <michael.friedrich@netways.de>
Mon, 2 Mar 2015 12:52:41 +0000 (13:52 +0100)
... instead of generating exception so that metric values can
still be passed to the graphing backend.
Also update icinga-perfdata tests to reflect these changes.

refs #5043

Signed-off-by: Michael Friedrich <michael.friedrich@netways.de>
lib/icinga/perfdatavalue.cpp
lib/icinga/perfdatavalue.hpp
test/CMakeLists.txt
test/icinga-perfdata.cpp

index 9c4518f90f0da02bafeacec12f1e31aad7644ceb..d643836caa91a5a8334b91fce33e47b18ffb9155 100644 (file)
@@ -20,6 +20,7 @@
 #include "icinga/perfdatavalue.hpp"
 #include "base/convert.hpp"
 #include "base/exception.hpp"
+#include "base/logger.hpp"
 #include <boost/algorithm/string/case_conv.hpp>
 #include <boost/algorithm/string/split.hpp>
 #include <boost/algorithm/string/classification.hpp>
@@ -113,17 +114,10 @@ PerfdataValue::Ptr PerfdataValue::Parse(const String& perfdata)
                BOOST_THROW_EXCEPTION(std::invalid_argument("Invalid performance data unit: " + unit));
        }
 
-       if (tokens.size() > 1 && tokens[1] != "U" && tokens[1] != "")
-               warn = Convert::ToDouble(tokens[1]);
-
-       if (tokens.size() > 2 && tokens[2] != "U" && tokens[2] != "")
-               crit = Convert::ToDouble(tokens[2]);
-
-       if (tokens.size() > 3 && tokens[3] != "U" && tokens[3] != "")
-               min = Convert::ToDouble(tokens[3]);
-
-       if (tokens.size() > 4 && tokens[4] != "U" && tokens[4] != "")
-               max = Convert::ToDouble(tokens[4]);
+       warn = ParseWarnCritMinMaxToken(tokens, 1, "warning");
+       crit = ParseWarnCritMinMaxToken(tokens, 2, "critical");
+       min = ParseWarnCritMinMaxToken(tokens, 3, "minimum");
+       max = ParseWarnCritMinMaxToken(tokens, 4, "maximum");
 
        value = value * base;
 
@@ -184,3 +178,15 @@ String PerfdataValue::Format(void) const
 
        return result.str();
 }
+
+Value PerfdataValue::ParseWarnCritMinMaxToken(const std::vector<String>& tokens, std::vector<String>::size_type index, const String& description)
+{
+       if (tokens.size() > index && tokens[index] != "U" && tokens[index] != "" && tokens[index].FindFirstNotOf("+-0123456789.e") == String::NPos)
+               return Convert::ToDouble(tokens[index]);
+       else {
+               if (tokens.size() > index && tokens[index] != "")
+                       Log(LogDebug, "PerfdataValue")
+                           << "Ignoring unsupported perfdata " << description << " range, value: '" << tokens[index] << "'.";
+               return Empty;
+       }
+}
index 1be5bd1740bb03f29bc8dcb0254c84d36a289a9f..3b6a391f1a64eb5f577c588fca4b09c0e41a00e8 100644 (file)
@@ -39,6 +39,10 @@ public:
 
        static PerfdataValue::Ptr Parse(const String& perfdata);
        String Format(void) const;
+
+private:
+       static Value ParseWarnCritMinMaxToken(const std::vector<String>& tokens,
+           std::vector<String>::size_type index, const String& description);
 };
 
 }
index 6bb966257c72e65817383cabd32848b001d190f4..dc4653f080371f503f021f4cf61b6470619bdff9 100644 (file)
@@ -103,6 +103,7 @@ add_boost_test(base
        icinga_perfdata/multiple
        icinga_perfdata/uom
        icinga_perfdata/warncritminmax
+       icinga_perfdata/ignore_invalid_warn_crit_min_max
        icinga_perfdata/invalid
        icinga_perfdata/multi
 )
index 134c7d5853e19ca3e3734ead1e619c0ed00ad2a6..7b06990b87d2ef31bf4efec826534ed946772bbd 100644 (file)
@@ -114,10 +114,23 @@ BOOST_AUTO_TEST_CASE(warncritminmax)
        BOOST_CHECK(pv->Format() == "test=123456B;1000;2000;3000;4000");
 }
 
+BOOST_AUTO_TEST_CASE(ignore_invalid_warn_crit_min_max)
+{
+       PerfdataValue::Ptr pv = PerfdataValue::Parse("test=123456;1000:2000;0:3000;3000;4000");
+       BOOST_CHECK(pv);
+       BOOST_CHECK(pv->GetValue() == 123456);
+       BOOST_CHECK(pv->GetWarn() == Empty);
+       BOOST_CHECK(pv->GetCrit() == Empty);
+       BOOST_CHECK(pv->GetMin() == 3000);
+       BOOST_CHECK(pv->GetMax() == 4000);
+
+       BOOST_CHECK(pv->Format() == "test=123456");
+}
+
 BOOST_AUTO_TEST_CASE(invalid)
 {
+       BOOST_CHECK_THROW(PerfdataValue::Parse("123456"), boost::exception);
        BOOST_CHECK_THROW(PerfdataValue::Parse("test=1,23456"), boost::exception);
-       BOOST_CHECK_THROW(PerfdataValue::Parse("test=123456;10%;20%"), boost::exception);
 }
 
 BOOST_AUTO_TEST_CASE(multi)