From 000e4ca33e24df9d947eabb25ac79db89e96f473 Mon Sep 17 00:00:00 2001 From: Antonio Nikishaev Date: Sun, 23 Nov 2014 15:23:33 +0400 Subject: [PATCH] -h (human-readable counters) for netstat --- usr.bin/netstat/if.c | 39 +++++++++++++++++++++++++-------------- usr.bin/netstat/main.c | 10 +++++++--- usr.bin/netstat/netstat.1 | 12 +++++++++--- usr.bin/netstat/netstat.h | 1 + 4 files changed, 42 insertions(+), 20 deletions(-) diff --git a/usr.bin/netstat/if.c b/usr.bin/netstat/if.c index 309121a5be..1dd197cd11 100644 --- a/usr.bin/netstat/if.c +++ b/usr.bin/netstat/if.c @@ -50,6 +50,7 @@ #endif #include +#include #include #include #include @@ -77,13 +78,23 @@ show_stat(const char *fmt, int width, u_long value, short showvalue) { char newfmt[32]; - /* Construct the format string */ - if (showvalue) { - sprintf(newfmt, "%%%d%s", width, fmt); - printf(newfmt, value); - } else { + if (! showvalue) { + /* no value, just the dash */ sprintf(newfmt, "%%%ds", width); printf(newfmt, "-"); + return; + } + + if (hflag) { /* human-readable */ + char buf[5]; + humanize_number(buf, sizeof buf, (int64_t) value, "", + HN_AUTOSCALE, + HN_NOSPACE | HN_DECIMAL); + sprintf(newfmt, "%%%ds", width); + printf(newfmt, buf); + } else { + sprintf(newfmt, "%%%d%s", width, fmt); + printf(newfmt, value); } } @@ -687,16 +698,16 @@ loop: off = (u_long)TAILQ_NEXT(&ifnet, if_link); } if (!first) { - printf("%10lu %5lu %10lu %10lu %5lu %10lu %5lu", - sum->ift_ip - total->ift_ip, - sum->ift_ie - total->ift_ie, - sum->ift_ib - total->ift_ib, - sum->ift_op - total->ift_op, - sum->ift_oe - total->ift_oe, - sum->ift_ob - total->ift_ob, - sum->ift_co - total->ift_co); + /* %10lu %5lu %10lu %10lu %5lu %10lu %5lu */ + show_stat("lu", 10, sum->ift_ip - total->ift_ip, 1); + show_stat("lu", 5+1, sum->ift_ie - total->ift_ie, 1); + show_stat("lu", 10+1, sum->ift_ib - total->ift_ib, 1); + show_stat("lu", 10+1, sum->ift_op - total->ift_op, 1); + show_stat("lu", 5+1, sum->ift_oe - total->ift_oe, 1); + show_stat("lu", 10+1, sum->ift_ob - total->ift_ob, 1); + show_stat("lu", 5+1, sum->ift_co - total->ift_co, 1); if (dflag) - printf(" %5u", sum->ift_dr - total->ift_dr); + show_stat("u", 5+1, sum->ift_dr - total->ift_dr, 1); } *total = *sum; } diff --git a/usr.bin/netstat/main.c b/usr.bin/netstat/main.c index 00b3995818..d69d4ecb69 100644 --- a/usr.bin/netstat/main.c +++ b/usr.bin/netstat/main.c @@ -261,6 +261,7 @@ int bflag; /* show i/f total bytes in/out */ int cpuflag = -1; /* dump route table from specific cpu */ int dflag; /* show i/f dropped packets */ int gflag; /* show group (multicast) routing or stats */ +int hflag; /* show counters in human readable format */ int iflag; /* show interfaces */ int Lflag; /* show size of listen queues */ int mflag; /* show memory stats */ @@ -291,7 +292,7 @@ main(int argc, char **argv) af = AF_UNSPEC; - while ((ch = getopt(argc, argv, "Aabc:df:gI:iLlM:mN:nPp:rSsBtuWw:z")) != -1) + while ((ch = getopt(argc, argv, "Aabc:df:ghI:iLlM:mN:nPp:rSsBtuWw:z")) != -1) switch(ch) { case 'A': Aflag = 1; @@ -343,6 +344,9 @@ main(int argc, char **argv) case 'g': gflag = 1; break; + case 'h': + hflag = 1; + break; case 'I': { char *cp; @@ -685,9 +689,9 @@ usage(void) (void)fprintf(stderr, "%s\n%s\n%s\n%s\n%s\n%s\n%s\n%s\n%s\n%s\n", "usage: netstat [-AaLnPSW] [-c cpu] [-f protocol_family | -p protocol]\n" " [-M core] [-N system]", -" netstat -i | -I interface [-aBbdnt] [-f address_family]\n" +" netstat -i | -I interface [-aBbdhnt] [-f address_family]\n" " [-M core] [-N system]", -" netstat -w wait [-I interface] [-d] [-M core] [-N system]", +" netstat -w wait [-I interface] [-dh] [-M core] [-N system]", " netstat -s [-s] [-z] [-f protocol_family | -p protocol] [-M core]", " netstat -i | -I interface -s [-f protocol_family | -p protocol]\n" " [-M core] [-N system]", diff --git a/usr.bin/netstat/netstat.1 b/usr.bin/netstat/netstat.1 index 14356e1143..e8f8b3d0e0 100644 --- a/usr.bin/netstat/netstat.1 +++ b/usr.bin/netstat/netstat.1 @@ -28,7 +28,7 @@ .\" @(#)netstat.1 8.8 (Berkeley) 4/18/94 .\" $FreeBSD: src/usr.bin/netstat/netstat.1,v 1.22.2.13 2003/05/03 22:10:02 keramida Exp $ .\" -.Dd July 12, 2008 +.Dd November 23, 2014 .Dt NETSTAT 1 .Os .Sh NAME @@ -86,7 +86,7 @@ but show ports symbolically. .Bk -words .Nm .Fl i | I Ar interface -.Op Fl aBbdnt +.Op Fl aBbdhnt .Op Fl f Ar address_family .Op Fl M Ar core .Op Fl N Ar system @@ -114,6 +114,9 @@ If .Fl d is also present, show the number of dropped packets. If +.Fl h +is also present, print all counters in human readable form. +If .Fl t is also present, show the contents of watchdog timers. If @@ -125,7 +128,7 @@ of current buffer usage. .Nm .Fl w Ar wait .Op Fl I Ar interface -.Op Fl d +.Op Fl dh .Op Fl M Ar core .Op Fl N Ar system .Ek @@ -140,6 +143,9 @@ or a single If .Fl d is also present, show the number of dropped packets. +If +.Fl h +is also present, print counters in human readable form. .It Xo .Bk -words .Nm diff --git a/usr.bin/netstat/netstat.h b/usr.bin/netstat/netstat.h index 83dc34017e..375f9b8a03 100644 --- a/usr.bin/netstat/netstat.h +++ b/usr.bin/netstat/netstat.h @@ -42,6 +42,7 @@ extern int bflag; /* show i/f total bytes in/out */ extern int cpuflag; extern int dflag; /* show i/f dropped packets */ extern int gflag; /* show group (multicast) routing or stats */ +extern int hflag; /* show counters in human readable format */ extern int iflag; /* show interfaces */ extern int Lflag; /* show size of listen queues */ extern int mflag; /* show memory stats */ -- 2.41.0