]> granicus.if.org Git - icinga2/commitdiff
Add 'used' feature to check_swap
authorMichael Insel <michael@insel.email>
Thu, 4 Oct 2018 08:55:21 +0000 (10:55 +0200)
committerMichael Insel <michael@insel.email>
Sat, 6 Oct 2018 18:26:43 +0000 (20:26 +0200)
This implements the _used_ feature to check_swap to print the used swap
instead of the default available swap.

plugins/check_swap.cpp

index 51f8bde22111cc5eb68e277b294a1b79e55b30c5..4c3a1deb06161d67b5ed1982d7d319ae57026535 100644 (file)
@@ -36,6 +36,7 @@ struct printInfoStruct
        double aSwap;
        double percentFree;
        Bunit unit = BunitMB;
+       bool showUsed;
 };
 
 struct pageFileInfo
@@ -73,6 +74,7 @@ static int parseArguments(int ac, WCHAR **av, po::variables_map& vm, printInfoSt
                ("warning,w", po::wvalue<std::wstring>(), "Warning threshold")
                ("critical,c", po::wvalue<std::wstring>(), "Critical threshold")
                ("unit,u", po::wvalue<std::wstring>(), "The unit to use for display (default MB)")
+               ("show-used,U", "Show used swap instead of the free swap")
                ;
 
        po::wcommand_line_parser parser(ac, av);
@@ -167,6 +169,12 @@ static int parseArguments(int ac, WCHAR **av, po::variables_map& vm, printInfoSt
                }
        }
 
+       if (vm.count("show-used")) {
+               printInfo.showUsed = true;
+               printInfo.warn.legal = true;
+               printInfo.crit.legal = true;
+       }
+
        return -1;
 }
 
@@ -177,30 +185,35 @@ static int printOutput(printInfoStruct& printInfo)
 
        state state = OK;
 
-       if (printInfo.warn.rend(printInfo.aSwap, printInfo.tSwap))
-               state = WARNING;
+       std::wcout << L"SWAP ";
 
-       if (printInfo.crit.rend(printInfo.aSwap, printInfo.tSwap))
-               state = CRITICAL;
+       double currentValue;
+
+       if (!printInfo.showUsed)
+               currentValue = printInfo.aSwap;
+       else
+               currentValue = printInfo.tSwap - printInfo.aSwap;
 
-       switch (state) {
-       case OK:
-               std::wcout << L"SWAP OK - " << printInfo.percentFree << L"% free | 'swap'=" << printInfo.aSwap << BunitStr(printInfo.unit) << L";"
-                       << printInfo.warn.pString(printInfo.tSwap) << L";" << printInfo.crit.pString(printInfo.tSwap)
-                       << L";0;" << printInfo.tSwap << '\n';
-               break;
-       case WARNING:
-               std::wcout << L"SWAP WARNING - " << printInfo.percentFree << L"% free | 'swap'=" << printInfo.aSwap << BunitStr(printInfo.unit) << L";"
-                       << printInfo.warn.pString(printInfo.tSwap) << L";" << printInfo.crit.pString(printInfo.tSwap)
-                       << L";0;" << printInfo.tSwap << '\n';
-               break;
-       case CRITICAL:
-               std::wcout << L"SWAP CRITICAL - " << printInfo.percentFree << L"% free | 'swap'=" << printInfo.aSwap << BunitStr(printInfo.unit) << L";"
-                       << printInfo.warn.pString(printInfo.tSwap) << L";" << printInfo.crit.pString(printInfo.tSwap)
-                       << L";0;" << printInfo.tSwap << '\n';
-               break;
+       if (printInfo.warn.rend(currentValue, printInfo.tSwap)) {
+               state = WARNING;
+               std::wcout << L"WARNING - ";
+       } else if (printInfo.crit.rend(currentValue, printInfo.tSwap)) {
+               state = CRITICAL;
+               std::wcout << L"CRITICAL - ";
+       } else {
+               state = OK;
+               std::wcout << L"OK - ";
        }
 
+       if (!printInfo.showUsed)
+               std::wcout << printInfo.percentFree << L"% free ";
+       else
+               std::wcout << 100 - printInfo.percentFree << L"% used ";
+
+       std::wcout << "| 'swap'=" << currentValue << BunitStr(printInfo.unit) << L";"
+               << printInfo.warn.pString(printInfo.tSwap) << L";" << printInfo.crit.pString(printInfo.tSwap)
+               << L";0;" << printInfo.tSwap << '\n';
+
        return state;
 }