1 /*****************************************************************************
3 * Nagios check_users plugin
6 * Copyright (c) 2000-2007 Nagios Plugins Development Team
8 * Last Modified: $Date$
12 * This file contains the check_users plugin
14 * This plugin checks the number of users currently logged in on the local
15 * system and generates an error if the number exceeds the thresholds
19 * This program is free software: you can redistribute it and/or modify
20 * it under the terms of the GNU General Public License as published by
21 * the Free Software Foundation, either version 3 of the License, or
22 * (at your option) any later version.
24 * This program is distributed in the hope that it will be useful,
25 * but WITHOUT ANY WARRANTY; without even the implied warranty of
26 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
27 * GNU General Public License for more details.
29 * You should have received a copy of the GNU General Public License
30 * along with this program. If not, see <http://www.gnu.org/licenses/>.
34 *****************************************************************************/
36 const char *progname
= "check_users";
37 const char *revision
= "$Revision$";
38 const char *copyright
= "2000-2007";
39 const char *email
= "nagiosplug-devel@lists.sourceforge.net";
45 #define possibly_set(a,b) ((a) == 0 ? (b) : 0)
47 int process_arguments (int, char **);
48 void print_help (void);
49 void print_usage (void);
55 main (int argc
, char **argv
)
58 int result
= STATE_UNKNOWN
;
59 char input_buffer
[MAX_INPUT_BUFFER
];
62 setlocale (LC_ALL
, "");
63 bindtextdomain (PACKAGE
, LOCALEDIR
);
68 if (process_arguments (argc
, argv
) == ERROR
)
69 usage4 (_("Could not parse arguments"));
72 child_process
= spopen (WHO_COMMAND
);
73 if (child_process
== NULL
) {
74 printf (_("Could not open pipe: %s\n"), WHO_COMMAND
);
78 child_stderr
= fdopen (child_stderr_array
[fileno (child_process
)], "r");
79 if (child_stderr
== NULL
)
80 printf (_("Could not open stderr for %s\n"), WHO_COMMAND
);
84 while (fgets (input_buffer
, MAX_INPUT_BUFFER
- 1, child_process
)) {
86 /* increment 'users' on all lines except total user count */
87 if (input_buffer
[0] != '#') {
92 /* get total logged in users */
93 if (sscanf (input_buffer
, _("# users=%d"), &users
) == 1)
99 if (fgets (input_buffer
, MAX_INPUT_BUFFER
- 1, child_stderr
))
100 result
= possibly_set (result
, STATE_UNKNOWN
);
101 (void) fclose (child_stderr
);
104 if (spclose (child_process
))
105 result
= possibly_set (result
, STATE_UNKNOWN
);
107 /* else check the user count against warning and critical thresholds */
109 result
= STATE_CRITICAL
;
110 else if (users
>= wusers
)
111 result
= STATE_WARNING
;
115 if (result
== STATE_UNKNOWN
)
116 printf ("%s\n", _("Unable to read output"));
118 asprintf(&perf
, "%s", perfdata ("users", users
, "",
123 printf (_("USERS %s - %d users currently logged in |%s\n"), state_text (result
),
132 /* process command-line arguments */
134 process_arguments (int argc
, char **argv
)
139 static struct option longopts
[] = {
140 {"critical", required_argument
, 0, 'c'},
141 {"warning", required_argument
, 0, 'w'},
142 {"version", no_argument
, 0, 'V'},
143 {"help", no_argument
, 0, 'h'},
151 c
= getopt_long (argc
, argv
, "+hVvc:w:", longopts
, &option
);
153 if (c
== -1 || c
== EOF
|| c
== 1)
157 case '?': /* print short usage statement if args not parsable */
162 case 'V': /* version */
163 print_revision (progname
, revision
);
165 case 'c': /* critical */
166 if (!is_intnonneg (optarg
))
167 usage4 (_("Critical threshold must be a positive integer"));
169 cusers
= atoi (optarg
);
171 case 'w': /* warning */
172 if (!is_intnonneg (optarg
))
173 usage4 (_("Warning threshold must be a positive integer"));
175 wusers
= atoi (optarg
);
181 if (wusers
== -1 && argc
> c
) {
182 if (is_intnonneg (argv
[c
]) == FALSE
)
183 usage4 (_("Warning threshold must be a positive integer"));
185 wusers
= atoi (argv
[c
++]);
188 if (cusers
== -1 && argc
> c
) {
189 if (is_intnonneg (argv
[c
]) == FALSE
)
190 usage4 (_("Warning threshold must be a positive integer"));
192 cusers
= atoi (argv
[c
]);
203 print_revision (progname
, revision
);
205 printf ("Copyright (c) 1999 Ethan Galstad\n");
206 printf (COPYRIGHT
, copyright
, email
);
208 printf ("%s\n", _("This plugin checks the number of users currently logged in on the local"));
209 printf ("%s\n", _("system and generates an error if the number exceeds the thresholds specified."));
215 printf (_(UT_HELP_VRSN
));
217 printf (" %s\n", "-w, --warning=INTEGER");
218 printf (" %s\n", _("Set WARNING status if more than INTEGER users are logged in"));
219 printf (" %s\n", "-c, --critical=INTEGER");
220 printf (" %s\n", _("Set CRITICAL status if more than INTEGER users are logged in"));
222 printf (_(UT_SUPPORT
));
229 printf (_("Usage:"));
230 printf ("%s -w <users> -c <users>\n", progname
);