Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #!/bin/sh
- # display sar network statistics in a friendly way
- # by Enlik
- # this script (that tries to be portable!)
- # is in public domain
- # put the interface name here or specify as parameter
- # if you specify a parameter passed to the script, it
- # will be used as interface name, overriding the variable
- # below; default: eth0
- interface="${1:-eth0}"
- # set interval here (the first numeric argument to sar)
- # defaults to 1 second
- # it can be overriden with second parameter passed to
- # the script
- sardelay="${2:-1}"
- die() {
- echo "$1" >&2
- exit 1
- }
- if [ ! -n "$interface" ]; then
- die "empty interface"
- fi
- out=$(ifconfig "$interface") || die "ifconfig error, interface = $interface"
- # note: IPv4
- myip=$(echo "x$out" | sed -n "s/.*inet addr:\([^ ]*\).*/\1/p")
- # we need fixed field positions, which would break with time like "02:25:00 AM"
- # ("sanity checks" FTW!)
- export LC_TIME=C
- # simple sanity checks
- which sar > /dev/null 2>&1 || die "can't find sar command"
- full_sar=$(sar -n DEV 1 1) || die "sanity check (1) failed: can't run sar; interface = $interface"
- echo "$full_sar" | grep -F -q "$interface" || die "sanity check (2) failed: no output for interface $interface"
- echo "$full_sar" | awk '{
- iface=0;
- if (/IFACE/) {
- iface=1;
- if ($5 == "rxkB/s" && $6 == "txkB/s") {
- exit 0
- } else {
- exit 1
- }
- }
- }
- END {
- if (iface != 1)
- exit 2
- }
- ' || die "santity check (3) failed - maybe you have different or broken sar? exit status: $?; output: $full_sar"
- # Interface: wlan0 IP: xxx.xxx.xxx.xxx UL: 10 KB/s DL: 12 KB/s
- sar -n DEV "$sardelay" | awk -v ip="$myip" -v iface="$interface" '{ if (index($0,iface)) { print "Interface:", iface, "IP:", ip, "UL:", $6, "kB/s DL:", $5, "kB/s" } }'
Advertisement
Add Comment
Please, Sign In to add comment