Code

semicolon needed where praogname define was replced
[nagiosplug.git] / plugins / check_users.c
1 /******************************************************************************
2  *
3  * CHECK_USERS.C
4  *
5  * Program: Current users plugin for Nagios
6  * License: GPL
7  * Copyright (c) 1999 Ethan Galstad (nagios@nagios.org)
8  *
9  * Last Modified: $Date$
10  * Modifications: 
11  *
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
17  *
18  * Command line: CHECK_USERS <wusers> <cusers>
19  *
20  * Description:
21  *
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.
28  *
29  * License Information:
30  *
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.
35  *
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.
40  *
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.
44  *
45  *****************************************************************************/
47 #include "common.h"
48 #include "popen.h"
49 #include "utils.h"
51 const char *progname = "check_users";
52 #define REVISION "$Revision$"
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);
63 int wusers = -1;
64 int cusers = -1;
66 int
67 main (int argc, char **argv)
68 {
69         int users = -1;
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");
76         /* run the command */
77         child_process = spopen (WHO_COMMAND);
78         if (child_process == NULL) {
79                 printf ("Could not open pipe: %s\n", WHO_COMMAND);
80                 return STATE_UNKNOWN;
81         }
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);
87         users = 0;
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] != '#') {
93                         users++;
94                         continue;
95                 }
97                 /* get total logged in users */
98                 if (sscanf (input_buffer, "# users=%d", &users) == 1)
99                         break;
101         }
103         /* check STDERR */
104         if (fgets (input_buffer, MAX_INPUT_BUFFER - 1, child_stderr))
105                 result = possibly_set (result, STATE_UNKNOWN);
106         (void) fclose (child_stderr);
108         /* close the pipe */
109         if (spclose (child_process))
110                 result = possibly_set (result, STATE_UNKNOWN);
112         /* else check the user count against warning and critical thresholds */
113         if (users >= cusers)
114                 result = STATE_CRITICAL;
115         else if (users >= wusers)
116                 result = STATE_WARNING;
117         else if (users >= 0)
118                 result = STATE_OK;
120         if (result == STATE_UNKNOWN)
121                 printf ("Unable to read output\n");
122         else
123                 printf ("USERS %s - %d users currently logged in\n", state_text (result),
124                                                 users);
126         return result;
133 /* process command-line arguments */
134 int
135 process_arguments (int argc, char **argv)
137         int c;
139 #ifdef HAVE_GETOPT_H
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'},
146                 {0, 0, 0, 0}
147         };
148 #endif
150         if (argc < 2)
151                 usage ("\n");
153         while (1) {
154 #ifdef HAVE_GETOPT_H
155                 c = getopt_long (argc, argv, "+hVvc:w:", long_options, &option_index);
156 #else
157                 c = getopt (argc, argv, "+hVvc:w:");
158 #endif
160                 if (c == -1 || c == EOF || c == 1)
161                         break;
163                 switch (c) {
164                 case '?':                                                                       /* print short usage statement if args not parsable */
165                         printf ("%s: Unknown argument: %s\n\n", progname, optarg);
166                         print_usage ();
167                         exit (STATE_UNKNOWN);
168                 case 'h':                                                                       /* help */
169                         print_help ();
170                         exit (STATE_OK);
171                 case 'V':                                                                       /* version */
172                         print_revision (progname, REVISION);
173                         exit (STATE_OK);
174                 case 'c':                                                                       /* critical */
175                         if (!is_intnonneg (optarg))
176                                 usage ("Critical threshold must be a nonnegative integer\n");
177                         cusers = atoi (optarg);
178                         break;
179                 case 'w':                                                                       /* warning */
180                         if (!is_intnonneg (optarg))
181                                 usage ("Warning threshold must be a nonnegative integer\n");
182                         wusers = atoi (optarg);
183                         break;
184                 }
185         }
187         c = optind;
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++]);
192         }
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]);
198         }
200         return OK;
207 void
208 print_usage (void)
210         printf ("Usage: %s -w <users> -c <users>\n", progname);
217 void
218 print_help (void)
220         print_revision (progname, REVISION);
221         printf
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");
225         print_usage ();
226         printf
227                 ("Options:\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"
232                  " -h, --help\n"
233                  "    Print detailed help screen\n"
234                  " -V, --version\n" "    Print version information\n");