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/check_ssh.c') 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 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/check_ssh.c') 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/check_ssh.c') 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/check_ssh.c') 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/check_ssh.c') 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/check_ssh.c') 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/check_ssh.c') 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/check_ssh.c') 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/check_ssh.c') 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/check_ssh.c') 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/check_ssh.c') 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/check_ssh.c') 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 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/check_ssh.c') 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