1 /*****************************************************************************
2 *
3 * Monitoring check_users plugin
4 *
5 * License: GPL
6 * Copyright (c) 2000-2012 Monitoring Plugins Development Team
7 *
8 * Description:
9 *
10 * This file contains the check_users plugin
11 *
12 * This plugin checks the number of users currently logged in on the local
13 * system and generates an error if the number exceeds the thresholds
14 * specified.
15 *
16 *
17 * This program is free software: you can redistribute it and/or modify
18 * it under the terms of the GNU General Public License as published by
19 * the Free Software Foundation, either version 3 of the License, or
20 * (at your option) any later version.
21 *
22 * This program is distributed in the hope that it will be useful,
23 * but WITHOUT ANY WARRANTY; without even the implied warranty of
24 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
25 * GNU General Public License for more details.
26 *
27 * You should have received a copy of the GNU General Public License
28 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
29 *
30 *
31 *****************************************************************************/
32 
33 const char *progname = "check_users";
34 const char *copyright = "2000-2007";
35 const char *email = "devel@monitoring-plugins.org";
36 
37 #include "common.h"
38 #include "utils.h"
39 
40 #if HAVE_WTSAPI32_H
41 # include <windows.h>
42 # include <wtsapi32.h>
43 # undef ERROR
44 # define ERROR -1
45 #elif HAVE_UTMPX_H
46 # include <utmpx.h>
47 #else
48 # include "popen.h"
49 #endif
50 
51 #define possibly_set(a,b) ((a) == 0 ? (b) : 0)
52 
53 int process_arguments (int, char **);
54 void print_help (void);
55 void print_usage (void);
56 
57 char *warning_range = NULL;
58 char *critical_range = NULL;
59 thresholds *thlds = NULL;
60 
61 int
main(int argc,char ** argv)62 main (int argc, char **argv)
63 {
64 	int users = -1;
65 	int result = STATE_UNKNOWN;
66 #if HAVE_WTSAPI32_H
67 	WTS_SESSION_INFO *wtsinfo;
68 	DWORD wtscount;
69 	DWORD index;
70 #elif HAVE_UTMPX_H
71 	struct utmpx *putmpx;
72 #else
73 	char input_buffer[MAX_INPUT_BUFFER];
74 #endif
75 
76 	setlocale (LC_ALL, ""); setlocale(LC_NUMERIC, "C");
77 	bindtextdomain (PACKAGE, LOCALEDIR);
78 	textdomain (PACKAGE);
79 
80 	/* Parse extra opts if any */
81 	argv = np_extra_opts (&argc, argv, progname);
82 
83 	if (process_arguments (argc, argv) == ERROR)
84 		usage4 (_("Could not parse arguments"));
85 
86 	users = 0;
87 
88 #if HAVE_WTSAPI32_H
89 	if (!WTSEnumerateSessions(WTS_CURRENT_SERVER_HANDLE,
90 	  0, 1, &wtsinfo, &wtscount)) {
91 		printf(_("Could not enumerate RD sessions: %d\n"), GetLastError());
92 		return STATE_UNKNOWN;
93 	}
94 
95 	for (index = 0; index < wtscount; index++) {
96 		LPTSTR username;
97 		DWORD size;
98 		int len;
99 
100 		if (!WTSQuerySessionInformation(WTS_CURRENT_SERVER_HANDLE,
101 		  wtsinfo[index].SessionId, WTSUserName, &username, &size))
102 			continue;
103 
104 		len = lstrlen(username);
105 
106 		WTSFreeMemory(username);
107 
108 		if (len == 0)
109 			continue;
110 
111 		if (wtsinfo[index].State == WTSActive ||
112 		  wtsinfo[index].State == WTSDisconnected)
113 			users++;
114 	}
115 
116 	WTSFreeMemory(wtsinfo);
117 #elif HAVE_UTMPX_H
118 	/* get currently logged users from utmpx */
119 	setutxent ();
120 
121 	while ((putmpx = getutxent ()) != NULL)
122 		if (putmpx->ut_type == USER_PROCESS)
123 			users++;
124 
125 	endutxent ();
126 #else
127 	/* run the command */
128 	child_process = spopen (WHO_COMMAND);
129 	if (child_process == NULL) {
130 		printf (_("Could not open pipe: %s\n"), WHO_COMMAND);
131 		return STATE_UNKNOWN;
132 	}
133 
134 	child_stderr = fdopen (child_stderr_array[fileno (child_process)], "r");
135 	if (child_stderr == NULL)
136 		printf (_("Could not open stderr for %s\n"), WHO_COMMAND);
137 
138 	while (fgets (input_buffer, MAX_INPUT_BUFFER - 1, child_process)) {
139 		/* increment 'users' on all lines except total user count */
140 		if (input_buffer[0] != '#') {
141 			users++;
142 			continue;
143 		}
144 
145 		/* get total logged in users */
146 		if (sscanf (input_buffer, _("# users=%d"), &users) == 1)
147 			break;
148 	}
149 
150 	/* check STDERR */
151 	if (fgets (input_buffer, MAX_INPUT_BUFFER - 1, child_stderr))
152 		result = possibly_set (result, STATE_UNKNOWN);
153 	(void) fclose (child_stderr);
154 
155 	/* close the pipe */
156 	if (spclose (child_process))
157 		result = possibly_set (result, STATE_UNKNOWN);
158 #endif
159 
160 	/* check the user count against warning and critical thresholds */
161 	result = get_status((double)users, thlds);
162 
163 	if (result == STATE_UNKNOWN)
164 		printf ("%s\n", _("Unable to read output"));
165 	else {
166 		printf (_("USERS %s - %d users currently logged in |%s\n"),
167 				state_text(result), users,
168 				sperfdata_int("users", users, "", warning_range,
169 							critical_range, TRUE, 0, FALSE, 0));
170 	}
171 
172 	return result;
173 }
174 
175 /* process command-line arguments */
176 int
process_arguments(int argc,char ** argv)177 process_arguments (int argc, char **argv)
178 {
179 	int c;
180 	int option = 0;
181 	static struct option longopts[] = {
182 		{"critical", required_argument, 0, 'c'},
183 		{"warning", required_argument, 0, 'w'},
184 		{"version", no_argument, 0, 'V'},
185 		{"help", no_argument, 0, 'h'},
186 		{0, 0, 0, 0}
187 	};
188 
189 	if (argc < 2)
190 		usage ("\n");
191 
192 	while (1) {
193 		c = getopt_long (argc, argv, "+hVvc:w:", longopts, &option);
194 
195 		if (c == -1 || c == EOF || c == 1)
196 			break;
197 
198 		switch (c) {
199 		case '?':									/* print short usage statement if args not parsable */
200 			usage5 ();
201 		case 'h':									/* help */
202 			print_help ();
203 			exit (STATE_UNKNOWN);
204 		case 'V':									/* version */
205 			print_revision (progname, NP_VERSION);
206 			exit (STATE_UNKNOWN);
207 		case 'c':									/* critical */
208 			critical_range = optarg;
209 			break;
210 		case 'w':									/* warning */
211 			warning_range = optarg;
212 			break;
213 		}
214 	}
215 
216 	c = optind;
217 	if (warning_range == NULL && argc > c)
218 		warning_range = argv[c++];
219 	if (critical_range == NULL && argc > c)
220 		critical_range = argv[c++];
221 
222 	/* this will abort in case of invalid ranges */
223 	set_thresholds (&thlds, warning_range, critical_range);
224 
225 	if (thlds->warning->end < 0)
226 		usage4 (_("Warning threshold must be a positive integer"));
227 	if (thlds->critical->end < 0)
228 		usage4 (_("Critical threshold must be a positive integer"));
229 
230 	return OK;
231 }
232 
233 void
print_help(void)234 print_help (void)
235 {
236 	print_revision (progname, NP_VERSION);
237 
238 	printf ("Copyright (c) 1999 Ethan Galstad\n");
239 	printf (COPYRIGHT, copyright, email);
240 
241 	printf ("%s\n", _("This plugin checks the number of users currently logged in on the local"));
242 	printf ("%s\n", _("system and generates an error if the number exceeds the thresholds specified."));
243 
244 	printf ("\n\n");
245 
246 	print_usage ();
247 
248 	printf (UT_HELP_VRSN);
249 	printf (UT_EXTRA_OPTS);
250 
251 	printf (" %s\n", "-w, --warning=INTEGER");
252 	printf ("    %s\n", _("Set WARNING status if more than INTEGER users are logged in"));
253 	printf (" %s\n", "-c, --critical=INTEGER");
254 	printf ("    %s\n", _("Set CRITICAL status if more than INTEGER users are logged in"));
255 
256 	printf (UT_SUPPORT);
257 }
258 
259 void
print_usage(void)260 print_usage (void)
261 {
262 	printf ("%s\n", _("Usage:"));
263 	printf ("%s -w <users> -c <users>\n", progname);
264 }
265