monitoring-plugins/plugins/check_users.c

286 lines
7.7 KiB
C
Raw Normal View History

/*****************************************************************************
2024-10-31 09:14:31 -04:00
*
* Monitoring check_users plugin
*
* License: GPL
2024-10-31 09:16:13 -04:00
* Copyright (c) 2000-2024 Monitoring Plugins Development Team
2024-10-31 09:14:31 -04:00
*
* 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 <http://www.gnu.org/licenses/>.
*
*
*****************************************************************************/
const char *progname = "check_users";
2024-10-31 09:16:13 -04:00
const char *copyright = "2000-2024";
const char *email = "devel@monitoring-plugins.org";
2025-03-12 17:01:46 -04:00
#include "check_users.d/users.h"
#include "output.h"
#include "perfdata.h"
#include "states.h"
#include "utils_base.h"
#include "./common.h"
#include "./utils.h"
#include "check_users.d/config.h"
#include "thresholds.h"
2014-04-21 15:51:19 -04:00
#if HAVE_WTSAPI32_H
2024-10-31 09:14:31 -04:00
# include <windows.h>
# include <wtsapi32.h>
# undef ERROR
# define ERROR -1
2014-04-21 15:51:19 -04:00
#elif HAVE_UTMPX_H
2024-10-31 09:14:31 -04:00
# include <utmpx.h>
#else
2024-10-31 09:14:31 -04:00
# include "popen.h"
#endif
#ifdef HAVE_LIBSYSTEMD
2024-10-31 09:14:31 -04:00
# include <systemd/sd-daemon.h>
# include <systemd/sd-login.h>
#endif
2025-03-12 17:01:46 -04:00
typedef struct process_argument_wrapper {
int errorcode;
check_users_config config;
} check_users_config_wrapper;
check_users_config_wrapper process_arguments(int /*argc*/, char ** /*argv*/);
2025-03-12 17:01:46 -04:00
void print_help(void);
2024-10-31 09:14:31 -04:00
void print_usage(void);
2024-10-31 09:14:31 -04:00
int main(int argc, char **argv) {
setlocale(LC_ALL, "");
bindtextdomain(PACKAGE, LOCALEDIR);
textdomain(PACKAGE);
/* Parse extra opts if any */
2024-10-31 09:14:31 -04:00
argv = np_extra_opts(&argc, argv, progname);
2025-03-12 17:01:46 -04:00
check_users_config_wrapper tmp_config = process_arguments(argc, argv);
if (tmp_config.errorcode == ERROR) {
2024-10-31 09:14:31 -04:00
usage4(_("Could not parse arguments"));
2025-03-12 16:34:58 -04:00
}
2025-03-12 17:01:46 -04:00
check_users_config config = tmp_config.config;
2014-04-21 15:51:19 -04:00
2025-03-12 17:01:46 -04:00
#ifdef _WIN32
# if HAVE_WTSAPI32_H
get_num_of_users_wrapper user_wrapper = get_num_of_users_windows();
# else
# error Did not find WTSAPI32
# endif // HAVE_WTSAPI32_H
#else
2025-03-12 17:01:46 -04:00
# ifdef HAVE_LIBSYSTEMD
get_num_of_users_wrapper user_wrapper = get_num_of_users_systemd();
# elif HAVE_UTMPX_H
get_num_of_users_wrapper user_wrapper = get_num_of_users_utmp();
# else // !HAVE_LIBSYSTEMD && !HAVE_UTMPX_H
get_num_of_users_wrapper user_wrapper = get_num_of_users_who_command();
# endif // HAVE_LIBSYSTEMD
#endif // _WIN32
mp_check overall = mp_check_init();
if (config.output_format_is_set) {
mp_set_format(config.output_format);
2025-03-12 16:34:58 -04:00
}
2025-03-12 17:01:46 -04:00
mp_subcheck sc_users = mp_subcheck_init();
2025-03-12 17:01:46 -04:00
if (user_wrapper.errorcode != 0) {
sc_users = mp_set_subcheck_state(sc_users, STATE_UNKNOWN);
sc_users.output = "Failed to retrieve number of users";
mp_add_subcheck_to_check(&overall, sc_users);
mp_exit(overall);
2025-03-12 16:34:58 -04:00
}
/* check the user count against warning and critical thresholds */
2025-03-12 17:01:46 -04:00
mp_perfdata users_pd = {
.label = "users",
.value = mp_create_pd_value(user_wrapper.users),
};
users_pd = mp_pd_set_thresholds(users_pd, config.thresholds);
mp_add_perfdata_to_subcheck(&sc_users, users_pd);
int tmp_status = mp_get_pd_status(users_pd);
sc_users = mp_set_subcheck_state(sc_users, tmp_status);
switch (tmp_status) {
case STATE_WARNING:
2025-09-15 06:59:37 -04:00
xasprintf(&sc_users.output,
"%d users currently logged in. This violates the warning threshold",
user_wrapper.users);
2025-03-12 17:01:46 -04:00
break;
case STATE_CRITICAL:
2025-09-15 06:59:37 -04:00
xasprintf(&sc_users.output,
"%d users currently logged in. This violates the critical threshold",
user_wrapper.users);
2025-03-12 17:01:46 -04:00
break;
default:
xasprintf(&sc_users.output, "%d users currently logged in", user_wrapper.users);
}
2025-03-12 17:01:46 -04:00
mp_add_subcheck_to_check(&overall, sc_users);
mp_exit(overall);
}
2025-03-12 17:01:46 -04:00
#define output_format_index CHAR_MAX + 1
/* process command-line arguments */
2025-03-12 17:01:46 -04:00
check_users_config_wrapper process_arguments(int argc, char **argv) {
2024-10-31 09:14:31 -04:00
static struct option longopts[] = {{"critical", required_argument, 0, 'c'},
{"warning", required_argument, 0, 'w'},
{"version", no_argument, 0, 'V'},
{"help", no_argument, 0, 'h'},
2025-03-12 17:01:46 -04:00
{"output-format", required_argument, 0, output_format_index},
2024-10-31 09:14:31 -04:00
{0, 0, 0, 0}};
2025-03-12 16:34:58 -04:00
if (argc < 2) {
2025-03-12 17:01:46 -04:00
usage(progname);
2025-03-12 16:34:58 -04:00
}
2025-03-12 17:01:46 -04:00
char *warning_range = NULL;
char *critical_range = NULL;
check_users_config_wrapper result = {
.config = check_users_config_init(),
.errorcode = OK,
};
2023-10-18 14:40:24 -04:00
while (true) {
2025-03-12 17:01:46 -04:00
int counter = getopt_long(argc, argv, "+hVvc:w:", longopts, NULL);
2025-03-12 17:01:46 -04:00
if (counter == -1 || counter == EOF || counter == 1) {
break;
2025-03-12 16:34:58 -04:00
}
2025-03-12 17:01:46 -04:00
switch (counter) {
2024-10-31 09:14:31 -04:00
case '?': /* print short usage statement if args not parsable */
usage5();
case 'h': /* help */
print_help();
exit(STATE_UNKNOWN);
case 'V': /* version */
print_revision(progname, NP_VERSION);
exit(STATE_UNKNOWN);
case 'c': /* critical */
critical_range = optarg;
break;
2024-10-31 09:14:31 -04:00
case 'w': /* warning */
warning_range = optarg;
break;
2025-03-12 17:01:46 -04:00
case output_format_index: {
parsed_output_format parser = mp_parse_output_format(optarg);
if (!parser.parsing_success) {
// TODO List all available formats here, maybe add anothoer usage function
printf("Invalid output format: %s\n", optarg);
exit(STATE_UNKNOWN);
}
2025-03-12 17:01:46 -04:00
result.config.output_format_is_set = true;
result.config.output_format = parser.output_format;
break;
}
}
2025-03-12 16:34:58 -04:00
}
2025-03-12 17:15:57 -04:00
int option_char = optind;
if (warning_range == NULL && argc > option_char) {
warning_range = argv[option_char++];
}
if (critical_range == NULL && argc > option_char) {
critical_range = argv[option_char++];
}
2025-03-12 17:01:46 -04:00
// TODO add proper verification for ranges here!
2025-03-12 17:15:57 -04:00
mp_range_parsed tmp;
2025-03-12 17:01:46 -04:00
if (warning_range) {
2025-03-12 17:15:57 -04:00
tmp = mp_parse_range_string(warning_range);
} else {
2025-03-12 18:37:36 -04:00
printf("Warning threshold missing\n");
print_usage();
exit(STATE_UNKNOWN);
2025-09-15 06:59:37 -04:00
}
2025-03-12 18:37:36 -04:00
2025-03-12 17:15:57 -04:00
if (tmp.error == MP_PARSING_SUCCES) {
result.config.thresholds.warning = tmp.range;
result.config.thresholds.warning_is_set = true;
} else {
printf("Failed to parse warning range: %s", warning_range);
exit(STATE_UNKNOWN);
}
2025-03-12 17:01:46 -04:00
if (critical_range) {
2025-03-12 17:15:57 -04:00
tmp = mp_parse_range_string(critical_range);
} else {
2025-03-12 18:37:36 -04:00
printf("Critical threshold missing\n");
print_usage();
exit(STATE_UNKNOWN);
2025-03-12 17:15:57 -04:00
}
2025-03-12 18:37:36 -04:00
2025-03-12 17:15:57 -04:00
if (tmp.error == MP_PARSING_SUCCES) {
result.config.thresholds.critical = tmp.range;
result.config.thresholds.critical_is_set = true;
} else {
printf("Failed to parse critical range: %s", critical_range);
exit(STATE_UNKNOWN);
}
2025-03-12 17:01:46 -04:00
return result;
}
2024-10-31 09:14:31 -04:00
void print_help(void) {
print_revision(progname, NP_VERSION);
2024-10-31 09:14:31 -04:00
printf("Copyright (c) 1999 Ethan Galstad\n");
printf(COPYRIGHT, copyright, email);
2024-10-31 09:14:31 -04:00
printf("%s\n", _("This plugin checks the number of users currently logged in on the local"));
2025-09-15 06:59:37 -04:00
printf("%s\n",
_("system and generates an error if the number exceeds the thresholds specified."));
2024-10-31 09:14:31 -04:00
printf("\n\n");
2024-10-31 09:14:31 -04:00
print_usage();
2024-10-31 09:14:31 -04:00
printf(UT_HELP_VRSN);
printf(UT_EXTRA_OPTS);
2024-10-31 09:14:31 -04:00
printf(" %s\n", "-w, --warning=RANGE_EXPRESSION");
2025-09-15 06:59:37 -04:00
printf(" %s\n",
_("Set WARNING status if number of logged in users violates RANGE_EXPRESSION"));
2024-10-31 09:14:31 -04:00
printf(" %s\n", "-c, --critical=RANGE_EXPRESSION");
2025-09-15 06:59:37 -04:00
printf(" %s\n",
_("Set CRITICAL status if number of logged in users violates RANGE_EXPRESSION"));
2025-03-12 17:01:46 -04:00
printf(UT_OUTPUT_FORMAT);
2024-10-31 09:14:31 -04:00
printf(UT_SUPPORT);
}
2024-10-31 09:14:31 -04:00
void print_usage(void) {
printf("%s\n", _("Usage:"));
printf("%s -w <users> -c <users>\n", progname);
}