Datura-Network/4-Scripts/bandwidth.sh
2024-02-26 11:04:02 +01:00

167 lines
6.1 KiB
Bash

#!/bin/bash
# Current month total bandwidth in MB
#apt install vnstat -y ; systemctl enable --now vnstat
#vnstat -i enp5s0
#
# while true; do
#i=$(vnstat --oneline | awk -F\; '{ print $11 }')
#DAILY
#i=$(vnstat --oneline | awk -F\; '{ print $4 }')
#j=$(vnstat --oneline | awk -F\; '{ print $5 }')
#MONTHLY
#i=$(vnstat --oneline | awk -F\; '{ print $9 }')
#j=$(vnstat --oneline | awk -F\; '{ print $10 }')
#i=$(vnstat --oneline -i eth0 | awk -F\; '{ print $9 }')
j=$(vnstat --oneline -i eth0 | awk -F\; '{ print $10 }')
month=$(vnstat --oneline -i eth0 | awk -F\; '{ print $8 }')
bn1=$(echo $j | awk '{ print $1 }')
#bn1=$(echo $i | awk '{ print $1 }')
#bn2=$(echo $j | awk '{ print $1 }')
bunit1=$(echo $j | awk '{ print $2 }')
#bunit2=$(echo $j | awk '{ print $2 }')
#echo "bn1= $bn1 bunit1= $bunit1"
#case "$bunit1" in
# KiB) bnB1=$(echo "$bn1*1024" | bc)
# ;;
# MiB) bnB1=$(echo "$bn1*1024*1024" | bc)
# ;;
# GiB) bnB1=$(echo "$bn1*1024*1024*1024" | bc)
# ;;
# TiB) bnB1=$(echo "$bn1*1024*1024*1024*1024" | bc)
# ;;
#esac
# we want TiBs ! not Bytes!
case "$bunit1" in
KiB) bnB1=$(echo "$bn1/1024/1024" | bc)
;;
MiB) bnB1=$(echo "$bn1/1024" | bc)
;;
GiB) bnB1=$(echo "$bn1" | bc)
;;
TiB) bnB1=$(echo "$bn1*1024" | bc)
;;
esac
#echo "bn1= $bn1 bunit1= $bunit1 bnB1= $bnB1"
#case "$bunit2" in
# KiB) bnB2=$(echo "$bn2*1024" | bc)
# ;;
# MiB) bnB2=$(echo "$bn2*1024*1024" | bc)
# ;;
# GiB) bnB2=$(echo "$bn2*1024*1024*1024" | bc)
# ;;
# TiB) bnB2=$(echo "$bn2*1024*1024*1024*1024" | bc)
# ;;
#esac
#if (( $(echo "$bnB1 > $bnB2" |bc -l) )); then
# bandwidth_number=$bnB1
#else
# bandwidth_number=$bnB2
#fi
bandwidth_number=$bnB1 # here only outbound matters!
#convert gibibytes into gigabyte (*1.073742)
echo -en "total sent out for $month : "
final=$(echo "$bandwidth_number * 1.073742" | bc)
echo -en $final GB
echo
#final="50001.0001"
monthly_limit=20000 # in gigabytes, here its a monthly 20TB
server_bandwidth=1000 # in megabytes here its a 1Gbps server (1000Mbps)
if [ "$(echo "$final < ($monthly_limit*0.5)" | bc -l)" == 1 ]; then
echo -en "Bandwidth usage below 50% ($final/$monthly_limit GB monthly), leaving bandwidth at 100% ("
echo -en "$(echo "$server_bandwidth * 1 " | bc -l) Mbps)"
## wondershaper eth0 1024*1000 1024*1000 # this is upload limit in Kbps : so 1000Mbps
wondershaper eth0 1024000 1024000
echo
wondershaper clear eth0
else
echo -en "Bandwidth usage above 50% ($final/$monthly_limit GB monthly), limiting the Bandwidth to 75% ("
echo -en "$(echo "$server_bandwidth * 0.75 " | bc -l) Mbps)"
## wondershaper eth0 1024*1000 1024*750 # this is upload limit in Kbps : so 750Mbps instead of 1000Mbps
wondershaper clear eth0
wondershaper eth0 768000 768000
fi
if [ "$(echo "$final < ($monthly_limit*0.75)" | bc -l)" == 1 ]; then
echo -en "Bandwidth usage below 75% ($final/$monthly_limit GB monthly), leaving bandwidth as is \n"
else
echo -en "Bandwidth usage above 75% ($final/$monthly_limit GB monthly), limiting the Bandwidth to 50% ("
echo -en "$(echo "$server_bandwidth * 0.5 " | bc -l) Mbps)"
## wondershaper eth0 1024*1000 1024*750 # this is upload limit in Kbps : so 750Mbps instead of 1000Mbps
wondershaper clear eth0
wondershaper eth0 512000 512000
fi
if [ "$(echo "$final < ($monthly_limit*0.80)" | bc -l)" == 1 ]; then
echo -en "Bandwidth usage below 80% ($final/$monthly_limit GB monthly), leaving bandwidth as is \n"
else
echo -en "Bandwidth usage above 80% ($final/$monthly_limit GB monthly), limiting the Bandwidth to 25% ("
echo -en "$(echo "$server_bandwidth * 0.5 " | bc -l) Mbps)"
## wondershaper eth0 1024*1000 1024*750 # this is upload limit in Kbps : so 750Mbps instead of 1000Mbps
wondershaper clear eth0
wondershaper eth0 256000 256000
fi
if [ "$(echo "$final < ($monthly_limit*0.90)" | bc -l)" == 1 ]; then
echo -en "Bandwidth usage below 90% ($final/$monthly_limit GB monthly), leaving bandwidth as is \n"
else
echo -en "Bandwidth usage above 90% ($final/$monthly_limit GB monthly), limiting the Bandwidth to 10% ("
echo -en "$(echo "$server_bandwidth * 0.5 " | bc -l) Mbps)"
## wondershaper eth0 1024*1000 1024*750 # this is upload limit in Kbps : so 750Mbps instead of 1000Mbps
wondershaper clear eth0
wondershaper eth0 10240 10240
fi
if [ "$(echo "$final < ($monthly_limit*0.95)" | bc -l)" == 1 ]; then
echo -en "Bandwidth usage below 95% ($final/$monthly_limit GB monthly), leaving bandwidth as is \n"
else
echo -en "Bandwidth usage above 95% ($final/$monthly_limit GB monthly), limiting the Bandwidth to 5% ("
echo -en "$(echo "$server_bandwidth * 0.5 " | bc -l) Mbps)"
## wondershaper eth0 1024*1000 1024*750 # this is upload limit in Kbps : so 750Mbps instead of 1000Mbps
wondershaper clear eth0
wondershaper eth0 5120 5120
fi
# if below : leave as default
# if datacap > 75% : limit bandwidth to 50%
## wondershaper -a eth0 -u 1024*500 # this is upload limit in Kbps : so 750Mbps instead of 1000Mbps
# if datacap > 80% : limit bandwidth to 25%
## wondershaper -a eth0 -u 1024*250 # this is upload limit in Kbps : so 750Mbps instead of 1000Mbps
# if datacap > 90% : limit bandwidth to 10%
## wondershaper -a eth0 -u 1024*100 # this is upload limit in Kbps : so 750Mbps instead of 1000Mbps
# if datacap > 95% : limit bandwidth to 5% >>> THROTTLE INVIDIOUS HARD TO FINISH THE MONTH!
## wondershaper -a eth0 -u 1024*50 # this is upload limit in Kbps : so 750Mbps instead of 1000Mbps
# if datacap > 98% : limit bandwidth to 1%
## wondershaper -a eth0 -u 1024*10 # this is upload limit in Kbps : so 750Mbps instead of 1000Mbps
echo
# done