diff options
author | Thorsten Kukuk <kukuk@suse.com> | 2023-06-16 07:28:21 (GMT) |
---|---|---|
committer | Thorsten Kukuk <5908016+thkukuk@users.noreply.github.com> | 2023-08-28 12:55:56 (GMT) |
commit | 801784ae89224d004dc79af95545acbdf547ce64 (patch) | |
tree | a168eac393325f52698cc2bc59bfccece86524c1 /plugins/check_users.c | |
parent | 43131b73d69e77a3faee69814dac1bbc88162887 (diff) | |
download | monitoring-plugins-801784ae89224d004dc79af95545acbdf547ce64.tar.gz |
check_users: prefer systemd-logind over utmprefs/pull/1888/head
Prefer systemd-logind over utmp to get the number of logged in users.
utmp is not reliable for this (e.g. some terminals create utmp entries,
other not) and utmp is not Y2038 safe with glibc on Linux.
Diffstat (limited to 'plugins/check_users.c')
-rw-r--r-- | plugins/check_users.c | 37 |
1 files changed, 25 insertions, 12 deletions
diff --git a/plugins/check_users.c b/plugins/check_users.c index f6f4b36..2a9ee98 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,6 +48,11 @@ 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 **); |
@@ -85,6 +90,11 @@ main (int argc, char **argv) | |||
85 | 90 | ||
86 | users = 0; | 91 | users = 0; |
87 | 92 | ||
93 | #ifdef HAVE_LIBSYSTEMD | ||
94 | if (sd_booted () > 0) | ||
95 | users = sd_get_sessions (NULL); | ||
96 | else { | ||
97 | #endif | ||
88 | #if HAVE_WTSAPI32_H | 98 | #if HAVE_WTSAPI32_H |
89 | if (!WTSEnumerateSessions(WTS_CURRENT_SERVER_HANDLE, | 99 | if (!WTSEnumerateSessions(WTS_CURRENT_SERVER_HANDLE, |
90 | 0, 1, &wtsinfo, &wtscount)) { | 100 | 0, 1, &wtsinfo, &wtscount)) { |
@@ -156,6 +166,9 @@ main (int argc, char **argv) | |||
156 | if (spclose (child_process)) | 166 | if (spclose (child_process)) |
157 | result = possibly_set (result, STATE_UNKNOWN); | 167 | result = possibly_set (result, STATE_UNKNOWN); |
158 | #endif | 168 | #endif |
169 | #ifdef HAVE_LIBSYSTEMD | ||
170 | } | ||
171 | #endif | ||
159 | 172 | ||
160 | /* check the user count against warning and critical thresholds */ | 173 | /* check the user count against warning and critical thresholds */ |
161 | result = get_status((double)users, thlds); | 174 | result = get_status((double)users, thlds); |
@@ -163,7 +176,7 @@ main (int argc, char **argv) | |||
163 | if (result == STATE_UNKNOWN) | 176 | if (result == STATE_UNKNOWN) |
164 | printf ("%s\n", _("Unable to read output")); | 177 | printf ("%s\n", _("Unable to read output")); |
165 | else { | 178 | else { |
166 | printf (_("USERS %s - %d users currently logged in |%s\n"), | 179 | printf (_("USERS %s - %d users currently logged in |%s\n"), |
167 | state_text(result), users, | 180 | state_text(result), users, |
168 | sperfdata_int("users", users, "", warning_range, | 181 | sperfdata_int("users", users, "", warning_range, |
169 | critical_range, TRUE, 0, FALSE, 0)); | 182 | critical_range, TRUE, 0, FALSE, 0)); |