Code

Fix for config.h at top level. Required for intl/
[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         setlocale (LC_ALL, "");
45         bindtextdomain (PACKAGE, LOCALEDIR);
46         textdomain (PACKAGE);
48         if (process_arguments (argc, argv) == ERROR)
49                 usage (_("Could not parse arguments\n"));
51         /* run the command */
52         child_process = spopen (WHO_COMMAND);
53         if (child_process == NULL) {
54                 printf (_("Could not open pipe: %s\n"), WHO_COMMAND);
55                 return STATE_UNKNOWN;
56         }
58         child_stderr = fdopen (child_stderr_array[fileno (child_process)], "r");
59         if (child_stderr == NULL)
60                 printf (_("Could not open stderr for %s\n"), WHO_COMMAND);
62         users = 0;
64         while (fgets (input_buffer, MAX_INPUT_BUFFER - 1, child_process)) {
66                 /* increment 'users' on all lines except total user count */
67                 if (input_buffer[0] != '#') {
68                         users++;
69                         continue;
70                 }
72                 /* get total logged in users */
73                 if (sscanf (input_buffer, _("# users=%d"), &users) == 1)
74                         break;
76         }
78         /* check STDERR */
79         if (fgets (input_buffer, MAX_INPUT_BUFFER - 1, child_stderr))
80                 result = possibly_set (result, STATE_UNKNOWN);
81         (void) fclose (child_stderr);
83         /* close the pipe */
84         if (spclose (child_process))
85                 result = possibly_set (result, STATE_UNKNOWN);
87         /* else check the user count against warning and critical thresholds */
88         if (users >= cusers)
89                 result = STATE_CRITICAL;
90         else if (users >= wusers)
91                 result = STATE_WARNING;
92         else if (users >= 0)
93                 result = STATE_OK;
95         if (result == STATE_UNKNOWN)
96                 printf (_("Unable to read output\n"));
97         else
98                 printf (_("USERS %s - %d users currently logged in\n"), state_text (result),
99                                                 users);
101         return result;
108 /* process command-line arguments */
109 int
110 process_arguments (int argc, char **argv)
112         int c;
114         int option = 0;
115         static struct option longopts[] = {
116                 {"critical", required_argument, 0, 'c'},
117                 {"warning", required_argument, 0, 'w'},
118                 {"version", no_argument, 0, 'V'},
119                 {"help", no_argument, 0, 'h'},
120                 {0, 0, 0, 0}
121         };
123         if (argc < 2)
124                 usage ("\n");
126         while (1) {
127                 c = getopt_long (argc, argv, "+hVvc:w:", longopts, &option);
129                 if (c == -1 || c == EOF || c == 1)
130                         break;
132                 switch (c) {
133                 case '?':                                                                       /* print short usage statement if args not parsable */
134                         printf (_("%s: Unknown argument: %s\n\n"), progname, optarg);
135                         print_usage ();
136                         exit (STATE_UNKNOWN);
137                 case 'h':                                                                       /* help */
138                         print_help ();
139                         exit (STATE_OK);
140                 case 'V':                                                                       /* version */
141                         print_revision (progname, revision);
142                         exit (STATE_OK);
143                 case 'c':                                                                       /* critical */
144                         if (!is_intnonneg (optarg))
145                                 usage (_("Critical threshold must be a nonnegative integer\n"));
146                         else
147                                 cusers = atoi (optarg);
148                         break;
149                 case 'w':                                                                       /* warning */
150                         if (!is_intnonneg (optarg))
151                                 usage (_("Warning threshold must be a nonnegative integer\n"));
152                         else
153                                 wusers = atoi (optarg);
154                         break;
155                 }
156         }
158         c = optind;
159         if (wusers == -1 && argc > c) {
160                 if (is_intnonneg (argv[c]) == FALSE)
161                         usage (_("Warning threshold must be a nonnegative integer\n"));
162                 else
163                         wusers = atoi (argv[c++]);
164         }
166         if (cusers == -1 && argc > c) {
167                 if (is_intnonneg (argv[c]) == FALSE)
168                         usage (_("Warning threshold must be a nonnegative integer\n"));
169                 else
170                         cusers = atoi (argv[c]);
171         }
173         return OK;
180 \f
181 void
182 print_help (void)
184         print_revision (progname, revision);
186         printf (_("Copyright (c) 1999 Ethan Galstad\n"));
187         printf (_(COPYRIGHT), copyright, email);
189         printf (_("\
190 This plugin checks the number of users currently logged in on the local\n\
191 system and generates an error if the number exceeds the thresholds specified.\n"));
193         print_usage ();
195         printf (_(UT_HELP_VRSN));
197         printf (_("\
198  -w, --warning=INTEGER\n\
199     Set WARNING status if more than INTEGER users are logged in\n\
200  -c, --critical=INTEGER\n\
201     Set CRITICAL status if more than INTEGER users are logged in\n"));
203         printf (_(UT_SUPPORT));
209 void
210 print_usage (void)
212         printf ("Usage: %s -w <users> -c <users>\n", progname);
213         printf (_(UT_HLP_VRS), progname, progname);