From 801784ae89224d004dc79af95545acbdf547ce64 Mon Sep 17 00:00:00 2001 From: Thorsten Kukuk Date: Fri, 16 Jun 2023 09:28:21 +0200 Subject: check_users: prefer systemd-logind over utmp 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. --- plugins/check_users.c | 37 +++++++++++++++++++++++++------------ 1 file changed, 25 insertions(+), 12 deletions(-) (limited to 'plugins/check_users.c') diff --git a/plugins/check_users.c b/plugins/check_users.c index f6f4b362..2a9ee986 100644 --- a/plugins/check_users.c +++ b/plugins/check_users.c @@ -1,33 +1,33 @@ /***************************************************************************** -* +* * Monitoring check_users plugin -* +* * License: GPL * Copyright (c) 2000-2012 Monitoring Plugins Development Team -* +* * Description: -* +* * This file contains the check_users plugin -* +* * This plugin checks the number of users currently logged in on the local * system and generates an error if the number exceeds the thresholds * specified. -* -* +* +* * This program is free software: you can redistribute it and/or modify * it under the terms of the GNU General Public License as published by * the Free Software Foundation, either version 3 of the License, or * (at your option) any later version. -* +* * This program is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * GNU General Public License for more details. -* +* * You should have received a copy of the GNU General Public License * along with this program. If not, see . -* -* +* +* *****************************************************************************/ const char *progname = "check_users"; @@ -48,6 +48,11 @@ const char *email = "devel@monitoring-plugins.org"; # include "popen.h" #endif +#ifdef HAVE_LIBSYSTEMD +#include +#include +#endif + #define possibly_set(a,b) ((a) == 0 ? (b) : 0) int process_arguments (int, char **); @@ -85,6 +90,11 @@ main (int argc, char **argv) users = 0; +#ifdef HAVE_LIBSYSTEMD + if (sd_booted () > 0) + users = sd_get_sessions (NULL); + else { +#endif #if HAVE_WTSAPI32_H if (!WTSEnumerateSessions(WTS_CURRENT_SERVER_HANDLE, 0, 1, &wtsinfo, &wtscount)) { @@ -156,6 +166,9 @@ main (int argc, char **argv) if (spclose (child_process)) result = possibly_set (result, STATE_UNKNOWN); #endif +#ifdef HAVE_LIBSYSTEMD + } +#endif /* check the user count against warning and critical thresholds */ result = get_status((double)users, thlds); @@ -163,7 +176,7 @@ main (int argc, char **argv) if (result == STATE_UNKNOWN) printf ("%s\n", _("Unable to read output")); else { - printf (_("USERS %s - %d users currently logged in |%s\n"), + printf (_("USERS %s - %d users currently logged in |%s\n"), state_text(result), users, sperfdata_int("users", users, "", warning_range, critical_range, TRUE, 0, FALSE, 0)); -- cgit v1.2.3-74-g34f1