Reverted check_procs for solaris back to using pst3 due to truncation
[monitoring-plugins.git] / plugins / check_users.c
blobc81822bddd9328ec8365061a9946d5fcf70d13c8
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 if (process_arguments (argc, argv) == ERROR)
69 usage4 (_("Could not parse arguments"));
71 /* run the command */
72 child_process = spopen (WHO_COMMAND);
73 if (child_process == NULL) {
74 printf (_("Could not open pipe: %s\n"), WHO_COMMAND);
75 return STATE_UNKNOWN;
78 child_stderr = fdopen (child_stderr_array[fileno (child_process)], "r");
79 if (child_stderr == NULL)
80 printf (_("Could not open stderr for %s\n"), WHO_COMMAND);
82 users = 0;
84 while (fgets (input_buffer, MAX_INPUT_BUFFER - 1, child_process)) {
86 /* increment 'users' on all lines except total user count */
87 if (input_buffer[0] != '#') {
88 users++;
89 continue;
92 /* get total logged in users */
93 if (sscanf (input_buffer, _("# users=%d"), &users) == 1)
94 break;
98 /* check STDERR */
99 if (fgets (input_buffer, MAX_INPUT_BUFFER - 1, child_stderr))
100 result = possibly_set (result, STATE_UNKNOWN);
101 (void) fclose (child_stderr);
103 /* close the pipe */
104 if (spclose (child_process))
105 result = possibly_set (result, STATE_UNKNOWN);
107 /* else check the user count against warning and critical thresholds */
108 if (users >= cusers)
109 result = STATE_CRITICAL;
110 else if (users >= wusers)
111 result = STATE_WARNING;
112 else if (users >= 0)
113 result = STATE_OK;
115 if (result == STATE_UNKNOWN)
116 printf ("%s\n", _("Unable to read output"));
117 else {
118 asprintf(&perf, "%s", perfdata ("users", users, "",
119 TRUE, wusers,
120 TRUE, cusers,
121 TRUE, 0,
122 FALSE, 0));
123 printf (_("USERS %s - %d users currently logged in |%s\n"), state_text (result),
124 users, perf);
127 return result;
132 /* process command-line arguments */
134 process_arguments (int argc, char **argv)
136 int c;
138 int option = 0;
139 static struct option longopts[] = {
140 {"critical", required_argument, 0, 'c'},
141 {"warning", required_argument, 0, 'w'},
142 {"version", no_argument, 0, 'V'},
143 {"help", no_argument, 0, 'h'},
144 {0, 0, 0, 0}
147 if (argc < 2)
148 usage ("\n");
150 while (1) {
151 c = getopt_long (argc, argv, "+hVvc:w:", longopts, &option);
153 if (c == -1 || c == EOF || c == 1)
154 break;
156 switch (c) {
157 case '?': /* print short usage statement if args not parsable */
158 usage5 ();
159 case 'h': /* help */
160 print_help ();
161 exit (STATE_OK);
162 case 'V': /* version */
163 print_revision (progname, revision);
164 exit (STATE_OK);
165 case 'c': /* critical */
166 if (!is_intnonneg (optarg))
167 usage4 (_("Critical threshold must be a positive integer"));
168 else
169 cusers = atoi (optarg);
170 break;
171 case 'w': /* warning */
172 if (!is_intnonneg (optarg))
173 usage4 (_("Warning threshold must be a positive integer"));
174 else
175 wusers = atoi (optarg);
176 break;
180 c = optind;
181 if (wusers == -1 && argc > c) {
182 if (is_intnonneg (argv[c]) == FALSE)
183 usage4 (_("Warning threshold must be a positive integer"));
184 else
185 wusers = atoi (argv[c++]);
188 if (cusers == -1 && argc > c) {
189 if (is_intnonneg (argv[c]) == FALSE)
190 usage4 (_("Warning threshold must be a positive integer"));
191 else
192 cusers = atoi (argv[c]);
195 return OK;
200 void
201 print_help (void)
203 print_revision (progname, revision);
205 printf ("Copyright (c) 1999 Ethan Galstad\n");
206 printf (COPYRIGHT, copyright, email);
208 printf ("%s\n", _("This plugin checks the number of users currently logged in on the local"));
209 printf ("%s\n", _("system and generates an error if the number exceeds the thresholds specified."));
211 printf ("\n\n");
213 print_usage ();
215 printf (_(UT_HELP_VRSN));
217 printf (" %s\n", "-w, --warning=INTEGER");
218 printf (" %s\n", _("Set WARNING status if more than INTEGER users are logged in"));
219 printf (" %s\n", "-c, --critical=INTEGER");
220 printf (" %s\n", _("Set CRITICAL status if more than INTEGER users are logged in"));
222 printf (_(UT_SUPPORT));
226 void
227 print_usage (void)
229 printf (_("Usage:"));
230 printf ("%s -w <users> -c <users>\n", progname);