1#!/bin/sh -e
2#
3#    network: calculate the network up/down rates
4#
5#    Copyright (C) 2008 Canonical Ltd.
6#    Copyright (C) 2011-2014 Dustin Kirkland
7#
8#    Authors: Dustin Kirkland <kirkland@byobu.org>
9#
10#    This program is free software: you can redistribute it and/or modify
11#    it under the terms of the GNU General Public License as published by
12#    the Free Software Foundation, version 3 of the License.
13#
14#    This program is distributed in the hope that it will be useful,
15#    but WITHOUT ANY WARRANTY; without even the implied warranty of
16#    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
17#    GNU General Public License for more details.
18#
19#    You should have received a copy of the GNU General Public License
20#    along with this program.  If not, see <http://www.gnu.org/licenses/>.
21
22__network_detail() {
23	get_network_interface; local interface="$_RET"
24	LC_ALL=C /sbin/ip addr show "$interface" | $BYOBU_SED 's/\s*$//'
25}
26
27__network() {
28	get_network_interface; local interface="$_RET"
29	local x1=0 x2=0 tx1=0 i= t= unit= symbol= cache= rate=
30	status_freq network
31	t="$_RET"
32	# By default, we won't bug the user with the display of network traffic
33	# below NETWORK_THRESHOLD in kbps; override in $BYOBU_CONFIG_DIR/status
34	[ -n "$NETWORK_THRESHOLD" ] || NETWORK_THRESHOLD=20
35	for i in up down; do
36		unit="kb"
37		case $i in
38			up) symbol="$ICON_UP" ;;
39			down) symbol="$ICON_DN" ;;
40		esac
41		cache="$BYOBU_RUN_DIR/cache.$BYOBU_BACKEND/network.$i"
42		[ -r "$cache" ] && read x1 < "$cache" || tx1=0
43		local iface rbytes rpackets rerrs rdrop rfifo rframe rcompressed rmulticast tbytes tpackets terrs tdrop tfifo tcolls tcarrier tcompressed
44		cat /proc/net/dev > "$cache".dev
45		while read iface rbytes rpackets rerrs rdrop rfifo rframe rcompressed rmulticast tbytes tpackets terrs tdrop tfifo tcolls tcarrier tcompressed; do
46			case "$iface" in
47				${interface}:)
48					[ "$i" = "up" ] && x2=${tbytes} || x2=${rbytes}
49					break;
50				;;
51				${interface}:*)
52					# Interface and tbytes got munged together
53					[ "$i" = "up" ] && x2=${rmulticast##*:} || x2=${iface##*:}
54					break;
55				;;
56			esac
57		done < "$cache".dev
58		printf "%s" "$x2" > "$cache"
59		rate=$((8*($x2 - $x1) / $t / 1024))  # in kbps
60		[ "$rate" -lt 0 ] && rate=0
61		if [ $rate -gt $NETWORK_THRESHOLD ]; then
62			case "$NETWORK_UNITS" in
63				bytes)
64					rate=$(($rate/8))
65					if [ "$rate" -gt 1048576 ]; then
66						fpdiv "$rate" 1048576 1
67						rate=${_RET}
68						unit="GB/s"
69					elif [ "$rate" -gt 1024 ]; then
70						fpdiv "$rate" 1024 1
71						rate=${_RET}
72						unit="MB/s"
73					else
74						unit="kB/s"
75					fi
76				;;
77				*)
78					# Default to bps
79					# Why 1000 and not 1024?  http://en.wikipedia.org/wiki/Data_rate_units
80					if [ "$rate" -gt 1000000 ]; then
81						fpdiv "$rate" 1000000 1
82						rate=${_RET}
83						unit="Gb"
84					elif [ "$rate" -gt 1000 ]; then
85						fpdiv "$rate" 1000 1
86						rate=${_RET}
87						unit="Mb"
88					fi
89				;;
90			esac
91			[ -n "$rate" ] || continue
92			color b m w; printf "%s%s" "$symbol" "$rate"; color -; color m w; printf "%s" "$unit"; color --
93		else
94			rm -f "$BYOBU_RUN_DIR/status.$BYOBU_BACKEND/network"*
95		fi
96	done
97}
98
99# vi: syntax=sh ts=4 noexpandtab
100