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