diff options
Diffstat (limited to 'plugins/check_users.c')
-rw-r--r-- | plugins/check_users.c | 95 |
1 files changed, 46 insertions, 49 deletions
diff --git a/plugins/check_users.c b/plugins/check_users.c index 54415a48..2a9ee986 100644 --- a/plugins/check_users.c +++ b/plugins/check_users.c | |||
@@ -1,33 +1,33 @@ | |||
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"; |
@@ -48,21 +48,26 @@ const char *email = "devel@monitoring-plugins.org"; | |||
48 | # include "popen.h" | 48 | # include "popen.h" |
49 | #endif | 49 | #endif |
50 | 50 | ||
51 | #ifdef HAVE_LIBSYSTEMD | ||
52 | #include <systemd/sd-daemon.h> | ||
53 | #include <systemd/sd-login.h> | ||
54 | #endif | ||
55 | |||
51 | #define possibly_set(a,b) ((a) == 0 ? (b) : 0) | 56 | #define possibly_set(a,b) ((a) == 0 ? (b) : 0) |
52 | 57 | ||
53 | int process_arguments (int, char **); | 58 | int process_arguments (int, char **); |
54 | void print_help (void); | 59 | void print_help (void); |
55 | void print_usage (void); | 60 | void print_usage (void); |
56 | 61 | ||
57 | int wusers = -1; | 62 | char *warning_range = NULL; |
58 | int cusers = -1; | 63 | char *critical_range = NULL; |
64 | thresholds *thlds = NULL; | ||
59 | 65 | ||
60 | int | 66 | int |
61 | main (int argc, char **argv) | 67 | main (int argc, char **argv) |
62 | { | 68 | { |
63 | int users = -1; | 69 | int users = -1; |
64 | int result = STATE_UNKNOWN; | 70 | int result = STATE_UNKNOWN; |
65 | char *perf; | ||
66 | #if HAVE_WTSAPI32_H | 71 | #if HAVE_WTSAPI32_H |
67 | WTS_SESSION_INFO *wtsinfo; | 72 | WTS_SESSION_INFO *wtsinfo; |
68 | DWORD wtscount; | 73 | DWORD wtscount; |
@@ -77,8 +82,6 @@ main (int argc, char **argv) | |||
77 | bindtextdomain (PACKAGE, LOCALEDIR); | 82 | bindtextdomain (PACKAGE, LOCALEDIR); |
78 | textdomain (PACKAGE); | 83 | textdomain (PACKAGE); |
79 | 84 | ||
80 | perf = strdup (""); | ||
81 | |||
82 | /* Parse extra opts if any */ | 85 | /* Parse extra opts if any */ |
83 | argv = np_extra_opts (&argc, argv, progname); | 86 | argv = np_extra_opts (&argc, argv, progname); |
84 | 87 | ||
@@ -87,6 +90,11 @@ main (int argc, char **argv) | |||
87 | 90 | ||
88 | users = 0; | 91 | users = 0; |
89 | 92 | ||
93 | #ifdef HAVE_LIBSYSTEMD | ||
94 | if (sd_booted () > 0) | ||
95 | users = sd_get_sessions (NULL); | ||
96 | else { | ||
97 | #endif | ||
90 | #if HAVE_WTSAPI32_H | 98 | #if HAVE_WTSAPI32_H |
91 | if (!WTSEnumerateSessions(WTS_CURRENT_SERVER_HANDLE, | 99 | if (!WTSEnumerateSessions(WTS_CURRENT_SERVER_HANDLE, |
92 | 0, 1, &wtsinfo, &wtscount)) { | 100 | 0, 1, &wtsinfo, &wtscount)) { |
@@ -158,25 +166,20 @@ main (int argc, char **argv) | |||
158 | if (spclose (child_process)) | 166 | if (spclose (child_process)) |
159 | result = possibly_set (result, STATE_UNKNOWN); | 167 | result = possibly_set (result, STATE_UNKNOWN); |
160 | #endif | 168 | #endif |
169 | #ifdef HAVE_LIBSYSTEMD | ||
170 | } | ||
171 | #endif | ||
161 | 172 | ||
162 | /* check the user count against warning and critical thresholds */ | 173 | /* check the user count against warning and critical thresholds */ |
163 | if (users > cusers) | 174 | result = get_status((double)users, thlds); |
164 | result = STATE_CRITICAL; | ||
165 | else if (users > wusers) | ||
166 | result = STATE_WARNING; | ||
167 | else if (users >= 0) | ||
168 | result = STATE_OK; | ||
169 | 175 | ||
170 | if (result == STATE_UNKNOWN) | 176 | if (result == STATE_UNKNOWN) |
171 | printf ("%s\n", _("Unable to read output")); | 177 | printf ("%s\n", _("Unable to read output")); |
172 | else { | 178 | else { |
173 | xasprintf (&perf, "%s", perfdata ("users", users, "", | 179 | printf (_("USERS %s - %d users currently logged in |%s\n"), |
174 | TRUE, wusers, | 180 | state_text(result), users, |
175 | TRUE, cusers, | 181 | sperfdata_int("users", users, "", warning_range, |
176 | TRUE, 0, | 182 | critical_range, TRUE, 0, FALSE, 0)); |
177 | FALSE, 0)); | ||
178 | printf (_("USERS %s - %d users currently logged in |%s\n"), state_text (result), | ||
179 | users, perf); | ||
180 | } | 183 | } |
181 | 184 | ||
182 | return result; | 185 | return result; |
@@ -215,33 +218,27 @@ process_arguments (int argc, char **argv) | |||
215 | print_revision (progname, NP_VERSION); | 218 | print_revision (progname, NP_VERSION); |
216 | exit (STATE_UNKNOWN); | 219 | exit (STATE_UNKNOWN); |
217 | case 'c': /* critical */ | 220 | case 'c': /* critical */ |
218 | if (!is_intnonneg (optarg)) | 221 | critical_range = optarg; |
219 | usage4 (_("Critical threshold must be a positive integer")); | ||
220 | else | ||
221 | cusers = atoi (optarg); | ||
222 | break; | 222 | break; |
223 | case 'w': /* warning */ | 223 | case 'w': /* warning */ |
224 | if (!is_intnonneg (optarg)) | 224 | warning_range = optarg; |
225 | usage4 (_("Warning threshold must be a positive integer")); | ||
226 | else | ||
227 | wusers = atoi (optarg); | ||
228 | break; | 225 | break; |
229 | } | 226 | } |
230 | } | 227 | } |
231 | 228 | ||
232 | c = optind; | 229 | c = optind; |
233 | if (wusers == -1 && argc > c) { | 230 | if (warning_range == NULL && argc > c) |
234 | if (is_intnonneg (argv[c]) == FALSE) | 231 | warning_range = argv[c++]; |
235 | usage4 (_("Warning threshold must be a positive integer")); | 232 | if (critical_range == NULL && argc > c) |
236 | else | 233 | critical_range = argv[c++]; |
237 | wusers = atoi (argv[c++]); | 234 | |
238 | } | 235 | /* this will abort in case of invalid ranges */ |
239 | if (cusers == -1 && argc > c) { | 236 | set_thresholds (&thlds, warning_range, critical_range); |
240 | if (is_intnonneg (argv[c]) == FALSE) | 237 | |
241 | usage4 (_("Warning threshold must be a positive integer")); | 238 | if (thlds->warning->end < 0) |
242 | else | 239 | usage4 (_("Warning threshold must be a positive integer")); |
243 | cusers = atoi (argv[c]); | 240 | if (thlds->critical->end < 0) |
244 | } | 241 | usage4 (_("Critical threshold must be a positive integer")); |
245 | 242 | ||
246 | return OK; | 243 | return OK; |
247 | } | 244 | } |