[monitoring-plugins] check_icmp: add long options, add output format ...

Lorenz Kästle git at monitoring-plugins.org
Mon Jun 23 12:10:11 CEST 2025


 Module: monitoring-plugins
 Branch: master
 Commit: cd20cc063245523d51013849fa28eb0cac013171
 Author: Lorenz Kästle <12514511+RincewindsHat at users.noreply.github.com>
   Date: Mon Jun 23 10:17:28 2025 +0200
    URL: https://www.monitoring-plugins.org/repositories/monitoring-plugins/commit/?id=cd20cc06

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.

---

 plugins-root/check_icmp.c                      | 52 +++++++++++++++++++++++++-
 plugins-root/check_icmp.d/check_icmp_helpers.c |  2 +
 plugins-root/check_icmp.d/config.h             |  6 ++-
 3 files changed, 57 insertions(+), 3 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) {
 
 	sa_family_t enforced_ai_family = AF_UNSPEC;
 
+	enum {
+		output_format_index = CHAR_MAX + 1,
+	};
+
+	struct option longopts[] = {
+		{"version", no_argument, 0, 'V'},
+		{"help", no_argument, 0, 'h'},
+		{"verbose", no_argument, 0, 'v'},
+		{"Host", required_argument, 0, 'H'},
+		{"ipv4-only", no_argument, 0, '4'},
+		{"ipv6-only", no_argument, 0, '6'},
+		{"warning", required_argument, 0, 'w'},
+		{"critical", required_argument, 0, 'c'},
+		{"rta-mode-thresholds", required_argument, 0, 'R'},
+		{"packet-loss-mode-thresholds", required_argument, 0, 'P'},
+		{"jitter-mode-thresholds", required_argument, 0, 'J'},
+		{"mos-mode-thresholds", required_argument, 0, 'M'},
+		{"score-mode-thresholds", required_argument, 0, 'S'},
+		{"out-of-order-packets", no_argument, 0, 'O'},
+		{"number-of-packets", required_argument, 0, 'n'},
+		{"number-of-packets", required_argument, 0, 'p'},
+		{"packet-interval", required_argument, 0, 'i'},
+		{"target-interval", required_argument, 0, 'I'},
+		{"outgoing-ttl", required_argument, 0, 'l'},
+		{"packet_payload_size", required_argument, 0, 'b'},
+		{"output-format", required_argument, 0, output_format_index},
+		{},
+	};
+
 	// Parse protocol arguments first
 	// and count hosts here
 	char *opts_str = "vhVw:c:n:p:t:H:s:i:b:I:l:m:P:R:J:S:M:O64";
 	for (int i = 1; i < argc; i++) {
 		long int arg;
-		while ((arg = getopt(argc, argv, opts_str)) != EOF) {
+		while ((arg = getopt_long(argc, argv, opts_str, longopts, NULL)) != EOF) {
 			switch (arg) {
 			case '4':
 				if (enforced_ai_family != AF_UNSPEC) {
@@ -374,7 +403,7 @@ check_icmp_config_wrapper process_arguments(int argc, char **argv) {
 	/* parse the arguments */
 	for (int i = 1; i < argc; i++) {
 		long int arg;
-		while ((arg = getopt(argc, argv, opts_str)) != EOF) {
+		while ((arg = getopt_long(argc, argv, opts_str, longopts, NULL)) != EOF) {
 			switch (arg) {
 			case 'b': {
 				long size = strtol(optarg, NULL, 0);
@@ -536,6 +565,18 @@ check_icmp_config_wrapper process_arguments(int argc, char **argv) {
 			case 'O': /* out of order mode */
 				result.config.modes.order_mode = true;
 				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);
+				}
+
+				result.config.output_format_is_set = true;
+				result.config.output_format = parser.output_format;
+				break;
+			}
 			}
 		}
 	}
@@ -803,6 +844,10 @@ int main(int argc, char **argv) {
 
 	const check_icmp_config config = tmp_config.config;
 
+	if (config.output_format_is_set) {
+		mp_set_format(config.output_format);
+	}
+
 	// int icmp_proto = IPPROTO_ICMP;
 	// add_target might change address_family
 	// switch (address_family) {
@@ -2104,6 +2149,9 @@ void print_help(void) {
 		   DEFAULT_PING_DATA_SIZE, ICMP_MINLEN);
 	printf(" %s\n", "-v");
 	printf("    %s\n", _("Verbosity, can be given multiple times (for debugging)"));
+
+	printf(UT_OUTPUT_FORMAT);
+
 	printf("\n");
 	printf("%s\n", _("Notes:"));
 	printf(" %s\n", _("If none of R,P,J,M,S or O is specified, default behavior is -R -P"));
diff --git a/plugins-root/check_icmp.d/check_icmp_helpers.c b/plugins-root/check_icmp.d/check_icmp_helpers.c
index ec786305..9acc96fd 100644
--- a/plugins-root/check_icmp.d/check_icmp_helpers.c
+++ b/plugins-root/check_icmp.d/check_icmp_helpers.c
@@ -52,6 +52,8 @@ check_icmp_config check_icmp_config_init() {
 
 		.number_of_hosts = 0,
 		.hosts = NULL,
+
+		.output_format_is_set = false,
 	};
 	return tmp;
 }
diff --git a/plugins-root/check_icmp.d/config.h b/plugins-root/check_icmp.d/config.h
index fc9dd5a6..8092e343 100644
--- a/plugins-root/check_icmp.d/config.h
+++ b/plugins-root/check_icmp.d/config.h
@@ -12,11 +12,12 @@
 #include <arpa/inet.h>
 #include <stdint.h>
 #include "./check_icmp_helpers.h"
+#include "output.h"
 
 /* threshold structure. all values are maximum allowed, exclusive */
 typedef struct {
 	unsigned char pl; /* max allowed packet loss in percent */
-	time_t rta; /* roundtrip time average, microseconds */
+	time_t rta;       /* roundtrip time average, microseconds */
 	double jitter;    /* jitter time average, microseconds */
 	double mos;       /* MOS */
 	double score;     /* Score */
@@ -77,6 +78,9 @@ typedef struct {
 
 	unsigned short number_of_hosts;
 	check_icmp_target_container *hosts;
+
+	mp_output_format output_format;
+	bool output_format_is_set;
 } check_icmp_config;
 
 check_icmp_config check_icmp_config_init();



More information about the Commits mailing list