[Nagiosplug-devel] [PATCH] Increment per-host sequence in check_icmp

Thomas Guyot-Sionnest dermoth at aei.ca
Thu Oct 15 01:54:37 CEST 2009


Hi,

I patched check_icmp to increment the packet sequence number on each
packet. This allow monitoring some devices that ignore duplicate ICMP
sequences. The plugin is still able to differentiate between hosts by
doing an integer division by the number of packet per host (the first
sequence number is the host index * packets).

Since I'm not very familiar with check_icmp, and considering that this
plugin is setuid root, I'd love to get some peer reviews before I
commit this patch.

Regards,

Thomas

---
 NEWS                      |    1 +
 plugins-root/check_icmp.c |   21 ++++++++++++++-------
 2 files changed, 15 insertions(+), 7 deletions(-)

diff --git a/NEWS b/NEWS
index 414ec67..a20dbbe 100644
--- a/NEWS
+++ b/NEWS
@@ -5,6 +5,7 @@ This file documents the major additions and syntax changes between releases.
 	FIXES
 	Fix check_ircd binding to wrong interface (#668778)
 	Add proxy-authorization option to check_http (Marcel Kuiper - #1323230, Bryan Irvine - #2863925)
+	check_icmp now increment the sequence counter in each packet
 	WARNINGS
 	Updated developer documentation to say that performance labels should not have an equals sign or
 	single quote in the label
diff --git a/plugins-root/check_icmp.c b/plugins-root/check_icmp.c
index cba7c44..e7d7825 100644
--- a/plugins-root/check_icmp.c
+++ b/plugins-root/check_icmp.c
@@ -333,14 +333,14 @@ handle_random_icmp(unsigned char *packet, struct sockaddr_in *addr)
 	 * to RFC 792). If it isn't, just ignore it */
 	memcpy(&sent_icmp, packet + 28, sizeof(sent_icmp));
 	if(sent_icmp.icmp_type != ICMP_ECHO || sent_icmp.icmp_id != pid ||
-	   sent_icmp.icmp_seq >= targets)
+	   sent_icmp.icmp_seq >= targets*packets)
 	{
 		if(debug) printf("Packet is no response to a packet we sent\n");
 		return 0;
 	}
 
 	/* it is indeed a response for us */
-	host = table[sent_icmp.icmp_seq];
+	host = table[sent_icmp.icmp_seq/packets];
 	if(debug) {
 		printf("Received \"%s\" from %s for ICMP ECHO sent to %s.\n",
 			   get_icmp_error_msg(p.icmp_type, p.icmp_code),
@@ -624,7 +624,7 @@ main(int argc, char **argv)
 	table = malloc(sizeof(struct rta_host **) * (argc - 1));
 	i = 0;
 	while(host) {
-		host->id = i;
+		host->id = i*packets;
 		table[i] = host;
 		host = host->next;
 		i++;
@@ -768,16 +768,19 @@ wait_for_reply(int sock, u_int t)
 			continue;
 		}
 
-		if(icp.icmp_type != ICMP_ECHOREPLY || icp.icmp_seq >= targets) {
-			if(debug > 2) printf("not a proper ICMP_ECHOREPLY\n");
+		if(icp.icmp_type != ICMP_ECHOREPLY || icp.icmp_seq >= targets*packets) {
+			if(debug > 2) printf("not a proper ICMP_ECHOREPLY %i:%i %i:%i\n", icp.icmp_type, ICMP_ECHOREPLY, icp.icmp_seq, targets*packets);
 			handle_random_icmp(buf + hlen, &resp_addr);
 			continue;
 		}
 
 		/* this is indeed a valid response */
 		memcpy(&data, icp.icmp_data, sizeof(data));
+		if (debug >= 3)
+			printf("Received ICMP echo-reply of len %u, id %u, seq %u, cksum 0x%X from host %s\n",
+			       sizeof(data), icp.icmp_id, icp.icmp_seq, icp.icmp_cksum, inet_ntoa(resp_addr.sin_addr));
 
-		host = table[icp.icmp_seq];
+		host = table[icp.icmp_seq/packets];
 		gettimeofday(&now, &tz);
 		tdiff = get_timevaldiff(&data.stime, &now);
 
@@ -848,9 +851,13 @@ send_icmp_ping(int sock, struct rta_host *host)
 	packet.icp->icmp_code = 0;
 	packet.icp->icmp_cksum = 0;
 	packet.icp->icmp_id = pid;
-	packet.icp->icmp_seq = host->id;
+	packet.icp->icmp_seq = host->id++;
 	packet.icp->icmp_cksum = icmp_checksum(packet.cksum_in, icmp_pkt_size);
 
+	if (debug >= 3)
+		printf("Sending ICMP echo-request of len %u, id %u, seq %u, cksum 0x%X to host %s\n",
+		       sizeof(data), packet.icp->icmp_id, packet.icp->icmp_seq, packet.icp->icmp_cksum, host->name);
+
 	len = sendto(sock, packet.buf, icmp_pkt_size, 0, (struct sockaddr *)addr,
 				 sizeof(struct sockaddr));
 
-- 
1.6.4.2





More information about the Devel mailing list