1
/******************************************************************************
5
* Program: Current users plugin for Nagios
7
* Copyright (c) 1999 Ethan Galstad (nagios@nagios.org)
9
* Last Modified: $Date: 2003/01/13 12:15:16 $
12
* 1999-11-17 Karl DeBisschop
13
* - check stderr and status from spoen/spclose
14
* - reformat commenst to fit 80-cahr screen
15
* - set default result to STATE_UNKNOWN
16
* - initialize users at -1, eliminate 'found' variable
18
* Command line: CHECK_USERS <wusers> <cusers>
22
* This plugin will use the /usr/bin/who command to check the number
23
* of users currently logged into the system. If number of logged in
24
* user exceeds the number specified by the <cusers> option, a
25
* STATE_CRITICAL is return. It it exceeds <wusers>, a STATE_WARNING
26
* is returned. Errors reading the output from the who command result
27
* in a STATE_UNKNOWN error.
29
* License Information:
31
* This program is free software; you can redistribute it and/or modify
32
* it under the terms of the GNU General Public License as published by
33
* the Free Software Foundation; either version 2 of the License, or
34
* (at your option) any later version.
36
* This program is distributed in the hope that it will be useful,
37
* but WITHOUT ANY WARRANTY; without even the implied warranty of
38
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
39
* GNU General Public License for more details.
41
* You should have received a copy of the GNU General Public License
42
* along with this program; if not, write to the Free Software
43
* Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
45
*****************************************************************************/
51
const char *progname = "check_users";
52
#define REVISION "$Revision: 1.3 $"
53
#define COPYRIGHT "1999-2002"
54
#define AUTHOR "Ethan Galstad"
55
#define EMAIL "nagios@nagios.org"
57
#define possibly_set(a,b) ((a) == 0 ? (b) : 0)
59
int process_arguments (int, char **);
60
void print_usage (void);
61
void print_help (void);
67
main (int argc, char **argv)
70
int result = STATE_OK;
71
char input_buffer[MAX_INPUT_BUFFER];
73
if (process_arguments (argc, argv) == ERROR)
74
usage ("Could not parse arguments\n");
77
child_process = spopen (WHO_COMMAND);
78
if (child_process == NULL) {
79
printf ("Could not open pipe: %s\n", WHO_COMMAND);
83
child_stderr = fdopen (child_stderr_array[fileno (child_process)], "r");
84
if (child_stderr == NULL)
85
printf ("Could not open stderr for %s\n", WHO_COMMAND);
89
while (fgets (input_buffer, MAX_INPUT_BUFFER - 1, child_process)) {
91
/* increment 'users' on all lines except total user count */
92
if (input_buffer[0] != '#') {
97
/* get total logged in users */
98
if (sscanf (input_buffer, "# users=%d", &users) == 1)
104
if (fgets (input_buffer, MAX_INPUT_BUFFER - 1, child_stderr))
105
result = possibly_set (result, STATE_UNKNOWN);
106
(void) fclose (child_stderr);
109
if (spclose (child_process))
110
result = possibly_set (result, STATE_UNKNOWN);
112
/* else check the user count against warning and critical thresholds */
114
result = STATE_CRITICAL;
115
else if (users >= wusers)
116
result = STATE_WARNING;
120
if (result == STATE_UNKNOWN)
121
printf ("Unable to read output\n");
123
printf ("USERS %s - %d users currently logged in\n", state_text (result),
133
/* process command-line arguments */
135
process_arguments (int argc, char **argv)
140
int option_index = 0;
141
static struct option long_options[] = {
142
{"critical", required_argument, 0, 'c'},
143
{"warning", required_argument, 0, 'w'},
144
{"version", no_argument, 0, 'V'},
145
{"help", no_argument, 0, 'h'},
155
c = getopt_long (argc, argv, "+hVvc:w:", long_options, &option_index);
157
c = getopt (argc, argv, "+hVvc:w:");
160
if (c == -1 || c == EOF || c == 1)
164
case '?': /* print short usage statement if args not parsable */
165
printf ("%s: Unknown argument: %s\n\n", progname, optarg);
167
exit (STATE_UNKNOWN);
171
case 'V': /* version */
172
print_revision (progname, REVISION);
174
case 'c': /* critical */
175
if (!is_intnonneg (optarg))
176
usage ("Critical threshold must be a nonnegative integer\n");
177
cusers = atoi (optarg);
179
case 'w': /* warning */
180
if (!is_intnonneg (optarg))
181
usage ("Warning threshold must be a nonnegative integer\n");
182
wusers = atoi (optarg);
188
if (wusers == -1 && argc > c) {
189
if (is_intnonneg (argv[c]) == FALSE)
190
usage ("Warning threshold must be a nonnegative integer\n");
191
wusers = atoi (argv[c++]);
194
if (cusers == -1 && argc > c) {
195
if (is_intnonneg (argv[c]) == FALSE)
196
usage ("Warning threshold must be a nonnegative integer\n");
197
cusers = atoi (argv[c]);
210
printf ("Usage: %s -w <users> -c <users>\n", progname);
220
print_revision (progname, REVISION);
222
("Copyright (c) " COPYRIGHT " " AUTHOR "(" EMAIL ")\n\n"
223
"This plugin checks the number of users currently logged in on the local\n"
224
"system and generates an error if the number exceeds the thresholds specified.\n");
228
" -w, --warning=INTEGER\n"
229
" Set WARNING status if more than INTEGER users are logged in\n"
230
" -c, --critical=INTEGER\n"
231
" Set CRITICAL status if more than INTEGER users are logged in\n"
233
" Print detailed help screen\n"
234
" -V, --version\n" " Print version information\n");