Skip to content
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
47 changes: 34 additions & 13 deletions scripts/network_bandwidth.sh
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,18 @@

INTERVAL="1" # update interval in seconds

network_name=$(tmux show-option -gqv "@tmux2k-network-bandwidth")
# Network interface to monitor
network_name="en0"
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hardcoding the network name doesn't feel like a good idea, we can use network name passed in as an option for better usability

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

so I thought that in mac or any laptop, people mostly use wifi to connect to the internet, and this en0 is the wifi. But, I will change it.

Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm merging this in, will add the name option and docs

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Okay


if [[ $(uname -s) == "Darwin" ]]; then
network_name="en0" # en0 is wifi, TODO: Update network_name
elif [[ $(uname -s) == "Linux" ]]; then
network_name=$(tmux show-option -gqv "@dracula-network-bandwidth")
else
# update this later for window
echo "Unknown operating system"
exit 1
fi

main() {
while true
Expand All @@ -12,40 +23,50 @@ main() {
output_download_unit=""
output_upload_unit=""

initial_download=$(cat /sys/class/net/"$network_name"/statistics/rx_bytes)
initial_upload=$(cat /sys/class/net/"$network_name"/statistics/tx_bytes)
if [[ $(uname -s) == "Linux" ]]; then
initial_download=$(cat /sys/class/net/"$network_name"/statistics/rx_bytes)
initial_upload=$(cat /sys/class/net/"$network_name"/statistics/tx_bytes)

sleep "$INTERVAL"

sleep "$INTERVAL"
final_download=$(cat /sys/class/net/"$network_name"/statistics/rx_bytes)
final_upload=$(cat /sys/class/net/"$network_name"/statistics/tx_bytes)
else
initial_download=$(netstat -I "$network_name" -b | tail -n 1 | awk '{print $7}')
initial_upload=$(netstat -I "$network_name" -b | tail -n 1 | awk '{print $10}')

final_download=$(cat /sys/class/net/"$network_name"/statistics/rx_bytes)
final_upload=$(cat /sys/class/net/"$network_name"/statistics/tx_bytes)
sleep $INTERVAL

final_download=$(netstat -I "$network_name" -b | tail -n 1 | awk '{print $7}')
final_upload=$(netstat -I "$network_name" -b | tail -n 1 | awk '{print $10}')
fi

total_download_bps=$(expr "$final_download" - "$initial_download")
total_upload_bps=$(expr "$final_upload" - "$initial_upload")
total_download_bps=$(expr $final_download - $initial_download)
total_upload_bps=$(expr $final_upload - $initial_upload)

if [ "$total_download_bps" -gt 1073741824 ]; then
if [ $total_download_bps -gt 1073741824 ]; then
output_download=$(echo "$total_download_bps 1024" | awk '{printf "%.2f \n", $1/($2 * $2 * $2)}')
output_download_unit="gB/s"
elif [ "$total_download_bps" -gt 1048576 ]; then
elif [ $total_download_bps -gt 1048576 ]; then
output_download=$(echo "$total_download_bps 1024" | awk '{printf "%.2f \n", $1/($2 * $2)}')
output_download_unit="mB/s"
else
output_download=$(echo "$total_download_bps 1024" | awk '{printf "%.2f \n", $1/$2}')
output_download_unit="kB/s"
fi

if [ "$total_upload_bps" -gt 1073741824 ]; then
if [ $total_upload_bps -gt 1073741824 ]; then
output_upload=$(echo "$total_download_bps 1024" | awk '{printf "%.2f \n", $1/($2 * $2 * $2)}')
output_upload_unit="gB/s"
elif [ "$total_upload_bps" -gt 1048576 ]; then
elif [ $total_upload_bps -gt 1048576 ]; then
output_upload=$(echo "$total_upload_bps 1024" | awk '{printf "%.2f \n", $1/($2 * $2)}')
output_upload_unit="mB/s"
else
output_upload=$(echo "$total_upload_bps 1024" | awk '{printf "%.2f \n", $1/$2}')
output_upload_unit="kB/s"
fi

echo "↓ $output_download $output_download_unit • ↑ $output_upload $output_upload_unit"
echo "↓ $output_download$output_download_unit • ↑ $output_upload$output_upload_unit"
done
}
main