From c2dfd19ff0b6b19d2f3a17d11040a54224e681f8 Mon Sep 17 00:00:00 2001 From: Gleb Smirnoff Date: Thu, 18 Aug 2005 21:04:12 +0000 Subject: [PATCH] Add a new switch -h for interface stats mode, which prints all interface statistics in human readable form. In collaboration with: vsevolod Reviewed by: cperciva --- usr.bin/netstat/if.c | 25 ++++++++++++++++++------- usr.bin/netstat/main.c | 8 ++++++-- usr.bin/netstat/netstat.1 | 7 +++++-- usr.bin/netstat/netstat.h | 1 + 4 files changed, 30 insertions(+), 11 deletions(-) diff --git a/usr.bin/netstat/if.c b/usr.bin/netstat/if.c index 1e1e13a902da..b8b5a81ec8b7 100644 --- a/usr.bin/netstat/if.c +++ b/usr.bin/netstat/if.c @@ -62,6 +62,7 @@ __FBSDID("$FreeBSD$"); #include #include +#include #include #include #include @@ -175,18 +176,28 @@ 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 == 0) { + /* Print just dash. */ sprintf(newfmt, "%%%ds", width); printf(newfmt, "-"); + return; + } + + if (hflag) { + char buf[5]; + + /* Format in human readable form. */ + humanize_number(buf, sizeof(buf), (int64_t)value, "", + HN_AUTOSCALE, HN_NOSPACE | HN_DECIMAL); + sprintf(newfmt, "%%%ds", width); + printf(newfmt, buf); + } else { + /* Construct the format string. */ + sprintf(newfmt, "%%%d%s", width, fmt); + printf(newfmt, value); } } - - /* * Print a description of the network interfaces. */ diff --git a/usr.bin/netstat/main.c b/usr.bin/netstat/main.c index 61a0ec19daff..7f33f80b61a1 100644 --- a/usr.bin/netstat/main.c +++ b/usr.bin/netstat/main.c @@ -273,6 +273,7 @@ int aflag; /* show all sockets (including servers) */ int bflag; /* show i/f total bytes in/out */ 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 */ @@ -300,7 +301,7 @@ main(int argc, char *argv[]) af = AF_UNSPEC; - while ((ch = getopt(argc, argv, "Aabdf:gI:iLlM:mN:np:rSstuWw:z")) != -1) + while ((ch = getopt(argc, argv, "Aabdf:ghI:iLlM:mN:np:rSstuWw: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; @@ -698,7 +702,7 @@ 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 [-AaLnSW] [-f protocol_family | -p protocol]\n" " [-M core] [-N system]", -" netstat -i | -I interface [-abdnt] [-f address_family]\n" +" netstat -i | -I interface [-abdhnt] [-f address_family]\n" " [-M core] [-N system]", " netstat -w wait [-I interface] [-d] [-M core] [-N system]", " netstat -s [-s] [-z] [-f protocol_family | -p protocol] [-M core]", diff --git a/usr.bin/netstat/netstat.1 b/usr.bin/netstat/netstat.1 index 32edfec3e537..62de37c86371 100644 --- a/usr.bin/netstat/netstat.1 +++ b/usr.bin/netstat/netstat.1 @@ -32,7 +32,7 @@ .\" @(#)netstat.1 8.8 (Berkeley) 4/18/94 .\" $FreeBSD$ .\" -.Dd March 25, 2004 +.Dd August 19, 2005 .Dt NETSTAT 1 .Os .Sh NAME @@ -89,7 +89,7 @@ but show ports symbolically. .Bk -words .Nm .Fl i | I Ar interface -.Op Fl abdnt +.Op Fl abdhnt .Op Fl f Ar address_family .Op Fl M Ar core .Op Fl N Ar system @@ -117,6 +117,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 diff --git a/usr.bin/netstat/netstat.h b/usr.bin/netstat/netstat.h index 3a519ac95a1b..5bc3a60f310a 100644 --- a/usr.bin/netstat/netstat.h +++ b/usr.bin/netstat/netstat.h @@ -41,6 +41,7 @@ extern int aflag; /* show all sockets (including servers) */ extern int bflag; /* show i/f total bytes in/out */ 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 */