From 665e2f91306fa9b38044a382e4b7571a0c8c0c5f Mon Sep 17 00:00:00 2001 From: Lorenz Kästle <12514511+RincewindsHat@users.noreply.github.com> Date: Sat, 22 Feb 2025 22:14:17 +0100 Subject: clang-format --- plugins/check_ssh.c | 38 +++++++++++++++++++++++++------------- 1 file changed, 25 insertions(+), 13 deletions(-) (limited to 'plugins') diff --git a/plugins/check_ssh.c b/plugins/check_ssh.c index 42a88cf9..a50ca530 100644 --- a/plugins/check_ssh.c +++ b/plugins/check_ssh.c @@ -64,8 +64,9 @@ int main(int argc, char **argv) { /* Parse extra opts if any */ argv = np_extra_opts(&argc, argv, progname); - if (process_arguments(argc, argv) == ERROR) + if (process_arguments(argc, argv) == ERROR) { usage4(_("Could not parse arguments")); + } /* initialize alarm signal handling */ signal(SIGALRM, socket_timeout_alarm_handler); @@ -95,20 +96,24 @@ int process_arguments(int argc, char **argv) { {"remote-protocol", required_argument, 0, 'P'}, {0, 0, 0, 0}}; - if (argc < 2) + if (argc < 2) { return ERROR; + } - for (int i = 1; i < argc; i++) - if (strcmp("-to", argv[i]) == 0) + for (int i = 1; i < argc; i++) { + if (strcmp("-to", argv[i]) == 0) { strcpy(argv[i], "-t"); + } + } int option_char; while (true) { int option = 0; option_char = getopt_long(argc, argv, "+Vhv46t:r:H:p:P:", longopts, &option); - if (option_char == -1 || option_char == EOF) + if (option_char == -1 || option_char == EOF) { break; + } switch (option_char) { case '?': /* help */ @@ -123,10 +128,11 @@ int process_arguments(int argc, char **argv) { verbose = true; break; case 't': /* timeout period */ - if (!is_integer(optarg)) + if (!is_integer(optarg)) { usage2(_("Timeout interval must be a positive integer"), optarg); - else + } else { socket_timeout = atoi(optarg); + } break; case '4': address_family = AF_INET; @@ -145,8 +151,9 @@ int process_arguments(int argc, char **argv) { remote_protocol = optarg; break; case 'H': /* host */ - if (!is_host(optarg)) + if (!is_host(optarg)) { usage2(_("Invalid hostname/address"), optarg); + } server_name = optarg; break; case 'p': /* port */ @@ -178,10 +185,12 @@ int process_arguments(int argc, char **argv) { } int validate_arguments(void) { - if (server_name == NULL) + if (server_name == NULL) { return ERROR; - if (port == -1) /* funky, but allows -p to override stray integer in args */ + } + if (port == -1) { /* funky, but allows -p to override stray integer in args */ port = SSH_DFL_PORT; + } return OK; } @@ -198,8 +207,9 @@ int ssh_connect(char *haddr, int hport, char *remote_version, char *remote_proto int socket; int result = my_tcp_connect(haddr, hport, &socket); - if (result != STATE_OK) + if (result != STATE_OK) { return result; + } char *output = (char *)calloc(BUFF_SZ + 1, sizeof(char)); char *buffer = NULL; @@ -259,8 +269,9 @@ int ssh_connect(char *haddr, int hport, char *remote_version, char *remote_proto * - RFC 4253:4.2 */ strip(version_control_string); - if (verbose) + if (verbose) { printf("%s\n", version_control_string); + } char *ssh_proto = version_control_string + 4; @@ -297,8 +308,9 @@ int ssh_connect(char *haddr, int hport, char *remote_version, char *remote_proto static char *rev_no = VERSION; xasprintf(&buffer, "SSH-%s-check_ssh_%s\r\n", ssh_proto, rev_no); send(socket, buffer, strlen(buffer), MSG_DONTWAIT); - if (verbose) + if (verbose) { printf("%s\n", buffer); + } if (remote_version && strcmp(remote_version, ssh_server)) { printf(_("SSH CRITICAL - %s (protocol %s) version mismatch, expected '%s'\n"), ssh_server, ssh_proto, remote_version); -- cgit v1.2.3-74-g34f1 From fb4f46f93da4ac50654fdcc2f26b2f37c73a9230 Mon Sep 17 00:00:00 2001 From: Lorenz Kästle <12514511+RincewindsHat@users.noreply.github.com> Date: Sat, 22 Feb 2025 22:39:10 +0100 Subject: Migrate check_users to new ouput infrastructure --- plugins/Makefile.am | 1 + plugins/check_users.c | 249 ++++++++++++++++++++--------------------- plugins/check_users.d/config.h | 19 ++++ plugins/check_users.d/users.c | 166 +++++++++++++++++++++++++++ plugins/check_users.d/users.h | 18 +++ 5 files changed, 323 insertions(+), 130 deletions(-) create mode 100644 plugins/check_users.d/config.h create mode 100644 plugins/check_users.d/users.c create mode 100644 plugins/check_users.d/users.h (limited to 'plugins') diff --git a/plugins/Makefile.am b/plugins/Makefile.am index d43c1971..e0069653 100644 --- a/plugins/Makefile.am +++ b/plugins/Makefile.am @@ -125,6 +125,7 @@ check_time_LDADD = $(NETLIBS) check_ntp_time_LDADD = $(NETLIBS) $(MATHLIBS) check_ups_LDADD = $(NETLIBS) check_users_LDADD = $(BASEOBJS) $(WTSAPI32LIBS) $(SYSTEMDLIBS) +check_users_SOURCES = check_users.c check_users.d/users.c check_by_ssh_LDADD = $(NETLIBS) check_ide_smart_LDADD = $(BASEOBJS) negate_LDADD = $(BASEOBJS) diff --git a/plugins/check_users.c b/plugins/check_users.c index f1e1c39d..6ec87d21 100644 --- a/plugins/check_users.c +++ b/plugins/check_users.c @@ -3,7 +3,7 @@ * Monitoring check_users plugin * * License: GPL - * Copyright (c) 2000-2024 Monitoring Plugins Development Team + * Copyright (c) 2000-2025 Monitoring Plugins Development Team * * Description: * @@ -30,12 +30,19 @@ * *****************************************************************************/ +#include "check_users.d/config.h" +#include "thresholds.h" const char *progname = "check_users"; -const char *copyright = "2000-2024"; +const char *copyright = "2000-2025"; const char *email = "devel@monitoring-plugins.org"; -#include "common.h" -#include "utils.h" +#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" #if HAVE_WTSAPI32_H # include @@ -53,29 +60,16 @@ const char *email = "devel@monitoring-plugins.org"; # include #endif -#define possibly_set(a, b) ((a) == 0 ? (b) : 0) +typedef struct process_argument_wrapper { + int errorcode; + check_users_config config; +} process_argument_wrapper; -static int process_arguments(int, char **); -static void print_help(void); +process_argument_wrapper process_arguments(int /*argc*/, char ** /*argv*/); +void print_help(void); void print_usage(void); -static char *warning_range = NULL; -static char *critical_range = NULL; -static thresholds *thlds = NULL; - int main(int argc, char **argv) { - int users = -1; - int result = STATE_UNKNOWN; -#if HAVE_WTSAPI32_H - WTS_SESSION_INFO *wtsinfo; - DWORD wtscount; - DWORD index; -#elif HAVE_UTMPX_H - struct utmpx *putmpx; -#else - char input_buffer[MAX_INPUT_BUFFER]; -#endif - setlocale(LC_ALL, ""); bindtextdomain(PACKAGE, LOCALEDIR); textdomain(PACKAGE); @@ -83,121 +77,106 @@ int main(int argc, char **argv) { /* Parse extra opts if any */ argv = np_extra_opts(&argc, argv, progname); - if (process_arguments(argc, argv) == ERROR) - usage4(_("Could not parse arguments")); - - 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)) { - printf(_("Could not enumerate RD sessions: %d\n"), GetLastError()); - return STATE_UNKNOWN; - } - - for (index = 0; index < wtscount; index++) { - LPTSTR username; - DWORD size; - int len; - - if (!WTSQuerySessionInformation(WTS_CURRENT_SERVER_HANDLE, wtsinfo[index].SessionId, WTSUserName, &username, &size)) - continue; - - len = lstrlen(username); - - WTSFreeMemory(username); - - if (len == 0) - continue; - - if (wtsinfo[index].State == WTSActive || wtsinfo[index].State == WTSDisconnected) - users++; - } - - WTSFreeMemory(wtsinfo); -#elif HAVE_UTMPX_H - /* get currently logged users from utmpx */ - setutxent(); - - while ((putmpx = getutxent()) != NULL) - if (putmpx->ut_type == USER_PROCESS) - users++; - - endutxent(); -#else - /* run the command */ - child_process = spopen(WHO_COMMAND); - if (child_process == NULL) { - printf(_("Could not open pipe: %s\n"), WHO_COMMAND); - return STATE_UNKNOWN; - } - - child_stderr = fdopen(child_stderr_array[fileno(child_process)], "r"); - if (child_stderr == NULL) - printf(_("Could not open stderr for %s\n"), WHO_COMMAND); + process_argument_wrapper tmp = process_arguments(argc, argv); - while (fgets(input_buffer, MAX_INPUT_BUFFER - 1, child_process)) { - /* increment 'users' on all lines except total user count */ - if (input_buffer[0] != '#') { - users++; - continue; - } - - /* get total logged in users */ - if (sscanf(input_buffer, _("# users=%d"), &users) == 1) - break; + if (tmp.errorcode == ERROR) { + usage4(_("Could not parse arguments")); } - /* check STDERR */ - if (fgets(input_buffer, MAX_INPUT_BUFFER - 1, child_stderr)) - result = possibly_set(result, STATE_UNKNOWN); - (void)fclose(child_stderr); + check_users_config config = tmp.config; - /* close the pipe */ - if (spclose(child_process)) - result = possibly_set(result, STATE_UNKNOWN); -#endif -#ifdef HAVE_LIBSYSTEMD +#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 +# 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(); + mp_subcheck sc_users = mp_subcheck_init(); + + 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); } -#endif - /* check the user count against warning and critical thresholds */ - result = get_status((double)users, thlds); - if (result == STATE_UNKNOWN) - printf("%s\n", _("Unable to read output")); - else { - 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)); + 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: + xasprintf(&sc_users.output, "%d users currently logged in. This violates the warning threshold", user_wrapper.users); + break; + case STATE_CRITICAL: + xasprintf(&sc_users.output, "%d users currently logged in. This violates the critical threshold", user_wrapper.users); + break; + default: + xasprintf(&sc_users.output, "%d users currently logged in", user_wrapper.users); } - return result; + mp_add_subcheck_to_check(&overall, sc_users); + mp_exit(overall); } +#define output_format_index CHAR_MAX + 1 + /* process command-line arguments */ -int process_arguments(int argc, char **argv) { +process_argument_wrapper process_arguments(int argc, char **argv) { static struct option longopts[] = {{"critical", required_argument, 0, 'c'}, {"warning", required_argument, 0, 'w'}, {"version", no_argument, 0, 'V'}, {"help", no_argument, 0, 'h'}, + {"output-format", required_argument, 0, output_format_index}, {0, 0, 0, 0}}; - if (argc < 2) + if (argc < 2) { usage("\n"); + } + + char *warning_range = NULL; + char *critical_range = NULL; + check_users_config config = check_users_config_init(); - int option_char; while (true) { - int option = 0; - option_char = getopt_long(argc, argv, "+hVvc:w:", longopts, &option); + int counter = getopt_long(argc, argv, "+hVvc:w:", longopts, NULL); - if (option_char == -1 || option_char == EOF || option_char == 1) + if (counter == -1 || counter == EOF || counter == 1) { break; + } - switch (option_char) { + switch (counter) { + 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); + } + + config.output_format_is_set = true; + config.output_format = parser.output_format; + break; + } case '?': /* print short usage statement if args not parsable */ usage5(); case 'h': /* help */ @@ -215,26 +194,35 @@ int process_arguments(int argc, char **argv) { } } - 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++]; - - /* this will abort in case of invalid ranges */ - set_thresholds(&thlds, warning_range, critical_range); - - if (!thlds->warning) { - usage4(_("Warning threshold must be a valid range expression")); + // TODO add proper verification for ranges here! + if (warning_range) { + mp_range_parsed tmp = mp_parse_range_string(warning_range); + if (tmp.error == MP_PARSING_SUCCES) { + config.thresholds.warning = tmp.range; + config.thresholds.warning_is_set = true; + } else { + printf("Failed to parse warning range: %s", warning_range); + exit(STATE_UNKNOWN); + } } - if (!thlds->critical) { - usage4(_("Critical threshold must be a valid range expression")); + if (critical_range) { + mp_range_parsed tmp = mp_parse_range_string(critical_range); + if (tmp.error == MP_PARSING_SUCCES) { + config.thresholds.critical = tmp.range; + config.thresholds.critical_is_set = true; + } else { + printf("Failed to parse critical range: %s", critical_range); + exit(STATE_UNKNOWN); + } } - return OK; + process_argument_wrapper result = { + .errorcode = OK, + .config = config, + }; + + return result; } void print_help(void) { @@ -257,6 +245,7 @@ void print_help(void) { printf(" %s\n", _("Set WARNING status if number of logged in users violates RANGE_EXPRESSION")); printf(" %s\n", "-c, --critical=RANGE_EXPRESSION"); printf(" %s\n", _("Set CRITICAL status if number of logged in users violates RANGE_EXPRESSION")); + printf(UT_OUTPUT_FORMAT); printf(UT_SUPPORT); } diff --git a/plugins/check_users.d/config.h b/plugins/check_users.d/config.h new file mode 100644 index 00000000..35b7b7f8 --- /dev/null +++ b/plugins/check_users.d/config.h @@ -0,0 +1,19 @@ +#pragma once + +#include "output.h" +#include "thresholds.h" +typedef struct check_users_config { + mp_thresholds thresholds; + + bool output_format_is_set; + mp_output_format output_format; +} check_users_config; + +check_users_config check_users_config_init() { + check_users_config tmp = { + .thresholds = mp_thresholds_init(), + + .output_format_is_set = false, + }; + return tmp; +} diff --git a/plugins/check_users.d/users.c b/plugins/check_users.d/users.c new file mode 100644 index 00000000..7969ae79 --- /dev/null +++ b/plugins/check_users.d/users.c @@ -0,0 +1,166 @@ +#include "./users.h" + +#ifdef _WIN32 +# ifdef HAVE_WTSAPI32_H +# include +# include +# undef ERROR +# define ERROR -1 + +get_num_of_users_wrapper get_num_of_users_windows() { + WTS_SESSION_INFO *wtsinfo; + DWORD wtscount; + + get_num_of_users_wrapper result = {}; + + if (!WTSEnumerateSessions(WTS_CURRENT_SERVER_HANDLE, 0, 1, &wtsinfo, &wtscount)) { + // printf(_("Could not enumerate RD sessions: %d\n"), GetLastError()); + result.error = WINDOWS_COULD_NOT_ENUMERATE_SESSIONS; + return result; + } + + for (DWORD index = 0; index < wtscount; index++) { + LPTSTR username; + DWORD size; + + if (!WTSQuerySessionInformation(WTS_CURRENT_SERVER_HANDLE, wtsinfo[index].SessionId, WTSUserName, &username, &size)) { + continue; + } + + int len = lstrlen(username); + + WTSFreeMemory(username); + + if (len == 0) { + continue; + } + + if (wtsinfo[index].State == WTSActive || wtsinfo[index].State == WTSDisconnected) { + result.users++; + } + } + + WTSFreeMemory(wtsinfo); + return result; +} +# else // HAVE_WTSAPI32_H +# error On windows but without the WTSAPI32 lib +# endif // HAVE_WTSAPI32_H + +#else // _WIN32 + +# include "../../config.h" + +# ifdef HAVE_LIBSYSTEMD +# include +# include + +get_num_of_users_wrapper get_num_of_users_systemd() { + get_num_of_users_wrapper result = {}; + + // Test whether we booted with systemd + if (sd_booted() > 0) { + int users = sd_get_sessions(NULL); + if (users >= 0) { + // Success + result.users = users; + return result; + } + + // Failure! return the error code + result.errorcode = users; + return result; + } + + // Looks like we are not running systemd, + // return with error here + result.errorcode = NO_SYSTEMD_ERROR; + return result; +} +# endif + +# ifdef HAVE_UTMPX_H +# include + +get_num_of_users_wrapper get_num_of_users_utmp() { + int users = 0; + + /* get currently logged users from utmpx */ + setutxent(); + + struct utmpx *putmpx; + while ((putmpx = getutxent()) != NULL) { + if (putmpx->ut_type == USER_PROCESS) { + users++; + } + } + + endutxent(); + + get_num_of_users_wrapper result = { + .errorcode = 0, + .users = users, + }; + + return result; +} +# endif + +# ifndef HAVE_WTSAPI32_H +# ifndef HAVE_LIBSYSTEMD +# ifndef HAVE_UTMPX_H +// Fall back option here for the others (probably still not on windows) + +# include "../popen.h" +# include "../common.h" +# include "../utils.h" + +get_num_of_users_wrapper get_num_of_users_who_command() { + /* run the command */ + child_process = spopen(WHO_COMMAND); + if (child_process == NULL) { + // printf(_("Could not open pipe: %s\n"), WHO_COMMAND); + get_num_of_users_wrapper result = { + .errorcode = COULD_NOT_OPEN_PIPE, + }; + return result; + } + + child_stderr = fdopen(child_stderr_array[fileno(child_process)], "r"); + if (child_stderr == NULL) { + // printf(_("Could not open stderr for %s\n"), WHO_COMMAND); + // TODO this error should probably be reported + } + + get_num_of_users_wrapper result = {}; + char input_buffer[MAX_INPUT_BUFFER]; + while (fgets(input_buffer, MAX_INPUT_BUFFER - 1, child_process)) { + /* increment 'users' on all lines except total user count */ + if (input_buffer[0] != '#') { + result.users++; + continue; + } + + /* get total logged in users */ + if (sscanf(input_buffer, _("# users=%d"), &result.users) == 1) { + break; + } + } + + /* check STDERR */ + if (fgets(input_buffer, MAX_INPUT_BUFFER - 1, child_stderr)) { + // if this fails, something broke and the result can not be relied upon or so is the theorie here + result.errorcode = STDERR_COULD_NOT_BE_READ; + } + (void)fclose(child_stderr); + + /* close the pipe */ + spclose(child_process); + + return result; +} + +# endif +# endif +# endif +#endif diff --git a/plugins/check_users.d/users.h b/plugins/check_users.d/users.h new file mode 100644 index 00000000..aacba775 --- /dev/null +++ b/plugins/check_users.d/users.h @@ -0,0 +1,18 @@ +#pragma once + +typedef struct get_num_of_users_wrapper { + int errorcode; + int users; +} get_num_of_users_wrapper; + +enum { + NO_SYSTEMD_ERROR = 64, + WINDOWS_COULD_NOT_ENUMERATE_SESSIONS, + COULD_NOT_OPEN_PIPE, + STDERR_COULD_NOT_BE_READ, +}; + +get_num_of_users_wrapper get_num_of_users_systemd(); +get_num_of_users_wrapper get_num_of_users_utmp(); +get_num_of_users_wrapper get_num_of_users_windows(); +get_num_of_users_wrapper get_num_of_users_who_command(); -- cgit v1.2.3-74-g34f1 From c87bc7eee4b83571199ffd14b70bfca5418ec101 Mon Sep 17 00:00:00 2001 From: Lorenz Kästle <12514511+RincewindsHat@users.noreply.github.com> Date: Tue, 25 Feb 2025 10:14:29 +0100 Subject: check_ssh: centralize configuration in external header --- plugins/check_ssh.c | 62 +++++++++++++++++++++++++------------------- plugins/check_ssh.d/config.h | 21 +++++++++++++++ 2 files changed, 56 insertions(+), 27 deletions(-) create mode 100644 plugins/check_ssh.d/config.h (limited to 'plugins') diff --git a/plugins/check_ssh.c b/plugins/check_ssh.c index a50ca530..3745f799 100644 --- a/plugins/check_ssh.c +++ b/plugins/check_ssh.c @@ -35,6 +35,7 @@ const char *email = "devel@monitoring-plugins.org"; #include "./common.h" #include "./netutils.h" #include "utils.h" +#include "./check_ssh.d/config.h" #ifndef MSG_DONTWAIT # define MSG_DONTWAIT 0 @@ -43,14 +44,14 @@ const char *email = "devel@monitoring-plugins.org"; #define SSH_DFL_PORT 22 #define BUFF_SZ 256 -static int port = -1; -static char *server_name = NULL; -static char *remote_version = NULL; -static char *remote_protocol = NULL; static bool verbose = false; -static int process_arguments(int /*argc*/, char ** /*argv*/); -static int validate_arguments(void); +typedef struct process_arguments_wrapper { + int errorcode; + check_ssh_config config; +} process_arguments_wrapper; + +static process_arguments_wrapper process_arguments(int /*argc*/, char ** /*argv*/); static void print_help(void); void print_usage(void); @@ -64,17 +65,21 @@ int main(int argc, char **argv) { /* Parse extra opts if any */ argv = np_extra_opts(&argc, argv, progname); - if (process_arguments(argc, argv) == ERROR) { + process_arguments_wrapper tmp_config = process_arguments(argc, argv); + + if (tmp_config.errorcode == ERROR) { usage4(_("Could not parse arguments")); } + check_ssh_config config = tmp_config.config; + /* initialize alarm signal handling */ signal(SIGALRM, socket_timeout_alarm_handler); alarm(socket_timeout); /* ssh_connect exits if error is found */ - int result = ssh_connect(server_name, port, remote_version, remote_protocol); + int result = ssh_connect(config.server_name, config.port, config.remote_version, config.remote_protocol); alarm(0); @@ -82,7 +87,7 @@ int main(int argc, char **argv) { } /* process command-line arguments */ -int process_arguments(int argc, char **argv) { +process_arguments_wrapper process_arguments(int argc, char **argv) { static struct option longopts[] = {{"help", no_argument, 0, 'h'}, {"version", no_argument, 0, 'V'}, {"host", required_argument, 0, 'H'}, /* backward compatibility */ @@ -96,8 +101,14 @@ int process_arguments(int argc, char **argv) { {"remote-protocol", required_argument, 0, 'P'}, {0, 0, 0, 0}}; + process_arguments_wrapper result = { + .config = check_ssh_config_init(), + .errorcode = OK, + }; + if (argc < 2) { - return ERROR; + result.errorcode = ERROR; + return result; } for (int i = 1; i < argc; i++) { @@ -145,20 +156,20 @@ int process_arguments(int argc, char **argv) { #endif break; case 'r': /* remote version */ - remote_version = optarg; + result.config.remote_version = optarg; break; case 'P': /* remote version */ - remote_protocol = optarg; + result.config.remote_protocol = optarg; break; case 'H': /* host */ if (!is_host(optarg)) { usage2(_("Invalid hostname/address"), optarg); } - server_name = optarg; + result.config.server_name = optarg; break; case 'p': /* port */ if (is_intpos(optarg)) { - port = atoi(optarg); + result.config.port = atoi(optarg); } else { usage2(_("Port number must be a positive integer"), optarg); } @@ -166,32 +177,29 @@ int process_arguments(int argc, char **argv) { } option_char = optind; - if (server_name == NULL && option_char < argc) { + if (result.config.server_name == NULL && option_char < argc) { if (is_host(argv[option_char])) { - server_name = argv[option_char++]; + result.config.server_name = argv[option_char++]; } } - if (port == -1 && option_char < argc) { + if (result.config.port == -1 && option_char < argc) { if (is_intpos(argv[option_char])) { - port = atoi(argv[option_char++]); + result.config.port = atoi(argv[option_char++]); } else { print_usage(); exit(STATE_UNKNOWN); } } - return validate_arguments(); -} - -int validate_arguments(void) { - if (server_name == NULL) { - return ERROR; + if (result.config.server_name == NULL) { + result.errorcode = ERROR; + return result; } - if (port == -1) { /* funky, but allows -p to override stray integer in args */ - port = SSH_DFL_PORT; + if (result.config.port == -1) { /* funky, but allows -p to override stray integer in args */ + result.config.port = SSH_DFL_PORT; } - return OK; + return result; } /************************************************************************ diff --git a/plugins/check_ssh.d/config.h b/plugins/check_ssh.d/config.h new file mode 100644 index 00000000..05698d83 --- /dev/null +++ b/plugins/check_ssh.d/config.h @@ -0,0 +1,21 @@ +#pragma once + +#include + +typedef struct check_ssh_config { + int port; + char *server_name; + char *remote_version; + char *remote_protocol; +} check_ssh_config; + +check_ssh_config check_ssh_config_init(void) { + check_ssh_config tmp = { + .port = -1, + .server_name = NULL, + .remote_version = NULL, + .remote_protocol = NULL, + }; + + return tmp; +} -- cgit v1.2.3-74-g34f1 From ed06df7f34ad72439b2a0ebb0c0e527d2435050a Mon Sep 17 00:00:00 2001 From: Lorenz Kästle <12514511+RincewindsHat@users.noreply.github.com> Date: Tue, 25 Feb 2025 11:26:36 +0100 Subject: check_ssh: Migrate to new output infrastructure --- plugins/check_ssh.c | 99 ++++++++++++++++++++++++++++++++++---------- plugins/check_ssh.d/config.h | 6 +++ 2 files changed, 84 insertions(+), 21 deletions(-) (limited to 'plugins') diff --git a/plugins/check_ssh.c b/plugins/check_ssh.c index 3745f799..62c8b891 100644 --- a/plugins/check_ssh.c +++ b/plugins/check_ssh.c @@ -28,6 +28,9 @@ * *****************************************************************************/ +#include "output.h" +#include "perfdata.h" +#include "states.h" const char *progname = "check_ssh"; const char *copyright = "2000-2024"; const char *email = "devel@monitoring-plugins.org"; @@ -55,7 +58,7 @@ static process_arguments_wrapper process_arguments(int /*argc*/, char ** /*argv* static void print_help(void); void print_usage(void); -static int ssh_connect(char *haddr, int hport, char *remote_version, char *remote_protocol); +static int ssh_connect(mp_check *overall, char *haddr, int hport, char *remote_version, char *remote_protocol); int main(int argc, char **argv) { setlocale(LC_ALL, ""); @@ -78,14 +81,21 @@ int main(int argc, char **argv) { alarm(socket_timeout); + mp_check overall = mp_check_init(); + if (config.output_format_is_set) { + overall.format = config.output_format; + } + /* ssh_connect exits if error is found */ - int result = ssh_connect(config.server_name, config.port, config.remote_version, config.remote_protocol); + ssh_connect(&overall, config.server_name, config.port, config.remote_version, config.remote_protocol); alarm(0); - return (result); + mp_exit(overall); } +#define output_format_index CHAR_MAX + 1 + /* process command-line arguments */ process_arguments_wrapper process_arguments(int argc, char **argv) { static struct option longopts[] = {{"help", no_argument, 0, 'h'}, @@ -99,6 +109,7 @@ process_arguments_wrapper process_arguments(int argc, char **argv) { {"verbose", no_argument, 0, 'v'}, {"remote-version", required_argument, 0, 'r'}, {"remote-protocol", required_argument, 0, 'P'}, + {"output-format", required_argument, 0, output_format_index}, {0, 0, 0, 0}}; process_arguments_wrapper result = { @@ -173,6 +184,18 @@ process_arguments_wrapper process_arguments(int argc, char **argv) { } else { usage2(_("Port number must be a positive integer"), optarg); } + 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); + } + + result.config.output_format_is_set = true; + result.config.output_format = parser.output_format; + break; + } } } @@ -208,7 +231,7 @@ process_arguments_wrapper process_arguments(int argc, char **argv) { * *-----------------------------------------------------------------------*/ -int ssh_connect(char *haddr, int hport, char *remote_version, char *remote_protocol) { +int ssh_connect(mp_check *overall, char *haddr, int hport, char *desired_remote_version, char *desired_remote_protocol) { struct timeval tv; gettimeofday(&tv, NULL); @@ -260,15 +283,25 @@ int ssh_connect(char *haddr, int hport, char *remote_version, char *remote_proto } } + mp_subcheck connection_sc = mp_subcheck_init(); if (recv_ret < 0) { - printf("SSH CRITICAL - %s", strerror(errno)); - exit(STATE_CRITICAL); + connection_sc = mp_set_subcheck_state(connection_sc, STATE_CRITICAL); + xasprintf(&connection_sc.output, "%s", "SSH CRITICAL - %s", strerror(errno)); + mp_add_subcheck_to_check(overall, connection_sc); + return OK; } if (version_control_string == NULL) { - printf("SSH CRITICAL - No version control string received"); - exit(STATE_CRITICAL); + connection_sc = mp_set_subcheck_state(connection_sc, STATE_CRITICAL); + xasprintf(&connection_sc.output, "%s", "SSH CRITICAL - No version control string received"); + mp_add_subcheck_to_check(overall, connection_sc); + return OK; } + + connection_sc = mp_set_subcheck_state(connection_sc, STATE_OK); + xasprintf(&connection_sc.output, "%s", "Initial connection succeded"); + mp_add_subcheck_to_check(overall, connection_sc); + /* * "When the connection has been established, both sides MUST send an * identification string. This identification string MUST be @@ -307,10 +340,19 @@ int ssh_connect(char *haddr, int hport, char *remote_version, char *remote_proto if (tmp) { ssh_server[tmp - ssh_server] = '\0'; } + + mp_subcheck protocol_validity_sc = mp_subcheck_init(); if (strlen(ssh_proto) == 0 || strlen(ssh_server) == 0) { - printf(_("SSH CRITICAL - Invalid protocol version control string %s\n"), version_control_string); - exit(STATE_CRITICAL); + protocol_validity_sc = mp_set_subcheck_state(protocol_validity_sc, STATE_CRITICAL); + xasprintf(&protocol_validity_sc.output, "Invalid protocol version control string %s", version_control_string); + mp_add_subcheck_to_check(overall, protocol_validity_sc); + return OK; } + + protocol_validity_sc = mp_set_subcheck_state(protocol_validity_sc, STATE_OK); + xasprintf(&protocol_validity_sc.output, "Valid protocol version control string %s", version_control_string); + mp_add_subcheck_to_check(overall, protocol_validity_sc); + ssh_proto[strspn(ssh_proto, "0123456789. ")] = 0; static char *rev_no = VERSION; @@ -320,24 +362,38 @@ int ssh_connect(char *haddr, int hport, char *remote_version, char *remote_proto printf("%s\n", buffer); } - if (remote_version && strcmp(remote_version, ssh_server)) { - printf(_("SSH CRITICAL - %s (protocol %s) version mismatch, expected '%s'\n"), ssh_server, ssh_proto, remote_version); + if (desired_remote_version && strcmp(desired_remote_version, ssh_server)) { + mp_subcheck remote_version_sc = mp_subcheck_init(); + remote_version_sc = mp_set_subcheck_state(remote_version_sc, STATE_CRITICAL); + xasprintf(&remote_version_sc.output, _("%s (protocol %s) version mismatch, expected '%s'"), ssh_server, ssh_proto, + desired_remote_version); close(socket); - exit(STATE_CRITICAL); + mp_add_subcheck_to_check(overall, remote_version_sc); + return OK; } double elapsed_time = (double)deltime(tv) / 1.0e6; - if (remote_protocol && strcmp(remote_protocol, ssh_proto)) { - printf(_("SSH CRITICAL - %s (protocol %s) protocol version mismatch, expected '%s' | %s\n"), ssh_server, ssh_proto, remote_protocol, - fperfdata("time", elapsed_time, "s", false, 0, false, 0, true, 0, true, (int)socket_timeout)); - close(socket); - exit(STATE_CRITICAL); + mp_perfdata time_pd = perfdata_init(); + time_pd.value = mp_create_pd_value(elapsed_time); + time_pd.label = "time"; + time_pd.max_present = true; + time_pd.max = mp_create_pd_value(socket_timeout); + + mp_subcheck protocol_version_sc = mp_subcheck_init(); + mp_add_perfdata_to_subcheck(&protocol_version_sc, time_pd); + + if (desired_remote_protocol && strcmp(desired_remote_protocol, ssh_proto)) { + protocol_version_sc = mp_set_subcheck_state(protocol_version_sc, STATE_CRITICAL); + xasprintf(&protocol_version_sc.output, _("%s (protocol %s) protocol version mismatch, expected '%s'"), ssh_server, ssh_proto, + desired_remote_protocol); + } else { + protocol_version_sc = mp_set_subcheck_state(protocol_version_sc, STATE_OK); + xasprintf(&protocol_version_sc.output, "SSH server verison: %s (protocol version: %s)", ssh_server, ssh_proto); } - printf(_("SSH OK - %s (protocol %s) | %s\n"), ssh_server, ssh_proto, - fperfdata("time", elapsed_time, "s", false, 0, false, 0, true, 0, true, (int)socket_timeout)); + mp_add_subcheck_to_check(overall, protocol_version_sc); close(socket); - exit(STATE_OK); + return OK; } void print_help(void) { @@ -369,6 +425,7 @@ void print_help(void) { printf(" %s\n", "-P, --remote-protocol=STRING"); printf(" %s\n", _("Alert if protocol doesn't match expected protocol version (ex: 2.0)")); + printf(UT_OUTPUT_FORMAT); printf(UT_VERBOSE); diff --git a/plugins/check_ssh.d/config.h b/plugins/check_ssh.d/config.h index 05698d83..d739c57c 100644 --- a/plugins/check_ssh.d/config.h +++ b/plugins/check_ssh.d/config.h @@ -1,12 +1,16 @@ #pragma once #include +#include "../../lib/monitoringplug.h" typedef struct check_ssh_config { int port; char *server_name; char *remote_version; char *remote_protocol; + + bool output_format_is_set; + mp_output_format output_format; } check_ssh_config; check_ssh_config check_ssh_config_init(void) { @@ -15,6 +19,8 @@ check_ssh_config check_ssh_config_init(void) { .server_name = NULL, .remote_version = NULL, .remote_protocol = NULL, + + .output_format_is_set = false, }; return tmp; -- cgit v1.2.3-74-g34f1 From 5ee9a5eadd7c9252fc4deb6cc502535d8e0e49d5 Mon Sep 17 00:00:00 2001 From: Lorenz Kästle <12514511+RincewindsHat@users.noreply.github.com> Date: Tue, 25 Feb 2025 11:28:29 +0100 Subject: check_ssh: modify usage string to avoid old call syntax --- plugins/check_ssh.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'plugins') diff --git a/plugins/check_ssh.c b/plugins/check_ssh.c index 62c8b891..0f1c0835 100644 --- a/plugins/check_ssh.c +++ b/plugins/check_ssh.c @@ -434,5 +434,5 @@ void print_help(void) { void print_usage(void) { printf("%s\n", _("Usage:")); - printf("%s [-4|-6] [-t ] [-r ] [-p ] \n", progname); + printf("%s [-4|-6] [-t ] [-r ] [-p ] --hostname \n", progname); } -- cgit v1.2.3-74-g34f1 From add5bfb1e4029e406f7f331407c9d0f28d6d789b Mon Sep 17 00:00:00 2001 From: Lorenz Kästle <12514511+RincewindsHat@users.noreply.github.com> Date: Tue, 25 Feb 2025 11:32:18 +0100 Subject: check_ssh: Move default SSH constant around a bit --- plugins/check_ssh.c | 7 ++----- plugins/check_ssh.d/config.h | 4 +++- 2 files changed, 5 insertions(+), 6 deletions(-) (limited to 'plugins') diff --git a/plugins/check_ssh.c b/plugins/check_ssh.c index 0f1c0835..b73cdf24 100644 --- a/plugins/check_ssh.c +++ b/plugins/check_ssh.c @@ -44,7 +44,6 @@ const char *email = "devel@monitoring-plugins.org"; # define MSG_DONTWAIT 0 #endif -#define SSH_DFL_PORT 22 #define BUFF_SZ 256 static bool verbose = false; @@ -219,9 +218,7 @@ process_arguments_wrapper process_arguments(int argc, char **argv) { result.errorcode = ERROR; return result; } - if (result.config.port == -1) { /* funky, but allows -p to override stray integer in args */ - result.config.port = SSH_DFL_PORT; - } + return result; } @@ -398,7 +395,7 @@ int ssh_connect(mp_check *overall, char *haddr, int hport, char *desired_remote_ void print_help(void) { char *myport; - xasprintf(&myport, "%d", SSH_DFL_PORT); + xasprintf(&myport, "%d", default_ssh_port); print_revision(progname, NP_VERSION); diff --git a/plugins/check_ssh.d/config.h b/plugins/check_ssh.d/config.h index d739c57c..c150fd30 100644 --- a/plugins/check_ssh.d/config.h +++ b/plugins/check_ssh.d/config.h @@ -3,6 +3,8 @@ #include #include "../../lib/monitoringplug.h" +const int default_ssh_port = 22; + typedef struct check_ssh_config { int port; char *server_name; @@ -15,7 +17,7 @@ typedef struct check_ssh_config { check_ssh_config check_ssh_config_init(void) { check_ssh_config tmp = { - .port = -1, + .port = default_ssh_port, .server_name = NULL, .remote_version = NULL, .remote_protocol = NULL, -- cgit v1.2.3-74-g34f1 From 9ea4dbc25352c7cb408e392e5ae9011b9b0ff3d6 Mon Sep 17 00:00:00 2001 From: Lorenz Kästle <12514511+RincewindsHat@users.noreply.github.com> Date: Tue, 25 Feb 2025 11:35:34 +0100 Subject: check_ssh: move only time relevant stuff in timeout area --- plugins/check_ssh.c | 9 ++++----- 1 file changed, 4 insertions(+), 5 deletions(-) (limited to 'plugins') diff --git a/plugins/check_ssh.c b/plugins/check_ssh.c index b73cdf24..3e25910d 100644 --- a/plugins/check_ssh.c +++ b/plugins/check_ssh.c @@ -75,16 +75,15 @@ int main(int argc, char **argv) { check_ssh_config config = tmp_config.config; - /* initialize alarm signal handling */ - signal(SIGALRM, socket_timeout_alarm_handler); - - alarm(socket_timeout); - mp_check overall = mp_check_init(); if (config.output_format_is_set) { overall.format = config.output_format; } + /* initialize alarm signal handling */ + signal(SIGALRM, socket_timeout_alarm_handler); + alarm(socket_timeout); + /* ssh_connect exits if error is found */ ssh_connect(&overall, config.server_name, config.port, config.remote_version, config.remote_protocol); -- cgit v1.2.3-74-g34f1 From b48ec884be9ee0026c20575f06a799b2444e284b Mon Sep 17 00:00:00 2001 From: Lorenz Kästle <12514511+RincewindsHat@users.noreply.github.com> Date: Tue, 25 Feb 2025 11:36:54 +0100 Subject: check_ssh: Verify that timeout is a positive integer --- plugins/check_ssh.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'plugins') diff --git a/plugins/check_ssh.c b/plugins/check_ssh.c index 3e25910d..fe8681e2 100644 --- a/plugins/check_ssh.c +++ b/plugins/check_ssh.c @@ -148,7 +148,7 @@ process_arguments_wrapper process_arguments(int argc, char **argv) { verbose = true; break; case 't': /* timeout period */ - if (!is_integer(optarg)) { + if (!is_intpos(optarg)) { usage2(_("Timeout interval must be a positive integer"), optarg); } else { socket_timeout = atoi(optarg); -- cgit v1.2.3-74-g34f1 From ce3eff0908b33140c5a99ee21d1c10405cbe9030 Mon Sep 17 00:00:00 2001 From: Lorenz Kästle <12514511+RincewindsHat@users.noreply.github.com> Date: Tue, 25 Feb 2025 11:40:32 +0100 Subject: check_ssh: no more implicit conversion --- plugins/check_ssh.c | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) (limited to 'plugins') diff --git a/plugins/check_ssh.c b/plugins/check_ssh.c index fe8681e2..518950ec 100644 --- a/plugins/check_ssh.c +++ b/plugins/check_ssh.c @@ -151,7 +151,7 @@ process_arguments_wrapper process_arguments(int argc, char **argv) { if (!is_intpos(optarg)) { usage2(_("Timeout interval must be a positive integer"), optarg); } else { - socket_timeout = atoi(optarg); + socket_timeout = (unsigned int) atoi(optarg); } break; case '4': @@ -240,16 +240,16 @@ int ssh_connect(mp_check *overall, char *haddr, int hport, char *desired_remote_ char *output = (char *)calloc(BUFF_SZ + 1, sizeof(char)); char *buffer = NULL; - ssize_t recv_ret = 0; + size_t recv_ret = 0; char *version_control_string = NULL; - ssize_t byte_offset = 0; - while ((version_control_string == NULL) && (recv_ret = recv(socket, output + byte_offset, BUFF_SZ - byte_offset, 0) > 0)) { + size_t byte_offset = 0; + while ((version_control_string == NULL) && (recv_ret = recv(socket, output + byte_offset, (unsigned long)( BUFF_SZ - byte_offset), 0) > 0)) { if (strchr(output, '\n')) { /* we've got at least one full line, start parsing*/ byte_offset = 0; char *index = NULL; - int len = 0; + unsigned long len = 0; while ((index = strchr(output + byte_offset, '\n')) != NULL) { /*Partition the buffer so that this line is a separate string, * by replacing the newline with NUL*/ -- cgit v1.2.3-74-g34f1 From 46683da7b7a35a70dcb7dcb0e64160b78f84e965 Mon Sep 17 00:00:00 2001 From: Lorenz Kästle <12514511+RincewindsHat@users.noreply.github.com> Date: Tue, 25 Feb 2025 11:40:45 +0100 Subject: check_ssh: fix typo --- plugins/check_ssh.c | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) (limited to 'plugins') diff --git a/plugins/check_ssh.c b/plugins/check_ssh.c index 518950ec..ab4af352 100644 --- a/plugins/check_ssh.c +++ b/plugins/check_ssh.c @@ -295,7 +295,7 @@ int ssh_connect(mp_check *overall, char *haddr, int hport, char *desired_remote_ } connection_sc = mp_set_subcheck_state(connection_sc, STATE_OK); - xasprintf(&connection_sc.output, "%s", "Initial connection succeded"); + xasprintf(&connection_sc.output, "%s", "Initial connection succeeded"); mp_add_subcheck_to_check(overall, connection_sc); /* @@ -384,7 +384,7 @@ int ssh_connect(mp_check *overall, char *haddr, int hport, char *desired_remote_ desired_remote_protocol); } else { protocol_version_sc = mp_set_subcheck_state(protocol_version_sc, STATE_OK); - xasprintf(&protocol_version_sc.output, "SSH server verison: %s (protocol version: %s)", ssh_server, ssh_proto); + xasprintf(&protocol_version_sc.output, "SSH server version: %s (protocol version: %s)", ssh_server, ssh_proto); } mp_add_subcheck_to_check(overall, protocol_version_sc); -- cgit v1.2.3-74-g34f1 From 92fb0ec66218059589d0b6b0fed08735b2765d60 Mon Sep 17 00:00:00 2001 From: Lorenz Kästle <12514511+RincewindsHat@users.noreply.github.com> Date: Tue, 25 Feb 2025 13:02:30 +0100 Subject: check_ssh: add missing break statement --- plugins/check_ssh.c | 1 + 1 file changed, 1 insertion(+) (limited to 'plugins') diff --git a/plugins/check_ssh.c b/plugins/check_ssh.c index ab4af352..3d6a0710 100644 --- a/plugins/check_ssh.c +++ b/plugins/check_ssh.c @@ -182,6 +182,7 @@ process_arguments_wrapper process_arguments(int argc, char **argv) { } else { usage2(_("Port number must be a positive integer"), optarg); } + break; case output_format_index: { parsed_output_format parser = mp_parse_output_format(optarg); if (!parser.parsing_success) { -- cgit v1.2.3-74-g34f1 From 6428696f31289c2777a34f4c0f761aa038da6284 Mon Sep 17 00:00:00 2001 From: Lorenz Kästle <12514511+RincewindsHat@users.noreply.github.com> Date: Tue, 25 Feb 2025 13:03:00 +0100 Subject: check_ssh: exit properly if TCP connection fails --- plugins/check_ssh.c | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) (limited to 'plugins') diff --git a/plugins/check_ssh.c b/plugins/check_ssh.c index 3d6a0710..d06af3e7 100644 --- a/plugins/check_ssh.c +++ b/plugins/check_ssh.c @@ -235,7 +235,11 @@ int ssh_connect(mp_check *overall, char *haddr, int hport, char *desired_remote_ int socket; int result = my_tcp_connect(haddr, hport, &socket); + mp_subcheck connection_sc = mp_subcheck_init(); if (result != STATE_OK) { + connection_sc = mp_set_subcheck_state(connection_sc, STATE_CRITICAL); + xasprintf(&connection_sc.output, "Failed to establish TCP connection to Host %s and Port %d", haddr, hport); + mp_add_subcheck_to_check(overall, connection_sc); return result; } @@ -280,7 +284,6 @@ int ssh_connect(mp_check *overall, char *haddr, int hport, char *desired_remote_ } } - mp_subcheck connection_sc = mp_subcheck_init(); if (recv_ret < 0) { connection_sc = mp_set_subcheck_state(connection_sc, STATE_CRITICAL); xasprintf(&connection_sc.output, "%s", "SSH CRITICAL - %s", strerror(errno)); -- cgit v1.2.3-74-g34f1 From 2e9f9ebf7d477956c5cc1779c0fa9c89352d7ab7 Mon Sep 17 00:00:00 2001 From: Lorenz Kästle <12514511+RincewindsHat@users.noreply.github.com> Date: Tue, 25 Feb 2025 13:03:27 +0100 Subject: check_ssh.c: clang-format --- plugins/check_ssh.c | 7 ++++--- 1 file changed, 4 insertions(+), 3 deletions(-) (limited to 'plugins') diff --git a/plugins/check_ssh.c b/plugins/check_ssh.c index d06af3e7..2ad7af66 100644 --- a/plugins/check_ssh.c +++ b/plugins/check_ssh.c @@ -44,7 +44,7 @@ const char *email = "devel@monitoring-plugins.org"; # define MSG_DONTWAIT 0 #endif -#define BUFF_SZ 256 +#define BUFF_SZ 256 static bool verbose = false; @@ -151,7 +151,7 @@ process_arguments_wrapper process_arguments(int argc, char **argv) { if (!is_intpos(optarg)) { usage2(_("Timeout interval must be a positive integer"), optarg); } else { - socket_timeout = (unsigned int) atoi(optarg); + socket_timeout = (unsigned int)atoi(optarg); } break; case '4': @@ -248,7 +248,8 @@ int ssh_connect(mp_check *overall, char *haddr, int hport, char *desired_remote_ size_t recv_ret = 0; char *version_control_string = NULL; size_t byte_offset = 0; - while ((version_control_string == NULL) && (recv_ret = recv(socket, output + byte_offset, (unsigned long)( BUFF_SZ - byte_offset), 0) > 0)) { + while ((version_control_string == NULL) && + (recv_ret = recv(socket, output + byte_offset, (unsigned long)(BUFF_SZ - byte_offset), 0) > 0)) { if (strchr(output, '\n')) { /* we've got at least one full line, start parsing*/ byte_offset = 0; -- cgit v1.2.3-74-g34f1 From 1ba8110da71abdccc9ee7f35cd0abefe78b8555b Mon Sep 17 00:00:00 2001 From: Lorenz Kästle <12514511+RincewindsHat@users.noreply.github.com> Date: Tue, 25 Feb 2025 13:04:04 +0100 Subject: check_ssh: adapt tests --- plugins/t/check_ssh.t | 114 ++++++++++++++++++++++++++++++++++++-------------- 1 file changed, 83 insertions(+), 31 deletions(-) (limited to 'plugins') diff --git a/plugins/t/check_ssh.t b/plugins/t/check_ssh.t index 907d33a8..8a20782e 100644 --- a/plugins/t/check_ssh.t +++ b/plugins/t/check_ssh.t @@ -5,10 +5,10 @@ # use strict; +use warnings; use Test::More; use NPTest; - -my $res; +use JSON; # Required parameters my $ssh_host = getTestParameter("NP_SSH_HOST", @@ -23,30 +23,38 @@ my $hostname_invalid = getTestParameter("NP_HOSTNAME_INVALID", "An invalid (not known to DNS) hostname", "nosuchhost" ); + my $outputFormat = '--output-format mp-test-json'; + +plan tests => 24; -plan tests => 14 + 6; +my $output; +my $result; SKIP: { skip "SSH_HOST must be defined", 6 unless $ssh_host; + + my $result = NPTest->testCmd( - "./check_ssh -H $ssh_host" + "./check_ssh -H $ssh_host" ." ". $outputFormat ); cmp_ok($result->return_code, '==', 0, "Exit with return code 0 (OK)"); - like($result->output, '/^SSH OK - /', "Status text if command returned none (OK)"); + $output = decode_json($result->output); + is($output->{'state'}, "OK", "State was correct"); $result = NPTest->testCmd( - "./check_ssh -H $host_nonresponsive -t 2" + "./check_ssh -H $host_nonresponsive -t 2" ." ". $outputFormat ); - cmp_ok($result->return_code, '==', 2, "Exit with return code 0 (OK)"); - like($result->output, '/^CRITICAL - Socket timeout after 2 seconds/', "Status text if command returned none (OK)"); + cmp_ok($result->return_code, '==', 0, "Exit with return code 0 (OK)"); + $output = decode_json($result->output); + is($output->{'state'}, "CRITICAL", "State was correct"); $result = NPTest->testCmd( - "./check_ssh -H $hostname_invalid -t 2" + "./check_ssh -H $hostname_invalid -t 2" ." ". $outputFormat ); - cmp_ok($result->return_code, '==', 3, "Exit with return code 0 (OK)"); + cmp_ok($result->return_code, '==', 3, "Exit with return code 3 (UNKNOWN)"); like($result->output, '/^check_ssh: Invalid hostname/', "Status text if command returned none (OK)"); @@ -63,46 +71,80 @@ SKIP: { # # where `comments` is optional, protoversion is the SSH protocol version and # softwareversion is an arbitrary string representing the server software version + + my $found_version = 0; + open(NC, "echo 'SSH-2.0-nagiosplug.ssh.0.1' | nc ${nc_flags}|"); sleep 0.1; - $res = NPTest->testCmd( "./check_ssh -H localhost -p 5003" ); - cmp_ok( $res->return_code, '==', 0, "Got SSH protocol version control string"); - like( $res->output, '/^SSH OK - nagiosplug.ssh.0.1 \(protocol 2.0\)/', "Output OK"); + $result = NPTest->testCmd( "./check_ssh -H localhost -p 5003" ." ". $outputFormat); + cmp_ok($result->return_code, '==', 0, "Exit with return code 0 (OK)"); + $output = decode_json($result->output); + is($output->{'state'}, "OK", "State was correct"); + + # looking for the version + for my $subcheck (@{$output->{'checks'}}) { + if ($subcheck->{'output'} =~ /.*nagiosplug.ssh.0.1 \(protocol version: 2.0\).*/ ){ + $found_version = 1; + } + } + cmp_ok($found_version, '==', 1, "Output OK"); close NC; open(NC, "echo 'SSH-2.0-3.2.9.1' | nc ${nc_flags}|"); sleep 0.1; - $res = NPTest->testCmd( "./check_ssh -H localhost -p 5003" ); - cmp_ok( $res->return_code, "==", 0, "Got SSH protocol version control string with non-alpha softwareversion string"); - like( $res->output, '/^SSH OK - 3.2.9.1 \(protocol 2.0\)/', "Output OK for non-alpha softwareversion string"); + $result = NPTest->testCmd( "./check_ssh -H localhost -p 5003" ." ". $outputFormat); + cmp_ok($result->return_code, '==', 0, "Exit with return code 0 (OK)"); + $output = decode_json($result->output); + is($output->{'state'}, "OK", "State was correct"); + + $found_version = 0; + for my $subcheck (@{$output->{'checks'}}) { + if ($subcheck->{'output'} =~ /3.2.9.1 \(protocol version: 2.0\)/ ){ + $found_version = 1; + } + } + cmp_ok($found_version, '==', 1, "Output OK"); close NC; open(NC, "echo 'SSH-2.0-nagiosplug.ssh.0.1 this is a comment' | nc ${nc_flags} |"); sleep 0.1; - $res = NPTest->testCmd( "./check_ssh -H localhost -p 5003 -r nagiosplug.ssh.0.1" ); - cmp_ok( $res->return_code, '==', 0, "Got SSH protocol version control string, and parsed comment appropriately"); - like( $res->output, '/^SSH OK - nagiosplug.ssh.0.1 \(protocol 2.0\)/', "Output OK"); + $result = NPTest->testCmd( "./check_ssh -H localhost -p 5003 -r nagiosplug.ssh.0.1" ." ". $outputFormat); + cmp_ok($result->return_code, '==', 0, "Exit with return code 0 (OK)"); + $output = decode_json($result->output); + is($output->{'state'}, "OK", "State was correct"); + + # looking for the version + $found_version = 0; + for my $subcheck (@{$output->{'checks'}}) { + if ($subcheck->{'output'} =~ /nagiosplug.ssh.0.1 \(protocol version: 2.0\)/ ){ + $found_version = 1; + } + } + cmp_ok($found_version, '==', 1, "Output OK"); close NC; open(NC, "echo 'SSH-' | nc ${nc_flags}|"); sleep 0.1; - $res = NPTest->testCmd( "./check_ssh -H localhost -p 5003" ); - cmp_ok( $res->return_code, '==', 2, "Got invalid SSH protocol version control string"); - like( $res->output, '/^SSH CRITICAL/', "Output OK"); + $result = NPTest->testCmd( "./check_ssh -H localhost -p 5003" ." ". $outputFormat); + cmp_ok($result->return_code, '==', 0, "Exit with return code 0 (OK)"); + $output = decode_json($result->output); + is($output->{'state'}, "CRITICAL", "Got invalid SSH protocol version control string"); close NC; open(NC, "echo '' | nc ${nc_flags}|"); sleep 0.1; - $res = NPTest->testCmd( "./check_ssh -H localhost -p 5003" ); - cmp_ok( $res->return_code, '==', 2, "No version control string received"); - like( $res->output, '/^SSH CRITICAL - No version control string received/', "Output OK"); + $result = NPTest->testCmd( "./check_ssh -H localhost -p 5003" ." ". $outputFormat); + cmp_ok($result->return_code, '==', 0, "Exit with return code 0 (OK)"); + $output = decode_json($result->output); + is($output->{'state'}, "CRITICAL", "No version control string received"); close NC; open(NC, "echo 'Not a version control string' | nc ${nc_flags}|"); sleep 0.1; - $res = NPTest->testCmd( "./check_ssh -H localhost -p 5003" ); - cmp_ok( $res->return_code, '==', 2, "No version control string received"); - like( $res->output, '/^SSH CRITICAL - No version control string received/', "Output OK"); + $result = NPTest->testCmd( "./check_ssh -H localhost -p 5003" ." ". $outputFormat); + cmp_ok($result->return_code, '==', 0, "Exit with return code 0 (OK)"); + $output = decode_json($result->output); + is($output->{'state'}, "CRITICAL", "No version control string received"); close NC; @@ -116,8 +158,18 @@ SKIP: { echo 'Some\nPrepended\nData\nLines\n'; sleep 0.2; echo 'SSH-2.0-nagiosplug.ssh.0.2';} | nc ${nc_flags}|"); sleep 0.1; - $res = NPTest->testCmd( "./check_ssh -H localhost -p 5003" ); - cmp_ok( $res->return_code, '==', 0, "Got delayed SSH protocol version control string"); - like( $res->output, '/^SSH OK - nagiosplug.ssh.0.2 \(protocol 2.0\)/', "Output OK"); + $result = NPTest->testCmd( "./check_ssh -H localhost -p 5003" ." ". $outputFormat); + cmp_ok($result->return_code, '==', 0, "Exit with return code 0 (OK)"); + $output = decode_json($result->output); + is($output->{'state'}, "OK", "State was correct"); + + # looking for the version + $found_version = 0; + for my $subcheck (@{$output->{'checks'}}) { + if ($subcheck->{'output'} =~ /nagiosplug.ssh.0.2 \(protocol version: 2.0\)/ ){ + $found_version = 1; + } + } + cmp_ok($found_version, '==', 1, "Output OK"); close NC; } -- cgit v1.2.3-74-g34f1 From 53247ae748174ddb639bb5fd9a19c8a6ad121717 Mon Sep 17 00:00:00 2001 From: Lorenz Kästle <12514511+RincewindsHat@users.noreply.github.com> Date: Tue, 25 Feb 2025 13:04:43 +0100 Subject: netutils.c: clang-format + do NOT print here --- plugins/netutils.c | 79 ++++++++++++++++++++++++++++++++---------------------- 1 file changed, 47 insertions(+), 32 deletions(-) (limited to 'plugins') diff --git a/plugins/netutils.c b/plugins/netutils.c index ee81912a..f5df7f3d 100644 --- a/plugins/netutils.c +++ b/plugins/netutils.c @@ -43,10 +43,11 @@ int address_family = AF_INET; /* handles socket timeouts */ void socket_timeout_alarm_handler(int sig) { - if (sig == SIGALRM) + if (sig == SIGALRM) { printf(_("%s - Socket timeout after %d seconds\n"), state_text(socket_timeout_state), socket_timeout); - else + } else { printf(_("%s - Abnormal timeout after %d seconds\n"), state_text(socket_timeout_state), socket_timeout); + } exit(socket_timeout_state); } @@ -65,12 +66,13 @@ int process_tcp_request2(const char *server_address, int server_port, const char int recv_length = 0; result = np_net_connect(server_address, server_port, &sd, IPPROTO_TCP); - if (result != STATE_OK) + if (result != STATE_OK) { return STATE_CRITICAL; + } send_result = send(sd, send_buffer, strlen(send_buffer), 0); if (send_result < 0 || (size_t)send_result != strlen(send_buffer)) { - printf("%s\n", _("Send failed")); + // printf("%s\n", _("Send failed")); result = STATE_WARNING; } @@ -87,7 +89,7 @@ int process_tcp_request2(const char *server_address, int server_port, const char if (!FD_ISSET(sd, &readfds)) { /* it hasn't */ if (!recv_length) { strcpy(recv_buffer, ""); - printf("%s\n", _("No data was received from host!")); + // printf("%s\n", _("No data was received from host!")); result = STATE_WARNING; } else { /* this one failed, but previous ones worked */ recv_buffer[recv_length] = 0; @@ -130,8 +132,9 @@ int process_request(const char *server_address, int server_port, int proto, cons result = STATE_OK; result = np_net_connect(server_address, server_port, &sd, proto); - if (result != STATE_OK) + if (result != STATE_OK) { return STATE_CRITICAL; + } result = send_request(sd, proto, send_buffer, recv_buffer, recv_size); @@ -169,15 +172,16 @@ int np_net_connect(const char *host_name, int port, int *sd, int proto) { host_name++; len -= 2; } - if (len >= sizeof(host)) + if (len >= sizeof(host)) { return STATE_UNKNOWN; + } memcpy(host, host_name, len); host[len] = '\0'; snprintf(port_str, sizeof(port_str), "%d", port); result = getaddrinfo(host, port_str, &hints, &res); if (result != 0) { - printf("%s\n", gai_strerror(result)); + // printf("%s\n", gai_strerror(result)); return STATE_UNKNOWN; } @@ -187,7 +191,7 @@ int np_net_connect(const char *host_name, int port, int *sd, int proto) { *sd = socket(r->ai_family, socktype, r->ai_protocol); if (*sd < 0) { - printf("%s\n", _("Socket creation failed")); + // printf("%s\n", _("Socket creation failed")); freeaddrinfo(r); return STATE_UNKNOWN; } @@ -226,21 +230,23 @@ int np_net_connect(const char *host_name, int port, int *sd, int proto) { die(STATE_UNKNOWN, _("Socket creation failed")); } result = connect(*sd, (struct sockaddr *)&su, sizeof(su)); - if (result < 0 && errno == ECONNREFUSED) + if (result < 0 && errno == ECONNREFUSED) { was_refused = true; + } } - if (result == 0) + if (result == 0) { return STATE_OK; - else if (was_refused) { + } else if (was_refused) { switch (econn_refuse_state) { /* a user-defined expected outcome */ case STATE_OK: case STATE_WARNING: /* user wants WARN or OK on refusal, or... */ case STATE_CRITICAL: /* user did not set econn_refuse_state, or wanted critical */ - if (is_socket) - printf("connect to file socket %s: %s\n", host_name, strerror(errno)); - else - printf("connect to address %s and port %d: %s\n", host_name, port, strerror(errno)); + if (is_socket) { + // printf("connect to file socket %s: %s\n", host_name, strerror(errno)); + } else { + // printf("connect to address %s and port %d: %s\n", host_name, port, strerror(errno)); + } return STATE_CRITICAL; break; default: /* it's a logic error if we do not end up in STATE_(OK|WARNING|CRITICAL) */ @@ -248,10 +254,11 @@ int np_net_connect(const char *host_name, int port, int *sd, int proto) { break; } } else { - if (is_socket) - printf("connect to file socket %s: %s\n", host_name, strerror(errno)); - else - printf("connect to address %s and port %d: %s\n", host_name, port, strerror(errno)); + if (is_socket) { + // printf("connect to file socket %s: %s\n", host_name, strerror(errno)); + } else { + // printf("connect to address %s and port %d: %s\n", host_name, port, strerror(errno)); + } return STATE_CRITICAL; } } @@ -265,7 +272,7 @@ int send_request(int sd, int proto, const char *send_buffer, char *recv_buffer, send_result = send(sd, send_buffer, strlen(send_buffer), 0); if (send_result < 0 || (size_t)send_result != strlen(send_buffer)) { - printf("%s\n", _("Send failed")); + // printf("%s\n", _("Send failed")); result = STATE_WARNING; } @@ -280,7 +287,7 @@ int send_request(int sd, int proto, const char *send_buffer, char *recv_buffer, /* make sure some data has arrived */ if (!FD_ISSET(sd, &readfds)) { strcpy(recv_buffer, ""); - printf("%s\n", _("No data was received from host!")); + // printf("%s\n", _("No data was received from host!")); result = STATE_WARNING; } @@ -288,11 +295,13 @@ int send_request(int sd, int proto, const char *send_buffer, char *recv_buffer, recv_result = recv(sd, recv_buffer, (size_t)recv_size - 1, 0); if (recv_result == -1) { strcpy(recv_buffer, ""); - if (proto != IPPROTO_TCP) - printf("%s\n", _("Receive failed")); + if (proto != IPPROTO_TCP) { + // printf("%s\n", _("Receive failed")); + } result = STATE_WARNING; - } else + } else { recv_buffer[recv_result] = 0; + } /* die returned string */ recv_buffer[recv_size - 1] = 0; @@ -301,26 +310,30 @@ int send_request(int sd, int proto, const char *send_buffer, char *recv_buffer, } bool is_host(const char *address) { - if (is_addr(address) || is_hostname(address)) + if (is_addr(address) || is_hostname(address)) { return (true); + } return (false); } void host_or_die(const char *str) { - if (!str || (!is_addr(str) && !is_hostname(str))) + if (!str || (!is_addr(str) && !is_hostname(str))) { usage_va(_("Invalid hostname/address - %s"), str); + } } bool is_addr(const char *address) { #ifdef USE_IPV6 - if (address_family == AF_INET && is_inet_addr(address)) + if (address_family == AF_INET && is_inet_addr(address)) { return true; - else if (address_family == AF_INET6 && is_inet6_addr(address)) + } else if (address_family == AF_INET6 && is_inet6_addr(address)) { return true; + } #else - if (is_inet_addr(address)) + if (is_inet_addr(address)) { return (true); + } #endif return (false); @@ -335,11 +348,13 @@ int dns_lookup(const char *in, struct sockaddr_storage *ss, int family) { hints.ai_family = family; retval = getaddrinfo(in, NULL, &hints, &res); - if (retval != 0) + if (retval != 0) { return false; + } - if (ss != NULL) + if (ss != NULL) { memcpy(ss, res->ai_addr, res->ai_addrlen); + } freeaddrinfo(res); return true; } -- cgit v1.2.3-74-g34f1 From d35c43e95f9efed3ef89bee288b8ec8f65ad2014 Mon Sep 17 00:00:00 2001 From: Lorenz Kästle <12514511+RincewindsHat@users.noreply.github.com> Date: Tue, 25 Feb 2025 15:02:55 +0100 Subject: Revert "Migrate check_users to new ouput infrastructure" This reverts commit 10086edbf03fd807794bcfb3fff626da69a9fdb2. --- plugins/Makefile.am | 1 - plugins/check_users.c | 249 +++++++++++++++++++++-------------------- plugins/check_users.d/config.h | 19 ---- plugins/check_users.d/users.c | 166 --------------------------- plugins/check_users.d/users.h | 18 --- 5 files changed, 130 insertions(+), 323 deletions(-) delete mode 100644 plugins/check_users.d/config.h delete mode 100644 plugins/check_users.d/users.c delete mode 100644 plugins/check_users.d/users.h (limited to 'plugins') diff --git a/plugins/Makefile.am b/plugins/Makefile.am index e0069653..d43c1971 100644 --- a/plugins/Makefile.am +++ b/plugins/Makefile.am @@ -125,7 +125,6 @@ check_time_LDADD = $(NETLIBS) check_ntp_time_LDADD = $(NETLIBS) $(MATHLIBS) check_ups_LDADD = $(NETLIBS) check_users_LDADD = $(BASEOBJS) $(WTSAPI32LIBS) $(SYSTEMDLIBS) -check_users_SOURCES = check_users.c check_users.d/users.c check_by_ssh_LDADD = $(NETLIBS) check_ide_smart_LDADD = $(BASEOBJS) negate_LDADD = $(BASEOBJS) diff --git a/plugins/check_users.c b/plugins/check_users.c index 6ec87d21..f1e1c39d 100644 --- a/plugins/check_users.c +++ b/plugins/check_users.c @@ -3,7 +3,7 @@ * Monitoring check_users plugin * * License: GPL - * Copyright (c) 2000-2025 Monitoring Plugins Development Team + * Copyright (c) 2000-2024 Monitoring Plugins Development Team * * Description: * @@ -30,19 +30,12 @@ * *****************************************************************************/ -#include "check_users.d/config.h" -#include "thresholds.h" const char *progname = "check_users"; -const char *copyright = "2000-2025"; +const char *copyright = "2000-2024"; const char *email = "devel@monitoring-plugins.org"; -#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 "common.h" +#include "utils.h" #if HAVE_WTSAPI32_H # include @@ -60,16 +53,29 @@ const char *email = "devel@monitoring-plugins.org"; # include #endif -typedef struct process_argument_wrapper { - int errorcode; - check_users_config config; -} process_argument_wrapper; +#define possibly_set(a, b) ((a) == 0 ? (b) : 0) -process_argument_wrapper process_arguments(int /*argc*/, char ** /*argv*/); -void print_help(void); +static int process_arguments(int, char **); +static void print_help(void); void print_usage(void); +static char *warning_range = NULL; +static char *critical_range = NULL; +static thresholds *thlds = NULL; + int main(int argc, char **argv) { + int users = -1; + int result = STATE_UNKNOWN; +#if HAVE_WTSAPI32_H + WTS_SESSION_INFO *wtsinfo; + DWORD wtscount; + DWORD index; +#elif HAVE_UTMPX_H + struct utmpx *putmpx; +#else + char input_buffer[MAX_INPUT_BUFFER]; +#endif + setlocale(LC_ALL, ""); bindtextdomain(PACKAGE, LOCALEDIR); textdomain(PACKAGE); @@ -77,106 +83,121 @@ int main(int argc, char **argv) { /* Parse extra opts if any */ argv = np_extra_opts(&argc, argv, progname); - process_argument_wrapper tmp = process_arguments(argc, argv); - - if (tmp.errorcode == ERROR) { + if (process_arguments(argc, argv) == ERROR) usage4(_("Could not parse arguments")); - } - check_users_config config = tmp.config; + 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)) { + printf(_("Could not enumerate RD sessions: %d\n"), GetLastError()); + return STATE_UNKNOWN; + } + + for (index = 0; index < wtscount; index++) { + LPTSTR username; + DWORD size; + int len; -#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 + if (!WTSQuerySessionInformation(WTS_CURRENT_SERVER_HANDLE, wtsinfo[index].SessionId, WTSUserName, &username, &size)) + continue; + + len = lstrlen(username); + + WTSFreeMemory(username); + + if (len == 0) + continue; + + if (wtsinfo[index].State == WTSActive || wtsinfo[index].State == WTSDisconnected) + users++; + } + + WTSFreeMemory(wtsinfo); +#elif HAVE_UTMPX_H + /* get currently logged users from utmpx */ + setutxent(); + + while ((putmpx = getutxent()) != NULL) + if (putmpx->ut_type == USER_PROCESS) + users++; + + endutxent(); #else -# 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(); - mp_subcheck sc_users = mp_subcheck_init(); - - 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); + /* run the command */ + child_process = spopen(WHO_COMMAND); + if (child_process == NULL) { + printf(_("Could not open pipe: %s\n"), WHO_COMMAND); + return STATE_UNKNOWN; } + + child_stderr = fdopen(child_stderr_array[fileno(child_process)], "r"); + if (child_stderr == NULL) + printf(_("Could not open stderr for %s\n"), WHO_COMMAND); + + while (fgets(input_buffer, MAX_INPUT_BUFFER - 1, child_process)) { + /* increment 'users' on all lines except total user count */ + if (input_buffer[0] != '#') { + users++; + continue; + } + + /* get total logged in users */ + if (sscanf(input_buffer, _("# users=%d"), &users) == 1) + break; + } + + /* check STDERR */ + if (fgets(input_buffer, MAX_INPUT_BUFFER - 1, child_stderr)) + result = possibly_set(result, STATE_UNKNOWN); + (void)fclose(child_stderr); + + /* close the pipe */ + 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); - 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: - xasprintf(&sc_users.output, "%d users currently logged in. This violates the warning threshold", user_wrapper.users); - break; - case STATE_CRITICAL: - xasprintf(&sc_users.output, "%d users currently logged in. This violates the critical threshold", user_wrapper.users); - break; - default: - xasprintf(&sc_users.output, "%d users currently logged in", user_wrapper.users); + if (result == STATE_UNKNOWN) + printf("%s\n", _("Unable to read output")); + else { + 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)); } - mp_add_subcheck_to_check(&overall, sc_users); - mp_exit(overall); + return result; } -#define output_format_index CHAR_MAX + 1 - /* process command-line arguments */ -process_argument_wrapper process_arguments(int argc, char **argv) { +int process_arguments(int argc, char **argv) { static struct option longopts[] = {{"critical", required_argument, 0, 'c'}, {"warning", required_argument, 0, 'w'}, {"version", no_argument, 0, 'V'}, {"help", no_argument, 0, 'h'}, - {"output-format", required_argument, 0, output_format_index}, {0, 0, 0, 0}}; - if (argc < 2) { + if (argc < 2) usage("\n"); - } - - char *warning_range = NULL; - char *critical_range = NULL; - check_users_config config = check_users_config_init(); + int option_char; while (true) { - int counter = getopt_long(argc, argv, "+hVvc:w:", longopts, NULL); + int option = 0; + option_char = getopt_long(argc, argv, "+hVvc:w:", longopts, &option); - if (counter == -1 || counter == EOF || counter == 1) { + if (option_char == -1 || option_char == EOF || option_char == 1) break; - } - switch (counter) { - 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); - } - - config.output_format_is_set = true; - config.output_format = parser.output_format; - break; - } + switch (option_char) { case '?': /* print short usage statement if args not parsable */ usage5(); case 'h': /* help */ @@ -194,35 +215,26 @@ process_argument_wrapper process_arguments(int argc, char **argv) { } } - // TODO add proper verification for ranges here! - if (warning_range) { - mp_range_parsed tmp = mp_parse_range_string(warning_range); - if (tmp.error == MP_PARSING_SUCCES) { - config.thresholds.warning = tmp.range; - config.thresholds.warning_is_set = true; - } else { - printf("Failed to parse warning range: %s", warning_range); - exit(STATE_UNKNOWN); - } - } + option_char = optind; - if (critical_range) { - mp_range_parsed tmp = mp_parse_range_string(critical_range); - if (tmp.error == MP_PARSING_SUCCES) { - config.thresholds.critical = tmp.range; - config.thresholds.critical_is_set = true; - } else { - printf("Failed to parse critical range: %s", critical_range); - exit(STATE_UNKNOWN); - } + if (warning_range == NULL && argc > option_char) + warning_range = argv[option_char++]; + + if (critical_range == NULL && argc > option_char) + critical_range = argv[option_char++]; + + /* this will abort in case of invalid ranges */ + set_thresholds(&thlds, warning_range, critical_range); + + if (!thlds->warning) { + usage4(_("Warning threshold must be a valid range expression")); } - process_argument_wrapper result = { - .errorcode = OK, - .config = config, - }; + if (!thlds->critical) { + usage4(_("Critical threshold must be a valid range expression")); + } - return result; + return OK; } void print_help(void) { @@ -245,7 +257,6 @@ void print_help(void) { printf(" %s\n", _("Set WARNING status if number of logged in users violates RANGE_EXPRESSION")); printf(" %s\n", "-c, --critical=RANGE_EXPRESSION"); printf(" %s\n", _("Set CRITICAL status if number of logged in users violates RANGE_EXPRESSION")); - printf(UT_OUTPUT_FORMAT); printf(UT_SUPPORT); } diff --git a/plugins/check_users.d/config.h b/plugins/check_users.d/config.h deleted file mode 100644 index 35b7b7f8..00000000 --- a/plugins/check_users.d/config.h +++ /dev/null @@ -1,19 +0,0 @@ -#pragma once - -#include "output.h" -#include "thresholds.h" -typedef struct check_users_config { - mp_thresholds thresholds; - - bool output_format_is_set; - mp_output_format output_format; -} check_users_config; - -check_users_config check_users_config_init() { - check_users_config tmp = { - .thresholds = mp_thresholds_init(), - - .output_format_is_set = false, - }; - return tmp; -} diff --git a/plugins/check_users.d/users.c b/plugins/check_users.d/users.c deleted file mode 100644 index 7969ae79..00000000 --- a/plugins/check_users.d/users.c +++ /dev/null @@ -1,166 +0,0 @@ -#include "./users.h" - -#ifdef _WIN32 -# ifdef HAVE_WTSAPI32_H -# include -# include -# undef ERROR -# define ERROR -1 - -get_num_of_users_wrapper get_num_of_users_windows() { - WTS_SESSION_INFO *wtsinfo; - DWORD wtscount; - - get_num_of_users_wrapper result = {}; - - if (!WTSEnumerateSessions(WTS_CURRENT_SERVER_HANDLE, 0, 1, &wtsinfo, &wtscount)) { - // printf(_("Could not enumerate RD sessions: %d\n"), GetLastError()); - result.error = WINDOWS_COULD_NOT_ENUMERATE_SESSIONS; - return result; - } - - for (DWORD index = 0; index < wtscount; index++) { - LPTSTR username; - DWORD size; - - if (!WTSQuerySessionInformation(WTS_CURRENT_SERVER_HANDLE, wtsinfo[index].SessionId, WTSUserName, &username, &size)) { - continue; - } - - int len = lstrlen(username); - - WTSFreeMemory(username); - - if (len == 0) { - continue; - } - - if (wtsinfo[index].State == WTSActive || wtsinfo[index].State == WTSDisconnected) { - result.users++; - } - } - - WTSFreeMemory(wtsinfo); - return result; -} -# else // HAVE_WTSAPI32_H -# error On windows but without the WTSAPI32 lib -# endif // HAVE_WTSAPI32_H - -#else // _WIN32 - -# include "../../config.h" - -# ifdef HAVE_LIBSYSTEMD -# include -# include - -get_num_of_users_wrapper get_num_of_users_systemd() { - get_num_of_users_wrapper result = {}; - - // Test whether we booted with systemd - if (sd_booted() > 0) { - int users = sd_get_sessions(NULL); - if (users >= 0) { - // Success - result.users = users; - return result; - } - - // Failure! return the error code - result.errorcode = users; - return result; - } - - // Looks like we are not running systemd, - // return with error here - result.errorcode = NO_SYSTEMD_ERROR; - return result; -} -# endif - -# ifdef HAVE_UTMPX_H -# include - -get_num_of_users_wrapper get_num_of_users_utmp() { - int users = 0; - - /* get currently logged users from utmpx */ - setutxent(); - - struct utmpx *putmpx; - while ((putmpx = getutxent()) != NULL) { - if (putmpx->ut_type == USER_PROCESS) { - users++; - } - } - - endutxent(); - - get_num_of_users_wrapper result = { - .errorcode = 0, - .users = users, - }; - - return result; -} -# endif - -# ifndef HAVE_WTSAPI32_H -# ifndef HAVE_LIBSYSTEMD -# ifndef HAVE_UTMPX_H -// Fall back option here for the others (probably still not on windows) - -# include "../popen.h" -# include "../common.h" -# include "../utils.h" - -get_num_of_users_wrapper get_num_of_users_who_command() { - /* run the command */ - child_process = spopen(WHO_COMMAND); - if (child_process == NULL) { - // printf(_("Could not open pipe: %s\n"), WHO_COMMAND); - get_num_of_users_wrapper result = { - .errorcode = COULD_NOT_OPEN_PIPE, - }; - return result; - } - - child_stderr = fdopen(child_stderr_array[fileno(child_process)], "r"); - if (child_stderr == NULL) { - // printf(_("Could not open stderr for %s\n"), WHO_COMMAND); - // TODO this error should probably be reported - } - - get_num_of_users_wrapper result = {}; - char input_buffer[MAX_INPUT_BUFFER]; - while (fgets(input_buffer, MAX_INPUT_BUFFER - 1, child_process)) { - /* increment 'users' on all lines except total user count */ - if (input_buffer[0] != '#') { - result.users++; - continue; - } - - /* get total logged in users */ - if (sscanf(input_buffer, _("# users=%d"), &result.users) == 1) { - break; - } - } - - /* check STDERR */ - if (fgets(input_buffer, MAX_INPUT_BUFFER - 1, child_stderr)) { - // if this fails, something broke and the result can not be relied upon or so is the theorie here - result.errorcode = STDERR_COULD_NOT_BE_READ; - } - (void)fclose(child_stderr); - - /* close the pipe */ - spclose(child_process); - - return result; -} - -# endif -# endif -# endif -#endif diff --git a/plugins/check_users.d/users.h b/plugins/check_users.d/users.h deleted file mode 100644 index aacba775..00000000 --- a/plugins/check_users.d/users.h +++ /dev/null @@ -1,18 +0,0 @@ -#pragma once - -typedef struct get_num_of_users_wrapper { - int errorcode; - int users; -} get_num_of_users_wrapper; - -enum { - NO_SYSTEMD_ERROR = 64, - WINDOWS_COULD_NOT_ENUMERATE_SESSIONS, - COULD_NOT_OPEN_PIPE, - STDERR_COULD_NOT_BE_READ, -}; - -get_num_of_users_wrapper get_num_of_users_systemd(); -get_num_of_users_wrapper get_num_of_users_utmp(); -get_num_of_users_wrapper get_num_of_users_windows(); -get_num_of_users_wrapper get_num_of_users_who_command(); -- cgit v1.2.3-74-g34f1 From 3cbd84ce754b1f7ce1ac8f9bc46059da62392ffe Mon Sep 17 00:00:00 2001 From: Lorenz Kästle <12514511+RincewindsHat@users.noreply.github.com> Date: Tue, 25 Feb 2025 16:35:51 +0100 Subject: Switch timeout handler to new output infrastructure --- plugins/netutils.c | 14 +++++++++++--- 1 file changed, 11 insertions(+), 3 deletions(-) (limited to 'plugins') diff --git a/plugins/netutils.c b/plugins/netutils.c index f5df7f3d..e2916c65 100644 --- a/plugins/netutils.c +++ b/plugins/netutils.c @@ -28,6 +28,8 @@ *****************************************************************************/ #include "common.h" +#include "output.h" +#include "states.h" #include "netutils.h" unsigned int socket_timeout = DEFAULT_SOCKET_TIMEOUT; @@ -43,13 +45,19 @@ int address_family = AF_INET; /* handles socket timeouts */ void socket_timeout_alarm_handler(int sig) { + mp_subcheck timeout_sc = mp_subcheck_init(); + timeout_sc = mp_set_subcheck_state(timeout_sc, socket_timeout_state); + if (sig == SIGALRM) { - printf(_("%s - Socket timeout after %d seconds\n"), state_text(socket_timeout_state), socket_timeout); + xasprintf(&timeout_sc.output, _("Socket timeout after %d seconds\n"), socket_timeout); } else { - printf(_("%s - Abnormal timeout after %d seconds\n"), state_text(socket_timeout_state), socket_timeout); + xasprintf(&timeout_sc.output, _("Abnormal timeout after %d seconds\n"), socket_timeout); } - exit(socket_timeout_state); + mp_check overall = mp_check_init(); + mp_add_subcheck_to_check(&overall, timeout_sc); + + mp_exit(overall); } /* connects to a host on a specified tcp port, sends a string, and gets a -- cgit v1.2.3-74-g34f1 From 72fd885f4ff423d5351e3387867f2415f1ffc2d8 Mon Sep 17 00:00:00 2001 From: Lorenz Kästle <12514511+RincewindsHat@users.noreply.github.com> Date: Tue, 25 Feb 2025 17:20:05 +0100 Subject: Transform output format to a global state This commit removes the format parameter from the mp_check object and creates a module global variable instead. This prevents thread safe usage of different mp_check objects which should likely not present a big problem for now. The reason for this change is effectively the very same, the format was lost if an exit was triggered by a signal handler (timeout in this example). --- lib/output.c | 12 +++++++++--- lib/output.h | 7 ++++++- plugins/check_ssh.c | 2 +- plugins/check_swap.c | 2 +- 4 files changed, 17 insertions(+), 6 deletions(-) (limited to 'plugins') diff --git a/lib/output.c b/lib/output.c index 17919afc..61fbf832 100644 --- a/lib/output.c +++ b/lib/output.c @@ -11,6 +11,9 @@ #include "perfdata.h" #include "states.h" +// == Global variables +static mp_output_format output_format = MP_FORMAT_DEFAULT; + // == Prototypes == static char *fmt_subcheck_output(mp_output_format output_format, mp_subcheck check, unsigned int indentation); static inline cJSON *json_serialize_subcheck(mp_subcheck subcheck); @@ -55,7 +58,6 @@ static inline char *fmt_subcheck_perfdata(mp_subcheck check) { */ mp_check mp_check_init(void) { mp_check check = {0}; - check.format = MP_FORMAT_DEFAULT; return check; } @@ -234,7 +236,7 @@ mp_state_enum mp_compute_check_state(const mp_check check) { char *mp_fmt_output(mp_check check) { char *result = NULL; - switch (check.format) { + switch (output_format) { case MP_FORMAT_MULTI_LINE: { if (check.summary == NULL) { check.summary = get_subcheck_summary(check); @@ -482,7 +484,7 @@ void mp_print_output(mp_check check) { puts(mp_fmt_output(check)); } */ void mp_exit(mp_check check) { mp_print_output(check); - if (check.format == MP_FORMAT_TEST_JSON) { + if (output_format == MP_FORMAT_TEST_JSON) { exit(0); } @@ -533,3 +535,7 @@ parsed_output_format mp_parse_output_format(char *format_string) { return result; } + +void mp_set_format(mp_output_format format) { output_format = format; } + +mp_output_format mp_get_format(void) { return output_format; } diff --git a/lib/output.h b/lib/output.h index ffc36f53..2bdfa074 100644 --- a/lib/output.h +++ b/lib/output.h @@ -35,6 +35,12 @@ typedef enum output_format { #define MP_FORMAT_DEFAULT MP_FORMAT_MULTI_LINE +/* + * Format related functions + */ + void mp_set_format(mp_output_format format); + mp_output_format mp_get_format(void); + /* * The main state object of a plugin. Exists only ONCE per plugin. * This is the "root" of a tree of singular checks. @@ -42,7 +48,6 @@ typedef enum output_format { * in the first layer of subchecks */ typedef struct { - mp_output_format format; // The output format char *summary; // Overall summary, if not set a summary will be automatically generated mp_subcheck_list *subchecks; } mp_check; diff --git a/plugins/check_ssh.c b/plugins/check_ssh.c index 2ad7af66..9d0d7cde 100644 --- a/plugins/check_ssh.c +++ b/plugins/check_ssh.c @@ -77,7 +77,7 @@ int main(int argc, char **argv) { mp_check overall = mp_check_init(); if (config.output_format_is_set) { - overall.format = config.output_format; + mp_set_format(config.output_format); } /* initialize alarm signal handling */ diff --git a/plugins/check_swap.c b/plugins/check_swap.c index 4d3b6099..cb95949a 100644 --- a/plugins/check_swap.c +++ b/plugins/check_swap.c @@ -93,7 +93,7 @@ int main(int argc, char **argv) { double percent_used; mp_check overall = mp_check_init(); if (config.output_format_is_set) { - overall.format = config.output_format; + mp_set_format(config.output_format); } mp_subcheck sc1 = mp_subcheck_init(); sc1 = mp_set_subcheck_default_state(sc1, STATE_OK); -- cgit v1.2.3-74-g34f1 From f63d249984e33030c7e4b3fd97ebe3f034abce3c Mon Sep 17 00:00:00 2001 From: Lorenz Kästle <12514511+RincewindsHat@users.noreply.github.com> Date: Sat, 8 Mar 2025 23:42:06 +0100 Subject: Adapt further test to changed output --- plugins/t/check_http.t | 2 +- plugins/t/check_jabber.t | 2 +- plugins/t/check_ldap.t | 2 +- plugins/t/check_ntp.t | 2 +- 4 files changed, 4 insertions(+), 4 deletions(-) (limited to 'plugins') diff --git a/plugins/t/check_http.t b/plugins/t/check_http.t index 6ab4a5b6..bb1fd27d 100644 --- a/plugins/t/check_http.t +++ b/plugins/t/check_http.t @@ -45,7 +45,7 @@ $res = NPTest->testCmd( "./$plugin $host_nonresponsive -wt 1 -ct 2 -t 3" ); cmp_ok( $res->return_code, '==', 2, "Webserver $host_nonresponsive not responding" ); -cmp_ok( $res->output, 'eq', "CRITICAL - Socket timeout after 3 seconds", "Output OK"); +like( $res->output, "/Socket timeout after/", "Output OK"); $res = NPTest->testCmd( "./$plugin $hostname_invalid -wt 1 -ct 2" diff --git a/plugins/t/check_jabber.t b/plugins/t/check_jabber.t index fcdae179..08cadcbd 100644 --- a/plugins/t/check_jabber.t +++ b/plugins/t/check_jabber.t @@ -17,7 +17,7 @@ my $hostname_invalid = getTestParameter("NP_HOSTNAME_INVALID", "An invalid (no my $jabberOK = '/JABBER OK\s-\s\d+\.\d+\ssecond response time on '.$host_tcp_jabber.' port 5222/'; -my $jabberUnresponsive = '/CRITICAL\s-\sSocket timeout after\s\d+\sseconds/'; +my $jabberUnresponsive = '/Socket timeout after\s\d+\sseconds/'; my $jabberInvalid = '/JABBER CRITICAL - Invalid hostname, address or socket:\s.+/'; diff --git a/plugins/t/check_ldap.t b/plugins/t/check_ldap.t index b8a4a766..fcba0393 100644 --- a/plugins/t/check_ldap.t +++ b/plugins/t/check_ldap.t @@ -24,7 +24,7 @@ SKIP: { $result = NPTest->testCmd("$command -H $host_nonresponsive -b ou=blah -t 2 -w 1 -c 1"); is( $result->return_code, 2, "$command -H $host_nonresponsive -b ou=blah -t 5 -w 2 -c 3" ); - is( $result->output, 'CRITICAL - Socket timeout after 2 seconds', "output ok" ); + like($result->output, '/Socket timeout after \d+ seconds/', "output ok" ); }; SKIP: { diff --git a/plugins/t/check_ntp.t b/plugins/t/check_ntp.t index b8fc8fdf..e7bb76e3 100644 --- a/plugins/t/check_ntp.t +++ b/plugins/t/check_ntp.t @@ -37,7 +37,7 @@ my $ntp_critmatch1 = '/^NTP\sCRITICAL:\sOffset\s-?[0-9]+(\.[0-9]+)?(e-[0-9]{2})? my $ntp_okmatch2 = '/^NTP\sOK:\sOffset\s-?[0-9]+(\.[0-9]+)?(e-[0-9]{2})?\ssecs,\sjitter=[0-9]+\.[0-9]+,\sstratum=[0-9]{1,2},\struechimers=[0-9]+/'; my $ntp_warnmatch2 = '/^NTP\sWARNING:\sOffset\s-?[0-9]+(\.[0-9]+)?(e-[0-9]{2})?\ssecs,\sjitter=[0-9]+\.[0-9]+,\sstratum=[0-9]{1,2}\s\(WARNING\),\struechimers=[0-9]+/'; my $ntp_critmatch2 = '/^NTP\sCRITICAL:\sOffset\s-?[0-9]+(\.[0-9]+)?(e-[0-9]{2})?\ssecs,\sjitter=[0-9]+\.[0-9]+\s\(CRITICAL\),\sstratum=[0-9]{1,2},\struechimers=[0-9]+/'; -my $ntp_noresponse = '/^(CRITICAL - Socket timeout after 3 seconds)|(NTP CRITICAL: No response from NTP server)$/'; +my $ntp_noresponse = '/^(.*Socket timeout after \d+ seconds.*)|.*No response from NTP server.*)$/'; my $ntp_nosuchhost = '/^check_ntp.*: Invalid hostname/address - ' . $hostname_invalid . '/'; -- cgit v1.2.3-74-g34f1 From fea6a662bd73496a29200023ed6a06bedefab415 Mon Sep 17 00:00:00 2001 From: Lorenz Kästle <12514511+RincewindsHat@users.noreply.github.com> Date: Sun, 9 Mar 2025 00:12:37 +0100 Subject: fix accidently remove parentheses in test --- plugins/t/check_ntp.t | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'plugins') diff --git a/plugins/t/check_ntp.t b/plugins/t/check_ntp.t index e7bb76e3..a355aa4a 100644 --- a/plugins/t/check_ntp.t +++ b/plugins/t/check_ntp.t @@ -37,7 +37,7 @@ my $ntp_critmatch1 = '/^NTP\sCRITICAL:\sOffset\s-?[0-9]+(\.[0-9]+)?(e-[0-9]{2})? my $ntp_okmatch2 = '/^NTP\sOK:\sOffset\s-?[0-9]+(\.[0-9]+)?(e-[0-9]{2})?\ssecs,\sjitter=[0-9]+\.[0-9]+,\sstratum=[0-9]{1,2},\struechimers=[0-9]+/'; my $ntp_warnmatch2 = '/^NTP\sWARNING:\sOffset\s-?[0-9]+(\.[0-9]+)?(e-[0-9]{2})?\ssecs,\sjitter=[0-9]+\.[0-9]+,\sstratum=[0-9]{1,2}\s\(WARNING\),\struechimers=[0-9]+/'; my $ntp_critmatch2 = '/^NTP\sCRITICAL:\sOffset\s-?[0-9]+(\.[0-9]+)?(e-[0-9]{2})?\ssecs,\sjitter=[0-9]+\.[0-9]+\s\(CRITICAL\),\sstratum=[0-9]{1,2},\struechimers=[0-9]+/'; -my $ntp_noresponse = '/^(.*Socket timeout after \d+ seconds.*)|.*No response from NTP server.*)$/'; +my $ntp_noresponse = '/^(.*Socket timeout after \d+ seconds.*)|(.*No response from NTP server.*)$/'; my $ntp_nosuchhost = '/^check_ntp.*: Invalid hostname/address - ' . $hostname_invalid . '/'; -- cgit v1.2.3-74-g34f1 From 809e79c7454c4b5771fe47c4da309dc82f917c25 Mon Sep 17 00:00:00 2001 From: Lorenz Kästle <12514511+RincewindsHat@users.noreply.github.com> Date: Sun, 9 Mar 2025 00:30:33 +0100 Subject: Remove failing test --- plugins/t/check_smtp.t | 1 - 1 file changed, 1 deletion(-) (limited to 'plugins') diff --git a/plugins/t/check_smtp.t b/plugins/t/check_smtp.t index 1a1ebe3e..11e22644 100644 --- a/plugins/t/check_smtp.t +++ b/plugins/t/check_smtp.t @@ -73,7 +73,6 @@ SKIP: { my $unused_port = 4465; $res = NPTest->testCmd( "./check_smtp -H $host_tcp_smtp_tls -p $unused_port --ssl" ); is ($res->return_code, 2, "Check rc of connecting to $host_tcp_smtp_tls with TLS on unused port $unused_port" ); - like ($res->output, qr/^connect to address $host_tcp_smtp_tls and port $unused_port: Connection refused/, "Check output of connecting to $host_tcp_smtp_tls with TLS on unused port $unused_port"); } $res = NPTest->testCmd( "./check_smtp $host_nonresponsive" ); -- cgit v1.2.3-74-g34f1 From 69819d8c952a623f092186f7b99b10d6cb894d9c Mon Sep 17 00:00:00 2001 From: Lorenz Kästle <12514511+RincewindsHat@users.noreply.github.com> Date: Sun, 9 Mar 2025 10:24:28 +0100 Subject: Adapt test regex to new output --- plugins/t/check_ntp.t | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'plugins') diff --git a/plugins/t/check_ntp.t b/plugins/t/check_ntp.t index a355aa4a..a8ac7bb8 100644 --- a/plugins/t/check_ntp.t +++ b/plugins/t/check_ntp.t @@ -37,7 +37,7 @@ my $ntp_critmatch1 = '/^NTP\sCRITICAL:\sOffset\s-?[0-9]+(\.[0-9]+)?(e-[0-9]{2})? my $ntp_okmatch2 = '/^NTP\sOK:\sOffset\s-?[0-9]+(\.[0-9]+)?(e-[0-9]{2})?\ssecs,\sjitter=[0-9]+\.[0-9]+,\sstratum=[0-9]{1,2},\struechimers=[0-9]+/'; my $ntp_warnmatch2 = '/^NTP\sWARNING:\sOffset\s-?[0-9]+(\.[0-9]+)?(e-[0-9]{2})?\ssecs,\sjitter=[0-9]+\.[0-9]+,\sstratum=[0-9]{1,2}\s\(WARNING\),\struechimers=[0-9]+/'; my $ntp_critmatch2 = '/^NTP\sCRITICAL:\sOffset\s-?[0-9]+(\.[0-9]+)?(e-[0-9]{2})?\ssecs,\sjitter=[0-9]+\.[0-9]+\s\(CRITICAL\),\sstratum=[0-9]{1,2},\struechimers=[0-9]+/'; -my $ntp_noresponse = '/^(.*Socket timeout after \d+ seconds.*)|(.*No response from NTP server.*)$/'; +my $ntp_noresponse = '/(.*Socket timeout after \d+ seconds.*)|(.*No response from NTP server.*)/'; my $ntp_nosuchhost = '/^check_ntp.*: Invalid hostname/address - ' . $hostname_invalid . '/'; -- cgit v1.2.3-74-g34f1 From 9d5774aede083297656bce14bd52e2c27a9d3169 Mon Sep 17 00:00:00 2001 From: Lorenz Kästle <12514511+RincewindsHat@users.noreply.github.com> Date: Sun, 9 Mar 2025 10:25:01 +0100 Subject: Adapt number of tests --- plugins/t/check_smtp.t | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'plugins') diff --git a/plugins/t/check_smtp.t b/plugins/t/check_smtp.t index 11e22644..73b4a1fd 100644 --- a/plugins/t/check_smtp.t +++ b/plugins/t/check_smtp.t @@ -24,7 +24,7 @@ my $hostname_invalid = getTestParameter( "NP_HOSTNAME_INVALID", "An invalid (not known to DNS) hostname", "nosuchhost" ); my $res; -plan tests => 16; +plan tests => 15; SKIP: { skip "No SMTP server defined", 4 unless $host_tcp_smtp; -- cgit v1.2.3-74-g34f1 From 0426b8947d9e03375219074c3741616599651db4 Mon Sep 17 00:00:00 2001 From: Lorenz Kästle <12514511+RincewindsHat@users.noreply.github.com> Date: Sun, 9 Mar 2025 11:04:32 +0100 Subject: Add check_ssh config to distributable files --- plugins/Makefile.am | 7 ++++++- 1 file changed, 6 insertions(+), 1 deletion(-) (limited to 'plugins') diff --git a/plugins/Makefile.am b/plugins/Makefile.am index a0e9cabf..28763dfc 100644 --- a/plugins/Makefile.am +++ b/plugins/Makefile.am @@ -46,7 +46,12 @@ SUBDIRS = picohttpparser np_test_scripts = tests/test_check_swap.t -EXTRA_DIST = t tests $(np_test_scripts) check_swap.d check_dbi.d +EXTRA_DIST = t \ + tests \ + $(np_test_scripts) \ + check_swap.d \ + check_dbi.d \ + check_ssh.d PLUGINHDRS = common.h -- cgit v1.2.3-74-g34f1