From de9adb26873f2b803b8c78d7312caeb3f274f7fb Mon Sep 17 00:00:00 2001 From: Jean Flach Date: Wed, 21 Feb 2018 14:59:21 +0100 Subject: [PATCH] Add 'used' feature to check_memory 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 | 59 +++++++++++++++++++++++++--------------- plugins/check_memory.h | 1 + 2 files changed, 38 insertions(+), 22 deletions(-) diff --git a/plugins/check_memory.cpp b/plugins/check_memory.cpp index cc15ee68e..45a56616d 100644 --- a/plugins/check_memory.cpp +++ b/plugins/check_memory.cpp @@ -59,6 +59,7 @@ INT parseArguments(INT ac, WCHAR ** av, po::variables_map& vm, printInfoStruct& ("warning,w", po::wvalue(), "Warning threshold") ("critical,c", po::wvalue(), "Critical threshold") ("unit,u", po::wvalue(), "The unit to use for display (default MB)") + ("show-used,U", "Show used memory instead of the free memory") ; po::basic_command_line_parser 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,31 +165,43 @@ INT printOutput(printInfoStruct& printInfo) if (debug) std::wcout << L"Constructing output string" << '\n'; - state state = OK; + state state; + std::wstringstream output; - 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";" + 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 WARNING: - std::wcout << L"MEMORY WARNING - " << 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";" - << printInfo.warn.pString(printInfo.tRam) << L";" << printInfo.crit.pString(printInfo.tRam) - << L";0;" << printInfo.tRam << '\n'; - break; + << 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; + } + + std::wcout << output.str() << std::endl; return state; } diff --git a/plugins/check_memory.h b/plugins/check_memory.h index bade75ca3..6bb793b11 100644 --- a/plugins/check_memory.h +++ b/plugins/check_memory.h @@ -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&);