add proper error handling for all final exec calls
[hband-tools.git] / user-tools / PMbwmon
blob4a584c137403bc681c1fad78789072c109e155ef
1 #!/bin/bash
3 true <<EOF
4 =pod
6 =head1 NAME
8 PMbwmon - Poor man's bandwidth monitor
10 =cut
12 EOF
15 set -e
17 usage()
19 echo "Usage: $0 [kMG][bB]" >&2
20 exit 1
24 declare -A rx0
25 declare -A tx0
26 declare -A rx1
27 declare -A tx1
28 declare -A rxd
29 declare -A txd
31 prefix=${1:-kb}
33 cd /sys/class/net
36 case "${prefix:0:1}" in
37 k) qt=1024;;
38 M) qt=$[1024*1024];;
39 G) qt=$[1024*1024*1024];;
40 b) qt=1;;
41 B) qt=8;;
42 *) usage;;
43 esac
44 if [ "${#prefix}" -gt 1 ]; then
45 case "${prefix:1:1}" in
46 B) qt=$[qt*8];;
47 b) true;;
48 *) usage;;
49 esac
53 shot=0
54 while true
56 shot=$[shot+1]
57 declare -A masters
59 for master in `cat /sys/class/net/bonding_masters 2>/dev/null`
61 masters[$master]=1
62 var=slaves_$master
63 declare -a $var
64 eval "$var=($(cat /sys/class/net/$master/bonding/slaves))"
65 done
67 for iface in *
69 [ ! -d "$iface" ] && continue
70 [ "$iface" = lo ] && continue
72 rx0[$iface]=${rx1[$iface]:-0}
73 tx0[$iface]=${tx1[$iface]:-0}
74 rx1[$iface]=`cat /sys/class/net/$iface/statistics/rx_bytes`
75 tx1[$iface]=`cat /sys/class/net/$iface/statistics/tx_bytes`
77 rxd[$iface]=$[ ${rx1[$iface]} - ${rx0[$iface]} ]
78 txd[$iface]=$[ ${tx1[$iface]} - ${tx0[$iface]} ]
80 if [ $shot -gt 1 ]
81 then
82 printf "%6s - RX: %8d TX: %8d %sps" "$iface" $[ ${rxd[$iface]} / $qt ] $[ ${txd[$iface]} / $qt ] "$prefix"
84 if [ -n "${masters[$iface]}" ]
85 then
86 taps_rx_sum=0
87 taps_tx_sum=0
88 var=slaves_$iface
89 declare -a slaves
90 eval "slaves=(\${slaves_$iface[@]})"
92 for slave in "${slaves[@]}"
94 taps_rx_sum=$[ $taps_rx_sum + ${rxd[$slave]} ]
95 taps_tx_sum=$[ $taps_tx_sum + ${txd[$slave]} ]
96 done
97 taps_rx_sum=$[ $taps_rx_sum / $qt ]
98 taps_tx_sum=$[ $taps_tx_sum / $qt ]
99 bond_rx=$[ ${rxd[$iface]} / $qt ]
100 bond_tx=$[ ${txd[$iface]} / $qt ]
102 rx_comp_rt=''
103 tx_comp_rt=''
104 if [ $taps_rx_sum -ne 0 -a $taps_rx_sum -ne $bond_rx ]
105 then
106 rx_comp_rt=$[ ${bond_rx} / $taps_rx_sum ]
108 if [ $taps_tx_sum -ne 0 -a $taps_tx_sum -ne $bond_tx ]
109 then
110 tx_comp_rt=$[ ${bond_tx} / $taps_tx_sum ]
113 printf " - comp%% RX: %5.1f%% TX: %5.1f%%" "$rx_comp_rt" "$tx_comp_rt"
115 echo
117 done
119 echo
120 sleep 1
121 done