Determine outgoing apache bandwidth usage with built in commands

To find the bandwidth usage of your Apache server, you can use many existing tools. Like vnstat, awstat.  The most common thing about these tools is they need the utility installed. What if you dont have this installed and you want to calculate your bandwidth? This is can be easily done by parsing Apache access logs. This technique will only work if you are a web master and you have no other bandwidth eating service other than apache. Most web developers will fall in this category. So here is the technique to find apache bandwidth usage.

Note: You need ssh access to perform these actions. Also I assume you have not deleted your log files.

  1. Determine the date range for which you want to find the bandwidth usage for. For example I want to deter mine bandwidth usage from Oct 1st 2012 to Oct 30 2012. Note you must have access log files for that range.
  2. Now the big command. Assuming your apache log directory is /var/log/apache2
    1. find /var/log/apache2 -type f \
          -name '*.gz' \
          -newermt "2012-10-01 00:00:00" \
          -not \
          -newermt "2012-10-30 23:59:59" \ 
          -exec zcat '{}' \; | 
      egrep '"[^"]+" +200 [0-9]+' -o | 
      awk '{sum+=$(NF)} END {print sum/1024/1024/1024 " GB"}'
  3. This will print something like “34.345 GB

3 thoughts on “Determine outgoing apache bandwidth usage with built in commands

  1. This is the Apache2 port of mod_bandwidth. Set up is similar. I removed all the mod_cband settings in the config and emerge -pv mod_bw. Added a new -D flag in /etc/conf.d/apache2 called -D BW.

  2. Exactly what I needed. For those doing this on OSX, here’s an updated command because zcat doesn’t work:

    find . -type f \
    -name ‘*.gz’ \
    -exec gunzip -c ‘{}’ \; |
    egrep ‘”[^”]+” +200 [0-9]+’ -o |
    awk ‘{sum+=$(NF)} END {print sum/1024/1024/1024 ” GB”}’

Leave a Reply

Your email address will not be published. Required fields are marked *

Solve * Time limit is exhausted. Please reload the CAPTCHA.