Revert "check_disk - show all disks if state is ok and option error only is used"
[monitoring-plugins.git] / plugins / check_users.c
blobf6f4b36208bed06d481d6e311d91f94fb78f96de
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
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.
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.
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.
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/>.
31 *****************************************************************************/
33 const char *progname = "check_users";
34 const char *copyright = "2000-2007";
35 const char *email = "devel@monitoring-plugins.org";
37 #include "common.h"
38 #include "utils.h"
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
51 #define possibly_set(a,b) ((a) == 0 ? (b) : 0)
53 int process_arguments (int, char **);
54 void print_help (void);
55 void print_usage (void);
57 char *warning_range = NULL;
58 char *critical_range = NULL;
59 thresholds *thlds = NULL;
61 int
62 main (int argc, char **argv)
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
76 setlocale (LC_ALL, "");
77 bindtextdomain (PACKAGE, LOCALEDIR);
78 textdomain (PACKAGE);
80 /* Parse extra opts if any */
81 argv = np_extra_opts (&argc, argv, progname);
83 if (process_arguments (argc, argv) == ERROR)
84 usage4 (_("Could not parse arguments"));
86 users = 0;
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;
95 for (index = 0; index < wtscount; index++) {
96 LPTSTR username;
97 DWORD size;
98 int len;
100 if (!WTSQuerySessionInformation(WTS_CURRENT_SERVER_HANDLE,
101 wtsinfo[index].SessionId, WTSUserName, &username, &size))
102 continue;
104 len = lstrlen(username);
106 WTSFreeMemory(username);
108 if (len == 0)
109 continue;
111 if (wtsinfo[index].State == WTSActive ||
112 wtsinfo[index].State == WTSDisconnected)
113 users++;
116 WTSFreeMemory(wtsinfo);
117 #elif HAVE_UTMPX_H
118 /* get currently logged users from utmpx */
119 setutxent ();
121 while ((putmpx = getutxent ()) != NULL)
122 if (putmpx->ut_type == USER_PROCESS)
123 users++;
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;
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);
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;
145 /* get total logged in users */
146 if (sscanf (input_buffer, _("# users=%d"), &users) == 1)
147 break;
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);
155 /* close the pipe */
156 if (spclose (child_process))
157 result = possibly_set (result, STATE_UNKNOWN);
158 #endif
160 /* check the user count against warning and critical thresholds */
161 result = get_status((double)users, thlds);
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));
172 return result;
175 /* process command-line arguments */
177 process_arguments (int argc, char **argv)
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}
189 if (argc < 2)
190 usage ("\n");
192 while (1) {
193 c = getopt_long (argc, argv, "+hVvc:w:", longopts, &option);
195 if (c == -1 || c == EOF || c == 1)
196 break;
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;
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++];
222 /* this will abort in case of invalid ranges */
223 set_thresholds (&thlds, warning_range, critical_range);
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"));
230 return OK;
233 void
234 print_help (void)
236 print_revision (progname, NP_VERSION);
238 printf ("Copyright (c) 1999 Ethan Galstad\n");
239 printf (COPYRIGHT, copyright, email);
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."));
244 printf ("\n\n");
246 print_usage ();
248 printf (UT_HELP_VRSN);
249 printf (UT_EXTRA_OPTS);
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"));
256 printf (UT_SUPPORT);
259 void
260 print_usage (void)
262 printf ("%s\n", _("Usage:"));
263 printf ("%s -w <users> -c <users>\n", progname);