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

« back to all changes in this revision

Viewing changes to usr/lib/byobu/disk_io

  • Committer: Package Import Robot
  • Author(s): Alexander Chernyakhovsky
  • Date: 2011-10-02 19:04:37 UTC
  • mfrom: (0.5.2 upstream) (0.1.145 oneiric)
  • Revision ID: package-import@ubuntu.com-20111002190437-apn9z04063rh052s
Tags: 4.37-1
* Syncing from Ubuntu.
* Switch to dh short syntax
* Change maintainer (adopting this package)
* Added a substvar ${perl:Depends} dependency into debian/control
* Bump standards version to 3.9.2, no changes 

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
1
#!/bin/sh -e
2
2
#
3
3
#    disk_io: calculate the disk io rate
 
4
#
4
5
#    Copyright (C) 2010 Canonical Ltd.
 
6
#    Copyright (C) 2011 Dustin Kirkland
5
7
#
6
 
#    Authors: Dustin Kirkland <kirkland@canonical.com>
 
8
#    Authors: Dustin Kirkland <kirkland@ubuntu.com>
7
9
#
8
10
#    This program is free software: you can redistribute it and/or modify
9
11
#    it under the terms of the GNU General Public License as published by
17
19
#    You should have received a copy of the GNU General Public License
18
20
#    along with this program.  If not, see <http://www.gnu.org/licenses/>.
19
21
 
20
 
PKG="byobu"
21
 
color 2>/dev/null || color() { true; }
22
 
 
23
 
# Default to disk providing /, but let users override with MONITORED_DISK
24
 
[ -z "$MONITORED_DISK" ] && MP="/" ||  MP="$MONITORED_DISK"
25
 
case $MP in
26
 
        /dev/*) disk="$MP" ;;
27
 
        *) disk=$(grep -m 1 " $MP " /etc/mtab | sed -e "s: .*$::") ;;
28
 
esac
29
 
disk=$(echo "$disk" | sed -e "s: .*$::" -e "s:^.*/::" -e "s:\([hsv]d[a-z]\)[0-9]*$:\1:")
30
 
 
31
 
if [ "$1" = "--detail" ]; then
32
 
        if which iostat >/dev/null; then
 
22
__disk_io_detail() {
 
23
        if command -v iostat >/dev/null; then
33
24
                iostat -d -m -h
34
25
        else
35
26
                echo "Please install iostat if you want detailed information on your disk throughput"
36
27
        fi
37
 
        exit 0
38
 
fi
39
 
 
40
 
[ -d "/var/run/screen/S-$USER" ] && DIR="/var/run/screen/S-$USER" || DIR="$HOME/.byobu"
41
 
t2=`date +%s`
42
 
for i in "read" "write"; do
43
 
        cache="$DIR/$PKG.disk_$i"
44
 
        t1=`stat -c %Y "$cache"` 2>/dev/null || t1=0
45
 
        unit="kB/s"
46
 
        if [ $t2 -le $t1 ]; then
47
 
                rate=0
 
28
}
 
29
 
 
30
 
 
31
getdisk() {
 
32
        local t=""
 
33
        if [ -L "${1}" ]; then
 
34
                t=$($READLINK -f "$1")
48
35
        else
49
 
                x1=`cat "$cache"` 2>/dev/null || tx1=0
50
 
                if [ "$i" = "read" ]; then
51
 
                        symbol="<"
52
 
                        x2=`awk '{print $3}' /sys/block/$disk/stat`
 
36
                t="$1"
 
37
        fi
 
38
        t="${t##*/}";
 
39
        [ -h "/sys/block/$t" ] && _RET="$t" || rtrim "$t" "0-9"
 
40
}
 
41
 
 
42
__disk_io() {
 
43
        local part= i=
 
44
        # Default to disk providing /, but let users override with MONITORED_DISK
 
45
        [ -z "$MONITORED_DISK" ] && mount_point="/" ||  mount_point="$MONITORED_DISK"
 
46
        # By default, we won't bug the user with the display of network traffic
 
47
        # below DISK_IO_THRESHOLD in kB/s; override in $BYOBU_CONFIG_DIR/status
 
48
        [ -n "$DISK_IO_THRESHOLD" ] || DISK_IO_THRESHOLD=50
 
49
        case "$mount_point" in
 
50
                /dev/*) part="${mount_point}";;
 
51
                *) part=$(awk '$2 == mp { print $1 ; exit(0); }' "mp=$mount_point" /etc/mtab);;
 
52
        esac
 
53
        getdisk "$part"
 
54
        local disk=${_RET}
 
55
        local t2=`date +%s` t1=
 
56
        for i in "read" "write"; do
 
57
                local cache="$BYOBU_RUN_DIR/cache.$BYOBU_BACKEND/disk.$i"
 
58
                t1=`stat -c %Y "$cache"` 2>/dev/null || t1=0
 
59
                local unit="kB/s"
 
60
                local rate= x1= x2= symbol= unit=
 
61
                if [ $t2 -le $t1 ]; then
 
62
                        rate=0
53
63
                else
54
 
                        symbol=">"
55
 
                        x2=`awk '{print $7}' /sys/block/$disk/stat`
56
 
                fi
57
 
                echo "$x2" > "$cache"
58
 
                rate=`echo "$t1" "$t2" "$x1" "$x2" | awk '{printf "%.0f", ($4 - $3) / ($2 - $1) * 512 / 1024 }'`
59
 
                if [ "$rate" -lt 0 ]; then
60
 
                        rate=0
61
 
                elif [ "$rate" -gt 1024 ]; then
62
 
                        rate=`echo "$rate" | awk '{printf "%.1f", $1/1024}'`
63
 
                        unit="MB/s"
64
 
                fi
65
 
        fi
66
 
        printf "$symbol$(color b M W)$rate$(color -)$(color M W)$unit$(color -) "
67
 
done
 
64
                        [ -r "$cache" ] && read x1 < "$cache" || x1=0
 
65
                        local a1= a2= a3= a4= a5= a6= a7= a8= a9= a10=
 
66
                        read a1 a2 a3 a4 a5 a6 a7 a8 a9 a10 other < "/sys/block/$disk/stat"
 
67
                        if [ "$i" = "read" ]; then
 
68
                                symbol="$ICON_RD"
 
69
                                x2="$a3"
 
70
                        else
 
71
                                symbol="$ICON_WR"
 
72
                                x2="$a7"
 
73
                        fi
 
74
                        echo "$x2" > "$cache"
 
75
                        rate=$((($x2 - $x1) / ($t2 - $t1) * 512 / 1024))
 
76
                        if [ $rate -lt $DISK_IO_THRESHOLD ]; then
 
77
                                # Below threshold, don't print
 
78
                                continue
 
79
                        elif [ "$rate" -lt 0 ]; then
 
80
                                rate=0
 
81
                        elif [ "$rate" -gt 1048576 ]; then
 
82
                                unit="GB/s"
 
83
                                fpdiv "$rate" 1048576 0
 
84
                                rate=${_RET}
 
85
                        elif [ "$rate" -gt 1024 ]; then
 
86
                                unit="MB/s"
 
87
                                fpdiv "$rate" 1024 0
 
88
                                rate=${_RET}
 
89
                        else
 
90
                                unit="kB/s"
 
91
                        fi
 
92
                fi
 
93
                [ "$rate" != "0" ] || continue
 
94
                color b M W; printf "%s%s" "$symbol" "$rate"; color -; color M W; printf "%s" "$unit"; color --
 
95
        done
 
96
}
 
97
 
 
98
# vi: syntax=sh ts=4 noexpandtab