简体   繁体   English

限速 Ubuntu 基于流量

[英]Limit Speed in Ubuntu based on traffic

I have come across a script ie Wondershaper我遇到了一个脚本,即 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?说每天设置1TB,一旦达到1TB,脚本会自动开启?

I have thought about setting crn job,我曾想过设置 crn 工作,

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,凌晨 12 点清除 Wondershaper,每隔 15 分钟检查服务器当天是否超过 1TB 限制,如果是,则运行限制器,

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?但我不知道如何设置第二部分,我如何设置一种方式,使限制器在超过 1TB 后运行?

Remove Code删除代码

wondershaper -ca eth0

Limit Code限制代码

wondershaper -a eth0 -u 154000

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.我为此制作了一个自定义脚本,因为无法在系统内执行此操作,我必须发挥创造力并对数据中心进行 API 调用,然后运行 cron 作业。

I also used bashjson , to run it.我还使用bashjson来运行它。 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.您可以随时根据自己的喜好更新它。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM