1 /******************************************************************************
3 * Copyright (C) 2012-2014 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 ******************************************************************************/
24 #include "thresholds.h"
26 #include "boost/program_options.hpp"
30 namespace po = boost::program_options;
32 using std::endl; using std::wcout;
33 using std::cout; using std::wstring;
35 struct printInfoStruct
41 static int parseArguments(int, wchar_t **, po::variables_map&, printInfoStruct&);
42 static int printOutput(printInfoStruct&);
43 static int check_users(printInfoStruct&);
45 int wmain(int argc, wchar_t **argv)
47 printInfoStruct printInfo = { };
50 int ret = parseArguments(argc, argv, vm, printInfo);
54 ret = check_users(printInfo);
58 return printOutput(printInfo);
61 int parseArguments(int ac, wchar_t **av, po::variables_map& vm, printInfoStruct& printInfo)
63 wchar_t namePath[MAX_PATH];
64 GetModuleFileName(NULL, namePath, MAX_PATH);
65 wchar_t *progName = PathFindFileName(namePath);
67 po::options_description desc;
70 ("help,h", "print help message and exit")
71 ("version,V", "print version and exit")
72 ("warning,w", po::wvalue<wstring>(), "warning threshold")
73 ("critical,c", po::wvalue<wstring>(), "critical threshold")
76 po::basic_command_line_parser<wchar_t> parser(ac, av);
83 po::command_line_style::unix_style |
84 po::command_line_style::allow_long_disguise)
88 } catch (std::exception& e) {
89 cout << e.what() << endl << desc << endl;
93 if (vm.count("help")) {
94 wcout << progName << " Help\n\tVersion: " << VERSION << endl;
96 L"%s is a simple program to check a machines logged in users.\n"
97 L"You can use the following options to define its behaviour:\n\n", progName);
100 L"\nIt will then output a string looking something like this:\n\n"
101 L"\tUSERS WARNING 48|users=48;10;50;0\n\n"
102 L"\"USERS\" being the type of the check, \"WARNING\" the returned status\n"
103 L"and \"48\" is the returned value.\n"
104 L"The performance data is found behind the \"|\", in order:\n"
105 L"returned value, warning threshold, critical threshold, minimal value and,\n"
106 L"if applicable, the maximal value. Performance data will only be displayed when\n"
107 L"you set at least one threshold\n\n"
108 L"%s' exit codes denote the following:\n"
109 L" 0\tOK,\n\tNo Thresholds were broken or the programs check part was not executed\n"
110 L" 1\tWARNING,\n\tThe warning, but not the critical threshold was broken\n"
111 L" 2\tCRITICAL,\n\tThe critical threshold was broken\n"
112 L" 3\tUNKNOWN, \n\tThe program experienced an internal or input error\n\n"
113 L"Threshold syntax:\n\n"
115 L"warn if threshold is broken, which means VALUE > THRESHOLD\n"
116 L"(unless stated differently)\n\n"
118 L"inverts threshold check, VALUE < THRESHOLD (analogous to above)\n\n"
120 L"warn is VALUE is inside the range spanned by THR1 and THR2\n\n"
122 L"warn if VALUE is outside the range spanned by THR1 and THR2\n\n"
124 L"if the plugin accepts percentage based thresholds those will be used.\n"
125 L"Does nothing if the plugin does not accept percentages, or only uses\n"
126 L"percentage thresholds. Ranges can be used with \"%%\", but both range values need\n"
127 L"to end with a percentage sign.\n\n"
128 L"All of these options work with the critical threshold \"-c\" too."
134 if (vm.count("version"))
135 wcout << L"Version: " << VERSION << endl;
137 if (vm.count("warning")) {
139 printInfo.warn = threshold(vm["warning"].as<wstring>());
140 } catch (std::invalid_argument& e) {
141 cout << e.what() << endl;
145 if (vm.count("critical")) {
147 printInfo.crit = threshold(vm["critical"].as<wstring>());
148 } catch (std::invalid_argument& e) {
149 cout << e.what() << endl;
157 int printOutput(printInfoStruct& printInfo)
161 if (printInfo.warn.rend(printInfo.users))
164 if (printInfo.crit.rend(printInfo.users))
169 wcout << L"USERS OK " << printInfo.users << L" User(s) logged in | users=" << printInfo.users << L";"
170 << printInfo.warn.pString() << L";" << printInfo.crit.pString() << L";0;" << endl;
173 wcout << L"USERS WARNING " << printInfo.users << L" User(s) logged in | users=" << printInfo.users << L";"
174 << printInfo.warn.pString() << L";" << printInfo.crit.pString() << L";0;" << endl;
177 wcout << L"USERS CRITICAL " << printInfo.users << L" User(s) logged in | users=" << printInfo.users << L";"
178 << printInfo.warn.pString() << L";" << printInfo.crit.pString() << L";0;" << endl;
185 int check_users(printInfoStruct& printInfo)
188 WTS_SESSION_INFOW *pSessionInfo = NULL;
192 if (!WTSEnumerateSessions(WTS_CURRENT_SERVER_HANDLE, 0, 1, &pSessionInfo, &count)) {
193 wcout << L"Failed to enumerate terminal sessions" << endl;
195 WTSFreeMemory(pSessionInfo);
199 for (index = 0; index < count; index++) {
204 if (!WTSQuerySessionInformation(WTS_CURRENT_SERVER_HANDLE, pSessionInfo[index].SessionId,
205 WTSUserName, &name, &size))
208 len = lstrlenW(name);
215 if (pSessionInfo[index].State == WTSActive || pSessionInfo[index].State == WTSDisconnected)
219 WTSFreeMemory(pSessionInfo);
220 printInfo.users = users;