]> granicus.if.org Git - icinga2/blob - plugins/check_users.cpp
Fix debug info for indexer
[icinga2] / plugins / check_users.cpp
1 /******************************************************************************
2  * Icinga 2                                                                   *
3  * Copyright (C) 2012-2014 Icinga Development Team (http://www.icinga.org)    *
4  *                                                                            *
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.                     *
9  *                                                                            *
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.                               *
14  *                                                                            *
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  ******************************************************************************/
19 #include <Windows.h>
20 #include <Shlwapi.h>
21 #include <wtsapi32.h>
22 #include <iostream>
23
24 #include "thresholds.h"
25
26 #include "boost/program_options.hpp"
27
28 #define VERSION 1.0
29
30 namespace po = boost::program_options;
31
32 using std::endl; using std::wcout;
33 using std::cout; using std::wstring;
34
35 struct printInfoStruct 
36 {
37         threshold warn, crit;
38         double users;
39 };
40
41 static int parseArguments(int, wchar_t **, po::variables_map&, printInfoStruct&);
42 static int printOutput(printInfoStruct&);
43 static int check_users(printInfoStruct&);
44
45 int wmain(int argc, wchar_t **argv) 
46 {
47         printInfoStruct printInfo = { };
48         po::variables_map vm;
49
50         int ret = parseArguments(argc, argv, vm, printInfo);
51         if (ret != -1)
52                 return ret;
53
54         ret = check_users(printInfo);
55         if (ret != -1)
56                 return ret;
57
58         return printOutput(printInfo);
59 }
60
61 int parseArguments(int ac, wchar_t **av, po::variables_map& vm, printInfoStruct& printInfo) 
62 {
63         wchar_t namePath[MAX_PATH];
64         GetModuleFileName(NULL, namePath, MAX_PATH);
65         wchar_t *progName = PathFindFileName(namePath);
66
67         po::options_description desc;
68
69         desc.add_options()
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")
74                 ;
75
76         po::basic_command_line_parser<wchar_t> parser(ac, av);
77
78         try {
79                 po::store(
80                         parser
81                         .options(desc)
82                         .style(
83                         po::command_line_style::unix_style |
84                         po::command_line_style::allow_long_disguise)
85                         .run(),
86                         vm);
87                 vm.notify();
88         } catch (std::exception& e) {
89                 cout << e.what() << endl << desc << endl;
90                 return 3;
91         }
92
93         if (vm.count("help")) {
94                 wcout << progName << " Help\n\tVersion: " << VERSION << endl;
95                 wprintf(
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);
98                 cout << desc;
99                 wprintf(
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"
114                         L"-w THRESHOLD\n"
115                         L"warn if threshold is broken, which means VALUE > THRESHOLD\n"
116                         L"(unless stated differently)\n\n"
117                         L"-w !THRESHOLD\n"
118                         L"inverts threshold check, VALUE < THRESHOLD (analogous to above)\n\n"
119                         L"-w [THR1-THR2]\n"
120                         L"warn is VALUE is inside the range spanned by THR1 and THR2\n\n"
121                         L"-w ![THR1-THR2]\n"
122                         L"warn if VALUE is outside the range spanned by THR1 and THR2\n\n"
123                         L"-w THRESHOLD%%\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."
129                         , progName);
130                 cout << endl;
131                 return 0;
132         }
133
134         if (vm.count("version"))
135                 wcout << L"Version: " << VERSION << endl;
136
137         if (vm.count("warning")) {
138                 try {
139                         printInfo.warn = threshold(vm["warning"].as<wstring>());
140                 } catch (std::invalid_argument& e) {
141                         cout << e.what() << endl;
142                         return 3;
143                 }
144         }
145         if (vm.count("critical")) {
146                 try {
147                         printInfo.crit = threshold(vm["critical"].as<wstring>());
148                 } catch (std::invalid_argument& e) {
149                         cout << e.what() << endl;
150                         return 3;
151                 }
152         }
153
154         return -1;
155 }
156
157 int printOutput(printInfoStruct& printInfo) 
158 {
159         state state = OK;
160
161         if (printInfo.warn.rend(printInfo.users))
162                 state = WARNING;
163
164         if (printInfo.crit.rend(printInfo.users))
165                 state = CRITICAL;
166
167         switch (state) {
168         case OK:
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;
171                 break;
172         case WARNING:
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;
175                 break;
176         case CRITICAL:
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;
179                 break;
180         }
181
182         return state;
183 }
184
185 int check_users(printInfoStruct& printInfo) 
186 {
187         double users = 0;
188         WTS_SESSION_INFOW *pSessionInfo = NULL;
189         DWORD count;
190         DWORD index;
191
192         if (!WTSEnumerateSessions(WTS_CURRENT_SERVER_HANDLE, 0, 1, &pSessionInfo, &count)) {
193                 wcout << L"Failed to enumerate terminal sessions" << endl;
194                 if (pSessionInfo)
195                         WTSFreeMemory(pSessionInfo);
196                 return 3;
197         }
198
199         for (index = 0; index < count; index++) {
200                 LPWSTR name;
201                 DWORD size;
202                 int len;
203
204                 if (!WTSQuerySessionInformation(WTS_CURRENT_SERVER_HANDLE, pSessionInfo[index].SessionId,
205                                                                                 WTSUserName, &name, &size))
206                         continue;
207
208                 len = lstrlenW(name);
209
210                 WTSFreeMemory(name);
211
212                 if (!len)
213                         continue;
214
215                 if (pSessionInfo[index].State == WTSActive || pSessionInfo[index].State == WTSDisconnected)
216                         users++;
217         }
218
219         WTSFreeMemory(pSessionInfo);
220         printInfo.users = users;
221         return -1;
222 }