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.d/config.h | 21 +++++++++++++++++++++ 1 file changed, 21 insertions(+) create mode 100644 plugins/check_ssh.d/config.h (limited to 'plugins/check_ssh.d') 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.d') 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 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.d') 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