Skip to contentSkip to conversation

nstat: case-insensitive pattern matching

PATCH In review Anton Danilov 2 participants 6 messages Jul 8, 2020 – Jul 20, 2020

Subsystems (2)
lore
Share & tools
lore
#0 lore raw

PATCH 1/1 nstat: case-insensitive pattern matching

The option 'nocase' allows ignore case in the pattern matching.

Examples:
nstat --nocase *drop*
nstat -azi icmp*

---
 man/man8/rtacct.8 |  8 +++++++-
 misc/nstat.c      | 14 ++++++++++----
 2 files changed, 17 insertions(+), 5 deletions(-)
man/man8/rtacct.8 +7 -1
index ccdbf6ca..cb6ac912 100644
@@ -4,7 +4,7 @@
 nstat, rtacct - network statistics tools.
 
 .SH SYNOPSIS
-Usage: nstat [ -h?vVzrnasd:t:jp ] [ PATTERN [ PATTERN ] ]
+Usage: nstat [ -h?vVzrnasd:t:jpi ] [ PATTERN [ PATTERN ] ]
 .br
 Usage: rtacct [ -h?vVzrnasd:t: ] [ ListOfRealms ]
 
@@ -14,6 +14,9 @@ and
 .B rtacct
 are simple tools to monitor kernel snmp counters and network interface statistics.
 
+.B nstat
+can filter kernel snmp counters by name with one or several specified wildcards.
+
 .SH OPTIONS
 .B \-h, \-\-help
 Print help
@@ -44,6 +47,9 @@ When combined with
 .BR \-\-json ,
 pretty print the output.
 .TP
+.B \-i, \-\-nocase
+Ignore case in pattern matching.
+.TP
 .B \-d, \-\-scan <INTERVAL>
 Run in daemon mode collecting statistics. <INTERVAL> is interval between measurements in seconds.
 .TP
misc/nstat.c +10 -4
index 425e75ef..243caebe 100644
@@ -43,6 +43,7 @@ int time_constant;
 double W;
 char **patterns;
 int npatterns;
+int nocase;
 
 char info_source[128];
 int source_mismatch;
@@ -114,7 +115,7 @@ static int match(const char *id)
 		return 1;
 
 	for (i = 0; i < npatterns; i++) {
-		if (!fnmatch(patterns[i], id, 0))
+		if (!fnmatch(patterns[i], id, nocase ? FNM_CASEFOLD : 0))
 			return 1;
 	}
 	return 0;
@@ -551,6 +552,7 @@ static void usage(void)
 		"   -h, --help		this message\n"
 		"   -a, --ignore	ignore history\n"
 		"   -d, --scan=SECS	sample every statistics every SECS\n"
+		"   -i, --nocase	ignore case in pattern matching\n"
 		"   -j, --json		format output in JSON\n"
 		"   -n, --nooutput	do history only\n"
 		"   -p, --pretty	pretty print\n"
