make getpeername() return the original socket address which before it was intercepted
[hband-tools.git] / user-tools / PMbwmon
blobbc8a2983561fc6a54f50c447ffa4f0c4d1e5782d
1 #!/bin/bash
3 true <<EOF
4 =pod
6 =head1 NAME
8 PMbwmon - Poor man's bandwidth monitor
10 =head1 SYNOPSIS
12 PMbwmon [kMG][B<bit> | B<Byte>] [I<INTERFACES>...]
14 =cut
16 EOF
19 set -e
21 . /usr/lib/tool/bash-utils
23 wrong_usage()
25 pod2text "$0" >&2
26 exit 1
30 declare -A rx0
31 declare -A tx0
32 declare -A rx1
33 declare -A tx1
34 declare -A rxd
35 declare -A txd
37 prefix=${1:-kbit}
38 shift
39 ifaces=("$@")
41 cd /sys/class/net
44 case "${prefix:0:1}" in
45 k) qt=1024;;
46 M) qt=$[1024*1024];;
47 G) qt=$[1024*1024*1024];;
48 b) qt=1;;
49 B) qt=8;;
50 *) wrong_usage;;
51 esac
52 if [ "${#prefix}" -gt 1 ]; then
53 case "${prefix:1}" in
54 Byte) qt=$[qt*8];;
55 bit) true;;
56 *) wrong_usage;;
57 esac
61 shot=0
62 while true
64 shot=$[shot+1]
65 declare -A masters
67 for master in `cat /sys/class/net/bonding_masters 2>/dev/null`
69 masters[$master]=1
70 var=slaves_$master
71 declare -a $var
72 eval "$var=($(cat /sys/class/net/$master/bonding/slaves))"
73 done
75 for iface in *
77 [ ! -d "$iface" ] && continue
78 [ "$iface" = lo ] && continue
79 if [ -n "${ifaces[0]}" ]
80 then
81 match=0
82 for iface_pattern in "${ifaces[@]}"
84 if [[ $iface_pattern =~ ^(.+)\+$ ]]
85 then
86 re=${BASH_REMATCH[1]}.+
87 if [[ $iface =~ ^$re$ ]]
88 then
89 match=1
91 elif [ "$iface" = "$iface_pattern" ]
92 then
93 match=1
95 if [ $match = 1 ]
96 then
97 break
99 done
100 if [ $match = 0 ]
101 then
102 continue
106 rx0[$iface]=${rx1[$iface]:-0}
107 tx0[$iface]=${tx1[$iface]:-0}
108 rx1[$iface]=`cat /sys/class/net/$iface/statistics/rx_bytes`
109 tx1[$iface]=`cat /sys/class/net/$iface/statistics/tx_bytes`
111 rxd[$iface]=$[ ${rx1[$iface]} - ${rx0[$iface]} ]
112 txd[$iface]=$[ ${tx1[$iface]} - ${tx0[$iface]} ]
114 if [ $shot -gt 1 ]
115 then
116 printf "%6s - RX: %8d TX: %8d %sps" "$iface" $[ ${rxd[$iface]} / $qt ] $[ ${txd[$iface]} / $qt ] "$prefix"
118 if [ -n "${masters[$iface]}" ]
119 then
120 taps_rx_sum=0
121 taps_tx_sum=0
122 var=slaves_$iface
123 declare -a slaves
124 eval "slaves=(\${slaves_$iface[@]})"
126 for slave in "${slaves[@]}"
128 taps_rx_sum=$[ $taps_rx_sum + ${rxd[$slave]} ]
129 taps_tx_sum=$[ $taps_tx_sum + ${txd[$slave]} ]
130 done
131 taps_rx_sum=$[ $taps_rx_sum / $qt ]
132 taps_tx_sum=$[ $taps_tx_sum / $qt ]
133 bond_rx=$[ ${rxd[$iface]} / $qt ]
134 bond_tx=$[ ${txd[$iface]} / $qt ]
136 rx_comp_rt=''
137 tx_comp_rt=''
138 if [ $taps_rx_sum -ne 0 -a $taps_rx_sum -ne $bond_rx ]
139 then
140 rx_comp_rt=$[ ${bond_rx} / $taps_rx_sum ]
142 if [ $taps_tx_sum -ne 0 -a $taps_tx_sum -ne $bond_tx ]
143 then
144 tx_comp_rt=$[ ${bond_tx} / $taps_tx_sum ]
147 printf " - comp%% RX: %5.1f%% TX: %5.1f%%" "$rx_comp_rt" "$tx_comp_rt"
149 echo
151 done
153 echo
154 sleep 1
155 done