Code

Fixed bug with malloc of wrong size
[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-2004";
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_UNKNOWN;
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) == ERROR)
54                 usage4 (_("Could not parse arguments"));
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                         usage2 (_("Unknown argument"), optarg);
144                 case 'h':                                                                       /* help */
145                         print_help ();
146                         exit (STATE_OK);
147                 case 'V':                                                                       /* version */
148                         print_revision (progname, revision);
149                         exit (STATE_OK);
150                 case 'c':                                                                       /* critical */
151                         if (!is_intnonneg (optarg))
152                                 usage4 (_("Critical threshold must be a positive integer"));
153                         else
154                                 cusers = atoi (optarg);
155                         break;
156                 case 'w':                                                                       /* warning */
157                         if (!is_intnonneg (optarg))
158                                 usage4 (_("Warning threshold must be a positive integer"));
159                         else
160                                 wusers = atoi (optarg);
161                         break;
162                 }
163         }
165         c = optind;
166         if (wusers == -1 && argc > c) {
167                 if (is_intnonneg (argv[c]) == FALSE)
168                         usage4 (_("Warning threshold must be a positive integer"));
169                 else
170                         wusers = atoi (argv[c++]);
171         }
173         if (cusers == -1 && argc > c) {
174                 if (is_intnonneg (argv[c]) == FALSE)
175                         usage4 (_("Warning threshold must be a positive integer"));
176                 else
177                         cusers = atoi (argv[c]);
178         }
180         return OK;
185 void
186 print_help (void)
188         print_revision (progname, revision);
190         printf ("Copyright (c) 1999 Ethan Galstad\n");
191         printf (COPYRIGHT, copyright, email);
193         printf (_("\
194 This plugin checks the number of users currently logged in on the local\n\
195 system and generates an error if the number exceeds the thresholds specified.\n"));
197         print_usage ();
199         printf (_(UT_HELP_VRSN));
201         printf (_("\
202  -w, --warning=INTEGER\n\
203     Set WARNING status if more than INTEGER users are logged in\n\
204  -c, --critical=INTEGER\n\
205     Set CRITICAL status if more than INTEGER users are logged in\n"));
207         printf (_(UT_SUPPORT));
211 void
212 print_usage (void)
214         printf ("Usage: %s -w <users> -c <users>\n", progname);