1 /*****************************************************************************
3 * Monitoring check_users plugin
6 * Copyright (c) 2000-2012 Monitoring Plugins Development Team
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
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";
42 # include <wtsapi32.h>
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
;
62 main (int argc
, char **argv
)
65 int result
= STATE_UNKNOWN
;
67 WTS_SESSION_INFO
*wtsinfo
;
73 char input_buffer
[MAX_INPUT_BUFFER
];
76 setlocale (LC_ALL
, "");
77 bindtextdomain (PACKAGE
, LOCALEDIR
);
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"));
89 if (!WTSEnumerateSessions(WTS_CURRENT_SERVER_HANDLE
,
90 0, 1, &wtsinfo
, &wtscount
)) {
91 printf(_("Could not enumerate RD sessions: %d\n"), GetLastError());
95 for (index
= 0; index
< wtscount
; index
++) {
100 if (!WTSQuerySessionInformation(WTS_CURRENT_SERVER_HANDLE
,
101 wtsinfo
[index
].SessionId
, WTSUserName
, &username
, &size
))
104 len
= lstrlen(username
);
106 WTSFreeMemory(username
);
111 if (wtsinfo
[index
].State
== WTSActive
||
112 wtsinfo
[index
].State
== WTSDisconnected
)
116 WTSFreeMemory(wtsinfo
);
118 /* get currently logged users from utmpx */
121 while ((putmpx
= getutxent ()) != NULL
)
122 if (putmpx
->ut_type
== USER_PROCESS
)
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] != '#') {
145 /* get total logged in users */
146 if (sscanf (input_buffer
, _("# users=%d"), &users
) == 1)
151 if (fgets (input_buffer
, MAX_INPUT_BUFFER
- 1, child_stderr
))
152 result
= possibly_set (result
, STATE_UNKNOWN
);
153 (void) fclose (child_stderr
);
156 if (spclose (child_process
))
157 result
= possibly_set (result
, STATE_UNKNOWN
);
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"));
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));
175 /* process command-line arguments */
177 process_arguments (int argc
, char **argv
)
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'},
193 c
= getopt_long (argc
, argv
, "+hVvc:w:", longopts
, &option
);
195 if (c
== -1 || c
== EOF
|| c
== 1)
199 case '?': /* print short usage statement if args not parsable */
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
;
210 case 'w': /* warning */
211 warning_range
= optarg
;
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"));
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."));
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"));
262 printf ("%s\n", _("Usage:"));
263 printf ("%s -w <users> -c <users>\n", progname
);