Not picking up solaris systems correctly for check_dhcp. Only
[monitoring-plugins.git] / plugins / check_users.c
blobe4ef681f6075227e38d2030692a43debddb1b9f8
1 /*****************************************************************************
2 *
3 * Nagios check_users plugin
4 *
5 * License: GPL
6 * Copyright (c) 2000-2007 Nagios Plugins Development Team
7 *
8 * Last Modified: $Date$
9 *
10 * Description:
12 * This file contains the check_users plugin
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
16 * specified.
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 3 of the License, or
22 * (at your option) any later version.
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.
29 * You should have received a copy of the GNU General Public License
30 * along with this program. If not, see <http://www.gnu.org/licenses/>.
32 * $Id$
34 *****************************************************************************/
36 const char *progname = "check_users";
37 const char *revision = "$Revision$";
38 const char *copyright = "2000-2007";
39 const char *email = "nagiosplug-devel@lists.sourceforge.net";
41 #include "common.h"
42 #include "popen.h"
43 #include "utils.h"
45 #define possibly_set(a,b) ((a) == 0 ? (b) : 0)
47 int process_arguments (int, char **);
48 void print_help (void);
49 void print_usage (void);
51 int wusers = -1;
52 int cusers = -1;
54 int
55 main (int argc, char **argv)
57 int users = -1;
58 int result = STATE_UNKNOWN;
59 char input_buffer[MAX_INPUT_BUFFER];
60 char *perf;
62 setlocale (LC_ALL, "");
63 bindtextdomain (PACKAGE, LOCALEDIR);
64 textdomain (PACKAGE);
66 perf = strdup("");
68 /* Parse extra opts if any */
69 argv=np_extra_opts (&argc, argv, progname);
71 if (process_arguments (argc, argv) == ERROR)
72 usage4 (_("Could not parse arguments"));
74 /* run the command */
75 child_process = spopen (WHO_COMMAND);
76 if (child_process == NULL) {
77 printf (_("Could not open pipe: %s\n"), WHO_COMMAND);
78 return STATE_UNKNOWN;
81 child_stderr = fdopen (child_stderr_array[fileno (child_process)], "r");
82 if (child_stderr == NULL)
83 printf (_("Could not open stderr for %s\n"), WHO_COMMAND);
85 users = 0;
87 while (fgets (input_buffer, MAX_INPUT_BUFFER - 1, child_process)) {
89 /* increment 'users' on all lines except total user count */
90 if (input_buffer[0] != '#') {
91 users++;
92 continue;
95 /* get total logged in users */
96 if (sscanf (input_buffer, _("# users=%d"), &users) == 1)
97 break;
101 /* check STDERR */
102 if (fgets (input_buffer, MAX_INPUT_BUFFER - 1, child_stderr))
103 result = possibly_set (result, STATE_UNKNOWN);
104 (void) fclose (child_stderr);
106 /* close the pipe */
107 if (spclose (child_process))
108 result = possibly_set (result, STATE_UNKNOWN);
110 /* else check the user count against warning and critical thresholds */
111 if (users >= cusers)
112 result = STATE_CRITICAL;
113 else if (users >= wusers)
114 result = STATE_WARNING;
115 else if (users >= 0)
116 result = STATE_OK;
118 if (result == STATE_UNKNOWN)
119 printf ("%s\n", _("Unable to read output"));
120 else {
121 asprintf(&perf, "%s", perfdata ("users", users, "",
122 TRUE, wusers,
123 TRUE, cusers,
124 TRUE, 0,
125 FALSE, 0));
126 printf (_("USERS %s - %d users currently logged in |%s\n"), state_text (result),
127 users, perf);
130 return result;
135 /* process command-line arguments */
137 process_arguments (int argc, char **argv)
139 int c;
141 int option = 0;
142 static struct option longopts[] = {
143 {"critical", required_argument, 0, 'c'},
144 {"warning", required_argument, 0, 'w'},
145 {"version", no_argument, 0, 'V'},
146 {"help", no_argument, 0, 'h'},
147 {0, 0, 0, 0}
150 if (argc < 2)
151 usage ("\n");
153 while (1) {
154 c = getopt_long (argc, argv, "+hVvc:w:", longopts, &option);
156 if (c == -1 || c == EOF || c == 1)
157 break;
159 switch (c) {
160 case '?': /* print short usage statement if args not parsable */
161 usage5 ();
162 case 'h': /* help */
163 print_help ();
164 exit (STATE_OK);
165 case 'V': /* version */
166 print_revision (progname, revision);
167 exit (STATE_OK);
168 case 'c': /* critical */
169 if (!is_intnonneg (optarg))
170 usage4 (_("Critical threshold must be a positive integer"));
171 else
172 cusers = atoi (optarg);
173 break;
174 case 'w': /* warning */
175 if (!is_intnonneg (optarg))
176 usage4 (_("Warning threshold must be a positive integer"));
177 else
178 wusers = atoi (optarg);
179 break;
183 c = optind;
184 if (wusers == -1 && argc > c) {
185 if (is_intnonneg (argv[c]) == FALSE)
186 usage4 (_("Warning threshold must be a positive integer"));
187 else
188 wusers = atoi (argv[c++]);
191 if (cusers == -1 && argc > c) {
192 if (is_intnonneg (argv[c]) == FALSE)
193 usage4 (_("Warning threshold must be a positive integer"));
194 else
195 cusers = atoi (argv[c]);
198 return OK;
203 void
204 print_help (void)
206 print_revision (progname, revision);
208 printf ("Copyright (c) 1999 Ethan Galstad\n");
209 printf (COPYRIGHT, copyright, email);
211 printf ("%s\n", _("This plugin checks the number of users currently logged in on the local"));
212 printf ("%s\n", _("system and generates an error if the number exceeds the thresholds specified."));
214 printf ("\n\n");
216 print_usage ();
218 printf (_(UT_HELP_VRSN));
219 printf (_(UT_EXTRA_OPTS));
221 printf (" %s\n", "-w, --warning=INTEGER");
222 printf (" %s\n", _("Set WARNING status if more than INTEGER users are logged in"));
223 printf (" %s\n", "-c, --critical=INTEGER");
224 printf (" %s\n", _("Set CRITICAL status if more than INTEGER users are logged in"));
226 #ifdef NP_EXTRA_OPTS
227 printf ("\n");
228 printf ("%s\n", _("Notes:"));
229 printf (_(UT_EXTRA_OPTS_NOTES));
230 #endif
232 printf (_(UT_SUPPORT));
236 void
237 print_usage (void)
239 printf (_("Usage:"));
240 printf ("%s -w <users> -c <users>\n", progname);