]> granicus.if.org Git - icinga2/blob - plugins/check_load.cpp
Merge pull request #6103 from Icinga/fix/http-security-fixes
[icinga2] / plugins / check_load.cpp
1 /******************************************************************************
2  * Icinga 2                                                                   *
3  * Copyright (C) 2012-2018 Icinga Development Team (https://www.icinga.com/)  *
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 <Pdh.h>
21 #include <Shlwapi.h>
22 #include <pdhmsg.h>
23 #include <iostream>
24
25 #include "check_load.h"
26
27 #include "boost/algorithm/string/split.hpp"
28 #include "boost/algorithm/string/classification.hpp"
29
30 #define VERSION 1.0
31
32 namespace po = boost::program_options;
33
34 static BOOL debug = FALSE;
35
36
37 INT wmain(INT argc, WCHAR **argv)
38 {
39         printInfoStruct printInfo{ };
40         po::variables_map vm;
41
42         INT ret = parseArguments(argc, argv, vm, printInfo);
43         if (ret != -1)
44                 return ret;
45
46         ret = check_load(printInfo);
47         if (ret != -1)
48                 return ret;
49
50         return printOutput(printInfo);
51 }
52
53 INT parseArguments(INT ac, WCHAR **av, po::variables_map& vm, printInfoStruct& printInfo) {
54         wchar_t namePath[MAX_PATH];
55         GetModuleFileName(NULL, namePath, MAX_PATH);
56         wchar_t *progName = PathFindFileName(namePath);
57
58         po::options_description desc;
59
60         desc.add_options()
61                 ("help,h", "Print usage message and exit")
62                 ("version,V", "Print version and exit")
63                 ("debug,d", "Verbose/Debug output")
64                 ("warning,w", po::wvalue<std::wstring>(), "Warning value (in percent)")
65                 ("critical,c", po::wvalue<std::wstring>(), "Critical value (in percent)")
66                 ;
67
68         po::basic_command_line_parser<wchar_t> parser(ac, av);
69
70         try {
71                 po::store(
72                         parser
73                         .options(desc)
74                         .style(
75                         po::command_line_style::unix_style |
76                         po::command_line_style::allow_long_disguise)
77                         .run(),
78                         vm);
79                 vm.notify();
80         } catch (std::exception& e) {
81                 std::cout << e.what() << '\n' << desc << '\n';
82                 return 3;
83         }
84
85         if (vm.count("help")) {
86                 std::wcout << progName << " Help\n\tVersion: " << VERSION << '\n';
87                 wprintf(
88                         L"%s is a simple program to check a machines CPU load.\n"
89                         L"You can use the following options to define its behaviour:\n\n", progName);
90                 std::cout << desc;
91                 wprintf(
92                         L"\nIt will then output a string looking something like this:\n\n"
93                         L"\tLOAD WARNING 67%% | load=67%%;50%%;90%%;0;100\n\n"
94                         L"\"LOAD\" being the type of the check, \"WARNING\" the returned status\n"
95                         L"and \"67%%\" is the returned value.\n"
96                         L"The performance data is found behind the \"|\", in order:\n"
97                         L"returned value, warning threshold, critical threshold, minimal value and,\n"
98                         L"if applicable, the maximal value.\n\n"
99                         L"%s' exit codes denote the following:\n"
100                         L" 0\tOK,\n\tNo Thresholds were broken or the programs check part was not executed\n"
101                         L" 1\tWARNING,\n\tThe warning, but not the critical threshold was broken\n"
102                         L" 2\tCRITICAL,\n\tThe critical threshold was broken\n"
103                         L" 3\tUNKNOWN, \n\tThe program experienced an internal or input error\n\n"
104                         L"Threshold syntax:\n\n"
105                         L"-w THRESHOLD\n"
106                         L"warn if threshold is broken, which means VALUE > THRESHOLD\n"
107                         L"(unless stated differently)\n\n"
108                         L"-w !THRESHOLD\n"
109                         L"inverts threshold check, VALUE < THRESHOLD (analogous to above)\n\n"
110                         L"-w [THR1-THR2]\n"
111                         L"warn is VALUE is inside the range spanned by THR1 and THR2\n\n"
112                         L"-w ![THR1-THR2]\n"
113                         L"warn if VALUE is outside the range spanned by THR1 and THR2\n\n"
114                         L"-w THRESHOLD%%\n"
115                         L"if the plugin accepts percentage based thresholds those will be used.\n"
116                         L"Does nothing if the plugin does not accept percentages, or only uses\n"
117                         L"percentage thresholds. Ranges can be used with \"%%\", but both range values need\n"
118                         L"to end with a percentage sign.\n\n"
119                         L"All of these options work with the critical threshold \"-c\" too."
120                         , progName);
121                 std::cout << '\n';
122                 return 0;
123         }
124
125         if (vm.count("version"))
126                 std::cout << "Version: " << VERSION << '\n';
127
128         if (vm.count("warning")) {
129                 try {
130                         std::wstring wthreshold = vm["warning"].as<std::wstring>();
131                         std::vector<std::wstring> tokens;
132                         boost::algorithm::split(tokens, wthreshold, boost::algorithm::is_any_of(","));
133                         printInfo.warn = threshold(tokens[0]);
134                 } catch (std::invalid_argument& e) {
135                         std::cout << e.what() << '\n';
136                         return 3;
137                 }
138         }
139         if (vm.count("critical")) {
140                 try {
141                         std::wstring cthreshold = vm["critical"].as<std::wstring>();
142                         std::vector<std::wstring> tokens;
143                         boost::algorithm::split(tokens, cthreshold, boost::algorithm::is_any_of(","));
144                         printInfo.crit = threshold(tokens[0]);
145                 } catch (std::invalid_argument& e) {
146                         std::cout << e.what() << '\n';
147                         return 3;
148                 }
149         }
150
151         if (vm.count("debug"))
152                 debug = TRUE;
153
154         return -1;
155 }
156
157 INT printOutput(printInfoStruct& printInfo)
158 {
159         if (debug)
160                 std::wcout << L"Constructing output string" << '\n';
161
162         state state = OK;
163
164         if (printInfo.warn.rend(printInfo.load))
165                 state = WARNING;
166
167         if (printInfo.crit.rend(printInfo.load))
168                 state = CRITICAL;
169
170         std::wstringstream perf;
171         perf << L"% | load=" << printInfo.load << L"%;" << printInfo.warn.pString() << L";"
172                 << printInfo.crit.pString() << L";0;100" << '\n';
173
174         switch (state) {
175         case OK:
176                 std::wcout << L"LOAD OK " << printInfo.load << perf.str();
177                 break;
178         case WARNING:
179                 std::wcout << L"LOAD WARNING " << printInfo.load << perf.str();
180                 break;
181         case CRITICAL:
182                 std::wcout << L"LOAD CRITICAL " << printInfo.load << perf.str();
183                 break;
184         }
185
186         return state;
187 }
188
189 INT check_load(printInfoStruct& printInfo)
190 {
191         PDH_HQUERY phQuery = NULL;
192         PDH_HCOUNTER phCounter;
193         DWORD dwBufferSize = 0;
194         DWORD CounterType;
195         PDH_FMT_COUNTERVALUE DisplayValue;
196         PDH_STATUS err;
197
198         LPCWSTR path = L"\\Processor(_Total)\\% Idle Time";
199
200         if (debug)
201                 std::wcout << L"Creating query and adding counter" << '\n';
202
203         err = PdhOpenQuery(NULL, NULL, &phQuery);
204         if (!SUCCEEDED(err))
205                 goto die;
206
207         err = PdhAddEnglishCounter(phQuery, path, NULL, &phCounter);
208         if (!SUCCEEDED(err))
209                 goto die;
210
211         if (debug)
212                 std::wcout << L"Collecting first batch of query data" << '\n';
213
214         err = PdhCollectQueryData(phQuery);
215         if (!SUCCEEDED(err))
216                 goto die;
217
218         if (debug)
219                 std::wcout << L"Sleep for one second" << '\n';
220
221         Sleep(1000);
222
223         if (debug)
224                 std::wcout << L"Collecting second batch of query data" << '\n';
225
226         err = PdhCollectQueryData(phQuery);
227         if (!SUCCEEDED(err))
228                 goto die;
229
230         if (debug)
231                 std::wcout << L"Creating formatted counter array" << '\n';
232
233         err = PdhGetFormattedCounterValue(phCounter, PDH_FMT_DOUBLE, &CounterType, &DisplayValue);
234         if (SUCCEEDED(err)) {
235                 if (DisplayValue.CStatus == PDH_CSTATUS_VALID_DATA) {
236                         if (debug)
237                                 std::wcout << L"Recieved Value of " << DisplayValue.doubleValue << L" (idle)" << '\n';
238                         printInfo.load = 100.0 - DisplayValue.doubleValue;
239                 } else {
240                         if (debug)
241                                 std::wcout << L"Received data was not valid\n";
242                         goto die;
243                 }
244
245                 if (debug)
246                         std::wcout << L"Finished collection. Cleaning up and returning" << '\n';
247
248                 PdhCloseQuery(phQuery);
249                 return -1;
250         }
251
252 die:
253         die();
254         if (phQuery)
255                 PdhCloseQuery(phQuery);
256         return 3;
257 }