1 /******************************************************************************
3 * Copyright (C) 2012-2014 Icinga Development Team (http://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 "thresholds.h"
25 #include "boost/program_options.hpp"
29 namespace po = boost::program_options;
31 using std::endl; using std::wcout; using std::wstring;
34 static BOOL debug = FALSE;
36 struct printInfoStruct
42 static int parseArguments(int, wchar_t **, po::variables_map&, printInfoStruct&);
43 static int printOutput(printInfoStruct&);
44 static int check_swap(printInfoStruct&);
46 int wmain(int argc, wchar_t **argv)
48 printInfoStruct printInfo = { };
51 int ret = parseArguments(argc, argv, vm, printInfo);
55 ret = check_swap(printInfo);
59 return printOutput(printInfo);
62 int parseArguments(int ac, wchar_t **av, po::variables_map& vm, printInfoStruct& printInfo)
64 wchar_t namePath[MAX_PATH];
65 GetModuleFileName(NULL, namePath, MAX_PATH);
66 wchar_t *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<wstring>(), "warning threshold")
75 ("critical,c", po::wvalue<wstring>(), "critical threshold")
78 po::basic_command_line_parser<wchar_t> parser(ac, av);
85 po::command_line_style::unix_style |
86 po::command_line_style::allow_long_disguise)
90 } catch (std::exception& e) {
91 cout << e.what() << endl << desc << endl;
95 if (vm.count("help")) {
96 wcout << progName << " Help\n\tVersion: " << VERSION << endl;
98 L"%s is a simple program to check a machines swap in percent.\n"
99 L"You can use the following options to define its behaviour:\n\n", progName);
102 L"\nIt will then output a string looking something like this:\n\n"
103 L"\tSWAP WARNING 23.8304%%|swap=23.8304%%;19.5;30;0;100\n\n"
104 L"\"SWAP\" being the type of the check, \"WARNING\" the returned status\n"
105 L"and \"23.8304%%\" is the returned value.\n"
106 L"The performance data is found behind the \"|\", in order:\n"
107 L"returned value, warning threshold, critical threshold, minimal value and,\n"
108 L"if applicable, the maximal value. Performance data will only be displayed when\n"
109 L"you set at least one threshold\n\n"
110 L"%s' exit codes denote the following:\n"
111 L" 0\tOK,\n\tNo Thresholds were broken or the programs check part was not executed\n"
112 L" 1\tWARNING,\n\tThe warning, but not the critical threshold was broken\n"
113 L" 2\tCRITICAL,\n\tThe critical threshold was broken\n"
114 L" 3\tUNKNOWN, \n\tThe program experienced an internal or input error\n\n"
115 L"Threshold syntax:\n\n"
117 L"warn if threshold is broken, which means VALUE > THRESHOLD\n"
118 L"(unless stated differently)\n\n"
120 L"inverts threshold check, VALUE < THRESHOLD (analogous to above)\n\n"
122 L"warn is VALUE is inside the range spanned by THR1 and THR2\n\n"
124 L"warn if VALUE is outside the range spanned by THR1 and THR2\n\n"
126 L"if the plugin accepts percentage based thresholds those will be used.\n"
127 L"Does nothing if the plugin does not accept percentages, or only uses\n"
128 L"percentage thresholds. Ranges can be used with \"%%\", but both range values need\n"
129 L"to end with a percentage sign.\n\n"
130 L"All of these options work with the critical threshold \"-c\" too.\n"
136 if (vm.count("version"))
137 wcout << L"Version: " << VERSION << endl;
139 if (vm.count("warning")) {
141 printInfo.warn = threshold(vm["warning"].as<wstring>());
142 } catch (std::invalid_argument& e) {
143 cout << e.what() << endl;
148 if (vm.count("critical")) {
150 printInfo.crit = threshold(vm["critical"].as<wstring>());
151 } catch (std::invalid_argument& e) {
152 cout << e.what() << endl;
157 if (vm.count("debug"))
163 int printOutput(printInfoStruct& printInfo)
166 wcout << L"Constructing output string" << endl;
170 if (printInfo.warn.rend(printInfo.swap))
173 if (printInfo.crit.rend(printInfo.swap))
178 wcout << L"SWAP OK " << printInfo.swap << L"% | swap=" << printInfo.swap << L"%;"
179 << printInfo.warn.pString() << L";" << printInfo.crit.pString() << L";0;100" << endl;
182 wcout << L"SWAP WARNING " << printInfo.swap << L"% | swap=" << printInfo.swap << L"%;"
183 << printInfo.warn.pString() << L";" << printInfo.crit.pString() << L";0;100" << endl;
186 wcout << L"SWAP CRITICAL " << printInfo.swap << L"% | swap=" << printInfo.swap << L"%;"
187 << printInfo.warn.pString() << L";" << printInfo.crit.pString() << L";0;100" << endl;
194 int check_swap(printInfoStruct& printInfo)
197 PDH_HCOUNTER phCounter;
198 DWORD dwBufferSize = 0;
200 PDH_FMT_COUNTERVALUE DisplayValue;
203 LPCWSTR path = L"\\Paging File(*)\\% Usage";
206 wcout << L"Opening querry handle" << endl;
208 err = PdhOpenQuery(NULL, NULL, &phQuery);
213 wcout << L"Adding counter" << endl;
215 err = PdhAddEnglishCounter(phQuery, path, NULL, &phCounter);
220 wcout << L"Collecting querry data" << endl;
222 err = PdhCollectQueryData(phQuery);
227 wcout << L"Formatting counter data" << endl;
229 err = PdhGetFormattedCounterValue(phCounter, PDH_FMT_DOUBLE, &CounterType, &DisplayValue);
230 if (SUCCEEDED(err)) {
231 printInfo.swap = DisplayValue.doubleValue;
232 PdhCloseQuery(phQuery);
238 PdhCloseQuery(phQuery);