summaryrefslogtreecommitdiffstats
path: root/plugins/check_users.c
diff options
context:
space:
mode:
Diffstat (limited to 'plugins/check_users.c')
-rw-r--r--plugins/check_users.c68
1 files changed, 18 insertions, 50 deletions
diff --git a/plugins/check_users.c b/plugins/check_users.c
index 8368612a..c581fb29 100644
--- a/plugins/check_users.c
+++ b/plugins/check_users.c
@@ -3,7 +3,7 @@
3* Nagios check_users plugin 3* Nagios check_users plugin
4* 4*
5* License: GPL 5* License: GPL
6* Copyright (c) 2000-2007 Nagios Plugins Development Team 6* Copyright (c) 2000-2012 Nagios Plugins Development Team
7* 7*
8* Description: 8* Description:
9* 9*
@@ -35,8 +35,8 @@ const char *copyright = "2000-2007";
35const char *email = "nagiosplug-devel@lists.sourceforge.net"; 35const char *email = "nagiosplug-devel@lists.sourceforge.net";
36 36
37#include "common.h" 37#include "common.h"
38#include "popen.h"
39#include "utils.h" 38#include "utils.h"
39#include <utmpx.h>
40 40
41#define possibly_set(a,b) ((a) == 0 ? (b) : 0) 41#define possibly_set(a,b) ((a) == 0 ? (b) : 0)
42 42
@@ -52,58 +52,33 @@ main (int argc, char **argv)
52{ 52{
53 int users = -1; 53 int users = -1;
54 int result = STATE_UNKNOWN; 54 int result = STATE_UNKNOWN;
55 char input_buffer[MAX_INPUT_BUFFER];
56 char *perf; 55 char *perf;
56 struct utmpx *putmpx;
57 57
58 setlocale (LC_ALL, ""); 58 setlocale (LC_ALL, "");
59 bindtextdomain (PACKAGE, LOCALEDIR); 59 bindtextdomain (PACKAGE, LOCALEDIR);
60 textdomain (PACKAGE); 60 textdomain (PACKAGE);
61 61
62 perf = strdup(""); 62 perf = strdup ("");
63 63
64 /* Parse extra opts if any */ 64 /* Parse extra opts if any */
65 argv=np_extra_opts (&argc, argv, progname); 65 argv = np_extra_opts (&argc, argv, progname);
66 66
67 if (process_arguments (argc, argv) == ERROR) 67 if (process_arguments (argc, argv) == ERROR)
68 usage4 (_("Could not parse arguments")); 68 usage4 (_("Could not parse arguments"));
69 69
70 /* run the command */
71 child_process = spopen (WHO_COMMAND);
72 if (child_process == NULL) {
73 printf (_("Could not open pipe: %s\n"), WHO_COMMAND);
74 return STATE_UNKNOWN;
75 }
76
77 child_stderr = fdopen (child_stderr_array[fileno (child_process)], "r");
78 if (child_stderr == NULL)
79 printf (_("Could not open stderr for %s\n"), WHO_COMMAND);
80
81 users = 0; 70 users = 0;
82 71
83 while (fgets (input_buffer, MAX_INPUT_BUFFER - 1, child_process)) { 72 /* get currently logged users from utmpx */
73 setutxent ();
84 74
85 /* increment 'users' on all lines except total user count */ 75 while ((putmpx = getutxent ()) != NULL)
86 if (input_buffer[0] != '#') { 76 if (putmpx->ut_type == USER_PROCESS)
87 users++; 77 users++;
88 continue;
89 }
90
91 /* get total logged in users */
92 if (sscanf (input_buffer, _("# users=%d"), &users) == 1)
93 break;
94
95 }
96 78
97 /* check STDERR */ 79 endutxent ();
98 if (fgets (input_buffer, MAX_INPUT_BUFFER - 1, child_stderr))
99 result = possibly_set (result, STATE_UNKNOWN);
100 (void) fclose (child_stderr);
101 80
102 /* close the pipe */ 81 /* check the user count against warning and critical thresholds */
103 if (spclose (child_process))
104 result = possibly_set (result, STATE_UNKNOWN);
105
106 /* else check the user count against warning and critical thresholds */
107 if (users > cusers) 82 if (users > cusers)
108 result = STATE_CRITICAL; 83 result = STATE_CRITICAL;
109 else if (users > wusers) 84 else if (users > wusers)
@@ -114,7 +89,7 @@ main (int argc, char **argv)
114 if (result == STATE_UNKNOWN) 89 if (result == STATE_UNKNOWN)
115 printf ("%s\n", _("Unable to read output")); 90 printf ("%s\n", _("Unable to read output"));
116 else { 91 else {
117 asprintf(&perf, "%s", perfdata ("users", users, "", 92 xasprintf (&perf, "%s", perfdata ("users", users, "",
118 TRUE, wusers, 93 TRUE, wusers,
119 TRUE, cusers, 94 TRUE, cusers,
120 TRUE, 0, 95 TRUE, 0,
@@ -126,14 +101,11 @@ main (int argc, char **argv)
126 return result; 101 return result;
127} 102}
128 103
129
130
131/* process command-line arguments */ 104/* process command-line arguments */
132int 105int
133process_arguments (int argc, char **argv) 106process_arguments (int argc, char **argv)
134{ 107{
135 int c; 108 int c;
136
137 int option = 0; 109 int option = 0;
138 static struct option longopts[] = { 110 static struct option longopts[] = {
139 {"critical", required_argument, 0, 'c'}, 111 {"critical", required_argument, 0, 'c'},
@@ -183,7 +155,6 @@ process_arguments (int argc, char **argv)
183 else 155 else
184 wusers = atoi (argv[c++]); 156 wusers = atoi (argv[c++]);
185 } 157 }
186
187 if (cusers == -1 && argc > c) { 158 if (cusers == -1 && argc > c) {
188 if (is_intnonneg (argv[c]) == FALSE) 159 if (is_intnonneg (argv[c]) == FALSE)
189 usage4 (_("Warning threshold must be a positive integer")); 160 usage4 (_("Warning threshold must be a positive integer"));
@@ -194,8 +165,6 @@ process_arguments (int argc, char **argv)
194 return OK; 165 return OK;
195} 166}
196 167
197
198
199void 168void
200print_help (void) 169print_help (void)
201{ 170{
@@ -205,9 +174,9 @@ print_help (void)
205 printf (COPYRIGHT, copyright, email); 174 printf (COPYRIGHT, copyright, email);
206 175
207 printf ("%s\n", _("This plugin checks the number of users currently logged in on the local")); 176 printf ("%s\n", _("This plugin checks the number of users currently logged in on the local"));
208 printf ("%s\n", _("system and generates an error if the number exceeds the thresholds specified.")); 177 printf ("%s\n", _("system and generates an error if the number exceeds the thresholds specified."));
209 178
210 printf ("\n\n"); 179 printf ("\n\n");
211 180
212 print_usage (); 181 print_usage ();
213 182
@@ -215,17 +184,16 @@ print_help (void)
215 printf (UT_EXTRA_OPTS); 184 printf (UT_EXTRA_OPTS);
216 185
217 printf (" %s\n", "-w, --warning=INTEGER"); 186 printf (" %s\n", "-w, --warning=INTEGER");
218 printf (" %s\n", _("Set WARNING status if more than INTEGER users are logged in")); 187 printf (" %s\n", _("Set WARNING status if more than INTEGER users are logged in"));
219 printf (" %s\n", "-c, --critical=INTEGER"); 188 printf (" %s\n", "-c, --critical=INTEGER");
220 printf (" %s\n", _("Set CRITICAL status if more than INTEGER users are logged in")); 189 printf (" %s\n", _("Set CRITICAL status if more than INTEGER users are logged in"));
221 190
222 printf (UT_SUPPORT); 191 printf (UT_SUPPORT);
223} 192}
224 193
225
226void 194void
227print_usage (void) 195print_usage (void)
228{ 196{
229 printf ("%s\n", _("Usage:")); 197 printf ("%s\n", _("Usage:"));
230 printf ("%s -w <users> -c <users>\n", progname); 198 printf ("%s -w <users> -c <users>\n", progname);
231} 199}