Code

return of process_arguments() is TRUE not OK !
[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  $Id$
18  
19 *****************************************************************************/
21 const char *progname = "check_users";
22 const char *revision = "$Revision$";
23 const char *copyright = "2000-2003";
24 const char *email = "nagiosplug-devel@lists.sourceforge.net";
26 #include "common.h"
27 #include "popen.h"
28 #include "utils.h"
30 #define possibly_set(a,b) ((a) == 0 ? (b) : 0)
32 int process_arguments (int, char **);
33 void print_help (void);
34 void print_usage (void);
36 int wusers = -1;
37 int cusers = -1;
39 int
40 main (int argc, char **argv)
41 {
42         int users = -1;
43         int result = STATE_OK;
44         char input_buffer[MAX_INPUT_BUFFER];
45         char *perf;
47         setlocale (LC_ALL, "");
48         bindtextdomain (PACKAGE, LOCALEDIR);
49         textdomain (PACKAGE);
51         perf = strdup("");
53         if (process_arguments (argc, argv) != TRUE)
54                 usage (_("check_users: could not parse arguments\n"));
56         /* run the command */
57         child_process = spopen (WHO_COMMAND);
58         if (child_process == NULL) {
59                 printf (_("Could not open pipe: %s\n"), WHO_COMMAND);
60                 return STATE_UNKNOWN;
61         }
63         child_stderr = fdopen (child_stderr_array[fileno (child_process)], "r");
64         if (child_stderr == NULL)
65                 printf (_("Could not open stderr for %s\n"), WHO_COMMAND);
67         users = 0;
69         while (fgets (input_buffer, MAX_INPUT_BUFFER - 1, child_process)) {
71                 /* increment 'users' on all lines except total user count */
72                 if (input_buffer[0] != '#') {
73                         users++;
74                         continue;
75                 }
77                 /* get total logged in users */
78                 if (sscanf (input_buffer, _("# users=%d"), &users) == 1)
79                         break;
81         }
83         /* check STDERR */
84         if (fgets (input_buffer, MAX_INPUT_BUFFER - 1, child_stderr))
85                 result = possibly_set (result, STATE_UNKNOWN);
86         (void) fclose (child_stderr);
88         /* close the pipe */
89         if (spclose (child_process))
90                 result = possibly_set (result, STATE_UNKNOWN);
92         /* else check the user count against warning and critical thresholds */
93         if (users >= cusers)
94                 result = STATE_CRITICAL;
95         else if (users >= wusers)
96                 result = STATE_WARNING;
97         else if (users >= 0)
98                 result = STATE_OK;
100         if (result == STATE_UNKNOWN)
101                 printf (_("Unable to read output\n"));
102         else {
103                 asprintf(&perf, "%s", perfdata ("users", users, "",
104                   TRUE, wusers,
105                   TRUE, cusers,
106                   TRUE, 0,
107                   FALSE, 0));
108                 printf (_("USERS %s - %d users currently logged in |%s\n"), state_text (result),
109                   users, perf);
110         }
112         return result;
117 /* process command-line arguments */
118 int
119 process_arguments (int argc, char **argv)
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 positive 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 positive 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 positive 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 positive integer\n"));
178                 else
179                         cusers = atoi (argv[c]);
180         }
182         return OK;
187 void
188 print_help (void)
190         print_revision (progname, revision);
192         printf ("Copyright (c) 1999 Ethan Galstad\n");
193         printf (COPYRIGHT, copyright, email);
195         printf (_("\
196 This plugin checks the number of users currently logged in on the local\n\
197 system and generates an error if the number exceeds the thresholds specified.\n"));
199         print_usage ();
201         printf (_(UT_HELP_VRSN));
203         printf (_("\
204  -w, --warning=INTEGER\n\
205     Set WARNING status if more than INTEGER users are logged in\n\
206  -c, --critical=INTEGER\n\
207     Set CRITICAL status if more than INTEGER users are logged in\n"));
209         printf (_(UT_SUPPORT));
214 void
215 print_usage (void)
217         printf ("Usage: %s -w <users> -c <users>\n", progname);
218         printf (_(UT_HLP_VRS), progname, progname);