How to avoid paying extra for additional traffic?

Hey guys,

I am kinda stuck with the question how I can avoid paying for additional traffic when my included traffic is used. Most providers do have unlimited traffic or stop the server when your included traffic is used.

I do have a server with servarica which has a limit of 2 TB per month. I'm kinda sure this will be more than enough but I am always scared of this bad awakening when something bad happens (misconfiguration or whatever).

So what do you think might be my best bet to add a protection? I am thinking about calculation the traffic and stop the server if it hits 1,9 tb in a month.

Many thanks in advance!

Comments

  • SpeedBusSpeedBus Hosting ProviderOG
    edited July 2020

    use vnstat to calculate/count traffic
    maybe some small bash script to shutdown the VPS if touches 1.9 TB ?

    good idea, let me try to whip something up


    EDIT: I've ran outta talent, so far this is what I whipped up, which errors with

    traffic_stop.sh: line 10: [: 2.37: integer expression expected

    if someone can improve it, would be cool :)

    #!/bin/bash
    VNSTAT="/usr/bin/vnstat"
    TRAFFIC_USAGE=$($VNSTAT -m | tail -n3 | head -n1 | awk {'print $9'})
    TRAFFIC_USAGE_2=$($VNSTAT -m | tail -n3 | head -n1 | awk {'print $10'})
    STOP_VPS_AT_USAGE="1.9"
    #use GiB for GB, TiB for TB
    STOP_VPS_AT_USAGE_2="GiB"
    if [ "$TRAFFIC_USAGE_2" = "$STOP_VPS_AT_USAGE_2" ]
    then
            if [ $TRAFFIC_USAGE -gt 1.9 ]
            then
                    shutdown -a now
            fi
    fi
    
    Thanked by (1)Multi_

    CrownCloud - Internet Services | Los Angeles, California | Frankfurt, Germany | Amsterdam, The Netherlands | Atlanta, Georgia | Miami, Florida

  • edited July 2020

    You can ask your provider for a hard-cap if you’re willing to accept some downtime until resolved bandwidth. However, if you consistently go over I’d suggest just upgrading to have additional bandwidth.

    Alternatively, you can do proactive monitoring, have custom alerts sent to you (eg, nagios) when criteria is reached, etc.

    Thanked by (1)Multi_
  • LittleCreekLittleCreek Hosting Provider
    edited July 2020

    Here is a partial perl script that I use.

    $from_email="";
    $to_email="";
    $bandwidth_limit="50";

    @a = /usr/bin/sar -n DEV 295 1;

    foreach $a(@a){

      if (($a =~ /Average/) && ($a =~ /enp3s0/)){
    
               @split = split (/\s+/, $a);
    
               $rx = $split[4];
               $tx = $split[5];
    
       }
    

    }

    $total = $rx + $tx;

    $mbits = $total * 8 / 1000;

    $totalmbits = $mbits

    if ($totalmbits > $bandwidth_limit){

       open(SENDMAIL, "| /usr/sbin/sendmail -t -n") || print "Unable to open sendmail";
       print SENDMAIL "To: $to_email\n";
       print SENDMAIL "From: $from_email\n";
       print SENDMAIL "Subject: Over Bandwidth Total\n";
       print SENDMAIL "$totalmbits Mbits\n\n";
       print SENDMAIL "Router1 $mbits Mbits\n\n";
       close(SENDMAIL);
    

    }

    Thanked by (1)Multi_

    LittleCreekHosting.com

  • comicomi OG
    edited July 2020

    @SpeedBus said:
    use vnstat to calculate/count traffic
    maybe some small bash script to shutdown the VPS if touches 1.9 TB ?

    good idea, let me try to whip something up


    EDIT: I've ran outta talent, so far this is what I whipped up, which errors with

    traffic_stop.sh: line 10: [: 2.37: integer expression expected

    if someone can improve it, would be cool :)

    #!/bin/bash
    VNSTAT="/usr/bin/vnstat"
    TRAFFIC_USAGE=$($VNSTAT -m | tail -n3 | head -n1 | awk {'print $9'})
    TRAFFIC_USAGE_2=$($VNSTAT -m | tail -n3 | head -n1 | awk {'print $10'})
    STOP_VPS_AT_USAGE="1.9"
    #use GiB for GB, TiB for TB
    STOP_VPS_AT_USAGE_2="GiB"
    if [ "$TRAFFIC_USAGE_2" = "$STOP_VPS_AT_USAGE_2" ]
    then
            if [ $TRAFFIC_USAGE -gt 1.9 ]
            then
                    shutdown -a now
            fi
    fi
    

    hi, well obviously 1.9 is not an integer so -gt fails.

    There are several ways to do it

    bc / dc
    awk
    $(()) boolean shenanigans
    

    or my personal favorite

    echo "1.9" | grep [1-9]\.[9] > /dev/null && echo High
    

    you can lookup "bash compare floats" to find them all

    Thanked by (1)Multi_
  • hostworldhostworld Hosting Provider
    edited July 2020

    @Multi_ said:
    Hey guys,

    I am kinda stuck with the question how I can avoid paying for additional traffic when my included traffic is used. Most providers do have unlimited traffic or stop the server when your included traffic is used.

    I do have a server with servarica which has a limit of 2 TB per month. I'm kinda sure this will be more than enough but I am always scared of this bad awakening when something bad happens (misconfiguration or whatever).

    So what do you think might be my best bet to add a protection? I am thinking about calculation the traffic and stop the server if it hits 1,9 tb in a month.

    Many thanks in advance!

    Hey. How about considering a provider who offers unlimited bandwidth with no cap? ;) At least that way you wouldn't need to worry about anything.

    The other option is to use the above as discussed.

    hostworld.uk - Web Hosting, Reseller Hosting, NVMe SSD VPS, Dedicated Servers & Domains. UK & US data centres & 24/7 UK support.

  • Consider also if there are ways to save on traffic by leveraging caching and optimizing assets. You could also consider using Cloudflare (perhaps just for a cdn.yourwebsite.com subdomain so you don't get MITMd on sensitive stuff).

    Thanked by (1)Multi_
  • @foxone said: Consider also if there are ways to save on traffic by leveraging caching and optimizing assets. You could also consider using Cloudflare (perhaps just for a cdn.yourwebsite.com subdomain so you don't get MITMd on sensitive stuff).

    If you are going to do this also use subresource integrity for any JS/CSS/etc you run.

Sign In or Register to comment.