[monitoring-plugins] * check_ping: added option for packet size

RincewindsHat git at monitoring-plugins.org
Thu Nov 18 19:00:13 CET 2021


    Module: monitoring-plugins
    Branch: check_ping_adjust_packet_size
    Commit: a2eb4e8210deb37133ece1e6e94aad4e6c5bea14
    Author: Oliver Skibbe <oliskibbe at gmail.com>
 Committer: RincewindsHat <12514511+RincewindsHat at users.noreply.github.com>
      Date: Mon Sep 28 14:22:09 2015 +0200
       URL: https://www.monitoring-plugins.org/repositories/monitoring-plugins/commit/?id=a2eb4e8

* check_ping: added option for packet size

  - added option (-s/--packetsize) for setting packet size
    (limited to 65528 Bytes, according to RFC791)
  - added DEFAULT_PACKET_SIZE constant with 56 Bytes)
  - disabled ping check for UnixWare in autoconf: if condition and
    with_ping_command had different syntax

---

 configure.ac         | 37 ++++++++++++++++++++++++++++++++-----
 plugins/check_ping.c | 36 +++++++++++++++++++++++++++++++-----
 2 files changed, 63 insertions(+), 10 deletions(-)

diff --git a/configure.ac b/configure.ac
index dfc37b5..5e2a0b3 100644
--- a/configure.ac
+++ b/configure.ac
@@ -1079,6 +1079,7 @@ AC_ARG_WITH(ping_command,
 AC_MSG_CHECKING(for ICMP ping syntax)
 ac_cv_ping_packets_first=no
 ac_cv_ping_has_timeout=no
+ac_cv_ping_has_packetsize=no
 if test -n "$with_ping_command"
 then
 	AC_MSG_RESULT([(command-line) $with_ping_command])
@@ -1102,12 +1103,22 @@ then
 	ac_cv_ping_has_timeout=yes
 	AC_MSG_RESULT([$with_ping_command])
 
-elif [[ "z$ac_cv_uname_s" = "zUnixWare" ]] && \
-	$PATH_TO_PING -n -s 127.0.0.1 56 1 2>/dev/null | \
+# XXX with_ping_command not same like if condition
+#elif [[ "z$ac_cv_uname_s" = "zUnixWare" ]] && \
+#	$PATH_TO_PING -n -s 127.0.0.1 56 1 2>/dev/null | \
+#	egrep -i "^round-trip|^rtt" >/dev/null
+#then
+#	with_ping_command="$PATH_TO_PING -n -U -c %d %s"
+#	ac_cv_ping_packets_first=yes
+#	AC_MSG_RESULT([$with_ping_command])
+
+elif $PATH_TO_PING -n -U -w 10 -c 1 -s 56 127.0.0.1 2>/dev/null | \
 	egrep -i "^round-trip|^rtt" >/dev/null
 then
-	with_ping_command="$PATH_TO_PING -n -U -c %d %s"
+	with_ping_command="$PATH_TO_PING -n -U -w %d -c %d -s %d %s"
 	ac_cv_ping_packets_first=yes
+	ac_cv_ping_has_timeout=yes
+	ac_cv_ping_has_packetsize=yes
 	AC_MSG_RESULT([$with_ping_command])
 
 elif $PATH_TO_PING -4 -n -U -w 10 -c 1 127.0.0.1 2>/dev/null | \
@@ -1126,6 +1137,14 @@ then
 	ac_cv_ping_has_timeout=yes
 	AC_MSG_RESULT([$with_ping_command])
 
+elif $PATH_TO_PING -n -U -c 1 -s 56 127.0.0.1 2>/dev/null | \
+	egrep -i "^round-trip|^rtt" >/dev/null
+then
+	with_ping_command="$PATH_TO_PING -n -U -c %d -s %d %s"
+	ac_cv_ping_packets_first=yes
+	ac_cv_ping_has_packetsize=yes
+	AC_MSG_RESULT([$with_ping_command])
+
 elif $PATH_TO_PING -n -U -c 1 127.0.0.1 2>/dev/null | \
 	egrep -i "^round-trip|^rtt" >/dev/null
 then
@@ -1155,13 +1174,15 @@ then
 elif $PATH_TO_PING -n -s 127.0.0.1 56 1 2>/dev/null | \
 	egrep -i "^round-trip|^rtt" >/dev/null
 then
-	with_ping_command="$PATH_TO_PING -n -s %s 56 %d"
+	with_ping_command="$PATH_TO_PING -n -s %s %d %d"
+	ac_cv_ping_has_packetsize=yes
 	AC_MSG_RESULT([$with_ping_command])
 
 elif $PATH_TO_PING -n -h 127.0.0.1 -s 56 -c 1 2>/dev/null | \
 	egrep -i "^round-trip|^rtt" >/dev/null
 then
-	with_ping_command="$PATH_TO_PING -n -h %s -s 56 -c %d"
+	with_ping_command="$PATH_TO_PING -n -h %s -s %d -c %d"
+	ac_cv_ping_has_packetsize=yes
 	AC_MSG_RESULT([$with_ping_command])
 
 elif $PATH_TO_PING -n -s 56 -c 1 127.0.0.1 2>/dev/null | \
@@ -1197,6 +1218,12 @@ then
 		[Define if ping has its own timeout option that should be set])
 fi
 
