]> granicus.if.org Git - icinga2/commitdiff
Add 'used' feature to check_memory
authorJean Flach <jean-marcel.flach@icinga.com>
Wed, 21 Feb 2018 13:59:21 +0000 (14:59 +0100)
committerJean Flach <jean-marcel.flach@icinga.com>
Wed, 21 Feb 2018 14:10:26 +0000 (15:10 +0100)
Analogous to check_disk -U can now be used to check against and print the
used memory instead of the default available memory.

fixes #6108

plugins/check_memory.cpp
plugins/check_memory.h

index cc15ee68e59b2c56438d50817187281fea33bd8f..45a56616d90bdd8bf61627944bf26aaafbb62186 100644 (file)
@@ -59,6 +59,7 @@ INT parseArguments(INT ac, WCHAR ** av, po::variables_map& vm, printInfoStruct&
                ("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 memory instead of the free memory")
                ;
 
        po::basic_command_line_parser<WCHAR> parser(ac, av);
@@ -154,6 +155,8 @@ INT parseArguments(INT ac, WCHAR ** av, po::variables_map& vm, printInfoStruct&
                }
        }
 
+       printInfo.showUsed = vm.count("show-used") > 0;
+
        return -1;
 }
 
@@ -162,32 +165,44 @@ INT printOutput(printInfoStruct& printInfo)
        if (debug)
                std::wcout << L"Constructing output string" << '\n';
 
-       state state = OK;
-
-       if (printInfo.warn.rend(printInfo.aRam, printInfo.tRam))
-               state = WARNING;
-
-       if (printInfo.crit.rend(printInfo.aRam, printInfo.tRam))
-               state = CRITICAL;
-
-       switch (state) {
-       case OK:
-               std::wcout << L"MEMORY OK - " << printInfo.percentFree << L"% free | memory=" << printInfo.aRam << BunitStr(printInfo.unit) << L";"
-                       << printInfo.warn.pString(printInfo.tRam) << L";" << printInfo.crit.pString(printInfo.tRam)
-                       << L";0;" << printInfo.tRam << '\n';
-               break;
-       case WARNING:
-               std::wcout << L"MEMORY WARNING - " << printInfo.percentFree << L"% free | memory=" << printInfo.aRam << BunitStr(printInfo.unit) << L";"
+       state state;
+       std::wstringstream output;
+
+       if (!printInfo.showUsed) {
+               if (printInfo.warn.rend(printInfo.aRam, printInfo.tRam)) {
+                       state = WARNING;
+                       output << L"MEMORY WARNING - ";
+               } else if (printInfo.crit.rend(printInfo.aRam, printInfo.tRam)) {
+                       state = CRITICAL;
+                       output << L"MEMORY CRITICAL - ";
+               } else {
+                       state = OK;
+                       output << L"MEMORY OK - ";
+               }
+               output << printInfo.percentFree << L"% free | memory = " << printInfo.aRam << BunitStr(printInfo.unit) << L";"
                        << printInfo.warn.pString(printInfo.tRam) << L";" << printInfo.crit.pString(printInfo.tRam)
-                       << L";0;" << printInfo.tRam << '\n';
-               break;
-       case CRITICAL:
-               std::wcout << L"MEMORY CRITICAL - " << printInfo.percentFree << L"% free | memory=" << printInfo.aRam << BunitStr(printInfo.unit) << L";"
+                       << L";0;" << printInfo.tRam;
+       }
+       else {
+               if (printInfo.warn.rend(printInfo.tRam - printInfo.aRam, printInfo.tRam)) {
+                       state = WARNING;
+                       output << L"MEMORY WARNING - ";
+               }
+               else if (printInfo.crit.rend(printInfo.tRam - printInfo.aRam, printInfo.tRam)) {
+                       state = CRITICAL;
+                       output << L"MEMORY CRITICAL - ";
+               }
+               else {
+                       state = OK;
+                       output << L"MEMORY OK - ";
+               }
+               output << 100 - printInfo.percentFree << L"% used | memory = " << printInfo.tRam - printInfo.aRam << BunitStr(printInfo.unit) << L";"
                        << printInfo.warn.pString(printInfo.tRam) << L";" << printInfo.crit.pString(printInfo.tRam)
-                       << L";0;" << printInfo.tRam << '\n';
-               break;
+                       << L";0;" << printInfo.tRam;
        }
 
+       std::wcout << output.str() << std::endl;
+
        return state;
 }
 
index bade75ca337105ccff16d766ab8a508c0a3d116e..6bb793b118e33ceff7c5f8b3c0f1a5ffd8a9eefa 100644 (file)
@@ -29,6 +29,7 @@ struct printInfoStruct
        DOUBLE tRam, aRam;
        DOUBLE percentFree;
        Bunit unit = BunitMB;
+       BOOL showUsed;
 };
 
 INT parseArguments(INT, WCHAR **, boost::program_options::variables_map&, printInfoStruct&);