]> granicus.if.org Git - icinga2/commitdiff
Fix state conditions in check_memory and check_swap 6811/head
authorMichael Insel <michael@insel.email>
Thu, 29 Nov 2018 21:03:26 +0000 (22:03 +0100)
committerMichael Insel <michael@insel.email>
Thu, 29 Nov 2018 21:16:57 +0000 (22:16 +0100)
This fixes the state conditions in check_memory and check_swap. This
turns the if/else if/else statements in simple if statements, since they
won't work properly when both thresholds are broken.

This also implements a new function to get a given state as wstring.

plugins/check_memory.cpp
plugins/check_swap.cpp
plugins/thresholds.cpp
plugins/thresholds.hpp

index e8cb8aae9700326426caf54322b61c6b6907cc29..4b71517a452b75557d4ebabd8dd93b10502e7d90 100644 (file)
@@ -164,7 +164,7 @@ static int printOutput(printInfoStruct& printInfo)
        if (l_Debug)
                std::wcout << L"Constructing output string" << '\n';
 
-       state state;
+       state state = OK;
 
        std::wcout << L"MEMORY ";
 
@@ -175,16 +175,13 @@ static int printOutput(printInfoStruct& printInfo)
        else
                currentValue = printInfo.tRam - printInfo.aRam;
 
-       if (printInfo.warn.rend(currentValue, printInfo.tRam)) {
+       if (printInfo.warn.rend(currentValue, printInfo.tRam))
                state = WARNING;
-               std::wcout << L"WARNING";
-       } else if (printInfo.crit.rend(currentValue, printInfo.tRam)) {
+
+       if (printInfo.crit.rend(currentValue, printInfo.tRam))
                state = CRITICAL;
-               std::wcout << L"CRITICAL";
-       } else {
-               state = OK;
-               std::wcout << L"OK";
-       }
+
+       std::wcout << stateToString(state);
 
        if (!printInfo.showUsed)
                std::wcout << " - " << printInfo.percentFree << L"% free";
index 7f6db013ac5d8c239efee68b411ee8f3fe84f5f8..906a1017dd7631c70228440f3b40df92159c9629 100644 (file)
@@ -194,16 +194,13 @@ static int printOutput(printInfoStruct& printInfo)
        else
                currentValue = printInfo.tSwap - printInfo.aSwap;
 
-       if (printInfo.warn.rend(currentValue, printInfo.tSwap)) {
+       if (printInfo.warn.rend(currentValue, printInfo.tSwap))
                state = WARNING;
-               std::wcout << L"WARNING - ";
-       } else if (printInfo.crit.rend(currentValue, printInfo.tSwap)) {
+
+       if (printInfo.crit.rend(currentValue, printInfo.tSwap))
                state = CRITICAL;
-               std::wcout << L"CRITICAL - ";
-       } else {
-               state = OK;
-               std::wcout << L"OK - ";
-       }
+
+       std::wcout << stateToString(state) << " ";
 
        if (!printInfo.showUsed)
                std::wcout << printInfo.percentFree << L"% free ";
index f32e6d3600e3de31e0b6a5bff33045ded44ee9b1..acc2bcafb7e18a394ad5b683f85467b5935caeb6 100644 (file)
@@ -281,4 +281,13 @@ std::wstring formatErrorInfo(unsigned long err) {
        }
 
        return out.str();
+}
+
+std::wstring stateToString(const state& state) {
+       switch (state) {
+               case OK: return L"OK";
+               case WARNING: return L"WARNING";
+               case CRITICAL: return L"CRITICAL";
+               default: return L"UNKNOWN";
+       }
 }
\ No newline at end of file
index 8348306f218e4f3c67e3547b19fa8d4006459da5..e7d77f745ba7ea375f506d516a870213725a3a43 100644 (file)
@@ -76,4 +76,6 @@ std::wstring TunitStr(const Tunit&);
 void printErrorInfo(unsigned long err = 0);
 std::wstring formatErrorInfo(unsigned long err);
 
+std::wstring stateToString(const state&);
+
 #endif /* THRESHOLDS_H */