summaryrefslogtreecommitdiff
path: root/.local/bin/dwmbar/bar_net_activity.sh
blob: 7c3ec1bea013eac957a09102968c66975df3d46e (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
#!/bin/sh

# network activity

[ ! -f /tmp/netactup ] || touch /tmp/netactup
[ ! -f /tmp/netactdown ] || touch /tmp/netactdown

# generate a list of devices
net_devices=$(ls /sys/class/net | grep -E "[ew]")

# get previous results
rx_prev=$(cat /tmp/netactdown)
tx_prev=$(cat /tmp/netactup)

# convert results to integers
if [ -n "$rx_prev" ] && [ -n "$tx_prev" ] ; then
        rx_prev=$(($rx_prev))
        tx_prev=$(($tx_prev))
fi

# declare send and recieve variables
rx_now=0
tx_now=0

# convert variables to integers
rx_now=$(($rx_now))
tx_now=$(($tx_now))

# iterate through each device
for device in $net_devices; do
	rx_dev=$(cat /sys/class/net/"${device}"/statistics/rx_bytes)
	tx_dev=$(cat /sys/class/net/"${device}"/statistics/tx_bytes)

	rx_now=$(($rx_now + $rx_dev))
	tx_now=$(($tx_now + $tx_dev))
done

# caculate amount send and recieved
down_total=$((rx_now - rx_prev))
up_total=$((tx_now - tx_prev))

echo $rx_now > /tmp/netactdown
echo $tx_now > /tmp/netactup

echo "D: $(numfmt --to=iec $down_total) U: $(numfmt --to=iec $up_total)"
#echo "D:" $down_total "U:" $up_total