Code

markup for translation
[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 void
29 print_usage (void)
30 {
31         printf ("Usage: %s -w <users> -c <users>\n", progname);
32         printf (_(UT_HLP_VRS), progname, progname);
33 }
35 void
36 print_help (void)
37 {
38         print_revision (progname, revision);
40         printf (_("Copyright (c) 1999 Ethan Galstad\n"));
41         printf (_(COPYRIGHT), copyright, email);
43         printf (_("\
44 This plugin checks the number of users currently logged in on the local\n\
45 system and generates an error if the number exceeds the thresholds specified.\n"));
47         print_usage ();
49         printf (_(UT_HELP_VRSN));
51         printf (_("\
52  -w, --warning=INTEGER\n\
53     Set WARNING status if more than INTEGER users are logged in\n\
54  -c, --critical=INTEGER\n\
55     Set CRITICAL status if more than INTEGER users are logged in\n"));
57         printf (_(UT_SUPPORT));
58 }
59 \f
60 #define possibly_set(a,b) ((a) == 0 ? (b) : 0)
62 int process_arguments (int, char **);
63 void print_usage (void);
64 void print_help (void);
66 int wusers = -1;
67 int cusers = -1;
69 int
70 main (int argc, char **argv)
71 {
72         int users = -1;
73         int result = STATE_OK;
74         char input_buffer[MAX_INPUT_BUFFER];
76         if (process_arguments (argc, argv) == ERROR)
77                 usage (_("Could not parse arguments\n"));
79         /* run the command */
80         child_process = spopen (WHO_COMMAND);
81         if (child_process == NULL) {
82                 printf (_("Could not open pipe: %s\n"), WHO_COMMAND);
83                 return STATE_UNKNOWN;
84         }
86         child_stderr = fdopen (child_stderr_array[fileno (child_process)], "r");
87         if (child_stderr == NULL)
88                 printf (_("Could not open stderr for %s\n"), WHO_COMMAND);
90         users = 0;
92         while (fgets (input_buffer, MAX_INPUT_BUFFER - 1, child_process)) {
94                 /* increment 'users' on all lines except total user count */
95                 if (input_buffer[0] != '#') {
96                         users++;
97                         continue;
98                 }
100                 /* get total logged in users */
101                 if (sscanf (input_buffer, _("# users=%d"), &users) == 1)
102                         break;
104         }
106         /* check STDERR */
107         if (fgets (input_buffer, MAX_INPUT_BUFFER - 1, child_stderr))
108                 result = possibly_set (result, STATE_UNKNOWN);
109         (void) fclose (child_stderr);
111         /* close the pipe */
112         if (spclose (child_process))
113                 result = possibly_set (result, STATE_UNKNOWN);
115         /* else check the user count against warning and critical thresholds */
116         if (users >= cusers)
117                 result = STATE_CRITICAL;
118         else if (users >= wusers)
119                 result = STATE_WARNING;
120         else if (users >= 0)
121                 result = STATE_OK;
123         if (result == STATE_UNKNOWN)
124                 printf (_("Unable to read output\n"));
125         else
126                 printf (_("USERS %s - %d users currently logged in\n"), state_text (result),
127                                                 users);
129         return result;
136 /* process command-line arguments */
137 int
138 process_arguments (int argc, char **argv)
140         int c;
142         int option_index = 0;
143         static struct option long_options[] = {
144                 {"critical", required_argument, 0, 'c'},
145                 {"warning", required_argument, 0, 'w'},
146                 {"version", no_argument, 0, 'V'},
147                 {"help", no_argument, 0, 'h'},
148                 {0, 0, 0, 0}
149         };
151         if (argc < 2)
152                 usage ("\n");
154         while (1) {
155                 c = getopt_long (argc, argv, "+hVvc:w:", long_options, &option_index);
157                 if (c == -1 || c == EOF || c == 1)
158                         break;
160                 switch (c) {
161                 case '?':                                                                       /* print short usage statement if args not parsable */
162                         printf (_("%s: Unknown argument: %s\n\n"), progname, optarg);
163                         print_usage ();
164                         exit (STATE_UNKNOWN);
165                 case 'h':                                                                       /* help */
166                         print_help ();
167                         exit (STATE_OK);
168                 case 'V':                                                                       /* version */
169                         print_revision (progname, revision);
170                         exit (STATE_OK);
171                 case 'c':                                                                       /* critical */
172                         if (!is_intnonneg (optarg))
173                                 usage (_("Critical threshold must be a nonnegative integer\n"));
174                         cusers = atoi (optarg);
175                         break;
176                 case 'w':                                                                       /* warning */
177                         if (!is_intnonneg (optarg))
178                                 usage (_("Warning threshold must be a nonnegative integer\n"));
179                         wusers = atoi (optarg);
180                         break;
181                 }
182         }
184         c = optind;
185         if (wusers == -1 && argc > c) {
186                 if (is_intnonneg (argv[c]) == FALSE)
187                         usage (_("Warning threshold must be a nonnegative integer\n"));
188                 wusers = atoi (argv[c++]);
189         }
191         if (cusers == -1 && argc > c) {
192                 if (is_intnonneg (argv[c]) == FALSE)
193                         usage (_("Warning threshold must be a nonnegative integer\n"));
194                 cusers = atoi (argv[c]);
195         }
197         return OK;