Search code examples
performanceubuntubandwidth-throttling

Limit Speed in Ubuntu based on traffic


I have come across a script i.e. Wondershaper

The script is terrific, however any way to make it smarter?

Like it runs after certain traffic has gone through?

Say 1TB is set per day, once 1TB is hit, the script turns on automatically?

I have thought about setting crn job,

At 12 am it clears the wondershaper, and in 15mins interval, it checks if the server has crossed 1TB limit for the day, and then if it is true then it runs the limiter,

but I am not sure how to set up the 2nd part, how can i setup a way that will enable the limiter to run after 1TB is crossed?

Remove Code

wondershaper -ca eth0

Limit Code

wondershaper -a eth0 -u 154000

Solution

  • I have made a custom script for this, as it is not possible to do it within the system, i had to become creative and do a API call to the datacenter and then run cron job.

    I also used bashjson, to run it. I have attached the script below.

    date=$(date +%F)
    url='API URL /metrics/datatraffic?from='
    url1='T00:00:00Z&to='
    url2='T23:59:59Z&aggregation=SUM'
    final="$url$date$url1$date$url2"
    
    wget --no-check-certificate -O output.txt \
      --method GET \
      --timeout=0 \
      --header 'X-Lsw-Auth: API AUTH' \
       $final
    
    sed 's/[][]//g' output.txt >> test1.json // will remove '[]' from the code just to make things easier for bashjson to understand
    down=$(/root/bashjson/bashjson.sh test1.json metrics DOWN_PUBLIC values value) // outputs the data to variable
    up=$(/root/bashjson/bashjson.sh test1.json metrics UP_PUBLIC values value)
    
    
    newdown=$(printf "%.14f" $down)
    newup=$(printf "%.14f" $up)
    
    
    upp=$(printf "%.0f\n" "$newup") // removes scientific notation as bash does not like it
    downn=$(printf "%.0f\n" "$newdown")
    
    if (($upp>800000000000 | bc))
    then
    wondershaper -a eth0 -u 100000 //main command to limit
    else
    echo uppworks
    fi
    
    if (($downn>500000000000 | bc))
    then
    wondershaper -a eth0 -d 100000
    else
    echo downworks
    fi
    
    rm -rf output.txt test1.json
    
    echo $upp
    echo $downn
    

    You can always update it as per your preference.