1 /* Icinga 2 | (c) 2012 Icinga GmbH | GPLv2+ */
3 #include "plugins/thresholds.hpp"
4 #include <boost/program_options.hpp>
12 namespace po = boost::program_options;
14 struct printInfoStruct
23 static int parseArguments(int ac, WCHAR **av, po::variables_map& vm, printInfoStruct& printInfo)
25 WCHAR namePath[MAX_PATH];
26 GetModuleFileName(NULL, namePath, MAX_PATH);
27 WCHAR *progName = PathFindFileName(namePath);
29 po::options_description desc;
32 ("help,h", "Print help message and exit")
33 ("version,V", "Print version and exit")
34 ("debug,d", "Verbose/Debug output")
35 ("warning,w", po::wvalue<std::wstring>(), "Warning threshold")
36 ("critical,c", po::wvalue<std::wstring>(), "Critical threshold")
39 po::wcommand_line_parser parser(ac, av);
46 po::command_line_style::unix_style |
47 po::command_line_style::allow_long_disguise)
51 } catch (const std::exception& e) {
52 std::cout << e.what() << '\n' << desc << '\n';
56 if (vm.count("help")) {
57 std::wcout << progName << " Help\n\tVersion: " << VERSION << '\n';
59 L"%s is a simple program to check a machines logged in users.\n"
60 L"You can use the following options to define its behaviour:\n\n", progName);
63 L"\nIt will then output a string looking something like this:\n\n"
64 L"\tUSERS WARNING 48 | users=48;10;50;0\n\n"
65 L"\"USERS\" being the type of the check, \"WARNING\" the returned status\n"
66 L"and \"48\" is the returned value.\n"
67 L"The performance data is found behind the \"|\", in order:\n"
68 L"returned value, warning threshold, critical threshold, minimal value and,\n"
69 L"if applicable, the maximal value. Performance data will only be displayed when\n"
70 L"you set at least one threshold\n\n"
71 L"%s' exit codes denote the following:\n"
72 L" 0\tOK,\n\tNo Thresholds were broken or the programs check part was not executed\n"
73 L" 1\tWARNING,\n\tThe warning, but not the critical threshold was broken\n"
74 L" 2\tCRITICAL,\n\tThe critical threshold was broken\n"
75 L" 3\tUNKNOWN, \n\tThe program experienced an internal or input error\n\n"
76 L"Threshold syntax:\n\n"
78 L"warn if threshold is broken, which means VALUE > THRESHOLD\n"
79 L"(unless stated differently)\n\n"
81 L"inverts threshold check, VALUE < THRESHOLD (analogous to above)\n\n"
83 L"warn is VALUE is inside the range spanned by THR1 and THR2\n\n"
85 L"warn if VALUE is outside the range spanned by THR1 and THR2\n\n"
87 L"if the plugin accepts percentage based thresholds those will be used.\n"
88 L"Does nothing if the plugin does not accept percentages, or only uses\n"
89 L"percentage thresholds. Ranges can be used with \"%%\", but both range values need\n"
90 L"to end with a percentage sign.\n\n"
91 L"All of these options work with the critical threshold \"-c\" too."
97 if (vm.count("version"))
98 std::wcout << L"Version: " << VERSION << '\n';
100 if (vm.count("warning")) {
102 printInfo.warn = threshold(vm["warning"].as<std::wstring>());
103 } catch (const std::invalid_argument& e) {
104 std::cout << e.what() << '\n';
108 if (vm.count("critical")) {
110 printInfo.crit = threshold(vm["critical"].as<std::wstring>());
111 } catch (const std::invalid_argument& e) {
112 std::cout << e.what() << '\n';
117 l_Debug = vm.count("debug") > 0;
122 static int printOutput(printInfoStruct& printInfo)
125 std::wcout << L"Constructing output string" << '\n';
129 if (printInfo.warn.rend(printInfo.users))
132 if (printInfo.crit.rend(printInfo.users))
137 std::wcout << L"USERS OK " << printInfo.users << L" User(s) logged in | 'users'=" << printInfo.users << L";"
138 << printInfo.warn.pString() << L";" << printInfo.crit.pString() << L";0;" << '\n';
141 std::wcout << L"USERS WARNING " << printInfo.users << L" User(s) logged in | 'users'=" << printInfo.users << L";"
142 << printInfo.warn.pString() << L";" << printInfo.crit.pString() << L";0;" << '\n';
145 std::wcout << L"USERS CRITICAL " << printInfo.users << L" User(s) logged in | 'users'=" << printInfo.users << L";"
146 << printInfo.warn.pString() << L";" << printInfo.crit.pString() << L";0;" << '\n';
153 static int check_users(printInfoStruct& printInfo)
156 WTS_SESSION_INFOW *pSessionInfo = NULL;
161 std::wcout << L"Trying to enumerate terminal sessions" << '\n';
163 if (!WTSEnumerateSessions(WTS_CURRENT_SERVER_HANDLE, 0, 1, &pSessionInfo, &count)) {
164 std::wcout << L"Failed to enumerate terminal sessions" << '\n';
167 WTSFreeMemory(pSessionInfo);
172 std::wcout << L"Got all sessions (" << count << L"), traversing and counting active ones" << '\n';
174 for (index = 0; index < count; index++) {
180 std::wcout << L"Querrying session number " << index << '\n';
182 if (!WTSQuerySessionInformation(WTS_CURRENT_SERVER_HANDLE, pSessionInfo[index].SessionId,
183 WTSUserName, &name, &size))
187 std::wcout << L"Found \"" << name << L"\". Checking whether it's a real session" << '\n';
189 len = lstrlenW(name);
196 if (pSessionInfo[index].State == WTSActive || pSessionInfo[index].State == WTSDisconnected) {
199 std::wcout << L"\"" << name << L"\" is a real session, counting it. Now " << users << '\n';
204 std::wcout << "Finished coutning user sessions (" << users << "). Freeing memory and returning" << '\n';
206 WTSFreeMemory(pSessionInfo);
207 printInfo.users = users;
211 int wmain(int argc, WCHAR **argv)
213 printInfoStruct printInfo = { };
214 po::variables_map vm;
216 int ret = parseArguments(argc, argv, vm, printInfo);
220 ret = check_users(printInfo);
224 return printOutput(printInfo);