3
# network: calculate the network up/down rates
4
# Copyright (C) 2008 Canonical Ltd.
6
# Authors: Dustin Kirkland <kirkland@canonical.com>
8
# This program is free software: you can redistribute it and/or modify
9
# it under the terms of the GNU General Public License as published by
10
# the Free Software Foundation, version 3 of the License.
12
# This program is distributed in the hope that it will be useful,
13
# but WITHOUT ANY WARRANTY; without even the implied warranty of
14
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15
# GNU General Public License for more details.
17
# You should have received a copy of the GNU General Public License
18
# along with this program. If not, see <http://www.gnu.org/licenses/>.
21
color 2>/dev/null || color() { true; }
23
# Allow interface overrides in $HOME/.$PKG/status
24
if [ -n "$MONITORED_NETWORK" ]; then
25
interface="$MONITORED_NETWORK"
27
interface=`tail -n1 /proc/net/route | awk '{print $1}'`
30
if [ "$1" = "--detail" ]; then
31
LC_ALL=C /sbin/ifconfig "$interface" | sed 's/\s*$//'
35
[ -d "/var/run/screen/S-$USER" ] && DIR="/var/run/screen/S-$USER" || DIR="$HOME/.byobu"
38
cache="$DIR/$PKG.network_$i"
39
t1=`stat -c %Y "$cache"` 2>/dev/null || t1=0
41
if [ $t2 -le $t1 ]; then
44
x1=`cat "$cache"` 2>/dev/null || tx1=0
45
if [ "$i" = "up" ]; then
47
x2=`grep -m1 "\b$interface:" /proc/net/dev | sed "s/^.*://" | awk '{print $9}'`
50
x2=`grep -m1 "\b$interface:" /proc/net/dev | sed "s/^.*://" | awk '{print $1}'`
53
rate=`echo "$t1" "$t2" "$x1" "$x2" | awk '{printf "%.0f", ($4 - $3) / ($2 - $1) / 1024 }'`
54
if [ "$rate" -lt 0 ]; then
56
elif [ "$rate" -gt 1024 ]; then
57
rate=`echo "$rate" | awk '{printf "%.1f", $1/1024}'`
61
printf "$symbol$(color b m w)$rate$(color -)$(color m w)$unit$(color -) "