1 /******************************************************************************
3 * Copyright (C) 2012-2015 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 ******************************************************************************/
25 #include "check_load.h"
27 #include "boost/algorithm/string/split.hpp"
28 #include "boost/algorithm/string/classification.hpp"
32 namespace po = boost::program_options;
34 static BOOL debug = FALSE;
37 INT wmain(INT argc, WCHAR **argv)
39 printInfoStruct printInfo{ };
42 INT ret = parseArguments(argc, argv, vm, printInfo);
46 ret = check_load(printInfo);
50 return printOutput(printInfo);
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);
58 po::options_description desc;
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)")
68 po::basic_command_line_parser<wchar_t> parser(ac, av);
75 po::command_line_style::unix_style |
76 po::command_line_style::allow_long_disguise)
80 } catch (std::exception& e) {
81 std::cout << e.what() << '\n' << desc << '\n';
85 if (vm.count("help")) {
86 std::wcout << progName << " Help\n\tVersion: " << VERSION << '\n';
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);
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"
106 L"warn if threshold is broken, which means VALUE > THRESHOLD\n"
107 L"(unless stated differently)\n\n"
109 L"inverts threshold check, VALUE < THRESHOLD (analogous to above)\n\n"
111 L"warn is VALUE is inside the range spanned by THR1 and THR2\n\n"
113 L"warn if VALUE is outside the range spanned by THR1 and THR2\n\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."
125 if (vm.count("version"))
126 std::cout << "Version: " << VERSION << '\n';
128 if (vm.count("warning")) {
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';
139 if (vm.count("critical")) {
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';
151 if (vm.count("debug"))
157 INT printOutput(printInfoStruct& printInfo)
160 std::wcout << L"Constructing output string" << '\n';
164 if (printInfo.warn.rend(printInfo.load))
167 if (printInfo.crit.rend(printInfo.load))
170 std::wstringstream perf;
171 perf << L"% | load=" << printInfo.load << L"%;" << printInfo.warn.pString() << L";"
172 << printInfo.crit.pString() << L";0;100" << '\n';
176 std::wcout << L"LOAD OK " << printInfo.load << perf.str();
179 std::wcout << L"LOAD WARNING " << printInfo.load << perf.str();
182 std::wcout << L"LOAD CRITICAL " << printInfo.load << perf.str();
189 INT check_load(printInfoStruct& printInfo)
191 PDH_HQUERY phQuery = NULL;
192 PDH_HCOUNTER phCounter;
193 DWORD dwBufferSize = 0;
195 PDH_FMT_COUNTERVALUE DisplayValue;
198 LPCWSTR path = L"\\Processor(_Total)\\% Idle Time";
201 std::wcout << L"Creating query and adding counter" << '\n';
203 err = PdhOpenQuery(NULL, NULL, &phQuery);
207 err = PdhAddEnglishCounter(phQuery, path, NULL, &phCounter);
212 std::wcout << L"Collecting first batch of query data" << '\n';
214 err = PdhCollectQueryData(phQuery);
219 std::wcout << L"Sleep for one second" << '\n';
224 std::wcout << L"Collecting second batch of query data" << '\n';
226 err = PdhCollectQueryData(phQuery);
231 std::wcout << L"Creating formatted counter array" << '\n';
233 err = PdhGetFormattedCounterValue(phCounter, PDH_FMT_DOUBLE, &CounterType, &DisplayValue);
234 if (SUCCEEDED(err)) {
235 if (DisplayValue.CStatus == PDH_CSTATUS_VALID_DATA) {
237 std::wcout << L"Recieved Value of " << DisplayValue.doubleValue << L" (idle)" << '\n';
238 printInfo.load = 100.0 - DisplayValue.doubleValue;
242 std::wcout << L"Finished collection. Cleaning up and returning" << '\n';
244 PdhCloseQuery(phQuery);
251 PdhCloseQuery(phQuery);