~ubuntu-branches/debian/wheezy/byobu/wheezy

« back to all changes in this revision

Viewing changes to usr/lib/byobu/network

  • Committer: Bazaar Package Importer
  • Author(s): Philipp Kern
  • Date: 2010-06-28 18:04:16 UTC
  • mfrom: (0.5.1 upstream) (0.3.4 squeeze)
  • Revision ID: james.westby@ubuntu.com-20100628180416-3xgfpga8cvp872ye
Upload to Debian unstable.

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
#!/bin/sh -e
 
2
#
 
3
#    network: calculate the network up/down rates
 
4
#    Copyright (C) 2008 Canonical Ltd.
 
5
#
 
6
#    Authors: Dustin Kirkland <kirkland@canonical.com>
 
7
#
 
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.
 
11
#
 
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.
 
16
#
 
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/>.
 
19
 
 
20
PKG="byobu"
 
21
color 2>/dev/null || color() { true; }
 
22
 
 
23
# Allow interface overrides in $HOME/.$PKG/status
 
24
if [ -n "$MONITORED_NETWORK" ]; then
 
25
        interface="$MONITORED_NETWORK"
 
26
else
 
27
        interface=`tail -n1 /proc/net/route  | awk '{print $1}'`
 
28
fi
 
29
 
 
30
if [ "$1" = "--detail" ]; then
 
31
        LC_ALL=C /sbin/ifconfig "$interface" | sed 's/\s*$//'
 
32
        exit 0
 
33
fi
 
34
 
 
35
[ -d "/var/run/screen/S-$USER" ] && DIR="/var/run/screen/S-$USER" || DIR="$HOME/.byobu"
 
36
t2=`date +%s`
 
37
for i in up down; do
 
38
        cache="$DIR/$PKG.network_$i"
 
39
        t1=`stat -c %Y "$cache"` 2>/dev/null || t1=0
 
40
        unit="kB/s"
 
41
        if [ $t2 -le $t1 ]; then
 
42
                rate=0
 
43
        else
 
44
                x1=`cat "$cache"` 2>/dev/null || tx1=0
 
45
                if [ "$i" = "up" ]; then
 
46
                        symbol="^"
 
47
                        x2=`grep -m1 "\b$interface:" /proc/net/dev | sed "s/^.*://" | awk '{print $9}'`
 
48
                else
 
49
                        symbol="v"
 
50
                        x2=`grep -m1 "\b$interface:" /proc/net/dev | sed "s/^.*://" | awk '{print $1}'`
 
51
                fi
 
52
                echo "$x2" > "$cache"
 
53
                rate=`echo "$t1" "$t2" "$x1" "$x2" | awk '{printf "%.0f", ($4 - $3) / ($2 - $1) / 1024 }'`
 
54
                if [ "$rate" -lt 0 ]; then
 
55
                        rate=0
 
56
                elif [ "$rate" -gt 1024 ]; then
 
57
                        rate=`echo "$rate" | awk '{printf "%.1f", $1/1024}'`
 
58
                        unit="MB/s"
 
59
                fi
 
60
        fi
 
61
        printf "$symbol$(color b m w)$rate$(color -)$(color m w)$unit$(color -) "
 
62
done