1 /******************************************************************************
3 * Copyright (C) 2012-2018 Icinga Development Team (https://www.icinga.com/) *
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 ******************************************************************************/
20 #include "plugins/thresholds.hpp"
21 #include <boost/program_options.hpp>
22 #include <boost/algorithm/string/split.hpp>
23 #include <boost/algorithm/string/classification.hpp>
31 namespace po = boost::program_options;
33 struct printInfoStruct
42 static int parseArguments(int ac, WCHAR **av, po::variables_map& vm, printInfoStruct& printInfo)
44 wchar_t namePath[MAX_PATH];
45 GetModuleFileName(NULL, namePath, MAX_PATH);
46 wchar_t *progName = PathFindFileName(namePath);
48 po::options_description desc;
51 ("help,h", "Print usage message and exit")
52 ("version,V", "Print version and exit")
53 ("debug,d", "Verbose/Debug output")
54 ("warning,w", po::wvalue<std::wstring>(), "Warning value (in percent)")
55 ("critical,c", po::wvalue<std::wstring>(), "Critical value (in percent)")
58 po::wcommand_line_parser parser(ac, av);
65 po::command_line_style::unix_style |
66 po::command_line_style::allow_long_disguise)
70 } catch (const std::exception& e) {
71 std::cout << e.what() << '\n' << desc << '\n';
75 if (vm.count("help")) {
76 std::wcout << progName << " Help\n\tVersion: " << VERSION << '\n';
78 L"%s is a simple program to check a machines CPU load.\n"
79 L"You can use the following options to define its behaviour:\n\n", progName);
82 L"\nIt will then output a string looking something like this:\n\n"
83 L"\tLOAD WARNING 67%% | load=67%%;50%%;90%%;0;100\n\n"
84 L"\"LOAD\" being the type of the check, \"WARNING\" the returned status\n"
85 L"and \"67%%\" is the returned value.\n"
86 L"The performance data is found behind the \"|\", in order:\n"
87 L"returned value, warning threshold, critical threshold, minimal value and,\n"
88 L"if applicable, the maximal value.\n\n"
89 L"%s' exit codes denote the following:\n"
90 L" 0\tOK,\n\tNo Thresholds were broken or the programs check part was not executed\n"
91 L" 1\tWARNING,\n\tThe warning, but not the critical threshold was broken\n"
92 L" 2\tCRITICAL,\n\tThe critical threshold was broken\n"
93 L" 3\tUNKNOWN, \n\tThe program experienced an internal or input error\n\n"
94 L"Threshold syntax:\n\n"
96 L"warn if threshold is broken, which means VALUE > THRESHOLD\n"
97 L"(unless stated differently)\n\n"
99 L"inverts threshold check, VALUE < THRESHOLD (analogous to above)\n\n"
101 L"warn is VALUE is inside the range spanned by THR1 and THR2\n\n"
103 L"warn if VALUE is outside the range spanned by THR1 and THR2\n\n"
105 L"if the plugin accepts percentage based thresholds those will be used.\n"
106 L"Does nothing if the plugin does not accept percentages, or only uses\n"
107 L"percentage thresholds. Ranges can be used with \"%%\", but both range values need\n"
108 L"to end with a percentage sign.\n\n"
109 L"All of these options work with the critical threshold \"-c\" too."
115 if (vm.count("version"))
116 std::cout << "Version: " << VERSION << '\n';
118 if (vm.count("warning")) {
120 std::wstring wthreshold = vm["warning"].as<std::wstring>();
121 std::vector<std::wstring> tokens;
122 boost::algorithm::split(tokens, wthreshold, boost::algorithm::is_any_of(","));
123 printInfo.warn = threshold(tokens[0]);
124 } catch (const std::invalid_argument& e) {
125 std::cout << e.what() << '\n';
130 if (vm.count("critical")) {
132 std::wstring cthreshold = vm["critical"].as<std::wstring>();
133 std::vector<std::wstring> tokens;
134 boost::algorithm::split(tokens, cthreshold, boost::algorithm::is_any_of(","));
135 printInfo.crit = threshold(tokens[0]);
136 } catch (const std::invalid_argument& e) {
137 std::cout << e.what() << '\n';
142 l_Debug = vm.count("debug") > 0;
147 static int printOutput(printInfoStruct& printInfo)
150 std::wcout << L"Constructing output string" << '\n';
154 if (printInfo.warn.rend(printInfo.load))
157 if (printInfo.crit.rend(printInfo.load))
160 std::wcout << L"LOAD ";
167 std::wcout << L"WARNING";
170 std::wcout << L"CRITICAL";
174 std::wcout << " " << printInfo.load << L"% | 'load'=" << printInfo.load << L"%;"
175 << printInfo.warn.pString() << L";"
176 << printInfo.crit.pString() << L";0;100" << '\n';
181 static int check_load(printInfoStruct& printInfo)
184 std::wcout << L"Creating query and adding counter" << '\n';
187 PDH_STATUS err = PdhOpenQuery(NULL, NULL, &phQuery);
191 PDH_HCOUNTER phCounter;
192 err = PdhAddEnglishCounter(phQuery, L"\\Processor(_Total)\\% Idle Time", NULL, &phCounter);
197 std::wcout << L"Collecting first batch of query data" << '\n';
199 err = PdhCollectQueryData(phQuery);
204 std::wcout << L"Sleep for one second" << '\n';
209 std::wcout << L"Collecting second batch of query data" << '\n';
211 err = PdhCollectQueryData(phQuery);
216 std::wcout << L"Creating formatted counter array" << '\n';
219 PDH_FMT_COUNTERVALUE DisplayValue;
220 err = PdhGetFormattedCounterValue(phCounter, PDH_FMT_DOUBLE, &CounterType, &DisplayValue);
221 if (SUCCEEDED(err)) {
222 if (DisplayValue.CStatus == PDH_CSTATUS_VALID_DATA) {
224 std::wcout << L"Recieved Value of " << DisplayValue.doubleValue << L" (idle)" << '\n';
225 printInfo.load = 100.0 - DisplayValue.doubleValue;
229 std::wcout << L"Received data was not valid\n";
234 std::wcout << L"Finished collection. Cleaning up and returning" << '\n';
236 PdhCloseQuery(phQuery);
243 PdhCloseQuery(phQuery);
247 int wmain(int argc, WCHAR **argv)
249 printInfoStruct printInfo;
250 po::variables_map vm;
252 int ret = parseArguments(argc, argv, vm, printInfo);
256 ret = check_load(printInfo);
260 return printOutput(printInfo);