]> granicus.if.org Git - icinga2/blob - plugins/check_swap.cpp
Add -d/--debug option for windows plugins
[icinga2] / plugins / check_swap.cpp
1 /******************************************************************************
2  * Icinga 2                                                                   *
3  * Copyright (C) 2012-2014 Icinga Development Team (http://www.icinga.org)    *
4  *                                                                            *
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.                     *
9  *                                                                            *
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.                               *
14  *                                                                            *
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  ******************************************************************************/
19 #include <Shlwapi.h>
20 #include <Pdh.h>
21 #include <iostream>
22
23 #include "thresholds.h"
24
25 #include "boost/program_options.hpp"
26
27 #define VERSION 1.0
28
29 namespace po = boost::program_options;
30
31 using std::endl; using std::wcout; using std::wstring;
32 using std::cout;
33
34 static BOOL debug = FALSE;
35
36 struct printInfoStruct 
37 {
38         threshold warn, crit;
39         double swap;
40 };
41
42 static int parseArguments(int, wchar_t **, po::variables_map&, printInfoStruct&);
43 static int printOutput(printInfoStruct&);
44 static int check_swap(printInfoStruct&);
45
46 int wmain(int argc, wchar_t **argv) 
47 {
48         printInfoStruct printInfo = { };
49         po::variables_map vm;
50
51         int ret = parseArguments(argc, argv, vm, printInfo);
52         if (ret != -1)
53                 return ret;
54
55         ret = check_swap(printInfo);
56         if (ret != -1)
57                 return ret;
58
59         return printOutput(printInfo);
60 }
61
62 int parseArguments(int ac, wchar_t **av, po::variables_map& vm, printInfoStruct& printInfo) 
63 {
64         wchar_t namePath[MAX_PATH];
65         GetModuleFileName(NULL, namePath, MAX_PATH);
66         wchar_t *progName = PathFindFileName(namePath);
67
68         po::options_description desc;
69
70         desc.add_options()
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")
76                 ;
77
78         po::basic_command_line_parser<wchar_t> parser(ac, av);
79
80         try {
81                 po::store(
82                         parser
83                         .options(desc)
84                         .style(
85                         po::command_line_style::unix_style |
86                         po::command_line_style::allow_long_disguise)
87                         .run(),
88                         vm);
89                 vm.notify();
90         } catch (std::exception& e) {
91                 cout << e.what() << endl << desc << endl;
92                 return 3;
93         }
94
95         if (vm.count("help")) {
96                 wcout << progName << " Help\n\tVersion: " << VERSION << endl;
97                 wprintf(
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);
100                 cout << desc;
101                 wprintf(
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"
116                         L"-w THRESHOLD\n"
117                         L"warn if threshold is broken, which means VALUE > THRESHOLD\n"
118                         L"(unless stated differently)\n\n"
119                         L"-w !THRESHOLD\n"
120                         L"inverts threshold check, VALUE < THRESHOLD (analogous to above)\n\n"
121                         L"-w [THR1-THR2]\n"
122                         L"warn is VALUE is inside the range spanned by THR1 and THR2\n\n"
123                         L"-w ![THR1-THR2]\n"
124                         L"warn if VALUE is outside the range spanned by THR1 and THR2\n\n"
125                         L"-w THRESHOLD%%\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"
131                         , progName);
132                 cout << endl;
133                 return 0;
134         }
135
136         if (vm.count("version"))
137                 wcout << L"Version: " << VERSION << endl;
138
139         if (vm.count("warning")) {
140                 try {
141                         printInfo.warn = threshold(vm["warning"].as<wstring>());
142                 } catch (std::invalid_argument& e) {
143                         cout << e.what() << endl;
144                         return 3;
145                 }
146         }
147
148         if (vm.count("critical")) {
149                 try {
150                         printInfo.crit = threshold(vm["critical"].as<wstring>());
151                 } catch (std::invalid_argument& e) {
152                         cout << e.what() << endl;
153                         return 3;
154                 }
155         }
156
157         if (vm.count("debug"))
158                 debug = TRUE;
159
160         return -1;
161 }
162
163 int printOutput(printInfoStruct& printInfo) 
164 {
165         if (debug)
166                 wcout << L"Constructing output string" << endl;
167
168         state state = OK;
169
170         if (printInfo.warn.rend(printInfo.swap))
171                 state = WARNING;
172
173         if (printInfo.crit.rend(printInfo.swap))
174                 state = CRITICAL;
175
176         switch (state) {
177         case OK:
178                 wcout << L"SWAP OK " << printInfo.swap << L"% | swap=" << printInfo.swap << L"%;" 
179                         << printInfo.warn.pString() << L";" << printInfo.crit.pString() << L";0;100" << endl;
180                 break;
181         case WARNING:
182                 wcout << L"SWAP WARNING " << printInfo.swap << L"% | swap=" << printInfo.swap << L"%;"
183                         << printInfo.warn.pString() << L";" << printInfo.crit.pString() << L";0;100" << endl;
184                 break;
185         case CRITICAL:
186                 wcout << L"SWAP CRITICAL " << printInfo.swap << L"% | swap=" << printInfo.swap << L"%;"
187                         << printInfo.warn.pString() << L";" << printInfo.crit.pString() << L";0;100" << endl;
188                 break;
189         }
190
191         return state;
192 }
193
194 int check_swap(printInfoStruct& printInfo) 
195 {
196         PDH_HQUERY phQuery;
197         PDH_HCOUNTER phCounter;
198         DWORD dwBufferSize = 0;
199         DWORD CounterType;
200         PDH_FMT_COUNTERVALUE DisplayValue;
201         PDH_STATUS err;
202
203         LPCWSTR path = L"\\Paging File(*)\\% Usage";
204
205         if (debug)
206                 wcout << L"Opening querry handle" << endl;
207
208         err = PdhOpenQuery(NULL, NULL, &phQuery);
209         if (!SUCCEEDED(err))
210                 goto die;
211
212         if (debug)
213                 wcout << L"Adding counter" << endl;
214
215         err = PdhAddEnglishCounter(phQuery, path, NULL, &phCounter);
216         if (!SUCCEEDED(err))
217                 goto die;
218
219         if (debug)
220                 wcout << L"Collecting querry data" << endl;
221
222         err = PdhCollectQueryData(phQuery);
223         if (!SUCCEEDED(err))
224                 goto die;
225
226         if (debug)
227                 wcout << L"Formatting counter data" << endl;
228
229         err = PdhGetFormattedCounterValue(phCounter, PDH_FMT_DOUBLE, &CounterType, &DisplayValue);
230         if (SUCCEEDED(err)) {
231                 printInfo.swap = DisplayValue.doubleValue;
232                 PdhCloseQuery(phQuery);
233                 return -1;
234         }
235
236 die:
237         if (phQuery)
238                 PdhCloseQuery(phQuery);
239         die(err);
240         return 3;
241 }