From 554bf3e5256f5489aed0cd56f0c600bcb281a7f5 Mon Sep 17 00:00:00 2001 From: Lorenz Kästle <12514511+RincewindsHat@users.noreply.github.com> Date: Tue, 4 Mar 2025 11:02:33 +0100 Subject: Refactor check_tcp and implement new output format --- plugins/check_tcp.d/config.h | 78 ++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 78 insertions(+) create mode 100644 plugins/check_tcp.d/config.h (limited to 'plugins/check_tcp.d') diff --git a/plugins/check_tcp.d/config.h b/plugins/check_tcp.d/config.h new file mode 100644 index 00000000..7ecf51a6 --- /dev/null +++ b/plugins/check_tcp.d/config.h @@ -0,0 +1,78 @@ +#pragma once + +#include "../common.h" +#include "../../lib/utils_tcp.h" +#include + +typedef struct check_tcp_config { + char *server_address; + bool host_specified; + int server_port; // TODO can this be a uint16? + + int protocol; /* most common is default */ + char *service; + char *send; + char *quit; + char **server_expect; + size_t server_expect_count; +#ifdef HAVE_SSL + bool use_tls; + char *sni; + bool sni_specified; + bool check_cert; + int days_till_exp_warn; + int days_till_exp_crit; +#endif // HAVE_SSL + int match_flags; + int expect_mismatch_state; + unsigned int delay; + + bool warning_time_set; + double warning_time; + bool critical_time_set; + double critical_time; + + int econn_refuse_state; + + ssize_t maxbytes; + + bool hide_output; +} check_tcp_config; + +check_tcp_config check_tcp_config_init() { + check_tcp_config result = { + .server_address = "127.0.0.1", + .host_specified = false, + .server_port = 0, + + .protocol = IPPROTO_TCP, + .service = "TCP", + .send = NULL, + .quit = NULL, + .server_expect = NULL, + .server_expect_count = 0, +#ifdef HAVE_SSL + .use_tls = false, + .sni = NULL, + .sni_specified = false, + .check_cert = false, + .days_till_exp_warn = 0, + .days_till_exp_crit = 0, +#endif // HAVE_SSL + .match_flags = NP_MATCH_EXACT, + .expect_mismatch_state = STATE_WARNING, + .delay = 0, + + .warning_time_set = false, + .warning_time = 0, + .critical_time_set = false, + .critical_time = 0, + + .econn_refuse_state = STATE_CRITICAL, + + .maxbytes = 0, + + .hide_output = false, + }; + return result; +} -- cgit v1.2.3-74-g34f1 From d5ed6a2d8f3f3f388e5d1f2f7a8fc3ee2c9b6007 Mon Sep 17 00:00:00 2001 From: Lorenz Kästle <12514511+RincewindsHat@users.noreply.github.com> Date: Thu, 13 Mar 2025 00:43:22 +0100 Subject: check_tcp: small improvement + output format picker --- plugins/check_tcp.c | 46 +++++++++++++++++++++++++++++--------------- plugins/check_tcp.d/config.h | 9 +++++++-- 2 files changed, 38 insertions(+), 17 deletions(-) (limited to 'plugins/check_tcp.d') diff --git a/plugins/check_tcp.c b/plugins/check_tcp.c index f93152e5..793cfe7e 100644 --- a/plugins/check_tcp.c +++ b/plugins/check_tcp.c @@ -28,7 +28,6 @@ *****************************************************************************/ /* progname "check_tcp" changes depending on symlink called */ -#include "states.h" char *progname; const char *copyright = "1999-2025"; const char *email = "devel@monitoring-plugins.org"; @@ -37,6 +36,7 @@ const char *email = "devel@monitoring-plugins.org"; #include "./netutils.h" #include "./utils.h" #include "./check_tcp.d/config.h" +#include "states.h" #include #include @@ -61,10 +61,10 @@ ssize_t my_send(char *buf, size_t len) { typedef struct process_arguments_wrapper { int errorcode; check_tcp_config config; -} process_arguments_wrapper; +} check_tcp_config_wrapper; /* int my_recv(char *, size_t); */ -static process_arguments_wrapper process_arguments(int /*argc*/, char ** /*argv*/, check_tcp_config /*config*/); +static check_tcp_config_wrapper process_arguments(int /*argc*/, char ** /*argv*/, check_tcp_config /*config*/); void print_help(const char *service); void print_usage(void); @@ -207,7 +207,7 @@ int main(int argc, char **argv) { /* Parse extra opts if any */ argv = np_extra_opts(&argc, argv, progname); - process_arguments_wrapper paw = process_arguments(argc, argv, config); + check_tcp_config_wrapper paw = process_arguments(argc, argv, config); if (paw.errorcode == ERROR) { usage4(_("Could not parse arguments")); } @@ -229,6 +229,9 @@ int main(int argc, char **argv) { // Initialize check stuff before setting timers mp_check overall = mp_check_init(); + if (config.output_format_set) { + overall.format = config.output_format; + } /* set up the timer */ signal(SIGALRM, socket_timeout_alarm_handler); @@ -452,12 +455,12 @@ int main(int argc, char **argv) { } /* process command-line arguments */ -static process_arguments_wrapper process_arguments(int argc, char **argv, check_tcp_config config) { +static check_tcp_config_wrapper process_arguments(int argc, char **argv, check_tcp_config config) { enum { - SNI_OPTION = CHAR_MAX + 1 + SNI_OPTION = CHAR_MAX + 1, + output_format_index, }; - int option = 0; static struct option longopts[] = {{"hostname", required_argument, 0, 'H'}, {"critical", required_argument, 0, 'c'}, {"warning", required_argument, 0, 'w'}, @@ -484,6 +487,7 @@ static process_arguments_wrapper process_arguments(int argc, char **argv, check_ {"ssl", no_argument, 0, 'S'}, {"sni", required_argument, 0, SNI_OPTION}, {"certificate", required_argument, 0, 'D'}, + {"output-format", required_argument, 0, output_format_index}, {0, 0, 0, 0}}; if (argc < 2) { @@ -497,17 +501,17 @@ static process_arguments_wrapper process_arguments(int argc, char **argv, check_ argc--; } - int c; bool escape = false; while (true) { - c = getopt_long(argc, argv, "+hVv46EAH:s:e:q:m:c:w:t:p:C:W:d:Sr:jD:M:", longopts, &option); + int option = 0; + int option_index = getopt_long(argc, argv, "+hVv46EAH:s:e:q:m:c:w:t:p:C:W:d:Sr:jD:M:", longopts, &option); - if (c == -1 || c == EOF || c == 1) { + if (option_index == -1 || option_index == EOF || option_index == 1) { break; } - switch (c) { + switch (option_index) { case '?': /* print short usage statement if args not parsable */ usage5(); case 'h': /* help */ @@ -674,12 +678,24 @@ static process_arguments_wrapper process_arguments(int argc, char **argv, check_ case 'A': config.match_flags |= NP_MATCH_ALL; break; + 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_set = true; + config.output_format = parser.output_format; + break; + } } } - c = optind; - if (!config.host_specified && c < argc) { - config.server_address = strdup(argv[c++]); + int index = optind; + if (!config.host_specified && index < argc) { + config.server_address = strdup(argv[index++]); } if (config.server_address == NULL) { @@ -689,7 +705,7 @@ static process_arguments_wrapper process_arguments(int argc, char **argv, check_ config.server_address); } - process_arguments_wrapper result = { + check_tcp_config_wrapper result = { .config = config, .errorcode = OK, }; diff --git a/plugins/check_tcp.d/config.h b/plugins/check_tcp.d/config.h index 7ecf51a6..41db7224 100644 --- a/plugins/check_tcp.d/config.h +++ b/plugins/check_tcp.d/config.h @@ -1,10 +1,10 @@ #pragma once -#include "../common.h" #include "../../lib/utils_tcp.h" +#include "output.h" #include -typedef struct check_tcp_config { +typedef struct { char *server_address; bool host_specified; int server_port; // TODO can this be a uint16? @@ -37,6 +37,9 @@ typedef struct check_tcp_config { ssize_t maxbytes; bool hide_output; + + bool output_format_set; + mp_output_format output_format; } check_tcp_config; check_tcp_config check_tcp_config_init() { @@ -73,6 +76,8 @@ check_tcp_config check_tcp_config_init() { .maxbytes = 0, .hide_output = false, + + .output_format_set = false, }; return result; } -- cgit v1.2.3-74-g34f1 From 285000a2ad1198046275f5bd5b47227f1cd66471 Mon Sep 17 00:00:00 2001 From: Lorenz Kästle <12514511+RincewindsHat@users.noreply.github.com> Date: Thu, 13 Mar 2025 13:00:05 +0100 Subject: small fixes to check_tcp config --- plugins/check_tcp.d/config.h | 9 +++++---- 1 file changed, 5 insertions(+), 4 deletions(-) (limited to 'plugins/check_tcp.d') diff --git a/plugins/check_tcp.d/config.h b/plugins/check_tcp.d/config.h index 41db7224..dc25d79e 100644 --- a/plugins/check_tcp.d/config.h +++ b/plugins/check_tcp.d/config.h @@ -2,6 +2,7 @@ #include "../../lib/utils_tcp.h" #include "output.h" +#include "states.h" #include typedef struct { @@ -15,8 +16,8 @@ typedef struct { char *quit; char **server_expect; size_t server_expect_count; -#ifdef HAVE_SSL bool use_tls; +#ifdef HAVE_SSL char *sni; bool sni_specified; bool check_cert; @@ -24,7 +25,7 @@ typedef struct { int days_till_exp_crit; #endif // HAVE_SSL int match_flags; - int expect_mismatch_state; + mp_state_enum expect_mismatch_state; unsigned int delay; bool warning_time_set; @@ -32,7 +33,7 @@ typedef struct { bool critical_time_set; double critical_time; - int econn_refuse_state; + mp_state_enum econn_refuse_state; ssize_t maxbytes; @@ -54,8 +55,8 @@ check_tcp_config check_tcp_config_init() { .quit = NULL, .server_expect = NULL, .server_expect_count = 0, -#ifdef HAVE_SSL .use_tls = false, +#ifdef HAVE_SSL .sni = NULL, .sni_specified = false, .check_cert = false, -- cgit v1.2.3-74-g34f1