+if test "x$ac_cv_ping_has_packetsize" != "xno"
+then
+	AC_DEFINE(PING_HAS_PACKETSIZE,1,
+		[Define if ping has an option for custom packet size])
+fi
+
 AC_ARG_WITH(ping6_command,
 	ACX_HELP_STRING([--with-ping6-command=SYNTAX],
 		[sets syntax for ICMPv6 ping]),
diff --git a/plugins/check_ping.c b/plugins/check_ping.c
index ba7af37..de173f9 100644
--- a/plugins/check_ping.c
+++ b/plugins/check_ping.c
@@ -44,7 +44,8 @@ const char *email = "devel at monitoring-plugins.org";
 
 enum {
 	UNKNOWN_PACKET_LOSS = 200,    /* 200% */
-	DEFAULT_MAX_PACKETS = 5       /* default no. of ICMP ECHO packets */
+	DEFAULT_MAX_PACKETS = 5,       /* default no. of ICMP ECHO packets */
+	DEFAULT_PACKET_SIZE = 56      /* default size of ICMP ECHO packets */
 };
 
 int process_arguments (int, char **);
@@ -64,6 +65,7 @@ char **addresses = NULL;
 int n_addresses = 0;
 int max_addr = 1;
 int max_packets = -1;
+int packet_size = DEFAULT_PACKET_SIZE;
 int verbose = 0;
 
 float rta = UNKNOWN_TRIP_TIME;
@@ -122,13 +124,21 @@ main (int argc, char **argv)
 
 		/* does the host address of number of packets argument come first? */
 #ifdef PING_PACKETS_FIRST
-# ifdef PING_HAS_TIMEOUT
+# if defined(PING_HAS_TIMEOUT) && defined(PING_HAS_PACKETSIZE)
+		xasprintf (&cmd, rawcmd, timeout_interval, max_packets, packet_size, addresses[i]);
+# elif defined(PING_HAS_TIMEOUT)
 		xasprintf (&cmd, rawcmd, timeout_interval, max_packets, addresses[i]);
+# elif defined(PING_HAS_PACKETSIZE)
+		xasprintf (&cmd, rawcmd, max_packets, packet_size, addresses[i]);
 # else
 		xasprintf (&cmd, rawcmd, max_packets, addresses[i]);
 # endif
 #else
-		xasprintf (&cmd, rawcmd, addresses[i], max_packets);
+# ifdef PING_HAS_PACKETSIZE
+		xasprintf (&cmd, rawcmd, addresses[i], packet_size, max_packets);
+# else
+                xasprintf (&cmd, rawcmd, addresses[i], max_packets);
+# endif
 #endif
 
 		if (verbose >= 2)
@@ -202,6 +212,7 @@ process_arguments (int argc, char **argv)
 	static struct option longopts[] = {
 		STD_LONG_OPTS,
 		{"packets", required_argument, 0, 'p'},
+		{"packetsize",required_argument, 0, 's'}, 
 		{"nohtml", no_argument, 0, 'n'},
 		{"link", no_argument, 0, 'L'},
 		{"use-ipv4", no_argument, 0, '4'},
@@ -220,7 +231,7 @@ process_arguments (int argc, char **argv)
 	}
 
 	while (1) {
-		c = getopt_long (argc, argv, "VvhnL46t:c:w:H:p:", longopts, &option);
+		c = getopt_long (argc, argv, "VvhnL46t:c:w:H:p:s:", longopts, &option);
 
 		if (c == -1 || c == EOF)
 			break;
@@ -277,6 +288,12 @@ process_arguments (int argc, char **argv)
 			else
 				usage2 (_("<max_packets> (%s) must be a non-negative number\n"), optarg);
 			break;
+		case 's':	/* number of packets to send */
+			if (is_intnonneg (optarg))
+				packet_size = atoi (optarg);
+			else
+				usage2 (_("<packet_size> (%s) must be a non-negative number\n"), optarg);
+			break;
 		case 'n':	/* no HTML */
 			display_html = FALSE;
 			break;
@@ -307,6 +324,7 @@ process_arguments (int argc, char **argv)
 		}
 	}
 
+	/* XXX shouldnt this be in function validate_arguments ? */
 	if (wpl == UNKNOWN_PACKET_LOSS) {
 		if (is_intpercent (argv[c]) == FALSE) {
 			printf (_("<wpl> (%s) must be an integer percentage\n"), argv[c]);
@@ -359,6 +377,7 @@ process_arguments (int argc, char **argv)
 			return ERROR;
 		}
 	}
+	/* XXX END  shouldnt this be in function validate_arguments ? */
 
 	return validate_arguments ();
 }
@@ -387,6 +406,10 @@ validate_arguments ()
 	float max_seconds;
 	int i;
 
+	if (packet_size > 65528) {
+		printf (_("max packet size cannot be larger than 65528 Bytes"));
+		return ERROR;
+	}
 	if (wrta < 0.0) {
 		printf (_("<wrta> was not set\n"));
 		return ERROR;
@@ -591,6 +614,9 @@ print_help (void)
   printf (" %s\n", "-p, --packets=INTEGER");
   printf ("    %s ", _("number of ICMP ECHO packets to send"));
   printf (_("(Default: %d)\n"), DEFAULT_MAX_PACKETS);
+  printf (" %s\n", "-s, --packetsize=INTEGER");
+  printf ("    %s ", _("size of ICMP ECHO packet to send"));
+  printf (_("(Default: %d)\n"), DEFAULT_PACKET_SIZE);
   printf (" %s\n", "-L, --link");
   printf ("    %s\n", _("show HTML in the plugin output (obsoleted by urlize)"));
 
@@ -615,5 +641,5 @@ print_usage (void)
 {
   printf ("%s\n", _("Usage:"));
 	printf ("%s -H <host_address> -w <wrta>,<wpl>%% -c <crta>,<cpl>%%\n", progname);
-  printf (" [-p packets] [-t timeout] [-4|-6]\n");
+  printf (" [-p packets] [-s packetsize] [-t timeout] [-4|-6]\n");
 }



More information about the Commits mailing list