From 5dc900369d21e4ed562b7557d28872a2a8e588de Mon Sep 17 00:00:00 2001 From: Michael Insel Date: Fri, 22 Jun 2018 13:01:58 +0200 Subject: [PATCH] Fix wrong UOM in check_uptime windows plugin This fixes the usage of unvalid UOM in the check_uptime windows plugin. The performance data will now provided in seconds. --- plugins/check_uptime.cpp | 22 +++++++++++++--------- plugins/thresholds.cpp | 36 ++++++++++++++++++++++++++++++++++++ plugins/thresholds.hpp | 1 + 3 files changed, 50 insertions(+), 9 deletions(-) diff --git a/plugins/check_uptime.cpp b/plugins/check_uptime.cpp index 27a656ef2..2e5997e8c 100644 --- a/plugins/check_uptime.cpp +++ b/plugins/check_uptime.cpp @@ -33,6 +33,7 @@ struct printInfoStruct threshold warn; threshold crit; long long time; + long long timeInSeconds; Tunit unit; }; @@ -166,19 +167,19 @@ static int printOutput(printInfoStruct& printInfo) switch (state) { case OK: - std::wcout << L"UPTIME OK " << printInfo.time << TunitStr(printInfo.unit) << L" | 'uptime'=" << printInfo.time - << TunitStr(printInfo.unit) << L";" << printInfo.warn.pString() << L";" - << printInfo.crit.pString() << L";0;" << '\n'; + std::wcout << L"UPTIME OK " << printInfo.time << TunitStr(printInfo.unit) << L" | 'uptime'=" << printInfo.timeInSeconds + << "s" << L";" << printInfo.warn.toSeconds(printInfo.unit).pString() << L";" + << printInfo.crit.toSeconds(printInfo.unit).pString() << L";0;" << '\n'; break; case WARNING: - std::wcout << L"UPTIME WARNING " << printInfo.time << TunitStr(printInfo.unit) << L" | 'uptime'=" << printInfo.time - << TunitStr(printInfo.unit) << L";" << printInfo.warn.pString() << L";" - << printInfo.crit.pString() << L";0;" << '\n'; + std::wcout << L"UPTIME WARNING " << printInfo.time << TunitStr(printInfo.unit) << L" | 'uptime'=" << printInfo.timeInSeconds + << "s" << L";" << printInfo.warn.toSeconds(printInfo.unit).pString() << L";" + << printInfo.crit.toSeconds(printInfo.unit).pString() << L";0;" << '\n'; break; case CRITICAL: - std::wcout << L"UPTIME CRITICAL " << printInfo.time << TunitStr(printInfo.unit) << L" | 'uptime'=" << printInfo.time - << TunitStr(printInfo.unit) << L";" << printInfo.warn.pString() << L";" - << printInfo.crit.pString() << L";0;" << '\n'; + std::wcout << L"UPTIME CRITICAL " << printInfo.time << TunitStr(printInfo.unit) << L" | 'uptime'=" << printInfo.timeInSeconds + << "s" << L";" << printInfo.warn.toSeconds(printInfo.unit).pString() << L";" + << printInfo.crit.toSeconds(printInfo.unit).pString() << L";0;" << '\n'; break; } @@ -209,6 +210,9 @@ static void getUptime(printInfoStruct& printInfo) printInfo.time = uptime.count(); break; } + + // For the Performance Data we need the time in seconds + printInfo.timeInSeconds = boost::chrono::duration_cast(uptime).count(); } int wmain(int argc, WCHAR **argv) diff --git a/plugins/thresholds.cpp b/plugins/thresholds.cpp index f80e7d634..36992f12d 100644 --- a/plugins/thresholds.cpp +++ b/plugins/thresholds.cpp @@ -28,6 +28,13 @@ threshold::threshold() : set(false) {} +threshold::threshold(const double v, const double c, bool l , bool p ) { + lower = v; + upper = c; + legal = l; + perc = p; +} + threshold::threshold(const std::wstring& stri) { if (stri.empty()) @@ -126,6 +133,35 @@ std::wstring threshold::pString(const double max) return s; } +threshold threshold::toSeconds(const Tunit& fromUnit) { + if (!set) + return *this; + + double lowerAbs = lower; + double upperAbs = upper; + + switch (fromUnit) { + case TunitMS: + lowerAbs = lowerAbs / 1000; + upperAbs = upperAbs / 1000; + break; + case TunitS: + lowerAbs = lowerAbs ; + upperAbs = upperAbs ; + break; + case TunitM: + lowerAbs = lowerAbs * 60; + upperAbs = upperAbs * 60; + break; + case TunitH: + lowerAbs = lowerAbs * 60 * 60; + upperAbs = upperAbs * 60 * 60; + break; + } + + return threshold(lowerAbs, upperAbs, legal, perc); +} + std::wstring removeZero(double val) { std::wstring ret = boost::lexical_cast(val); diff --git a/plugins/thresholds.hpp b/plugins/thresholds.hpp index 1a63e5a4d..bffeb6209 100644 --- a/plugins/thresholds.hpp +++ b/plugins/thresholds.hpp @@ -62,6 +62,7 @@ public: // returns a printable string of the threshold std::wstring pString(const double max = 100.0); + threshold toSeconds(const Tunit& fromUnit); }; std::wstring removeZero(double);