@@ -566,11 +568,12 @@ static const struct option longopts[] = {
 	{ "help", 0, 0, 'h' },
 	{ "ignore",  0,  0, 'a' },
 	{ "scan", 1, 0, 'd'},
-	{ "nooutput", 0, 0, 'n' },
+	{ "nocase", 0, 0, 'i' },
 	{ "json", 0, 0, 'j' },
+	{ "nooutput", 0, 0, 'n' },
+	{ "pretty", 0, 0, 'p' },
 	{ "reset", 0, 0, 'r' },
 	{ "noupdate", 0, 0, 's' },
-	{ "pretty", 0, 0, 'p' },
 	{ "interval", 1, 0, 't' },
 	{ "version", 0, 0, 'V' },
 	{ "zeros", 0, 0, 'z' },
@@ -585,7 +588,7 @@ int main(int argc, char *argv[])
 	int ch;
 	int fd;
 
-	while ((ch = getopt_long(argc, argv, "h?vVzrnasd:t:jp",
+	while ((ch = getopt_long(argc, argv, "h?vVzrnasd:t:jpi",
 				 longopts, NULL)) != EOF) {
 		switch (ch) {
 		case 'z':
@@ -619,6 +622,9 @@ int main(int argc, char *argv[])
 		case 'p':
 			pretty = 1;
 			break;
+		case 'i':
+			nocase = 1;
+			break;
 		case 'v':
 		case 'V':
 			printf("nstat utility, iproute2-ss%s\n", SNAPSHOT);
Signature · 1 line
2.26.2
#1 lore raw
Quoted 7 lines · On Wed, 8 Jul 2020 15:38:02 +0300 Anton Danilov…

The option 'nocase' allows ignore case in the pattern matching.

Examples:
nstat --nocase *drop*
nstat -azi icmp*

Why not just make it the default?
I can't imagine a scenario where user would want to match on icmp different than ICMP

#2 lore raw

PATCH v2 1/1 misc: make the pattern matching case-insensitive

To improve the usability better use case-insensitive pattern-matching in ifstat, nstat and ss tools.

---
 man/man8/rtacct.8 | 7 +++++++
 misc/ifstat.c     | 2 +-
 misc/nstat.c      | 2 +-
 misc/ss.c         | 2 +-
 4 files changed, 10 insertions(+), 3 deletions(-)
man/man8/rtacct.8 +7 -0
index ccdbf6ca..988a6d1b 100644
@@ -14,6 +14,13 @@ and
 .B rtacct
 are simple tools to monitor kernel snmp counters and network interface statistics.
 
+.B nstat
+can filter kernel snmp counters by name with one or several specified wildcards. Wildcards are case-insensitive and can include special symbols
+.B ?
+and
+.B *
+.
+
 .SH OPTIONS
 .B \-h, \-\-help
 Print help
misc/ifstat.c +1 -1
index 60efe6cb..03327af8 100644
@@ -104,7 +104,7 @@ static int match(const char *id)
 		return 1;
 
 	for (i = 0; i < npatterns; i++) {
-		if (!fnmatch(patterns[i], id, 0))
+		if (!fnmatch(patterns[i], id, FNM_CASEFOLD))
 			return 1;
 	}
 	return 0;
misc/nstat.c +1 -1
index 425e75ef..88f52eaf 100644
@@ -114,7 +114,7 @@ static int match(const char *id)
 		return 1;
 
 	for (i = 0; i < npatterns; i++) {
-		if (!fnmatch(patterns[i], id, 0))
+		if (!fnmatch(patterns[i], id, FNM_CASEFOLD))
 			return 1;
 	}
 	return 0;
misc/ss.c +1 -1
index f3d01812..5aa10e4a 100644
@@ -1670,7 +1670,7 @@ static int unix_match(const inet_prefix *a, const inet_prefix *p)
 		return 1;
 	if (addr == NULL)
 		addr = "";
-	return !fnmatch(pattern, addr, 0);
+	return !fnmatch(pattern, addr, FNM_CASEFOLD);
 }
 
 static int run_ssfilter(struct ssfilter *f, struct sockstat *s)
Signature · 1 line
2.26.2
#3 lore raw

Re: [PATCH v2 iproute2] misc: make the pattern matching case-insensitive

Quoted 4 lines · On Thu, 9 Jul 2020 18:03:43 +0300 Anton Danilov…

To improve the usability better use case-insensitive pattern-matching in ifstat, nstat and ss tools.

Applied

#5 lore raw

Hello, Stephen.

Thanks for feedback.

Quoted 2 lines

Why not just make it the default?
I can't imagine a scenario where user would want to match on icmp different than ICMP

Yes, I'm agreed. I'll make it the default in the v2 patch.

Quoted 1 line

Perhaps it should also be applied to ifstat and ss.

I'll prepare the patches for ifstat ans ss too.