diff mbox series

[iproute2] nstat: case-insensitive pattern matching

Message ID 20200708123801.878-1-littlesmilingcloud@gmail.com
State Accepted
Delegated to: stephen hemminger
Headers show
Series [iproute2] nstat: case-insensitive pattern matching | expand

Commit Message

Anton Danilov July 8, 2020, 12:38 p.m. UTC
The option 'nocase' allows ignore case in the pattern matching.

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

Signed-off-by: Anton Danilov <littlesmilingcloud@gmail.com>
---
 man/man8/rtacct.8 |  8 +++++++-
 misc/nstat.c      | 14 ++++++++++----
 2 files changed, 17 insertions(+), 5 deletions(-)

Comments

Stephen Hemminger July 8, 2020, 3:28 p.m. UTC | #1
On Wed,  8 Jul 2020 15:38:02 +0300
Anton Danilov <littlesmilingcloud@gmail.com> wrote:

> The option 'nocase' allows ignore case in the pattern matching.
> 
> Examples:
>     nstat --nocase *drop*
>     nstat -azi icmp*
> 
> Signed-off-by: Anton Danilov <littlesmilingcloud@gmail.com>

Why not just make it the default?
I can't imagine a scenario where user would want to match on icmp different than ICMP
Stephen Hemminger July 8, 2020, 3:40 p.m. UTC | #2
On Wed,  8 Jul 2020 15:38:02 +0300
Anton Danilov <littlesmilingcloud@gmail.com> wrote:

> The option 'nocase' allows ignore case in the pattern matching.
> 
> Examples:
>     nstat --nocase *drop*
>     nstat -azi icmp*
> 
> Signed-off-by: Anton Danilov <littlesmilingcloud@gmail.com>

On second thought, this looks like a good idea.
Perhaps it should also be applied to ifstat and ss.
Anton Danilov July 8, 2020, 9:48 p.m. UTC | #3
Hello, Stephen.

Thanks for feedback.

> 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.

> Perhaps it should also be applied to ifstat and ss.

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

Patch

diff --git a/man/man8/rtacct.8 b/man/man8/rtacct.8
index ccdbf6ca..cb6ac912 100644
--- a/man/man8/rtacct.8
+++ b/man/man8/rtacct.8
@@ -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
diff --git a/misc/nstat.c b/misc/nstat.c
index 425e75ef..243caebe 100644
--- a/misc/nstat.c
+++ b/misc/nstat.c
@@ -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);