AndrzejL

Sar Script by Enlik v 1.02

Aug 11th, 2011
102
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.74 KB | None | 0 0
  1. #!/bin/sh
  2.  
  3. # display sar network statistics in a friendly way
  4.  
  5. # by Enlik
  6. # this script (that tries to be portable!)
  7. # is in public domain
  8.  
  9. # put the interface name here or specify as parameter
  10. # if you specify a parameter passed to the script, it
  11. # will be used as interface name, overriding the variable
  12. # below; default: eth0
  13. interface="${1:-eth0}"
  14.  
  15. # set interval here (the first numeric argument to sar)
  16. # defaults to 1 second
  17. # it can be overriden with second parameter passed to
  18. # the script
  19. sardelay="${2:-1}"
  20.  
  21. die() {
  22. echo "$1" >&2
  23. exit 1
  24. }
  25.  
  26. if [ ! -n "$interface" ]; then
  27. die "empty interface"
  28. fi
  29.  
  30. out=$(ifconfig "$interface") || die "ifconfig error, interface = $interface"
  31.  
  32. # note: IPv4
  33. myip=$(echo "x$out" | sed -n "s/.*inet addr:\([^ ]*\).*/\1/p")
  34.  
  35. # we need fixed field positions, which would break with time like "02:25:00 AM"
  36. # ("sanity checks" FTW!)
  37. export LC_TIME=C
  38.  
  39. # simple sanity checks
  40. which sar > /dev/null 2>&1 || die "can't find sar command"
  41. full_sar=$(sar -n DEV 1 1) || die "sanity check (1) failed: can't run sar; interface = $interface"
  42. echo "$full_sar" | grep -F -q "$interface" || die "sanity check (2) failed: no output for interface $interface"
  43. echo "$full_sar" | awk '{
  44. iface=0;
  45. if (/IFACE/) {
  46. iface=1;
  47. if ($5 == "rxkB/s" && $6 == "txkB/s") {
  48. exit 0
  49. } else {
  50. exit 1
  51. }
  52. }
  53. }
  54. END {
  55. if (iface != 1)
  56. exit 2
  57. }
  58. ' || die "santity check (3) failed - maybe you have different or broken sar? exit status: $?; output: $full_sar"
  59.  
  60. # Interface: wlan0 IP: xxx.xxx.xxx.xxx UL: 10 KB/s DL: 12 KB/s
  61. 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