]> granicus.if.org Git - icinga2/blob - plugins/check_swap.cpp
Some minor plugin fixes
[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 struct printInfoStruct 
35 {
36         threshold warn, crit;
37         double swap;
38 };
39
40 static int parseArguments(int, wchar_t **, po::variables_map&, printInfoStruct&);
41 static int printOutput(printInfoStruct&);
42 static int check_swap(printInfoStruct&);
43
44 int wmain(int argc, wchar_t **argv) 
45 {
46         printInfoStruct printInfo = { };
47         po::variables_map vm;
48
49         int ret = parseArguments(argc, argv, vm, printInfo);
50         if (ret != -1)
51                 return ret;
52
53         ret = check_swap(printInfo);
54         if (ret != -1)
55                 return ret;
56
57         return printOutput(printInfo);
58 }
59
60 int parseArguments(int ac, wchar_t **av, po::variables_map& vm, printInfoStruct& printInfo) 
61 {
62         wchar_t namePath[MAX_PATH];
63         GetModuleFileName(NULL, namePath, MAX_PATH);
64         wchar_t *progName = PathFindFileName(namePath);
65
66         po::options_description desc;
67
68         desc.add_options()
69                 (",h", "print help message and exit")
70                 ("help", "print verbose help and exit")
71                 ("version,v", "print version and exit")
72                 ("warning,w", po::wvalue<wstring>(), "warning threshold")
73                 ("critical,c", po::wvalue<wstring>(), "critical threshold")
74                 ;
75
76         po::basic_command_line_parser<wchar_t> parser(ac, av);
77
78         try {
79                 po::store(
80                         parser
81                         .options(desc)
82                         .style(
83                         po::command_line_style::unix_style |
84                         po::command_line_style::allow_long_disguise)
85                         .run(),
86                         vm);
87                 vm.notify();
88         } catch (std::exception& e) {
89                 cout << e.what() << endl << desc << endl;
90                 return 3;
91         }
92
93         if (vm.count("h")) {
94                 cout << desc << endl;
95                 return 0;
96         }
97     
98         if (vm.count("help")) {
99                 wcout << progName << " Help\n\tVersion: " << VERSION << endl;
100                 wprintf(
101                         L"%s is a simple program to check a machines swap in percent.\n"
102                         L"You can use the following options to define its behaviour:\n\n", progName);
103                 cout << desc;
104                 wprintf(
105                         L"\nIt will then output a string looking something like this:\n\n"
106                         L"\tSWAP WARNING 23.8304%%|swap=23.8304%%;19.5;30;0;100\n\n"
107                         L"\"SWAP\" being the type of the check, \"WARNING\" the returned status\n"
108                         L"and \"23.8304%%\" is the returned value.\n"
109                         L"The performance data is found behind the \"|\", in order:\n"
110                         L"returned value, warning threshold, critical threshold, minimal value and,\n"
111                         L"if applicable, the maximal value. Performance data will only be displayed when\n"
112                         L"you set at least one threshold\n\n"
113                         L"%s' exit codes denote the following:\n"
114                         L" 0\tOK,\n\tNo Thresholds were broken or the programs check part was not executed\n"
115                         L" 1\tWARNING,\n\tThe warning, but not the critical threshold was broken\n"
116                         L" 2\tCRITICAL,\n\tThe critical threshold was broken\n"
117                         L" 3\tUNKNOWN, \n\tThe program experienced an internal or input error\n\n"
118                         L"Threshold syntax:\n\n"
119                         L"-w THRESHOLD\n"
120                         L"warn if threshold is broken, which means VALUE > THRESHOLD\n"
121                         L"(unless stated differently)\n\n"
122                         L"-w !THRESHOLD\n"
123                         L"inverts threshold check, VALUE < THRESHOLD (analogous to above)\n\n"
124                         L"-w [THR1-THR2]\n"
125                         L"warn is VALUE is inside the range spanned by THR1 and THR2\n\n"
126                         L"-w ![THR1-THR2]\n"
127                         L"warn if VALUE is outside the range spanned by THR1 and THR2\n\n"
128                         L"-w THRESHOLD%%\n"
129                         L"if the plugin accepts percentage based thresholds those will be used.\n"
130                         L"Does nothing if the plugin does not accept percentages, or only uses\n"
131                         L"percentage thresholds. Ranges can be used with \"%%\", but both range values need\n"
132                         L"to end with a percentage sign.\n\n"
133                         L"All of these options work with the critical threshold \"-c\" too.\n"
134                         , progName);
135                 cout << endl;
136                 return 0;
137         }
138
139         if (vm.count("version"))
140                 wcout << L"Version: " << VERSION << endl;
141
142         if (vm.count("warning")) {
143                 try {
144                         printInfo.warn = threshold(vm["warning"].as<wstring>());
145                 } catch (std::invalid_argument& e) {
146                         cout << e.what() << endl;
147                         return 3;
148                 }
149         }
150         if (vm.count("critical")) {
151                 try {
152                         printInfo.crit = threshold(vm["critical"].as<wstring>());
153                 } catch (std::invalid_argument& e) {
154                         cout << e.what() << endl;
155                         return 3;
156                 }
157         }
158
159         return -1;
160 }
161
162 int printOutput(printInfoStruct& printInfo) 
163 {
164         state state = OK;
165
166         if (printInfo.warn.rend(printInfo.swap))
167                 state = WARNING;
168
169         if (printInfo.crit.rend(printInfo.swap))
170                 state = CRITICAL;
171
172         switch (state) {
173         case OK:
174                 wcout << L"SWAP OK " << printInfo.swap << L"%|swap=" << printInfo.swap << L"%;" 
175                         << printInfo.warn.pString() << L";" << printInfo.crit.pString() << L";0;100" << endl;
176                 break;
177         case WARNING:
178                 wcout << L"SWAP WARNING " << printInfo.swap << L"%|swap=" << printInfo.swap << L"%;"
179                         << printInfo.warn.pString() << L";" << printInfo.crit.pString() << L";0;100" << endl;
180                 break;
181         case CRITICAL:
182                 wcout << L"SWAP CRITICAL " << printInfo.swap << L"%|swap=" << printInfo.swap << L"%;"
183                         << printInfo.warn.pString() << L";" << printInfo.crit.pString() << L";0;100" << endl;
184                 break;
185         }
186
187         return state;
188 }
189
190 int check_swap(printInfoStruct& printInfo) 
191 {
192         PDH_HQUERY phQuery;
193         PDH_HCOUNTER phCounter;
194         DWORD dwBufferSize = 0;
195         DWORD CounterType;
196         PDH_FMT_COUNTERVALUE DisplayValue;
197         PDH_STATUS err;
198
199         LPCWSTR path = L"\\Paging File(*)\\% Usage";
200
201         err = PdhOpenQuery(NULL, NULL, &phQuery);
202         if (!SUCCEEDED(err))
203                 goto die;
204
205         err = PdhAddEnglishCounter(phQuery, path, NULL, &phCounter);
206         if (!SUCCEEDED(err))
207                 goto die;
208
209         err = PdhCollectQueryData(phQuery);
210         if (!SUCCEEDED(err))
211                 goto die;
212
213         err = PdhGetFormattedCounterValue(phCounter, PDH_FMT_DOUBLE, &CounterType, &DisplayValue);
214         if (SUCCEEDED(err)) {
215                 printInfo.swap = DisplayValue.doubleValue;
216                 PdhCloseQuery(phQuery);
217                 return -1;
218         }
219
220 die:
221         if (phQuery)
222                 PdhCloseQuery(phQuery);
223         die(err);
224         return 3;
225 }