diff options
author | Lorenz Kästle <12514511+RincewindsHat@users.noreply.github.com> | 2025-06-23 10:17:28 +0200 |
---|---|---|
committer | Lorenz Kästle <12514511+RincewindsHat@users.noreply.github.com> | 2025-06-23 10:17:28 +0200 |
commit | cd20cc063245523d51013849fa28eb0cac013171 (patch) | |
tree | a573a64c9c6c626b6c04d7eccfc74c62b7fd9b89 /plugins-root/check_icmp.c | |
parent | 8ae415ee4ceddeed1c1a1e0e6e64175cff6731c3 (diff) | |
download | monitoring-plugins-cd20cc06.tar.gz |
check_icmp: add long options, add output format option
This commit switches check_icmp from getopt to getopt_long
to provide long options too and (most importantly) homogenize
option parsing between the different plugins.
Diffstat (limited to 'plugins-root/check_icmp.c')
-rw-r--r-- | plugins-root/check_icmp.c | 52 |
1 files changed, 50 insertions, 2 deletions
diff --git a/plugins-root/check_icmp.c b/plugins-root/check_icmp.c index 0c69d31c..bad73cc4 100644 --- a/plugins-root/check_icmp.c +++ b/plugins-root/check_icmp.c | |||
@@ -319,12 +319,41 @@ check_icmp_config_wrapper process_arguments(int argc, char **argv) { | |||
319 | 319 | ||
320 | sa_family_t enforced_ai_family = AF_UNSPEC; | 320 | sa_family_t enforced_ai_family = AF_UNSPEC; |
321 | 321 | ||
322 | enum { | ||
323 | output_format_index = CHAR_MAX + 1, | ||
324 | }; | ||
325 | |||
326 | struct option longopts[] = { | ||
327 | {"version", no_argument, 0, 'V'}, | ||
328 | {"help", no_argument, 0, 'h'}, | ||
329 | {"verbose", no_argument, 0, 'v'}, | ||
330 | {"Host", required_argument, 0, 'H'}, | ||
331 | {"ipv4-only", no_argument, 0, '4'}, | ||
332 | {"ipv6-only", no_argument, 0, '6'}, | ||
333 | {"warning", required_argument, 0, 'w'}, | ||
334 | {"critical", required_argument, 0, 'c'}, | ||
335 | {"rta-mode-thresholds", required_argument, 0, 'R'}, | ||
336 | {"packet-loss-mode-thresholds", required_argument, 0, 'P'}, | ||
337 | {"jitter-mode-thresholds", required_argument, 0, 'J'}, | ||
338 | {"mos-mode-thresholds", required_argument, 0, 'M'}, | ||
339 | {"score-mode-thresholds", required_argument, 0, 'S'}, | ||
340 | {"out-of-order-packets", no_argument, 0, 'O'}, | ||
341 | {"number-of-packets", required_argument, 0, 'n'}, | ||
342 | {"number-of-packets", required_argument, 0, 'p'}, | ||
343 | {"packet-interval", required_argument, 0, 'i'}, | ||
344 | {"target-interval", required_argument, 0, 'I'}, | ||
345 | {"outgoing-ttl", required_argument, 0, 'l'}, | ||
346 | {"packet_payload_size", required_argument, 0, 'b'}, | ||
347 | {"output-format", required_argument, 0, output_format_index}, | ||
348 | {}, | ||
349 | }; | ||
350 | |||
322 | // Parse protocol arguments first | 351 | // Parse protocol arguments first |
323 | // and count hosts here | 352 | // and count hosts here |
324 | char *opts_str = "vhVw:c:n:p:t:H:s:i:b:I:l:m:P:R:J:S:M:O64"; | 353 | char *opts_str = "vhVw:c:n:p:t:H:s:i:b:I:l:m:P:R:J:S:M:O64"; |
325 | for (int i = 1; i < argc; i++) { | 354 | for (int i = 1; i < argc; i++) { |
326 | long int arg; | 355 | long int arg; |
327 | while ((arg = getopt(argc, argv, opts_str)) != EOF) { | 356 | while ((arg = getopt_long(argc, argv, opts_str, longopts, NULL)) != EOF) { |
328 | switch (arg) { | 357 | switch (arg) { |
329 | case '4': | 358 | case '4': |
330 | if (enforced_ai_family != AF_UNSPEC) { | 359 | if (enforced_ai_family != AF_UNSPEC) { |
@@ -374,7 +403,7 @@ check_icmp_config_wrapper process_arguments(int argc, char **argv) { | |||
374 | /* parse the arguments */ | 403 | /* parse the arguments */ |
375 | for (int i = 1; i < argc; i++) { | 404 | for (int i = 1; i < argc; i++) { |
376 | long int arg; | 405 | long int arg; |
377 | while ((arg = getopt(argc, argv, opts_str)) != EOF) { | 406 | while ((arg = getopt_long(argc, argv, opts_str, longopts, NULL)) != EOF) { |
378 | switch (arg) { | 407 | switch (arg) { |
379 | case 'b': { | 408 | case 'b': { |
380 | long size = strtol(optarg, NULL, 0); | 409 | long size = strtol(optarg, NULL, 0); |
@@ -536,6 +565,18 @@ check_icmp_config_wrapper process_arguments(int argc, char **argv) { | |||
536 | case 'O': /* out of order mode */ | 565 | case 'O': /* out of order mode */ |
537 | result.config.modes.order_mode = true; | 566 | result.config.modes.order_mode = true; |
538 | break; | 567 | break; |
568 | case output_format_index: { | ||
569 | parsed_output_format parser = mp_parse_output_format(optarg); | ||
570 | if (!parser.parsing_success) { | ||
571 | // TODO List all available formats here, maybe add anothoer usage function | ||
572 | printf("Invalid output format: %s\n", optarg); | ||
573 | exit(STATE_UNKNOWN); | ||
574 | } | ||
575 | |||
576 | result.config.output_format_is_set = true; | ||
577 | result.config.output_format = parser.output_format; | ||
578 | break; | ||
579 | } | ||
539 | } | 580 | } |
540 | } | 581 | } |
541 | } | 582 | } |
@@ -803,6 +844,10 @@ int main(int argc, char **argv) { | |||
803 | 844 | ||
804 | const check_icmp_config config = tmp_config.config; | 845 | const check_icmp_config config = tmp_config.config; |
805 | 846 | ||
847 | if (config.output_format_is_set) { | ||
848 | mp_set_format(config.output_format); | ||
849 | } | ||
850 | |||
806 | // int icmp_proto = IPPROTO_ICMP; | 851 | // int icmp_proto = IPPROTO_ICMP; |
807 | // add_target might change address_family | 852 | // add_target might change address_family |
808 | // switch (address_family) { | 853 | // switch (address_family) { |
@@ -2104,6 +2149,9 @@ void print_help(void) { | |||
2104 | DEFAULT_PING_DATA_SIZE, ICMP_MINLEN); | 2149 | DEFAULT_PING_DATA_SIZE, ICMP_MINLEN); |
2105 | printf(" %s\n", "-v"); | 2150 | printf(" %s\n", "-v"); |
2106 | printf(" %s\n", _("Verbosity, can be given multiple times (for debugging)")); | 2151 | printf(" %s\n", _("Verbosity, can be given multiple times (for debugging)")); |
2152 | |||
2153 | printf(UT_OUTPUT_FORMAT); | ||
2154 | |||
2107 | printf("\n"); | 2155 | printf("\n"); |
2108 | printf("%s\n", _("Notes:")); | 2156 | printf("%s\n", _("Notes:")); |
2109 | printf(" %s\n", _("If none of R,P,J,M,S or O is specified, default behavior is -R -P")); | 2157 | printf(" %s\n", _("If none of R,P,J,M,S or O is specified, default behavior is -R -P")); |