]> granicus.if.org Git - icinga2/blob - plugins/check_load.cpp
Remove http_vhosts config from Windows
[icinga2] / plugins / check_load.cpp
1 /******************************************************************************
2  * Icinga 2                                                                   *
3  * Copyright (C) 2012-2015 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
20 #include "thresholds.h"
21 #include <boost/program_options.hpp>
22 #include <boost/algorithm/string/split.hpp>
23 #include <boost/algorithm/string/classification.hpp>
24 #include <Pdh.h>
25 #include <Shlwapi.h>
26 #include <pdhmsg.h>
27 #include <iostream>
28
29 #define VERSION 1.0
30
31 namespace po = boost::program_options;
32
33 using std::endl; using std::cout; using std::wstring;
34 using std::wcout;
35
36 static BOOL debug = FALSE;
37
38 struct printInfoStruct 
39 {
40         threshold warn, crit;
41         double load;
42 };
43
44 static int parseArguments(int, wchar_t **, po::variables_map&, printInfoStruct&);
45 static int printOutput(printInfoStruct&);
46 static int check_load(printInfoStruct&);
47
48 int wmain(int argc, wchar_t **argv) 
49 {
50         printInfoStruct printInfo{ };
51         po::variables_map vm;
52
53         int ret = parseArguments(argc, argv, vm, printInfo);
54         if (ret != -1)
55                 return ret;
56
57         ret = check_load(printInfo);
58         if (ret != -1)
59                 return ret;
60
61         return printOutput(printInfo);
62 }
63
64 int parseArguments(int ac, wchar_t **av, po::variables_map& vm, printInfoStruct& printInfo) {
65         wchar_t namePath[MAX_PATH];
66         GetModuleFileName(NULL, namePath, MAX_PATH);
67         wchar_t *progName = PathFindFileName(namePath);
68
69         po::options_description desc;
70
71         desc.add_options()
72                 ("help,h", "print usage message and exit")
73                 ("version,V", "print version and exit")
74                 ("debug,d", "Verbose/Debug output")
75                 ("warning,w", po::wvalue<wstring>(), "warning value (in percent)")
76                 ("critical,c", po::wvalue<wstring>(), "critical value (in percent)")
77                 ;
78
79         po::basic_command_line_parser<wchar_t> parser(ac, av);
80
81         try {
82                 po::store(
83                         parser
84                         .options(desc)
85                         .style(
86                         po::command_line_style::unix_style |
87                         po::command_line_style::allow_long_disguise)
88                         .run(),
89                         vm);
90                 vm.notify();
91         } catch (std::exception& e) {
92                 cout << e.what() << endl << desc << endl;
93                 return 3;
94         }
95
96         if (vm.count("help")) {
97                 wcout << progName << " Help\n\tVersion: " << VERSION << endl;
98                 wprintf(
99                         L"%s is a simple program to check a machines CPU load.\n"
100                         L"You can use the following options to define its behaviour:\n\n", progName);
101                 cout << desc;
102                 wprintf(
103                         L"\nIt will then output a string looking something like this:\n\n"
104                         L"\tLOAD WARNING 67%% | load=67%%;50%%;90%%;0;100\n\n"
105                         L"\"LOAD\" being the type of the check, \"WARNING\" the returned status\n"
106                         L"and \"67%%\" is the returned value.\n"
107                         L"The performance data is found behind the \"|\", in order:\n"
108                         L"returned value, warning threshold, critical threshold, minimal value and,\n"
109                         L"if applicable, the maximal value.\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."
131                         , progName);
132                 cout << endl;
133                 return 0;
134         }
135
136         if (vm.count("version"))
137                 cout << "Version: " << VERSION << endl;
138
139         if (vm.count("warning")) {
140                 try {
141                         std::wstring wthreshold = vm["warning"].as<wstring>();
142                         std::vector<std::wstring> tokens;
143                         boost::algorithm::split(tokens, wthreshold, boost::algorithm::is_any_of(","));
144                         printInfo.warn = threshold(tokens[0]);
145                 } catch (std::invalid_argument& e) {
146                         cout << e.what() << endl;
147                         return 3;
148                 }
149         }
150         if (vm.count("critical")) {
151                 try {
152                         std::wstring cthreshold = vm["critical"].as<wstring>();
153                         std::vector<std::wstring> tokens;
154                         boost::algorithm::split(tokens, cthreshold, boost::algorithm::is_any_of(","));
155                         printInfo.crit = threshold(tokens[0]);
156                 } catch (std::invalid_argument& e) {
157                         cout << e.what() << endl;
158                         return 3;
159                 }
160         }
161
162         if (vm.count("debug"))
163                 debug = TRUE;
164
165         return -1;
166 }
167
168 int printOutput(printInfoStruct& printInfo) 
169 {
170         if (debug)
171                 wcout << L"Constructing output string" << endl;
172
173         state state = OK;
174
175         if (printInfo.warn.rend(printInfo.load))
176                 state = WARNING;
177
178         if (printInfo.crit.rend(printInfo.load))
179                 state = CRITICAL;
180
181         std::wstringstream perf;
182         perf << L"% | load=" << printInfo.load << L"%;" << printInfo.warn.pString() << L";" 
183                 << printInfo.crit.pString() << L";0;100" << endl;
184
185         switch (state) {
186         case OK:
187                 wcout << L"LOAD OK " << printInfo.load << perf.str();
188                 break;
189         case WARNING:
190                 wcout << L"LOAD WARNING " << printInfo.load << perf.str();
191                 break;
192         case CRITICAL:
193                 wcout << L"LOAD CRITICAL " << printInfo.load << perf.str();
194                 break;
195         }
196
197         return state;
198 }
199
200 int check_load(printInfoStruct& printInfo) 
201 {
202         PDH_HQUERY phQuery = NULL;
203         PDH_HCOUNTER phCounter;
204         DWORD dwBufferSize = 0;
205         DWORD CounterType;
206         PDH_FMT_COUNTERVALUE DisplayValue;
207         PDH_STATUS err;
208
209         LPCWSTR path = L"\\Processor(_Total)\\% Idle Time";
210
211         if (debug)
212                 wcout << L"Creating query and adding counter" << endl;
213
214         err = PdhOpenQuery(NULL, NULL, &phQuery);
215         if (!SUCCEEDED(err))
216                 goto die;
217
218         err = PdhAddEnglishCounter(phQuery, path, NULL, &phCounter);
219         if (!SUCCEEDED(err))
220                 goto die;
221
222         if (debug)
223                 wcout << L"Collecting first batch of query data" << endl;
224
225         err = PdhCollectQueryData(phQuery);
226         if (!SUCCEEDED(err))
227                 goto die;
228
229         if (debug)
230                 wcout << L"Sleep for one second" << endl;
231
232         Sleep(1000);
233
234         if (debug)
235                 wcout << L"Collecting second batch of query data" << endl;
236
237         err = PdhCollectQueryData(phQuery);
238         if (!SUCCEEDED(err))
239                 goto die;
240
241         if (debug)
242                 wcout << L"Creating formatted counter array" << endl;
243
244         err = PdhGetFormattedCounterValue(phCounter, PDH_FMT_DOUBLE, &CounterType, &DisplayValue);
245         if (SUCCEEDED(err)) {
246                 if (DisplayValue.CStatus == PDH_CSTATUS_VALID_DATA) {
247                         if (debug)
248                                 wcout << L"Recieved Value of " << DisplayValue.doubleValue << L" (idle)" << endl;
249                         printInfo.load = 100.0 - DisplayValue.doubleValue;
250                 }
251
252                 if (debug)
253                         wcout << L"Finished collection. Cleaning up and returning" << endl;
254
255                 PdhCloseQuery(phQuery);
256                 return -1;
257         }
258
259 die:
260         die();
261         if (phQuery)
262                 PdhCloseQuery(phQuery);
263         return 3;
264 }