1 /******************************************************************************
3 * Copyright (C) 2012-2018 Icinga Development Team (https://icinga.com/) *
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 ******************************************************************************/
20 #include "plugins/thresholds.hpp"
21 #include <boost/program_options.hpp>
29 namespace po = boost::program_options;
31 struct printInfoStruct
45 SIZE_T availableSpwap;
50 BOOL EnumPageFilesProc(LPVOID pContext, PENUM_PAGE_FILE_INFORMATION pPageFileInfo, LPCTSTR lpFilename) {
51 std::vector<pageFileInfo>* pageFile = static_cast<std::vector<pageFileInfo>*>(pContext);
52 SYSTEM_INFO systemInfo;
54 GetSystemInfo(&systemInfo);
56 // pPageFileInfo output is in pages, we need to multiply it by the page size
57 pageFile->push_back({ pPageFileInfo->TotalSize * systemInfo.dwPageSize, (pPageFileInfo->TotalSize - pPageFileInfo->TotalInUse) * systemInfo.dwPageSize });
62 static int parseArguments(int ac, WCHAR **av, po::variables_map& vm, printInfoStruct& printInfo)
64 WCHAR namePath[MAX_PATH];
65 GetModuleFileName(NULL, namePath, MAX_PATH);
66 WCHAR *progName = PathFindFileName(namePath);
68 po::options_description desc;
71 ("help,h", "Print help message and exit")
72 ("version,V", "Print version and exit")
73 ("debug,d", "Verbose/Debug output")
74 ("warning,w", po::wvalue<std::wstring>(), "Warning threshold")
75 ("critical,c", po::wvalue<std::wstring>(), "Critical threshold")
76 ("unit,u", po::wvalue<std::wstring>(), "The unit to use for display (default MB)")
77 ("show-used,U", "Show used swap instead of the free swap")
80 po::wcommand_line_parser parser(ac, av);
87 po::command_line_style::unix_style |
88 po::command_line_style::allow_long_disguise)
92 } catch (const std::exception& e) {
93 std::cout << e.what() << '\n' << desc << '\n';
97 if (vm.count("help")) {
98 std::wcout << progName << " Help\n\tVersion: " << VERSION << '\n';
100 L"%s is a simple program to check a machines swap in percent.\n"
101 L"You can use the following options to define its behaviour:\n\n", progName);
104 L"\nIt will then output a string looking something like this:\n\n"
105 L"\tSWAP WARNING - 20%% free | swap=2000B;3000;500;0;10000\n\n"
106 L"\"SWAP\" being the type of the check, \"WARNING\" the returned status\n"
107 L"and \"20%%\" is the returned value.\n"
108 L"The performance data is found behind the \"|\", in order:\n"
109 L"returned value, warning threshold, critical threshold, minimal value and,\n"
110 L"if applicable, the maximal value. Performance data will only be displayed when\n"
111 L"you set at least one threshold\n\n"
112 L"%s' exit codes denote the following:\n"
113 L" 0\tOK,\n\tNo Thresholds were broken or the programs check part was not executed\n"
114 L" 1\tWARNING,\n\tThe warning, but not the critical threshold was broken\n"
115 L" 2\tCRITICAL,\n\tThe critical threshold was broken\n"
116 L" 3\tUNKNOWN, \n\tThe program experienced an internal or input error\n\n"
117 L"Threshold syntax:\n\n"
119 L"warn if threshold is broken, which means VALUE > THRESHOLD\n"
120 L"(unless stated differently)\n\n"
122 L"inverts threshold check, VALUE < THRESHOLD (analogous to above)\n\n"
124 L"warn is VALUE is inside the range spanned by THR1 and THR2\n\n"
126 L"warn if VALUE is outside the range spanned by THR1 and THR2\n\n"
128 L"if the plugin accepts percentage based thresholds those will be used.\n"
129 L"Does nothing if the plugin does not accept percentages, or only uses\n"
130 L"percentage thresholds. Ranges can be used with \"%%\", but both range values need\n"
131 L"to end with a percentage sign.\n\n"
132 L"All of these options work with the critical threshold \"-c\" too.\n"
138 if (vm.count("version"))
139 std::wcout << L"Version: " << VERSION << '\n';
141 if (vm.count("warning")) {
143 printInfo.warn = threshold(vm["warning"].as<std::wstring>());
144 } catch (const std::invalid_argument& e) {
145 std::cout << e.what() << '\n';
148 printInfo.warn.legal = !printInfo.warn.legal;
151 if (vm.count("critical")) {
153 printInfo.crit = threshold(vm["critical"].as<std::wstring>());
154 } catch (const std::invalid_argument& e) {
155 std::cout << e.what() << '\n';
158 printInfo.crit.legal = !printInfo.crit.legal;
161 l_Debug = vm.count("debug") > 0;
163 if (vm.count("unit")) {
165 printInfo.unit = parseBUnit(vm["unit"].as<std::wstring>());
166 } catch (const std::invalid_argument& e) {
167 std::cout << e.what() << '\n';
172 if (vm.count("show-used")) {
173 printInfo.showUsed = true;
174 printInfo.warn.legal = true;
175 printInfo.crit.legal = true;
181 static int printOutput(printInfoStruct& printInfo)
184 std::wcout << L"Constructing output string" << '\n';
188 std::wcout << L"SWAP ";
192 if (!printInfo.showUsed)
193 currentValue = printInfo.aSwap;
195 currentValue = printInfo.tSwap - printInfo.aSwap;
197 if (printInfo.warn.rend(currentValue, printInfo.tSwap))
200 if (printInfo.crit.rend(currentValue, printInfo.tSwap))
203 std::wcout << stateToString(state) << " - ";
205 if (!printInfo.showUsed)
206 std::wcout << printInfo.percentFree << L"% free ";
208 std::wcout << 100 - printInfo.percentFree << L"% used ";
210 std::wcout << "| 'swap'=" << currentValue << BunitStr(printInfo.unit) << L";"
211 << printInfo.warn.pString(printInfo.tSwap) << L";" << printInfo.crit.pString(printInfo.tSwap)
212 << L";0;" << printInfo.tSwap << '\n';
217 static int check_swap(printInfoStruct& printInfo)
219 PENUM_PAGE_FILE_CALLBACK pageFileCallback = &EnumPageFilesProc;
220 std::vector<pageFileInfo> pageFiles;
222 if(!EnumPageFiles(pageFileCallback, &pageFiles)) {
227 for (int i = 0; i < pageFiles.size(); i++) {
228 printInfo.tSwap += round(pageFiles.at(i).totalSwap / pow(1024.0, printInfo.unit));
229 printInfo.aSwap += round(pageFiles.at(i).availableSpwap / pow(1024.0, printInfo.unit));
232 if (printInfo.aSwap > 0 && printInfo.tSwap > 0)
233 printInfo.percentFree = 100.0 * printInfo.aSwap / printInfo.tSwap;
235 printInfo.percentFree = 0;
240 int wmain(int argc, WCHAR **argv)
242 printInfoStruct printInfo = { };
243 po::variables_map vm;
245 int ret = parseArguments(argc, argv, vm, printInfo);
249 ret = check_swap(printInfo);
253 return printOutput(printInfo);