1 /******************************************************************************
3 * Copyright (C) 2012-2016 Icinga Development Team (https://www.icinga.org/) *
5 * This program is free software; you can redistribute it and/or *
6 * modify it under the terms of the GNU General Public License *
7 * as published by the Free Software Foundation; either version 2 *
8 * of the License, or (at your option) any later version. *
10 * This program is distributed in the hope that it will be useful, *
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of *
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
13 * GNU General Public License for more details. *
15 * You should have received a copy of the GNU General Public License *
16 * along with this program; if not, write to the Free Software Foundation *
17 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA. *
18 ******************************************************************************/
23 #include "check_memory.h"
27 namespace po = boost::program_options;
29 static BOOL debug = FALSE;
31 INT wmain(INT argc, WCHAR **argv)
33 printInfoStruct printInfo = {};
36 INT ret = parseArguments(argc, argv, vm, printInfo);
40 ret = check_memory(printInfo);
44 return printOutput(printInfo);
47 INT parseArguments(INT ac, WCHAR ** av, po::variables_map& vm, printInfoStruct& printInfo)
49 WCHAR namePath[MAX_PATH];
50 GetModuleFileName(NULL, namePath, MAX_PATH);
51 WCHAR *progName = PathFindFileName(namePath);
53 po::options_description desc;
56 ("help,h", "Print help message and exit")
57 ("version,V", "Print version and exit")
58 ("debug,d", "Verbose/Debug output")
59 ("warning,w", po::wvalue<std::wstring>(), "Warning threshold")
60 ("critical,c", po::wvalue<std::wstring>(), "Critical threshold")
61 ("unit,u", po::wvalue<std::wstring>(), "The unit to use for display (default MB)")
64 po::basic_command_line_parser<WCHAR> parser(ac, av);
71 po::command_line_style::unix_style |
72 po::command_line_style::allow_long_disguise)
76 } catch (std::exception& e) {
77 std::cout << e.what() << '\n' << desc << '\n';
81 if (vm.count("help")) {
82 std::wcout << progName << " Help\n\tVersion: " << VERSION << '\n';
84 L"%s is a simple program to check a machines physical memory.\n"
85 L"You can use the following options to define its behaviour:\n\n", progName);
88 L"\nIt will then output a string looking something like this:\n\n"
89 L"\tMEMORY WARNING - 50%% free | memory=2024MB;3000;500;0;4096\n\n"
90 L"\"MEMORY\" being the type of the check, \"WARNING\" the returned status\n"
91 L"and \"50%%\" is the returned value.\n"
92 L"The performance data is found behind the \"|\", in order:\n"
93 L"returned value, warning threshold, critical threshold, minimal value and,\n"
94 L"if applicable, the maximal value. Performance data will only be displayed when\n"
95 L"you set at least one threshold\n\n"
96 L"%s' exit codes denote the following:\n"
97 L" 0\tOK,\n\tNo Thresholds were broken or the programs check part was not executed\n"
98 L" 1\tWARNING,\n\tThe warning, but not the critical threshold was broken\n"
99 L" 2\tCRITICAL,\n\tThe critical threshold was broken\n"
100 L" 3\tUNKNOWN, \n\tThe program experienced an internal or input error\n\n"
101 L"Threshold syntax:\n\n"
103 L"warn if threshold is broken, which means VALUE > THRESHOLD\n"
104 L"(unless stated differently)\n\n"
106 L"inverts threshold check, VALUE < THRESHOLD (analogous to above)\n\n"
108 L"warn is VALUE is inside the range spanned by THR1 and THR2\n\n"
110 L"warn if VALUE is outside the range spanned by THR1 and THR2\n\n"
112 L"if the plugin accepts percentage based thresholds those will be used.\n"
113 L"Does nothing if the plugin does not accept percentages, or only uses\n"
114 L"percentage thresholds. Ranges can be used with \"%%\", but both range values need\n"
115 L"to end with a percentage sign.\n\n"
116 L"All of these options work with the critical threshold \"-c\" too.\n"
122 if (vm.count("version"))
123 std::wcout << L"Version: " << VERSION << '\n';
125 if (vm.count("warning")) {
127 printInfo.warn = threshold(vm["warning"].as<std::wstring>());
128 } catch (std::invalid_argument& e) {
129 std::cout << e.what() << '\n';
132 printInfo.warn.legal = !printInfo.warn.legal;
135 if (vm.count("critical")) {
137 printInfo.crit = threshold(vm["critical"].as<std::wstring>());
138 } catch (std::invalid_argument& e) {
139 std::cout << e.what() << '\n';
142 printInfo.crit.legal = !printInfo.crit.legal;
145 if (vm.count("debug"))
148 if (vm.count("unit")) {
150 printInfo.unit = parseBUnit(vm["unit"].as<std::wstring>());
151 } catch (std::invalid_argument& e) {
152 std::cout << e.what() << '\n';
160 INT printOutput(printInfoStruct& printInfo)
163 std::wcout << L"Constructing output string" << '\n';
167 if (printInfo.warn.rend(printInfo.aRam, printInfo.tRam))
170 if (printInfo.crit.rend(printInfo.aRam, printInfo.tRam))
175 std::wcout << L"MEMORY OK - " << printInfo.percentFree << L"% free | memory=" << printInfo.aRam << BunitStr(printInfo.unit) << L";"
176 << printInfo.warn.pString(printInfo.tRam) << L";" << printInfo.crit.pString(printInfo.tRam)
177 << L";0;" << printInfo.tRam << '\n';
180 std::wcout << L"MEMORY WARNING - " << printInfo.percentFree << L"% free | memory=" << printInfo.aRam << BunitStr(printInfo.unit) << L";"
181 << printInfo.warn.pString(printInfo.tRam) << L";" << printInfo.crit.pString(printInfo.tRam)
182 << L";0;" << printInfo.tRam << '\n';
185 std::wcout << L"MEMORY CRITICAL - " << printInfo.percentFree << L"% free | memory=" << printInfo.aRam << BunitStr(printInfo.unit) << L";"
186 << printInfo.warn.pString(printInfo.tRam) << L";" << printInfo.crit.pString(printInfo.tRam)
187 << L";0;" << printInfo.tRam << '\n';
194 INT check_memory(printInfoStruct& printInfo)
197 std::wcout << L"Accessing memory statistics via MemoryStatus" << '\n';
199 _MEMORYSTATUSEX *pMemBuf = new _MEMORYSTATUSEX;
201 pMemBuf->dwLength = sizeof(*pMemBuf);
203 GlobalMemoryStatusEx(pMemBuf);
205 printInfo.tRam = round(pMemBuf->ullTotalPhys / pow(1024.0, printInfo.unit));
206 printInfo.aRam = round(pMemBuf->ullAvailPhys / pow(1024.0, printInfo.unit));
207 printInfo.percentFree = 100.0 * pMemBuf->ullAvailPhys / pMemBuf->ullTotalPhys;
210 std::wcout << L"Found pMemBuf->dwTotalPhys: " << pMemBuf->ullTotalPhys << '\n'
211 << L"Found pMemBuf->dwAvailPhys: " << pMemBuf->ullAvailPhys << '\n';