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 /* Parse extra opts if any */
69 argv
=np_extra_opts (&argc
, argv
, progname
);
71 if (process_arguments (argc
, argv
) == ERROR
)
72 usage4 (_("Could not parse arguments"));
75 child_process
= spopen (WHO_COMMAND
);
76 if (child_process
== NULL
) {
77 printf (_("Could not open pipe: %s\n"), WHO_COMMAND
);
81 child_stderr
= fdopen (child_stderr_array
[fileno (child_process
)], "r");
82 if (child_stderr
== NULL
)
83 printf (_("Could not open stderr for %s\n"), WHO_COMMAND
);
87 while (fgets (input_buffer
, MAX_INPUT_BUFFER
- 1, child_process
)) {
89 /* increment 'users' on all lines except total user count */
90 if (input_buffer
[0] != '#') {
95 /* get total logged in users */
96 if (sscanf (input_buffer
, _("# users=%d"), &users
) == 1)
102 if (fgets (input_buffer
, MAX_INPUT_BUFFER
- 1, child_stderr
))
103 result
= possibly_set (result
, STATE_UNKNOWN
);
104 (void) fclose (child_stderr
);
107 if (spclose (child_process
))
108 result
= possibly_set (result
, STATE_UNKNOWN
);
110 /* else check the user count against warning and critical thresholds */
112 result
= STATE_CRITICAL
;
113 else if (users
>= wusers
)
114 result
= STATE_WARNING
;
118 if (result
== STATE_UNKNOWN
)
119 printf ("%s\n", _("Unable to read output"));
121 asprintf(&perf
, "%s", perfdata ("users", users
, "",
126 printf (_("USERS %s - %d users currently logged in |%s\n"), state_text (result
),
135 /* process command-line arguments */
137 process_arguments (int argc
, char **argv
)
142 static struct option longopts
[] = {
143 {"critical", required_argument
, 0, 'c'},
144 {"warning", required_argument
, 0, 'w'},
145 {"version", no_argument
, 0, 'V'},
146 {"help", no_argument
, 0, 'h'},
154 c
= getopt_long (argc
, argv
, "+hVvc:w:", longopts
, &option
);
156 if (c
== -1 || c
== EOF
|| c
== 1)
160 case '?': /* print short usage statement if args not parsable */
165 case 'V': /* version */
166 print_revision (progname
, revision
);
168 case 'c': /* critical */
169 if (!is_intnonneg (optarg
))
170 usage4 (_("Critical threshold must be a positive integer"));
172 cusers
= atoi (optarg
);
174 case 'w': /* warning */
175 if (!is_intnonneg (optarg
))
176 usage4 (_("Warning threshold must be a positive integer"));
178 wusers
= atoi (optarg
);
184 if (wusers
== -1 && argc
> c
) {
185 if (is_intnonneg (argv
[c
]) == FALSE
)
186 usage4 (_("Warning threshold must be a positive integer"));
188 wusers
= atoi (argv
[c
++]);
191 if (cusers
== -1 && argc
> c
) {
192 if (is_intnonneg (argv
[c
]) == FALSE
)
193 usage4 (_("Warning threshold must be a positive integer"));
195 cusers
= atoi (argv
[c
]);
206 print_revision (progname
, revision
);
208 printf ("Copyright (c) 1999 Ethan Galstad\n");
209 printf (COPYRIGHT
, copyright
, email
);
211 printf ("%s\n", _("This plugin checks the number of users currently logged in on the local"));
212 printf ("%s\n", _("system and generates an error if the number exceeds the thresholds specified."));
218 printf (_(UT_HELP_VRSN
));
219 printf (_(UT_EXTRA_OPTS
));
221 printf (" %s\n", "-w, --warning=INTEGER");
222 printf (" %s\n", _("Set WARNING status if more than INTEGER users are logged in"));
223 printf (" %s\n", "-c, --critical=INTEGER");
224 printf (" %s\n", _("Set CRITICAL status if more than INTEGER users are logged in"));
228 printf ("%s\n", _("Notes:"));
229 printf (_(UT_EXTRA_OPTS_NOTES
));
232 printf (_(UT_SUPPORT
));
239 printf (_("Usage:"));
240 printf ("%s -w <users> -c <users>\n", progname
);