diff options
author | Lorenz Kästle <12514511+RincewindsHat@users.noreply.github.com> | 2025-03-13 00:43:22 +0100 |
---|---|---|
committer | Lorenz Kästle <12514511+RincewindsHat@users.noreply.github.com> | 2025-03-13 00:43:22 +0100 |
commit | d5ed6a2d8f3f3f388e5d1f2f7a8fc3ee2c9b6007 (patch) | |
tree | a1e49d438fe891e3e78949d36c7cfacbb5128fe4 /plugins | |
parent | 554bf3e5256f5489aed0cd56f0c600bcb281a7f5 (diff) | |
download | monitoring-plugins-d5ed6a2d8f3f3f388e5d1f2f7a8fc3ee2c9b6007.tar.gz |
check_tcp: small improvement + output format picker
Diffstat (limited to 'plugins')
-rw-r--r-- | plugins/check_tcp.c | 46 | ||||
-rw-r--r-- | plugins/check_tcp.d/config.h | 9 |
2 files changed, 38 insertions, 17 deletions
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 @@ | |||
28 | *****************************************************************************/ | 28 | *****************************************************************************/ |
29 | 29 | ||
30 | /* progname "check_tcp" changes depending on symlink called */ | 30 | /* progname "check_tcp" changes depending on symlink called */ |
31 | #include "states.h" | ||
32 | char *progname; | 31 | char *progname; |
33 | const char *copyright = "1999-2025"; | 32 | const char *copyright = "1999-2025"; |
34 | const char *email = "devel@monitoring-plugins.org"; | 33 | const char *email = "devel@monitoring-plugins.org"; |
@@ -37,6 +36,7 @@ const char *email = "devel@monitoring-plugins.org"; | |||
37 | #include "./netutils.h" | 36 | #include "./netutils.h" |
38 | #include "./utils.h" | 37 | #include "./utils.h" |
39 | #include "./check_tcp.d/config.h" | 38 | #include "./check_tcp.d/config.h" |
39 | #include "states.h" | ||
40 | 40 | ||
41 | #include <sys/types.h> | 41 | #include <sys/types.h> |
42 | #include <ctype.h> | 42 | #include <ctype.h> |
@@ -61,10 +61,10 @@ ssize_t my_send(char *buf, size_t len) { | |||
61 | typedef struct process_arguments_wrapper { | 61 | typedef struct process_arguments_wrapper { |
62 | int errorcode; | 62 | int errorcode; |
63 | check_tcp_config config; | 63 | check_tcp_config config; |
64 | } process_arguments_wrapper; | 64 | } check_tcp_config_wrapper; |
65 | 65 | ||
66 | /* int my_recv(char *, size_t); */ | 66 | /* int my_recv(char *, size_t); */ |
67 | static process_arguments_wrapper process_arguments(int /*argc*/, char ** /*argv*/, check_tcp_config /*config*/); | 67 | static check_tcp_config_wrapper process_arguments(int /*argc*/, char ** /*argv*/, check_tcp_config /*config*/); |
68 | void print_help(const char *service); | 68 | void print_help(const char *service); |
69 | void print_usage(void); | 69 | void print_usage(void); |
70 | 70 | ||
@@ -207,7 +207,7 @@ int main(int argc, char **argv) { | |||
207 | /* Parse extra opts if any */ | 207 | /* Parse extra opts if any */ |
208 | argv = np_extra_opts(&argc, argv, progname); | 208 | argv = np_extra_opts(&argc, argv, progname); |
209 | 209 | ||
210 | process_arguments_wrapper paw = process_arguments(argc, argv, config); | 210 | check_tcp_config_wrapper paw = process_arguments(argc, argv, config); |
211 | if (paw.errorcode == ERROR) { | 211 | if (paw.errorcode == ERROR) { |
212 | usage4(_("Could not parse arguments")); | 212 | usage4(_("Could not parse arguments")); |
213 | } | 213 | } |
@@ -229,6 +229,9 @@ int main(int argc, char **argv) { | |||
229 | 229 | ||
230 | // Initialize check stuff before setting timers | 230 | // Initialize check stuff before setting timers |
231 | mp_check overall = mp_check_init(); | 231 | mp_check overall = mp_check_init(); |
232 | if (config.output_format_set) { | ||
233 | overall.format = config.output_format; | ||
234 | } | ||
232 | 235 | ||
233 | /* set up the timer */ | 236 | /* set up the timer */ |
234 | signal(SIGALRM, socket_timeout_alarm_handler); | 237 | signal(SIGALRM, socket_timeout_alarm_handler); |
@@ -452,12 +455,12 @@ int main(int argc, char **argv) { | |||
452 | } | 455 | } |
453 | 456 | ||
454 | /* process command-line arguments */ | 457 | /* process command-line arguments */ |
455 | static process_arguments_wrapper process_arguments(int argc, char **argv, check_tcp_config config) { | 458 | static check_tcp_config_wrapper process_arguments(int argc, char **argv, check_tcp_config config) { |
456 | enum { | 459 | enum { |
457 | SNI_OPTION = CHAR_MAX + 1 | 460 | SNI_OPTION = CHAR_MAX + 1, |
461 | output_format_index, | ||
458 | }; | 462 | }; |
459 | 463 | ||
460 | int option = 0; | ||
461 | static struct option longopts[] = {{"hostname", required_argument, 0, 'H'}, | 464 | static struct option longopts[] = {{"hostname", required_argument, 0, 'H'}, |
462 | {"critical", required_argument, 0, 'c'}, | 465 | {"critical", required_argument, 0, 'c'}, |
463 | {"warning", required_argument, 0, 'w'}, | 466 | {"warning", required_argument, 0, 'w'}, |
@@ -484,6 +487,7 @@ static process_arguments_wrapper process_arguments(int argc, char **argv, check_ | |||
484 | {"ssl", no_argument, 0, 'S'}, | 487 | {"ssl", no_argument, 0, 'S'}, |
485 | {"sni", required_argument, 0, SNI_OPTION}, | 488 | {"sni", required_argument, 0, SNI_OPTION}, |
486 | {"certificate", required_argument, 0, 'D'}, | 489 | {"certificate", required_argument, 0, 'D'}, |
490 | {"output-format", required_argument, 0, output_format_index}, | ||
487 | {0, 0, 0, 0}}; | 491 | {0, 0, 0, 0}}; |
488 | 492 | ||
489 | if (argc < 2) { | 493 | if (argc < 2) { |
@@ -497,17 +501,17 @@ static process_arguments_wrapper process_arguments(int argc, char **argv, check_ | |||
497 | argc--; | 501 | argc--; |
498 | } | 502 | } |
499 | 503 | ||
500 | int c; | ||
501 | bool escape = false; | 504 | bool escape = false; |
502 | 505 | ||
503 | while (true) { | 506 | while (true) { |
504 | c = getopt_long(argc, argv, "+hVv46EAH:s:e:q:m:c:w:t:p:C:W:d:Sr:jD:M:", longopts, &option); | 507 | int option = 0; |
508 | int option_index = getopt_long(argc, argv, "+hVv46EAH:s:e:q:m:c:w:t:p:C:W:d:Sr:jD:M:", longopts, &option); | ||
505 | 509 | ||
506 | if (c == -1 || c == EOF || c == 1) { | 510 | if (option_index == -1 || option_index == EOF || option_index == 1) { |
507 | break; | 511 | break; |
508 | } | 512 | } |
509 | 513 | ||
510 | switch (c) { | 514 | switch (option_index) { |
511 | case '?': /* print short usage statement if args not parsable */ | 515 | case '?': /* print short usage statement if args not parsable */ |
512 | usage5(); | 516 | usage5(); |
513 | case 'h': /* help */ | 517 | case 'h': /* help */ |
@@ -674,12 +678,24 @@ static process_arguments_wrapper process_arguments(int argc, char **argv, check_ | |||
674 | case 'A': | 678 | case 'A': |
675 | config.match_flags |= NP_MATCH_ALL; | 679 | config.match_flags |= NP_MATCH_ALL; |
676 | break; | 680 | break; |
681 | case output_format_index: { | ||
682 | parsed_output_format parser = mp_parse_output_format(optarg); | ||
683 | if (!parser.parsing_success) { | ||
684 | // TODO List all available formats here, maybe add anothoer usage function | ||
685 | printf("Invalid output format: %s\n", optarg); | ||
686 | exit(STATE_UNKNOWN); | ||
687 | } | ||
688 | |||
689 | config.output_format_set = true; | ||
690 | config.output_format = parser.output_format; | ||
691 | break; | ||
692 | } | ||
677 | } | 693 | } |
678 | } | 694 | } |
679 | 695 | ||
680 | c = optind; | 696 | int index = optind; |
681 | if (!config.host_specified && c < argc) { | 697 | if (!config.host_specified && index < argc) { |
682 | config.server_address = strdup(argv[c++]); | 698 | config.server_address = strdup(argv[index++]); |
683 | } | 699 | } |
684 | 700 | ||
685 | if (config.server_address == NULL) { | 701 | if (config.server_address == NULL) { |
@@ -689,7 +705,7 @@ static process_arguments_wrapper process_arguments(int argc, char **argv, check_ | |||
689 | config.server_address); | 705 | config.server_address); |
690 | } | 706 | } |
691 | 707 | ||
692 | process_arguments_wrapper result = { | 708 | check_tcp_config_wrapper result = { |
693 | .config = config, | 709 | .config = config, |
694 | .errorcode = OK, | 710 | .errorcode = OK, |
695 | }; | 711 | }; |
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 @@ | |||
1 | #pragma once | 1 | #pragma once |
2 | 2 | ||
3 | #include "../common.h" | ||
4 | #include "../../lib/utils_tcp.h" | 3 | #include "../../lib/utils_tcp.h" |
4 | #include "output.h" | ||
5 | #include <netinet/in.h> | 5 | #include <netinet/in.h> |
6 | 6 | ||
7 | typedef struct check_tcp_config { | 7 | typedef struct { |
8 | char *server_address; | 8 | char *server_address; |
9 | bool host_specified; | 9 | bool host_specified; |
10 | int server_port; // TODO can this be a uint16? | 10 | int server_port; // TODO can this be a uint16? |
@@ -37,6 +37,9 @@ typedef struct check_tcp_config { | |||
37 | ssize_t maxbytes; | 37 | ssize_t maxbytes; |
38 | 38 | ||
39 | bool hide_output; | 39 | bool hide_output; |
40 | |||
41 | bool output_format_set; | ||
42 | mp_output_format output_format; | ||
40 | } check_tcp_config; | 43 | } check_tcp_config; |
41 | 44 | ||
42 | check_tcp_config check_tcp_config_init() { | 45 | check_tcp_config check_tcp_config_init() { |
@@ -73,6 +76,8 @@ check_tcp_config check_tcp_config_init() { | |||
73 | .maxbytes = 0, | 76 | .maxbytes = 0, |
74 | 77 | ||
75 | .hide_output = false, | 78 | .hide_output = false, |
79 | |||
80 | .output_format_set = false, | ||
76 | }; | 81 | }; |
77 | return result; | 82 | return result; |
78 | } | 83 | } |