diff options
Diffstat (limited to 'dot_config/i3/scripts/executable_bandwidth2')
-rw-r--r-- | dot_config/i3/scripts/executable_bandwidth2 | 105 |
1 files changed, 105 insertions, 0 deletions
diff --git a/dot_config/i3/scripts/executable_bandwidth2 b/dot_config/i3/scripts/executable_bandwidth2 new file mode 100644 index 0000000..ebd398a --- /dev/null +++ b/dot_config/i3/scripts/executable_bandwidth2 | |||
@@ -0,0 +1,105 @@ | |||
1 | #!/usr/bin/env bash | ||
2 | # | ||
3 | # Copyright (C) 2015 James Murphy | ||
4 | # Licensed under the terms of the GNU GPL v2 only. | ||
5 | # | ||
6 | # i3blocks blocklet script to monitor bandwidth usage | ||
7 | |||
8 | iface="${BLOCK_INSTANCE}" | ||
9 | iface="${IFACE:-$iface}" | ||
10 | dt="${DT:-3}" | ||
11 | unit="${UNIT:-KB}" | ||
12 | LABEL="${LABEL:-: }" # down arrow up arrow | ||
13 | #LABEL="${LABEL:-<span font='FontAwesome'> </span>}" # down arrow up arrow | ||
14 | printf_command="${PRINTF_COMMAND:-"printf \"${LABEL}%1.0f/%1.0f %s/s\\n\", rx, wx, unit;"}" | ||
15 | |||
16 | function default_interface { | ||
17 | ip route | awk '/^default via/ {print $5; exit}' | ||
18 | } | ||
19 | |||
20 | function check_proc_net_dev { | ||
21 | if [ ! -f "/proc/net/dev" ]; then | ||
22 | echo "/proc/net/dev not found" | ||
23 | exit 1 | ||
24 | fi | ||
25 | } | ||
26 | |||
27 | function list_interfaces { | ||
28 | check_proc_net_dev | ||
29 | echo "Interfaces in /proc/net/dev:" | ||
30 | grep -o "^[^:]\\+:" /proc/net/dev | tr -d " :" | ||
31 | } | ||
32 | |||
33 | while getopts i:t:u:p:lh opt; do | ||
34 | case "$opt" in | ||
35 | i) iface="$OPTARG" ;; | ||
36 | t) dt="$OPTARG" ;; | ||
37 | u) unit="$OPTARG" ;; | ||
38 | p) printf_command="$OPTARG" ;; | ||
39 | l) list_interfaces && exit 0 ;; | ||
40 | h) printf \ | ||
41 | "Usage: bandwidth3 [-i interface] [-t time] [-u unit] [-p printf_command] [-l] [-h] | ||
42 | Options: | ||
43 | -i\tNetwork interface to measure. Default determined using \`ip route\`. | ||
44 | -t\tTime interval in seconds between measurements. Default: 3 | ||
45 | -u\tUnits to measure bytes in. Default: Mb | ||
46 | \tAllowed units: Kb, KB, Mb, MB, Gb, GB, Tb, TB | ||
47 | \tUnits may have optional it/its/yte/ytes on the end, e.g. Mbits, KByte | ||
48 | -p\tAwk command to be called after a measurement is made. | ||
49 | \tDefault: printf \"<span font='FontAwesome'> </span>%%-5.1f/%%5.1f %%s/s\\\\n\", rx, wx, unit; | ||
50 | \tExposed variables: rx, wx, tx, unit, iface | ||
51 | -l\tList available interfaces in /proc/net/dev | ||
52 | -h\tShow this help text | ||
53 | " && exit 0;; | ||
54 | esac | ||
55 | done | ||
56 | |||
57 | check_proc_net_dev | ||
58 | |||
59 | iface="${iface:-$(default_interface)}" | ||
60 | while [ -z "$iface" ]; do | ||
61 | echo No default interface | ||
62 | sleep "$dt" | ||
63 | iface=$(default_interface) | ||
64 | done | ||
65 | |||
66 | case "$unit" in | ||
67 | Kb|Kbit|Kbits) bytes_per_unit=$((1024 / 8));; | ||
68 | KB|KByte|KBytes) bytes_per_unit=$((1024));; | ||
69 | Mb|Mbit|Mbits) bytes_per_unit=$((1024 * 1024 / 8));; | ||
70 | MB|MByte|MBytes) bytes_per_unit=$((1024 * 1024));; | ||
71 | Gb|Gbit|Gbits) bytes_per_unit=$((1024 * 1024 * 1024 / 8));; | ||
72 | GB|GByte|GBytes) bytes_per_unit=$((1024 * 1024 * 1024));; | ||
73 | Tb|Tbit|Tbits) bytes_per_unit=$((1024 * 1024 * 1024 * 1024 / 8));; | ||
74 | TB|TByte|TBytes) bytes_per_unit=$((1024 * 1024 * 1024 * 1024));; | ||
75 | *) echo Bad unit "$unit" && exit 1;; | ||
76 | esac | ||
77 | |||
78 | scalar=$((bytes_per_unit * dt)) | ||
79 | init_line=$(cat /proc/net/dev | grep "^[ ]*$iface:") | ||
80 | if [ -z "$init_line" ]; then | ||
81 | echo Interface not found in /proc/net/dev: "$iface" | ||
82 | exit 1 | ||
83 | fi | ||
84 | |||
85 | init_received=$(awk '{print $2}' <<< $init_line) | ||
86 | init_sent=$(awk '{print $10}' <<< $init_line) | ||
87 | |||
88 | (while true; do cat /proc/net/dev; sleep "$dt"; done) |\ | ||
89 | stdbuf -oL grep "^[ ]*$iface:" |\ | ||
90 | awk -v scalar="$scalar" -v unit="$unit" -v iface="$iface" ' | ||
91 | BEGIN{old_received='"$init_received"';old_sent='"$init_sent"'} | ||
92 | { | ||
93 | received=$2 | ||
94 | sent=$10 | ||
95 | rx=(received-old_received)/scalar; | ||
96 | wx=(sent-old_sent)/scalar; | ||
97 | tx=rx+wr; | ||
98 | old_received=received; | ||
99 | old_sent=sent; | ||
100 | if(rx >= 0 && wx >= 0){ | ||
101 | '"$printf_command"'; | ||
102 | fflush(stdout); | ||
103 | } | ||
104 | } | ||
105 | ' | ||