Code

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