1 /*****************************************************************************
3 This program is free software; you can redistribute it and/or modify
4 it under the terms of the GNU General Public License as published by
5 the Free Software Foundation; either version 2 of the License, or
6 (at your option) any later version.
8 This program is distributed in the hope that it will be useful,
9 but WITHOUT ANY WARRANTY; without even the implied warranty of
10 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11 GNU General Public License for more details.
13 You should have received a copy of the GNU General Public License
14 along with this program; if not, write to the Free Software
15 Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
17 *****************************************************************************/
19 const char *progname = "check_users";
20 const char *revision = "$Revision$";
21 const char *copyright = "2000-2003";
22 const char *email = "nagiosplug-devel@lists.sourceforge.net";
24 #include "common.h"
25 #include "popen.h"
26 #include "utils.h"
28 #define possibly_set(a,b) ((a) == 0 ? (b) : 0)
30 int process_arguments (int, char **);
31 void print_help (void);
32 void print_usage (void);
34 int wusers = -1;
35 int cusers = -1;
37 int
38 main (int argc, char **argv)
39 {
40 int users = -1;
41 int result = STATE_OK;
42 char input_buffer[MAX_INPUT_BUFFER];
43 char *perf;
45 setlocale (LC_ALL, "");
46 bindtextdomain (PACKAGE, LOCALEDIR);
47 textdomain (PACKAGE);
49 perf = strdup("");
51 if (process_arguments (argc, argv) == ERROR)
52 usage (_("Could not parse arguments\n"));
54 /* run the command */
55 child_process = spopen (WHO_COMMAND);
56 if (child_process == NULL) {
57 printf (_("Could not open pipe: %s\n"), WHO_COMMAND);
58 return STATE_UNKNOWN;
59 }
61 child_stderr = fdopen (child_stderr_array[fileno (child_process)], "r");
62 if (child_stderr == NULL)
63 printf (_("Could not open stderr for %s\n"), WHO_COMMAND);
65 users = 0;
67 while (fgets (input_buffer, MAX_INPUT_BUFFER - 1, child_process)) {
69 /* increment 'users' on all lines except total user count */
70 if (input_buffer[0] != '#') {
71 users++;
72 continue;
73 }
75 /* get total logged in users */
76 if (sscanf (input_buffer, _("# users=%d"), &users) == 1)
77 break;
79 }
81 /* check STDERR */
82 if (fgets (input_buffer, MAX_INPUT_BUFFER - 1, child_stderr))
83 result = possibly_set (result, STATE_UNKNOWN);
84 (void) fclose (child_stderr);
86 /* close the pipe */
87 if (spclose (child_process))
88 result = possibly_set (result, STATE_UNKNOWN);
90 /* else check the user count against warning and critical thresholds */
91 if (users >= cusers)
92 result = STATE_CRITICAL;
93 else if (users >= wusers)
94 result = STATE_WARNING;
95 else if (users >= 0)
96 result = STATE_OK;
98 if (result == STATE_UNKNOWN)
99 printf (_("Unable to read output\n"));
100 else {
101 asprintf(&perf, "%s", perfdata ("users", users, "",
102 TRUE, wusers,
103 TRUE, cusers,
104 TRUE, 0,
105 FALSE, 0));
106 printf (_("USERS %s - %d users currently logged in |%s\n"), state_text (result),
107 users, perf);
108 }
110 return result;
111 }
117 /* process command-line arguments */
118 int
119 process_arguments (int argc, char **argv)
120 {
121 int c;
123 int option = 0;
124 static struct option longopts[] = {
125 {"critical", required_argument, 0, 'c'},
126 {"warning", required_argument, 0, 'w'},
127 {"version", no_argument, 0, 'V'},
128 {"help", no_argument, 0, 'h'},
129 {0, 0, 0, 0}
130 };
132 if (argc < 2)
133 usage ("\n");
135 while (1) {
136 c = getopt_long (argc, argv, "+hVvc:w:", longopts, &option);
138 if (c == -1 || c == EOF || c == 1)
139 break;
141 switch (c) {
142 case '?': /* print short usage statement if args not parsable */
143 printf (_("%s: Unknown argument: %s\n\n"), progname, optarg);
144 print_usage ();
145 exit (STATE_UNKNOWN);
146 case 'h': /* help */
147 print_help ();
148 exit (STATE_OK);
149 case 'V': /* version */
150 print_revision (progname, revision);
151 exit (STATE_OK);
152 case 'c': /* critical */
153 if (!is_intnonneg (optarg))
154 usage (_("Critical threshold must be a nonnegative integer\n"));
155 else
156 cusers = atoi (optarg);
157 break;
158 case 'w': /* warning */
159 if (!is_intnonneg (optarg))
160 usage (_("Warning threshold must be a nonnegative integer\n"));
161 else
162 wusers = atoi (optarg);
163 break;
164 }
165 }
167 c = optind;
168 if (wusers == -1 && argc > c) {
169 if (is_intnonneg (argv[c]) == FALSE)
170 usage (_("Warning threshold must be a nonnegative integer\n"));
171 else
172 wusers = atoi (argv[c++]);
173 }
175 if (cusers == -1 && argc > c) {
176 if (is_intnonneg (argv[c]) == FALSE)
177 usage (_("Warning threshold must be a nonnegative integer\n"));
178 else
179 cusers = atoi (argv[c]);
180 }
182 return OK;
183 }
189 \f
190 void
191 print_help (void)
192 {
193 print_revision (progname, revision);
195 printf (_("Copyright (c) 1999 Ethan Galstad\n"));
196 printf (_(COPYRIGHT), copyright, email);
198 printf (_("\
199 This plugin checks the number of users currently logged in on the local\n\
200 system and generates an error if the number exceeds the thresholds specified.\n"));
202 print_usage ();
204 printf (_(UT_HELP_VRSN));
206 printf (_("\
207 -w, --warning=INTEGER\n\
208 Set WARNING status if more than INTEGER users are logged in\n\
209 -c, --critical=INTEGER\n\
210 Set CRITICAL status if more than INTEGER users are logged in\n"));
212 printf (_(UT_SUPPORT));
213 }
218 void
219 print_usage (void)
220 {
221 printf ("Usage: %s -w <users> -c <users>\n", progname);
222 printf (_(UT_HLP_VRS), progname, progname);
223 }