Code

more pedantic compiler warnings
[nagiosplug.git] / plugins / check_users.c
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];
44         if (process_arguments (argc, argv) == ERROR)
45                 usage (_("Could not parse arguments\n"));
47         /* run the command */
48         child_process = spopen (WHO_COMMAND);
49         if (child_process == NULL) {
50                 printf (_("Could not open pipe: %s\n"), WHO_COMMAND);
51                 return STATE_UNKNOWN;
52         }
54         child_stderr = fdopen (child_stderr_array[fileno (child_process)], "r");
55         if (child_stderr == NULL)
56                 printf (_("Could not open stderr for %s\n"), WHO_COMMAND);
58         users = 0;
60         while (fgets (input_buffer, MAX_INPUT_BUFFER - 1, child_process)) {
62                 /* increment 'users' on all lines except total user count */
63                 if (input_buffer[0] != '#') {
64                         users++;
65                         continue;
66                 }
68                 /* get total logged in users */
69                 if (sscanf (input_buffer, _("# users=%d"), &users) == 1)
70                         break;
72         }
74         /* check STDERR */
75         if (fgets (input_buffer, MAX_INPUT_BUFFER - 1, child_stderr))
76                 result = possibly_set (result, STATE_UNKNOWN);
77         (void) fclose (child_stderr);
79         /* close the pipe */
80         if (spclose (child_process))
81                 result = possibly_set (result, STATE_UNKNOWN);
83         /* else check the user count against warning and critical thresholds */
84         if (users >= cusers)
85                 result = STATE_CRITICAL;
86         else if (users >= wusers)
87                 result = STATE_WARNING;
88         else if (users >= 0)
89                 result = STATE_OK;
91         if (result == STATE_UNKNOWN)
92                 printf (_("Unable to read output\n"));
93         else
94                 printf (_("USERS %s - %d users currently logged in\n"), state_text (result),
95                                                 users);
97         return result;
98 }
104 /* process command-line arguments */
105 int
106 process_arguments (int argc, char **argv)
108         int c;
110         int option_index = 0;
111         static struct option long_options[] = {
112                 {"critical", required_argument, 0, 'c'},
113                 {"warning", required_argument, 0, 'w'},
114                 {"version", no_argument, 0, 'V'},
115                 {"help", no_argument, 0, 'h'},
116                 {0, 0, 0, 0}
117         };
119         if (argc < 2)
120                 usage ("\n");
122         while (1) {
123                 c = getopt_long (argc, argv, "+hVvc:w:", long_options, &option_index);
125                 if (c == -1 || c == EOF || c == 1)
126                         break;
128                 switch (c) {
129                 case '?':                                                                       /* print short usage statement if args not parsable */
130                         printf (_("%s: Unknown argument: %s\n\n"), progname, optarg);
131                         print_usage ();
132                         exit (STATE_UNKNOWN);
133                 case 'h':                                                                       /* help */
134                         print_help ();
135                         exit (STATE_OK);
136                 case 'V':                                                                       /* version */
137                         print_revision (progname, revision);
138                         exit (STATE_OK);
139                 case 'c':                                                                       /* critical */
140                         if (!is_intnonneg (optarg))
141                                 usage (_("Critical threshold must be a nonnegative integer\n"));
142                         else
143                                 cusers = atoi (optarg);
144                         break;
145                 case 'w':                                                                       /* warning */
146                         if (!is_intnonneg (optarg))
147                                 usage (_("Warning threshold must be a nonnegative integer\n"));
148                         else
149                                 wusers = atoi (optarg);
150                         break;
151                 }
152         }
154         c = optind;
155         if (wusers == -1 && argc > c) {
156                 if (is_intnonneg (argv[c]) == FALSE)
157                         usage (_("Warning threshold must be a nonnegative integer\n"));
158                 else
159                         wusers = atoi (argv[c++]);
160         }
162         if (cusers == -1 && argc > c) {
163                 if (is_intnonneg (argv[c]) == FALSE)
164                         usage (_("Warning threshold must be a nonnegative integer\n"));
165                 else
166                         cusers = atoi (argv[c]);
167         }
169         return OK;
176 \f
177 void
178 print_help (void)
180         print_revision (progname, revision);
182         printf (_("Copyright (c) 1999 Ethan Galstad\n"));
183         printf (_(COPYRIGHT), copyright, email);
185         printf (_("\
186 This plugin checks the number of users currently logged in on the local\n\
187 system and generates an error if the number exceeds the thresholds specified.\n"));
189         print_usage ();
191         printf (_(UT_HELP_VRSN));
193         printf (_("\
194  -w, --warning=INTEGER\n\
195     Set WARNING status if more than INTEGER users are logged in\n\
196  -c, --critical=INTEGER\n\
197     Set CRITICAL status if more than INTEGER users are logged in\n"));
199         printf (_(UT_SUPPORT));
205 void
206 print_usage (void)
208         printf ("Usage: %s -w <users> -c <users>\n", progname);
209         printf (_(UT_HLP_VRS), progname, progname);