Code

updating help and usage and license
[nagiosplug.git] / plugins / check_users.c
1 /******************************************************************************
2 *
3 * Nagios check_users plugin
4 *
5 * License: GPL
6 * Copyright (c) 2002-2006 nagios-plugins team
7 *
8 * Last Modified: $Date$
9 *
10 * Description:
11 *
12 * This file contains the check_users plugin
13 *
14 * License Information:
15 *
16 * This program is free software; you can redistribute it and/or modify
17 * it under the terms of the GNU General Public License as published by
18 * the Free Software Foundation; either version 2 of the License, or
19 * (at your option) any later version.
20 *
21 * This program is distributed in the hope that it will be useful,
22 * but WITHOUT ANY WARRANTY; without even the implied warranty of
23 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
24 * GNU General Public License for more details.
25 *
26 * You should have received a copy of the GNU General Public License
27 * along with this program; if not, write to the Free Software
28 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
29 *
30 *  $Id$
31 *
32 *****************************************************************************/
34 const char *progname = "check_users";
35 const char *revision = "$Revision$";
36 const char *copyright = "2000-2006";
37 const char *email = "nagiosplug-devel@lists.sourceforge.net";
39 #include "common.h"
40 #include "popen.h"
41 #include "utils.h"
43 #define possibly_set(a,b) ((a) == 0 ? (b) : 0)
45 int process_arguments (int, char **);
46 void print_help (void);
47 void print_usage (void);
49 int wusers = -1;
50 int cusers = -1;
52 int
53 main (int argc, char **argv)
54 {
55         int users = -1;
56         int result = STATE_UNKNOWN;
57         char input_buffer[MAX_INPUT_BUFFER];
58         char *perf;
60         setlocale (LC_ALL, "");
61         bindtextdomain (PACKAGE, LOCALEDIR);
62         textdomain (PACKAGE);
64         perf = strdup("");
66         if (process_arguments (argc, argv) == ERROR)
67                 usage4 (_("Could not parse arguments"));
69         /* run the command */
70         child_process = spopen (WHO_COMMAND);
71         if (child_process == NULL) {
72                 printf (_("Could not open pipe: %s\n"), WHO_COMMAND);
73                 return STATE_UNKNOWN;
74         }
76         child_stderr = fdopen (child_stderr_array[fileno (child_process)], "r");
77         if (child_stderr == NULL)
78                 printf (_("Could not open stderr for %s\n"), WHO_COMMAND);
80         users = 0;
82         while (fgets (input_buffer, MAX_INPUT_BUFFER - 1, child_process)) {
84                 /* increment 'users' on all lines except total user count */
85                 if (input_buffer[0] != '#') {
86                         users++;
87                         continue;
88                 }
90                 /* get total logged in users */
91                 if (sscanf (input_buffer, _("# users=%d"), &users) == 1)
92                         break;
94         }
96         /* check STDERR */
97         if (fgets (input_buffer, MAX_INPUT_BUFFER - 1, child_stderr))
98                 result = possibly_set (result, STATE_UNKNOWN);
99         (void) fclose (child_stderr);
101         /* close the pipe */
102         if (spclose (child_process))
103                 result = possibly_set (result, STATE_UNKNOWN);
105         /* else check the user count against warning and critical thresholds */
106         if (users >= cusers)
107                 result = STATE_CRITICAL;
108         else if (users >= wusers)
109                 result = STATE_WARNING;
110         else if (users >= 0)
111                 result = STATE_OK;
113         if (result == STATE_UNKNOWN)
114                 printf ("%s\n", _("Unable to read output"));
115         else {
116                 asprintf(&perf, "%s", perfdata ("users", users, "",
117                   TRUE, wusers,
118                   TRUE, cusers,
119                   TRUE, 0,
120                   FALSE, 0));
121                 printf (_("USERS %s - %d users currently logged in |%s\n"), state_text (result),
122                   users, perf);
123         }
125         return result;
130 /* process command-line arguments */
131 int
132 process_arguments (int argc, char **argv)
134         int c;
136         int option = 0;
137         static struct option longopts[] = {
138                 {"critical", required_argument, 0, 'c'},
139                 {"warning", required_argument, 0, 'w'},
140                 {"version", no_argument, 0, 'V'},
141                 {"help", no_argument, 0, 'h'},
142                 {0, 0, 0, 0}
143         };
145         if (argc < 2)
146                 usage ("\n");
148         while (1) {
149                 c = getopt_long (argc, argv, "+hVvc:w:", longopts, &option);
151                 if (c == -1 || c == EOF || c == 1)
152                         break;
154                 switch (c) {
155                 case '?':                                                                       /* print short usage statement if args not parsable */
156                         usage2 (_("Unknown argument"), optarg);
157                 case 'h':                                                                       /* help */
158                         print_help ();
159                         exit (STATE_OK);
160                 case 'V':                                                                       /* version */
161                         print_revision (progname, revision);
162                         exit (STATE_OK);
163                 case 'c':                                                                       /* critical */
164                         if (!is_intnonneg (optarg))
165                                 usage4 (_("Critical threshold must be a positive integer"));
166                         else
167                                 cusers = atoi (optarg);
168                         break;
169                 case 'w':                                                                       /* warning */
170                         if (!is_intnonneg (optarg))
171                                 usage4 (_("Warning threshold must be a positive integer"));
172                         else
173                                 wusers = atoi (optarg);
174                         break;
175                 }
176         }
178         c = optind;
179         if (wusers == -1 && argc > c) {
180                 if (is_intnonneg (argv[c]) == FALSE)
181                         usage4 (_("Warning threshold must be a positive integer"));
182                 else
183                         wusers = atoi (argv[c++]);
184         }
186         if (cusers == -1 && argc > c) {
187                 if (is_intnonneg (argv[c]) == FALSE)
188                         usage4 (_("Warning threshold must be a positive integer"));
189                 else
190                         cusers = atoi (argv[c]);
191         }
193         return OK;
198 void
199 print_help (void)
201         print_revision (progname, revision);
203         printf ("Copyright (c) 1999 Ethan Galstad\n");
204         printf (COPYRIGHT, copyright, email);
206         printf ("%s\n", _("This plugin checks the number of users currently logged in on the local"));
207   printf ("%s\n", _("system and generates an error if the number exceeds the thresholds specified."));
209   printf ("\n\n");
211         print_usage ();
213         printf (_(UT_HELP_VRSN));
215         printf (" %s\n", "-w, --warning=INTEGER");
216   printf ("    %s\n", _("Set WARNING status if more than INTEGER users are logged in"));
217   printf (" %s\n", "-c, --critical=INTEGER");
218   printf ("    %s\n", _("Set CRITICAL status if more than INTEGER users are logged in"));
220         printf (_(UT_SUPPORT));
224 void
225 print_usage (void)
227   printf (_("Usage:"));
228         printf ("%s -w <users> -c <users>\n", progname);