On Thu, May 15, 2014 at 12:11 AM, Dan Egli <[email protected]> wrote:
>
> Well, as I said, it mainly started as an intellectual exercise.
Below I've pasted a gawk-based hack/script that comes in handy from time to
time. I'm no awk expert and the script is nothing to be proud of, but
maybe it'll give you some ideas on ways to abuse awk. :-) This is one of
the few scripts in my quiver that makes use of awk functions. It's been so
long since I cobbled this script together that I'd have to spend a while to
re-figure-out much of what it does and how it does it. Much of the
complexity arises from code that prints long numbers with
alternately-bolded groups of three digits. (Output is presumed to be a
terminal that supports bolding.) Consider it an archaeological exercise?
I hope the pasted content below doesn't get too mangled by the emailing
process. YMMV, etc.
Usage is something like this:
$ ifcounters 10 eth0
eth0 (delay=10 sec)
----------- Receive ----------- ---------- Transmit -----------
frames/sec bytes/sec bits/sec frames/sec bytes/sec bits/sec
---------- --------- ---------- ---------- --------- ----------
4 400 3203 2 333 2664
8 1034 8275 7 1079 8634
^C
Here's the script. Good luck. You may need it:
#!/usr/local/bin/bash
ttymsg() {
echo "$@" >/dev/tty
}
bail() {
ttymsg "$@"
exit 1
}
test -n "$1" || bail "Missing argument(s): [delay] ifname"
{ test -z "$2" || expr match "$1" '^[0-9]*$' >/dev/null; } || bail "First
arg must be a number (delay)."
DELAY=1
if expr match "$1" '^[0-9]*$' >/dev/null
then
DELAY="$1"
shift
fi
test -n "$1" || bail "Missing ifname argument."
while : ; do
grep "$1:" /proc/self/net/dev
sleep $DELAY
done \
| awk -v intf="$1" -v delay="$DELAY" '
BEGIN {
lines=0;
header_every=1+20; /* header counts as one line, so (1+20) => header
every 20 data lines */
}
function ps(s, width) {
"tput bold" | getline bold
"tput sgr0" | getline reset
l = length(s)
for (c = width ; c > 0 ; --c) {
if (c > l) {
printf(" ");
} else {
need_bold = ( (22 <= c && c <= 24) \
|| (16 <= c && c <= 18) \
|| (10 <= c && c <= 12) \
|| ( 4 <= c && c <= 6)) ? 1 : 0;
if (need_bold) { printf("%s", bold); }
f = substr(s,1,1);
printf("%s", f);
if (need_bold) { printf("%s", reset); }
s = substr(s,2);
}
}
}
function pi(i, width) {
fs = sprintf("%%%dd", width);
s = sprintf(fs, i);
ps(s, width);
}
{
rxb=$2;
rxp=$3;
txb=$10;
txp=$11
if ((lines%header_every)==0) {
printf(" %s%s\n", intf,
delay==1?"":sprintf(" (delay=%d sec)",delay));
printf("----------- Receive ----------- ---------- Transmit
-----------\n");
printf("%10s %9s %10s %9s %9s %10s\n",
"frames/sec", "bytes/sec", "bits/sec",
"frames/sec", "bytes/sec", "bits/sec");
printf("%10s %9s %10s %9s %9s %10s\n",
"----------", "---------", "----------",
"----------", "---------", "----------");
lines++;
}
if (prxp) {
pi((rxp-prxp)/delay, 10); printf(" ");
pi((rxb-prxb)/delay, 9); printf(" ");
pi((8*(rxb-prxb))/delay, 10); printf(" ");
pi((txp-ptxp)/delay, 10); printf(" ");
pi((txb-ptxb)/delay, 9); printf(" ");
pi((8*(txb-ptxb))/delay, 10); printf("\n");
lines++;
}
prxb=rxb;
ptxb=txb;
prxp=rxp;
ptxp=txp;
}'
/*
PLUG: http://plug.org, #utah on irc.freenode.net
Unsubscribe: http://plug.org/mailman/options/plug
Don't fear the penguin.
*/