From 789cd86fb06a0e131973461406521cd39991cde4 Mon Sep 17 00:00:00 2001 From: Sebastien GODARD Date: Sat, 15 Aug 2015 14:39:21 +0200 Subject: [PATCH] Check /proc/net/snmp format for ICMP statistics "InCsumErrors" field was added following "InErrors" in the ICMP line of statistics. So we may have two different formats to deal with: The old one, where "InCsumErrors" doesn't exist, and the new one including "InCsumErrors" field. This patch adds a check to know which format it is and read the various statistics at their right position. Signed-off-by: Sebastien GODARD --- rd_stats.c | 25 ++++++++++++++++++++++--- 1 file changed, 22 insertions(+), 3 deletions(-) diff --git a/rd_stats.c b/rd_stats.c index 153cfd9..009b770 100644 --- a/rd_stats.c +++ b/rd_stats.c @@ -1231,6 +1231,7 @@ void read_net_icmp(struct stats_net_icmp *st_net_icmp) { FILE *fp; char line[1024]; + static char format[256] = ""; int sw = FALSE; if ((fp = fopen(NET_SNMP, "r")) == NULL) @@ -1240,9 +1241,7 @@ void read_net_icmp(struct stats_net_icmp *st_net_icmp) if (!strncmp(line, "Icmp:", 5)) { if (sw) { - sscanf(line + 5, "%lu %*u %*u %*u %*u %*u %*u %*u " // 1 - 8, capture 1 - "%lu %lu %lu %lu %lu %lu %lu %*u %*u %*u %*u " // 9 - 19, capture 9,10,11,12,13,14,15 - "%*u %*u %lu %lu %lu %lu %lu %lu", // 20 - 27, capture 22,23,24,25,26,27 + sscanf(line + 5, format, &st_net_icmp->InMsgs, &st_net_icmp->InEchos, &st_net_icmp->InEchoReps, @@ -1261,6 +1260,26 @@ void read_net_icmp(struct stats_net_icmp *st_net_icmp) break; } else { + if (!strlen(format)) { + if (strstr(line, "InCsumErrors")) { + /* + * New format: InCsumErrors field exists at position #3. + * Capture: 1,9,10,11,12,13,14,15,22,23,24,25,26,27. + */ + strcpy(format, "%lu %*u %*u %*u %*u %*u %*u %*u " + "%lu %lu %lu %lu %lu %lu %lu %*u %*u %*u %*u " + "%*u %*u %lu %lu %lu %lu %lu %lu"); + } + else { + /* + * Old format: InCsumErrors field doesn't exist. + * Capture: 1,8,9,10,11,12,13,14,21,22,23,24,25,26. + */ + strcpy(format, "%lu %*u %*u %*u %*u %*u %*u " + "%lu %lu %lu %lu %lu %lu %lu %*u %*u %*u %*u " + "%*u %*u %lu %lu %lu %lu %lu %lu"); + } + } sw = TRUE; } } -- 2.40.0