diff options
-rw-r--r-- | plugins/check_users.c | 265 |
1 files changed, 125 insertions, 140 deletions
diff --git a/plugins/check_users.c b/plugins/check_users.c index 89b95369..1ff7fe6a 100644 --- a/plugins/check_users.c +++ b/plugins/check_users.c | |||
@@ -1,34 +1,34 @@ | |||
1 | /***************************************************************************** | 1 | /***************************************************************************** |
2 | * | 2 | * |
3 | * Monitoring check_users plugin | 3 | * Monitoring check_users plugin |
4 | * | 4 | * |
5 | * License: GPL | 5 | * License: GPL |
6 | * Copyright (c) 2000-2012 Monitoring Plugins Development Team | 6 | * Copyright (c) 2000-2012 Monitoring Plugins Development Team |
7 | * | 7 | * |
8 | * Description: | 8 | * Description: |
9 | * | 9 | * |
10 | * This file contains the check_users plugin | 10 | * This file contains the check_users plugin |
11 | * | 11 | * |
12 | * This plugin checks the number of users currently logged in on the local | 12 | * This plugin checks the number of users currently logged in on the local |
13 | * system and generates an error if the number exceeds the thresholds | 13 | * system and generates an error if the number exceeds the thresholds |
14 | * specified. | 14 | * specified. |
15 | * | 15 | * |
16 | * | 16 | * |
17 | * This program is free software: you can redistribute it and/or modify | 17 | * This program is free software: you can redistribute it and/or modify |
18 | * it under the terms of the GNU General Public License as published by | 18 | * it under the terms of the GNU General Public License as published by |
19 | * the Free Software Foundation, either version 3 of the License, or | 19 | * the Free Software Foundation, either version 3 of the License, or |
20 | * (at your option) any later version. | 20 | * (at your option) any later version. |
21 | * | 21 | * |
22 | * This program is distributed in the hope that it will be useful, | 22 | * This program is distributed in the hope that it will be useful, |
23 | * but WITHOUT ANY WARRANTY; without even the implied warranty of | 23 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
24 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | 24 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
25 | * GNU General Public License for more details. | 25 | * GNU General Public License for more details. |
26 | * | 26 | * |
27 | * You should have received a copy of the GNU General Public License | 27 | * You should have received a copy of the GNU General Public License |
28 | * along with this program. If not, see <http://www.gnu.org/licenses/>. | 28 | * along with this program. If not, see <http://www.gnu.org/licenses/>. |
29 | * | 29 | * |
30 | * | 30 | * |
31 | *****************************************************************************/ | 31 | *****************************************************************************/ |
32 | 32 | ||
33 | const char *progname = "check_users"; | 33 | const char *progname = "check_users"; |
34 | const char *copyright = "2000-2007"; | 34 | const char *copyright = "2000-2007"; |
@@ -38,34 +38,32 @@ const char *email = "devel@monitoring-plugins.org"; | |||
38 | #include "utils.h" | 38 | #include "utils.h" |
39 | 39 | ||
40 | #if HAVE_WTSAPI32_H | 40 | #if HAVE_WTSAPI32_H |
41 | # include <windows.h> | 41 | # include <windows.h> |
42 | # include <wtsapi32.h> | 42 | # include <wtsapi32.h> |
43 | # undef ERROR | 43 | # undef ERROR |
44 | # define ERROR -1 | 44 | # define ERROR -1 |
45 | #elif HAVE_UTMPX_H | 45 | #elif HAVE_UTMPX_H |
46 | # include <utmpx.h> | 46 | # include <utmpx.h> |
47 | #else | 47 | #else |
48 | # include "popen.h" | 48 | # include "popen.h" |
49 | #endif | 49 | #endif |
50 | 50 | ||
51 | #ifdef HAVE_LIBSYSTEMD | 51 | #ifdef HAVE_LIBSYSTEMD |
52 | #include <systemd/sd-daemon.h> | 52 | # include <systemd/sd-daemon.h> |
53 | #include <systemd/sd-login.h> | 53 | # include <systemd/sd-login.h> |
54 | #endif | 54 | #endif |
55 | 55 | ||
56 | #define possibly_set(a,b) ((a) == 0 ? (b) : 0) | 56 | #define possibly_set(a, b) ((a) == 0 ? (b) : 0) |
57 | 57 | ||
58 | int process_arguments (int, char **); | 58 | int process_arguments(int, char **); |
59 | void print_help (void); | 59 | void print_help(void); |
60 | void print_usage (void); | 60 | void print_usage(void); |
61 | 61 | ||
62 | char *warning_range = NULL; | 62 | char *warning_range = NULL; |
63 | char *critical_range = NULL; | 63 | char *critical_range = NULL; |
64 | thresholds *thlds = NULL; | 64 | thresholds *thlds = NULL; |
65 | 65 | ||
66 | int | 66 | int main(int argc, char **argv) { |
67 | main (int argc, char **argv) | ||
68 | { | ||
69 | int users = -1; | 67 | int users = -1; |
70 | int result = STATE_UNKNOWN; | 68 | int result = STATE_UNKNOWN; |
71 | #if HAVE_WTSAPI32_H | 69 | #if HAVE_WTSAPI32_H |
@@ -78,74 +76,71 @@ main (int argc, char **argv) | |||
78 | char input_buffer[MAX_INPUT_BUFFER]; | 76 | char input_buffer[MAX_INPUT_BUFFER]; |
79 | #endif | 77 | #endif |
80 | 78 | ||
81 | setlocale (LC_ALL, ""); | 79 | setlocale(LC_ALL, ""); |
82 | bindtextdomain (PACKAGE, LOCALEDIR); | 80 | bindtextdomain(PACKAGE, LOCALEDIR); |
83 | textdomain (PACKAGE); | 81 | textdomain(PACKAGE); |
84 | 82 | ||
85 | /* Parse extra opts if any */ | 83 | /* Parse extra opts if any */ |
86 | argv = np_extra_opts (&argc, argv, progname); | 84 | argv = np_extra_opts(&argc, argv, progname); |
87 | 85 | ||
88 | if (process_arguments (argc, argv) == ERROR) | 86 | if (process_arguments(argc, argv) == ERROR) |
89 | usage4 (_("Could not parse arguments")); | 87 | usage4(_("Could not parse arguments")); |
90 | 88 | ||
91 | users = 0; | 89 | users = 0; |
92 | 90 | ||
93 | #ifdef HAVE_LIBSYSTEMD | 91 | #ifdef HAVE_LIBSYSTEMD |
94 | if (sd_booted () > 0) | 92 | if (sd_booted() > 0) |
95 | users = sd_get_sessions (NULL); | 93 | users = sd_get_sessions(NULL); |
96 | else { | 94 | else { |
97 | #endif | 95 | #endif |
98 | #if HAVE_WTSAPI32_H | 96 | #if HAVE_WTSAPI32_H |
99 | if (!WTSEnumerateSessions(WTS_CURRENT_SERVER_HANDLE, | 97 | if (!WTSEnumerateSessions(WTS_CURRENT_SERVER_HANDLE, 0, 1, &wtsinfo, &wtscount)) { |
100 | 0, 1, &wtsinfo, &wtscount)) { | 98 | printf(_("Could not enumerate RD sessions: %d\n"), GetLastError()); |
101 | printf(_("Could not enumerate RD sessions: %d\n"), GetLastError()); | 99 | return STATE_UNKNOWN; |
102 | return STATE_UNKNOWN; | 100 | } |
103 | } | ||
104 | 101 | ||
105 | for (index = 0; index < wtscount; index++) { | 102 | for (index = 0; index < wtscount; index++) { |
106 | LPTSTR username; | 103 | LPTSTR username; |
107 | DWORD size; | 104 | DWORD size; |
108 | int len; | 105 | int len; |
109 | 106 | ||
110 | if (!WTSQuerySessionInformation(WTS_CURRENT_SERVER_HANDLE, | 107 | if (!WTSQuerySessionInformation(WTS_CURRENT_SERVER_HANDLE, wtsinfo[index].SessionId, WTSUserName, &username, &size)) |
111 | wtsinfo[index].SessionId, WTSUserName, &username, &size)) | 108 | continue; |
112 | continue; | ||
113 | 109 | ||
114 | len = lstrlen(username); | 110 | len = lstrlen(username); |
115 | 111 | ||
116 | WTSFreeMemory(username); | 112 | WTSFreeMemory(username); |
117 | 113 | ||
118 | if (len == 0) | 114 | if (len == 0) |
119 | continue; | 115 | continue; |
120 | 116 | ||
121 | if (wtsinfo[index].State == WTSActive || | 117 | if (wtsinfo[index].State == WTSActive || wtsinfo[index].State == WTSDisconnected) |
122 | wtsinfo[index].State == WTSDisconnected) | 118 | users++; |
123 | users++; | 119 | } |
124 | } | ||
125 | 120 | ||
126 | WTSFreeMemory(wtsinfo); | 121 | WTSFreeMemory(wtsinfo); |
127 | #elif HAVE_UTMPX_H | 122 | #elif HAVE_UTMPX_H |
128 | /* get currently logged users from utmpx */ | 123 | /* get currently logged users from utmpx */ |
129 | setutxent (); | 124 | setutxent(); |
130 | 125 | ||
131 | while ((putmpx = getutxent ()) != NULL) | 126 | while ((putmpx = getutxent()) != NULL) |
132 | if (putmpx->ut_type == USER_PROCESS) | 127 | if (putmpx->ut_type == USER_PROCESS) |
133 | users++; | 128 | users++; |
134 | 129 | ||
135 | endutxent (); | 130 | endutxent(); |
136 | #else | 131 | #else |
137 | /* run the command */ | 132 | /* run the command */ |
138 | child_process = spopen (WHO_COMMAND); | 133 | child_process = spopen(WHO_COMMAND); |
139 | if (child_process == NULL) { | 134 | if (child_process == NULL) { |
140 | printf (_("Could not open pipe: %s\n"), WHO_COMMAND); | 135 | printf(_("Could not open pipe: %s\n"), WHO_COMMAND); |
141 | return STATE_UNKNOWN; | 136 | return STATE_UNKNOWN; |
142 | } | 137 | } |
143 | 138 | ||
144 | child_stderr = fdopen (child_stderr_array[fileno (child_process)], "r"); | 139 | child_stderr = fdopen(child_stderr_array[fileno(child_process)], "r"); |
145 | if (child_stderr == NULL) | 140 | if (child_stderr == NULL) |
146 | printf (_("Could not open stderr for %s\n"), WHO_COMMAND); | 141 | printf(_("Could not open stderr for %s\n"), WHO_COMMAND); |
147 | 142 | ||
148 | while (fgets (input_buffer, MAX_INPUT_BUFFER - 1, child_process)) { | 143 | while (fgets(input_buffer, MAX_INPUT_BUFFER - 1, child_process)) { |
149 | /* increment 'users' on all lines except total user count */ | 144 | /* increment 'users' on all lines except total user count */ |
150 | if (input_buffer[0] != '#') { | 145 | if (input_buffer[0] != '#') { |
151 | users++; | 146 | users++; |
@@ -153,18 +148,18 @@ main (int argc, char **argv) | |||
153 | } | 148 | } |
154 | 149 | ||
155 | /* get total logged in users */ | 150 | /* get total logged in users */ |
156 | if (sscanf (input_buffer, _("# users=%d"), &users) == 1) | 151 | if (sscanf(input_buffer, _("# users=%d"), &users) == 1) |
157 | break; | 152 | break; |
158 | } | 153 | } |
159 | 154 | ||
160 | /* check STDERR */ | 155 | /* check STDERR */ |
161 | if (fgets (input_buffer, MAX_INPUT_BUFFER - 1, child_stderr)) | 156 | if (fgets(input_buffer, MAX_INPUT_BUFFER - 1, child_stderr)) |
162 | result = possibly_set (result, STATE_UNKNOWN); | 157 | result = possibly_set(result, STATE_UNKNOWN); |
163 | (void) fclose (child_stderr); | 158 | (void)fclose(child_stderr); |
164 | 159 | ||
165 | /* close the pipe */ | 160 | /* close the pipe */ |
166 | if (spclose (child_process)) | 161 | if (spclose(child_process)) |
167 | result = possibly_set (result, STATE_UNKNOWN); | 162 | result = possibly_set(result, STATE_UNKNOWN); |
168 | #endif | 163 | #endif |
169 | #ifdef HAVE_LIBSYSTEMD | 164 | #ifdef HAVE_LIBSYSTEMD |
170 | } | 165 | } |
@@ -174,53 +169,47 @@ main (int argc, char **argv) | |||
174 | result = get_status((double)users, thlds); | 169 | result = get_status((double)users, thlds); |
175 | 170 | ||
176 | if (result == STATE_UNKNOWN) | 171 | if (result == STATE_UNKNOWN) |
177 | printf ("%s\n", _("Unable to read output")); | 172 | printf("%s\n", _("Unable to read output")); |
178 | else { | 173 | else { |
179 | printf (_("USERS %s - %d users currently logged in |%s\n"), | 174 | printf(_("USERS %s - %d users currently logged in |%s\n"), state_text(result), users, |
180 | state_text(result), users, | 175 | sperfdata_int("users", users, "", warning_range, critical_range, true, 0, false, 0)); |
181 | sperfdata_int("users", users, "", warning_range, | ||
182 | critical_range, true, 0, false, 0)); | ||
183 | } | 176 | } |
184 | 177 | ||
185 | return result; | 178 | return result; |
186 | } | 179 | } |
187 | 180 | ||
188 | /* process command-line arguments */ | 181 | /* process command-line arguments */ |
189 | int | 182 | int process_arguments(int argc, char **argv) { |
190 | process_arguments (int argc, char **argv) | ||
191 | { | ||
192 | int c; | 183 | int c; |
193 | int option = 0; | 184 | int option = 0; |
194 | static struct option longopts[] = { | 185 | static struct option longopts[] = {{"critical", required_argument, 0, 'c'}, |
195 | {"critical", required_argument, 0, 'c'}, | 186 | {"warning", required_argument, 0, 'w'}, |
196 | {"warning", required_argument, 0, 'w'}, | 187 | {"version", no_argument, 0, 'V'}, |
197 | {"version", no_argument, 0, 'V'}, | 188 | {"help", no_argument, 0, 'h'}, |
198 | {"help", no_argument, 0, 'h'}, | 189 | {0, 0, 0, 0}}; |
199 | {0, 0, 0, 0} | ||
200 | }; | ||
201 | 190 | ||
202 | if (argc < 2) | 191 | if (argc < 2) |
203 | usage ("\n"); | 192 | usage("\n"); |
204 | 193 | ||
205 | while (true) { | 194 | while (true) { |
206 | c = getopt_long (argc, argv, "+hVvc:w:", longopts, &option); | 195 | c = getopt_long(argc, argv, "+hVvc:w:", longopts, &option); |
207 | 196 | ||
208 | if (c == -1 || c == EOF || c == 1) | 197 | if (c == -1 || c == EOF || c == 1) |
209 | break; | 198 | break; |
210 | 199 | ||
211 | switch (c) { | 200 | switch (c) { |
212 | case '?': /* print short usage statement if args not parsable */ | 201 | case '?': /* print short usage statement if args not parsable */ |
213 | usage5 (); | 202 | usage5(); |
214 | case 'h': /* help */ | 203 | case 'h': /* help */ |
215 | print_help (); | 204 | print_help(); |
216 | exit (STATE_UNKNOWN); | 205 | exit(STATE_UNKNOWN); |
217 | case 'V': /* version */ | 206 | case 'V': /* version */ |
218 | print_revision (progname, NP_VERSION); | 207 | print_revision(progname, NP_VERSION); |
219 | exit (STATE_UNKNOWN); | 208 | exit(STATE_UNKNOWN); |
220 | case 'c': /* critical */ | 209 | case 'c': /* critical */ |
221 | critical_range = optarg; | 210 | critical_range = optarg; |
222 | break; | 211 | break; |
223 | case 'w': /* warning */ | 212 | case 'w': /* warning */ |
224 | warning_range = optarg; | 213 | warning_range = optarg; |
225 | break; | 214 | break; |
226 | } | 215 | } |
@@ -235,48 +224,44 @@ process_arguments (int argc, char **argv) | |||
235 | critical_range = argv[c++]; | 224 | critical_range = argv[c++]; |
236 | 225 | ||
237 | /* this will abort in case of invalid ranges */ | 226 | /* this will abort in case of invalid ranges */ |
238 | set_thresholds (&thlds, warning_range, critical_range); | 227 | set_thresholds(&thlds, warning_range, critical_range); |
239 | 228 | ||
240 | if (!thlds->warning) { | 229 | if (!thlds->warning) { |
241 | usage4 (_("Warning threshold must be a valid range expression")); | 230 | usage4(_("Warning threshold must be a valid range expression")); |
242 | } | 231 | } |
243 | 232 | ||
244 | if (!thlds->critical) { | 233 | if (!thlds->critical) { |
245 | usage4 (_("Critical threshold must be a valid range expression")); | 234 | usage4(_("Critical threshold must be a valid range expression")); |
246 | } | 235 | } |
247 | 236 | ||
248 | return OK; | 237 | return OK; |
249 | } | 238 | } |
250 | 239 | ||
251 | void | 240 | void print_help(void) { |
252 | print_help (void) | 241 | print_revision(progname, NP_VERSION); |
253 | { | ||
254 | print_revision (progname, NP_VERSION); | ||
255 | 242 | ||
256 | printf ("Copyright (c) 1999 Ethan Galstad\n"); | 243 | printf("Copyright (c) 1999 Ethan Galstad\n"); |
257 | printf (COPYRIGHT, copyright, email); | 244 | printf(COPYRIGHT, copyright, email); |
258 | 245 | ||
259 | printf ("%s\n", _("This plugin checks the number of users currently logged in on the local")); | 246 | printf("%s\n", _("This plugin checks the number of users currently logged in on the local")); |
260 | printf ("%s\n", _("system and generates an error if the number exceeds the thresholds specified.")); | 247 | printf("%s\n", _("system and generates an error if the number exceeds the thresholds specified.")); |
261 | 248 | ||
262 | printf ("\n\n"); | 249 | printf("\n\n"); |
263 | 250 | ||
264 | print_usage (); | 251 | print_usage(); |
265 | 252 | ||
266 | printf (UT_HELP_VRSN); | 253 | printf(UT_HELP_VRSN); |
267 | printf (UT_EXTRA_OPTS); | 254 | printf(UT_EXTRA_OPTS); |
268 | 255 | ||
269 | printf (" %s\n", "-w, --warning=RANGE_EXPRESSION"); | 256 | printf(" %s\n", "-w, --warning=RANGE_EXPRESSION"); |
270 | printf (" %s\n", _("Set WARNING status if number of logged in users violates RANGE_EXPRESSION")); | 257 | printf(" %s\n", _("Set WARNING status if number of logged in users violates RANGE_EXPRESSION")); |
271 | printf (" %s\n", "-c, --critical=RANGE_EXPRESSION"); | 258 | printf(" %s\n", "-c, --critical=RANGE_EXPRESSION"); |
272 | printf (" %s\n", _("Set CRITICAL status if number of logged in users violates RANGE_EXPRESSION")); | 259 | printf(" %s\n", _("Set CRITICAL status if number of logged in users violates RANGE_EXPRESSION")); |
273 | 260 | ||
274 | printf (UT_SUPPORT); | 261 | printf(UT_SUPPORT); |
275 | } | 262 | } |
276 | 263 | ||
277 | void | 264 | void print_usage(void) { |
278 | print_usage (void) | 265 | printf("%s\n", _("Usage:")); |
279 | { | 266 | printf("%s -w <users> -c <users>\n", progname); |
280 | printf ("%s\n", _("Usage:")); | ||
281 | printf ("%s -w <users> -c <users>\n", progname); | ||
282 | } | 267 | } |