1 /* Icinga 2 | (c) 2012 Icinga GmbH | GPLv2+ */
3 #include "plugins/thresholds.hpp"
4 #include <boost/program_options.hpp>
5 #include <boost/chrono.hpp>
12 namespace po = boost::program_options;
14 struct printInfoStruct
19 long long timeInSeconds;
25 static int parseArguments(int ac, WCHAR **av, po::variables_map& vm, printInfoStruct& printInfo)
27 WCHAR namePath[MAX_PATH];
28 GetModuleFileName(NULL, namePath, MAX_PATH);
29 WCHAR *progName = PathFindFileName(namePath);
31 po::options_description desc;
34 ("help,h", "Print help message and exit")
35 ("version,V", "Print version and exit")
36 ("debug,d", "Verbose/Debug output")
37 ("warning,w", po::wvalue<std::wstring>(), "Warning threshold (Uses -unit)")
38 ("critical,c", po::wvalue<std::wstring>(), "Critical threshold (Uses -unit)")
39 ("unit,u", po::wvalue<std::wstring>(), "Unit to use:\nh\t- hours\nm\t- minutes\ns\t- seconds (default)\nms\t- milliseconds")
42 po::wcommand_line_parser parser(ac, av);
49 po::command_line_style::unix_style |
50 po::command_line_style::allow_long_disguise)
54 } catch (const std::exception& e) {
55 std::cout << e.what() << '\n' << desc << '\n';
59 if (vm.count("help")) {
60 std::wcout << progName << " Help\n\tVersion: " << VERSION << '\n';
62 L"%s is a simple program to check a machines uptime.\n"
63 L"You can use the following options to define its behaviour:\n\n", progName);
66 L"\nIt will then output a string looking something like this:\n\n"
67 L"\tUPTIME WARNING 712h | uptime=712h;700;1800;0\n\n"
68 L"\"UPTIME\" being the type of the check, \"WARNING\" the returned status\n"
69 L"and \"712h\" is the returned value.\n"
70 L"The performance data is found behind the \"|\", in order:\n"
71 L"returned value, warning threshold, critical threshold, minimal value and,\n"
72 L"if applicable, the maximal value. Performance data will only be displayed when\n"
73 L"you set at least one threshold\n\n"
74 L"Note that the returned time ins always rounded down,\n"
75 L"4 hours and 44 minutes will show as 4h.\n\n"
76 L"%s' exit codes denote the following:\n"
77 L" 0\tOK,\n\tNo Thresholds were broken or the programs check part was not executed\n"
78 L" 1\tWARNING,\n\tThe warning, but not the critical threshold was broken\n"
79 L" 2\tCRITICAL,\n\tThe critical threshold was broken\n"
80 L" 3\tUNKNOWN, \n\tThe program experienced an internal or input error\n\n"
81 L"Threshold syntax:\n\n"
83 L"warn if threshold is broken, which means VALUE > THRESHOLD\n"
84 L"(unless stated differently)\n\n"
86 L"inverts threshold check, VALUE < THRESHOLD (analogous to above)\n\n"
88 L"warn is VALUE is inside the range spanned by THR1 and THR2\n\n"
90 L"warn if VALUE is outside the range spanned by THR1 and THR2\n\n"
92 L"if the plugin accepts percentage based thresholds those will be used.\n"
93 L"Does nothing if the plugin does not accept percentages, or only uses\n"
94 L"percentage thresholds. Ranges can be used with \"%%\", but both range values need\n"
95 L"to end with a percentage sign.\n\n"
96 L"All of these options work with the critical threshold \"-c\" too.\n"
102 if (vm.count("version")) {
103 std::cout << VERSION << '\n';
107 if (vm.count("warning")) {
109 printInfo.warn = threshold(vm["warning"].as<std::wstring>());
110 } catch (const std::invalid_argument& e) {
111 std::cout << e.what() << '\n';
115 if (vm.count("critical")) {
117 printInfo.crit = threshold(vm["critical"].as<std::wstring>());
118 } catch (const std::invalid_argument& e) {
119 std::cout << e.what() << '\n';
124 if (vm.count("unit")) {
126 printInfo.unit = parseTUnit(vm["unit"].as<std::wstring>());
127 } catch (const std::invalid_argument&) {
128 std::wcout << L"Unknown unit type " << vm["unit"].as<std::wstring>() << '\n';
132 printInfo.unit = TunitS;
134 l_Debug = vm.count("debug") > 0;
139 static int printOutput(printInfoStruct& printInfo)
142 std::wcout << L"Constructing output string" << '\n';
146 if (printInfo.warn.rend(printInfo.time))
148 if (printInfo.crit.rend(printInfo.time))
153 std::wcout << L"UPTIME OK " << printInfo.time << TunitStr(printInfo.unit) << L" | 'uptime'=" << printInfo.timeInSeconds
154 << "s" << L";" << printInfo.warn.toSeconds(printInfo.unit).pString() << L";"
155 << printInfo.crit.toSeconds(printInfo.unit).pString() << L";0;" << '\n';
158 std::wcout << L"UPTIME WARNING " << printInfo.time << TunitStr(printInfo.unit) << L" | 'uptime'=" << printInfo.timeInSeconds
159 << "s" << L";" << printInfo.warn.toSeconds(printInfo.unit).pString() << L";"
160 << printInfo.crit.toSeconds(printInfo.unit).pString() << L";0;" << '\n';
163 std::wcout << L"UPTIME CRITICAL " << printInfo.time << TunitStr(printInfo.unit) << L" | 'uptime'=" << printInfo.timeInSeconds
164 << "s" << L";" << printInfo.warn.toSeconds(printInfo.unit).pString() << L";"
165 << printInfo.crit.toSeconds(printInfo.unit).pString() << L";0;" << '\n';
172 static void getUptime(printInfoStruct& printInfo)
175 std::wcout << L"Getting uptime in milliseconds" << '\n';
177 boost::chrono::milliseconds uptime = boost::chrono::milliseconds(GetTickCount64());
180 std::wcout << L"Converting requested unit (default: seconds)" << '\n';
182 switch (printInfo.unit) {
184 printInfo.time = boost::chrono::duration_cast<boost::chrono::hours>(uptime).count();
187 printInfo.time = boost::chrono::duration_cast<boost::chrono::minutes>(uptime).count();
190 printInfo.time = boost::chrono::duration_cast<boost::chrono::seconds>(uptime).count();
193 printInfo.time = uptime.count();
197 // For the Performance Data we need the time in seconds
198 printInfo.timeInSeconds = boost::chrono::duration_cast<boost::chrono::seconds>(uptime).count();
201 int wmain(int argc, WCHAR **argv)
203 po::variables_map vm;
204 printInfoStruct printInfo;
205 int ret = parseArguments(argc, argv, vm, printInfo);
210 getUptime(printInfo);
212 return printOutput(printInfo);