1 /* Icinga 2 | (c) 2012 Icinga GmbH | GPLv2+ */
3 #include "plugins/thresholds.hpp"
4 #include <boost/program_options.hpp>
11 namespace po = boost::program_options;
13 struct printInfoStruct
26 static int parseArguments(int ac, WCHAR ** av, po::variables_map& vm, printInfoStruct& printInfo)
28 WCHAR namePath[MAX_PATH];
29 GetModuleFileName(NULL, namePath, MAX_PATH);
30 WCHAR *progName = PathFindFileName(namePath);
32 po::options_description desc;
35 ("help,h", "Print help message and exit")
36 ("version,V", "Print version and exit")
37 ("debug,d", "Verbose/Debug output")
38 ("warning,w", po::wvalue<std::wstring>(), "Warning threshold")
39 ("critical,c", po::wvalue<std::wstring>(), "Critical threshold")
40 ("unit,u", po::wvalue<std::wstring>(), "The unit to use for display (default MB)")
41 ("show-used,U", "Show used memory instead of the free memory")
44 po::wcommand_line_parser parser(ac, av);
51 po::command_line_style::unix_style |
52 po::command_line_style::allow_long_disguise)
56 } catch (const std::exception& e) {
57 std::cout << e.what() << '\n' << desc << '\n';
61 if (vm.count("help")) {
62 std::wcout << progName << " Help\n\tVersion: " << VERSION << '\n';
64 L"%s is a simple program to check a machines physical memory.\n"
65 L"You can use the following options to define its behaviour:\n\n", progName);
68 L"\nIt will then output a string looking something like this:\n\n"
69 L"\tMEMORY WARNING - 50%% free | memory=2024MB;3000;500;0;4096\n\n"
70 L"\"MEMORY\" being the type of the check, \"WARNING\" the returned status\n"
71 L"and \"50%%\" is the returned value.\n"
72 L"The performance data is found behind the \"|\", in order:\n"
73 L"returned value, warning threshold, critical threshold, minimal value and,\n"
74 L"if applicable, the maximal value. Performance data will only be displayed when\n"
75 L"you set at least one threshold\n\n"
76 L"%s' exit codes denote the following:\n"
77 L" 0\tOK,\n\tNo Thresholds were broken or the programs check part was not executed\n"
78 L" 1\tWARNING,\n\tThe warning, but not the critical threshold was broken\n"
79 L" 2\tCRITICAL,\n\tThe critical threshold was broken\n"
80 L" 3\tUNKNOWN, \n\tThe program experienced an internal or input error\n\n"
81 L"Threshold syntax:\n\n"
83 L"warn if threshold is broken, which means VALUE > THRESHOLD\n"
84 L"(unless stated differently)\n\n"
86 L"inverts threshold check, VALUE < THRESHOLD (analogous to above)\n\n"
88 L"warn is VALUE is inside the range spanned by THR1 and THR2\n\n"
90 L"warn if VALUE is outside the range spanned by THR1 and THR2\n\n"
92 L"if the plugin accepts percentage based thresholds those will be used.\n"
93 L"Does nothing if the plugin does not accept percentages, or only uses\n"
94 L"percentage thresholds. Ranges can be used with \"%%\", but both range values need\n"
95 L"to end with a percentage sign.\n\n"
96 L"All of these options work with the critical threshold \"-c\" too.\n"
102 if (vm.count("version"))
103 std::wcout << L"Version: " << VERSION << '\n';
105 if (vm.count("warning")) {
107 printInfo.warn = threshold(vm["warning"].as<std::wstring>());
108 } catch (const std::invalid_argument& e) {
109 std::cout << e.what() << '\n';
112 printInfo.warn.legal = !printInfo.warn.legal;
115 if (vm.count("critical")) {
117 printInfo.crit = threshold(vm["critical"].as<std::wstring>());
118 } catch (const std::invalid_argument& e) {
119 std::cout << e.what() << '\n';
122 printInfo.crit.legal = !printInfo.crit.legal;
125 l_Debug = vm.count("debug") > 0;
127 if (vm.count("unit")) {
129 printInfo.unit = parseBUnit(vm["unit"].as<std::wstring>());
130 } catch (const std::invalid_argument& e) {
131 std::cout << e.what() << '\n';
136 if (vm.count("show-used")) {
137 printInfo.showUsed = true;
138 printInfo.warn.legal = true;
139 printInfo.crit.legal = true;
145 static int printOutput(printInfoStruct& printInfo)
148 std::wcout << L"Constructing output string" << '\n';
152 std::wcout << L"MEMORY ";
156 if (!printInfo.showUsed)
157 currentValue = printInfo.aRam;
159 currentValue = printInfo.tRam - printInfo.aRam;
161 if (printInfo.warn.rend(currentValue, printInfo.tRam))
164 if (printInfo.crit.rend(currentValue, printInfo.tRam))
167 std::wcout << stateToString(state);
169 if (!printInfo.showUsed)
170 std::wcout << " - " << printInfo.percentFree << L"% free";
172 std::wcout << " - " << 100 - printInfo.percentFree << L"% used";
174 std::wcout << "| 'memory'=" << currentValue << BunitStr(printInfo.unit) << L";"
175 << printInfo.warn.pString(printInfo.tRam) << L";" << printInfo.crit.pString(printInfo.tRam)
176 << L";0;" << printInfo.tRam << '\n';
181 static int check_memory(printInfoStruct& printInfo)
184 std::wcout << L"Accessing memory statistics via MemoryStatus" << '\n';
186 MEMORYSTATUSEX memBuf;
187 memBuf.dwLength = sizeof(memBuf);
188 GlobalMemoryStatusEx(&memBuf);
190 printInfo.tRam = round((memBuf.ullTotalPhys / pow(1024.0, printInfo.unit) * pow(10.0, printInfo.unit))) / pow(10.0, printInfo.unit);
191 printInfo.aRam = round((memBuf.ullAvailPhys / pow(1024.0, printInfo.unit) * pow(10.0, printInfo.unit))) / pow(10.0, printInfo.unit);
192 printInfo.percentFree = 100.0 * memBuf.ullAvailPhys / memBuf.ullTotalPhys;
195 std::wcout << L"Found memBuf.dwTotalPhys: " << memBuf.ullTotalPhys << '\n'
196 << L"Found memBuf.dwAvailPhys: " << memBuf.ullAvailPhys << '\n';
201 int wmain(int argc, WCHAR **argv)
203 printInfoStruct printInfo = {};
204 po::variables_map vm;
206 int ret = parseArguments(argc, argv, vm, printInfo);
210 ret = check_memory(printInfo);
214 return printOutput(printInfo);