Last active
May 31, 2024 00:01
-
-
Save naranyala/8bbd5e59ce370005061b73ca29d2063b to your computer and use it in GitHub Desktop.
display daily internet usage in polybar
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/bin/bash | |
# Get the network interface (e.g., eth0, wlan0) | |
INTERFACE=$(ip route | grep default | awk '{print $5}') | |
# Default values if files are not found | |
DEFAULT_RX_BYTES=0 | |
DEFAULT_TX_BYTES=0 | |
# Get the current bytes received and transmitted | |
RX_BYTES_FILE="/sys/class/net/$INTERFACE/statistics/rx_bytes" | |
TX_BYTES_FILE="/sys/class/net/$INTERFACE/statistics/tx_bytes" | |
if [[ -f $RX_BYTES_FILE ]]; then | |
RX_BYTES=$(cat $RX_BYTES_FILE) | |
else | |
RX_BYTES=$DEFAULT_RX_BYTES | |
echo "Warning: $RX_BYTES_FILE not found, using default value $DEFAULT_RX_BYTES" | |
fi | |
if [[ -f $TX_BYTES_FILE ]]; then | |
TX_BYTES=$(cat $TX_BYTES_FILE) | |
else | |
TX_BYTES=$DEFAULT_TX_BYTES | |
echo "Warning: $TX_BYTES_FILE not found, using default value $DEFAULT_TX_BYTES" | |
fi | |
# Ensure the values are numbers | |
RX_BYTES=${RX_BYTES:-$DEFAULT_RX_BYTES} | |
TX_BYTES=${TX_BYTES:-$DEFAULT_TX_BYTES} | |
# File to store daily byte counts | |
DATA_FILE="$HOME/.config/polybar/network_bytes_data" | |
# Read previous data | |
if [[ -f $DATA_FILE ]]; then | |
read -r PREV_RX PREV_TX TOTAL_RX TOTAL_TX < $DATA_FILE | |
else | |
PREV_RX=$RX_BYTES | |
PREV_TX=$TX_BYTES | |
TOTAL_RX=0 | |
TOTAL_TX=0 | |
fi | |
# Ensure previous values are numbers | |
PREV_RX=${PREV_RX:-0} | |
PREV_TX=${PREV_TX:-0} | |
TOTAL_RX=${TOTAL_RX:-0} | |
TOTAL_TX=${TOTAL_TX:-0} | |
# Calculate the difference since last check | |
DIFF_RX=$(echo "$RX_BYTES - $PREV_RX" | bc 2>/dev/null) | |
DIFF_TX=$(echo "$TX_BYTES - $PREV_TX" | bc 2>/dev/null) | |
# Ensure the differences are numbers | |
DIFF_RX=${DIFF_RX:-0} | |
DIFF_TX=${DIFF_TX:-0} | |
# Update total counts | |
TOTAL_RX=$(echo "$TOTAL_RX + $DIFF_RX" | bc 2>/dev/null) | |
TOTAL_TX=$(echo "$TOTAL_TX + $DIFF_TX" | bc 2>/dev/null) | |
# Ensure total values are numbers | |
TOTAL_RX=${TOTAL_RX:-0} | |
TOTAL_TX=${TOTAL_TX:-0} | |
# Convert bytes to human-readable format | |
convert_to_human() { | |
local bytes=$1 | |
local unit="B" | |
local value=$(echo "scale=2; $bytes / 1" | bc 2>/dev/null) # keep two decimal places | |
if (( $(echo "$bytes >= 1024" | bc -l 2>/dev/null) )); then | |
value=$(echo "scale=2; $value / 1024" | bc 2>/dev/null) | |
unit="KB" | |
fi | |
if (( $(echo "$value >= 1024" | bc -l 2>/dev/null) )); then | |
value=$(echo "scale=2; $value / 1024" | bc 2>/dev/null) | |
unit="MB" | |
fi | |
if (( $(echo "$value >= 1024" | bc -l 2>/dev/null) )); then | |
value=$(echo "scale=2; $value / 1024" | bc 2>/dev/null) | |
unit="GB" | |
fi | |
echo "$value $unit" | |
} | |
# Store current and total data for the next run | |
echo "$RX_BYTES $TX_BYTES $TOTAL_RX $TOTAL_TX" > $DATA_FILE | |
# Output the result | |
echo "IN ($(convert_to_human $TOTAL_RX)) OUT ($(convert_to_human $TOTAL_TX))" | |
### | |
# [module/net-count-bytes] | |
# type = custom/script | |
# exec = ~/.config/polybar/scripts/net-count-bytes.sh | |
# interval = 60 | |
# label = %output% | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment