[nagiosplug] Improve parsing of ping6(1) output

Nagios Plugin Development nagios-plugins at users.sourceforge.net
Mon Jun 11 23:50:16 CEST 2012


 Module: nagiosplug
 Branch: master
 Commit: a80eafbf9cab9d80c526c504edf2ee4446a072d9
 Author: Holger Weiss <holger at zedat.fu-berlin.de>
   Date: Mon Jun 11 23:40:04 2012 +0200
    URL: http://nagiosplug.git.sf.net/git/gitweb.cgi?p=nagiosplug/nagiosplug;a=commit;h=a80eafb

Improve parsing of ping6(1) output

The ping6(1) implementation provided by Debian's iputils-ping package
may produce output such as the following:

| 3 packets transmitted, 0 received, +3 errors, 100% packet loss, time 2009ms

There's a corresponding pattern in check_ping.c:458:

| "%*d packets transmitted, %*d received, +%*d errors, %d%% packet loss"

Without this fix, the pattern in check_ping.c:456 matched first (as
sscanf(3) interprets "+3" as a match for "%d"):

| "%*d packets transmitted, %*d received, %d%% loss, time"

(#1894850 - Debian bug report #514588 - Matej Vela)

---

 NEWS                 |    1 +
 THANKS.in            |    1 +
 plugins/check_ping.c |   36 +++++++++++++++++++-----------------
 3 files changed, 21 insertions(+), 17 deletions(-)

diff --git a/NEWS b/NEWS
index dd47139..7452a6b 100644
--- a/NEWS
+++ b/NEWS
@@ -23,6 +23,7 @@ This file documents the major additions and syntax changes between releases.
 	Fix check_procs where regex input of '|' would get displayed in output - now replaced with ','
 	Fix segfault in check_host when hostname returns multiple IP addresses (Sebastian Harl)
 	Fix check_smtp and check_tcp where duplicate messages were displayed for certificate errors
+	Fix check_ping's parsing of the output of Debian's ping6(1) implementation (#1894850 - Matej Vela)
 	Disable RFC4507 support, to work around SSL negotiation issues with (at least) some Tomcat versions
 
 1.4.15 27th July 2010
diff --git a/THANKS.in b/THANKS.in
index c858ce4..32a8868 100644
--- a/THANKS.in
+++ b/THANKS.in
@@ -272,3 +272,4 @@ Sebastian Harl
 Jason Lunn
 Alex Griffin
 Marc Remy
+Matej Vela
diff --git a/plugins/check_ping.c b/plugins/check_ping.c
index ecdd618..1367e90 100644
--- a/plugins/check_ping.c
+++ b/plugins/check_ping.c
@@ -432,6 +432,7 @@ run_ping (const char *cmd, const char *addr)
 {
 	char buf[MAX_INPUT_BUFFER];
 	int result = STATE_UNKNOWN;
+	int match;
 
 	if ((child_process = spopen (cmd)) == NULL)
 		die (STATE_UNKNOWN, _("Could not open pipe: %s\n"), cmd);
@@ -448,28 +449,29 @@ run_ping (const char *cmd, const char *addr)
 		result = max_state (result, error_scan (buf, addr));
 
 		/* get the percent loss statistics */
-		if(sscanf(buf,"%*d packets transmitted, %*d packets received, +%*d errors, %d%% packet loss",&pl)==1 ||
-			 sscanf(buf,"%*d packets transmitted, %*d packets received, +%*d duplicates, %d%% packet loss", &pl) == 1 ||
-			 sscanf(buf,"%*d packets transmitted, %*d received, +%*d duplicates, %d%% packet loss", &pl) == 1 ||
-			 sscanf(buf,"%*d packets transmitted, %*d packets received, %d%% packet loss",&pl)==1 ||
-			 sscanf(buf,"%*d packets transmitted, %*d packets received, %d%% loss, time",&pl)==1 ||
-			 sscanf(buf,"%*d packets transmitted, %*d received, %d%% loss, time", &pl)==1 ||
-			 sscanf(buf,"%*d packets transmitted, %*d received, %d%% packet loss, time", &pl)==1 ||
-			 sscanf(buf,"%*d packets transmitted, %*d received, +%*d errors, %d%% packet loss", &pl) == 1 ||
-			 sscanf(buf,"%*d packets transmitted %*d received, +%*d errors, %d%% packet loss", &pl) == 1
+		match = 0;
+		if((sscanf(buf,"%*d packets transmitted, %*d packets received, +%*d errors, %d%% packet loss%n",&pl,&match) && match) ||
+			 (sscanf(buf,"%*d packets transmitted, %*d packets received, +%*d duplicates, %d%% packet loss%n",&pl,&match) && match) ||
+			 (sscanf(buf,"%*d packets transmitted, %*d received, +%*d duplicates, %d%% packet loss%n",&pl,&match) && match) ||
+			 (sscanf(buf,"%*d packets transmitted, %*d packets received, %d%% packet loss%n",&pl,&match) && match) ||
+			 (sscanf(buf,"%*d packets transmitted, %*d packets received, %d%% loss, time%n",&pl,&match) && match) ||
+			 (sscanf(buf,"%*d packets transmitted, %*d received, %d%% loss, time%n",&pl,&match) && match) ||
+			 (sscanf(buf,"%*d packets transmitted, %*d received, %d%% packet loss, time%n",&pl,&match) && match) ||
+			 (sscanf(buf,"%*d packets transmitted, %*d received, +%*d errors, %d%% packet loss%n",&pl,&match) && match) ||
+			 (sscanf(buf,"%*d packets transmitted %*d received, +%*d errors, %d%% packet loss%n",&pl,&match) && match)
 			 )
 			continue;
 
 		/* get the round trip average */
 		else
-			if(sscanf(buf,"round-trip min/avg/max = %*f/%f/%*f",&rta)==1 ||
-				 sscanf(buf,"round-trip min/avg/max/mdev = %*f/%f/%*f/%*f",&rta)==1 ||
-				 sscanf(buf,"round-trip min/avg/max/sdev = %*f/%f/%*f/%*f",&rta)==1 ||
-				 sscanf(buf,"round-trip min/avg/max/stddev = %*f/%f/%*f/%*f",&rta)==1 ||
-				 sscanf(buf,"round-trip min/avg/max/std-dev = %*f/%f/%*f/%*f",&rta)==1 ||
-				 sscanf(buf,"round-trip (ms) min/avg/max = %*f/%f/%*f",&rta)==1 ||
-				 sscanf(buf,"round-trip (ms) min/avg/max/stddev = %*f/%f/%*f/%*f",&rta)==1 ||
-				 sscanf(buf,"rtt min/avg/max/mdev = %*f/%f/%*f/%*f ms",&rta)==1)
+			if((sscanf(buf,"round-trip min/avg/max = %*f/%f/%*f%n",&rta,&match) && match) ||
+				 (sscanf(buf,"round-trip min/avg/max/mdev = %*f/%f/%*f/%*f%n",&rta,&match) && match) ||
+				 (sscanf(buf,"round-trip min/avg/max/sdev = %*f/%f/%*f/%*f%n",&rta,&match) && match) ||
+				 (sscanf(buf,"round-trip min/avg/max/stddev = %*f/%f/%*f/%*f%n",&rta,&match) && match) ||
+				 (sscanf(buf,"round-trip min/avg/max/std-dev = %*f/%f/%*f/%*f%n",&rta,&match) && match) ||
+				 (sscanf(buf,"round-trip (ms) min/avg/max = %*f/%f/%*f%n",&rta,&match) && match) ||
+				 (sscanf(buf,"round-trip (ms) min/avg/max/stddev = %*f/%f/%*f/%*f%n",&rta,&match) && match) ||
+				 (sscanf(buf,"rtt min/avg/max/mdev = %*f/%f/%*f/%*f ms%n",&rta,&match) && match))
 			continue;
 	}
 





More information about the Commits mailing list